This repository has been archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_bayer_renderer.cpp
executable file
·330 lines (292 loc) · 7.29 KB
/
test_bayer_renderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* @file test_bayer_renderer.cpp
* @author Max Smolens
* @brief Demonstration of Bayer pattern renderer.
*/
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <magick/api.h>
#include "GLUTFPSCounter.hpp"
#include "BayerRenderer.hpp"
#include <GL/glut.h>
// types for loading images
enum
{
GRAYSCALE,
RGB
};
// ids for menu
enum
{
MENU_SCREENSHOT,
MENU_QUIT
};
#define SCREENSHOT_FILENAME "out.tiff"
static int width = 640;
static int height = 480;
// ptr to bayer image
static GLubyte *bayer = NULL;
static GLUTFPSCounter fps_counter;
static BayerRenderer *br;
/// reads image file into array
GLubyte *
read_image (const char *filename,
int type,
int *width,
int *height)
{
ExceptionInfo exception;
Image *image;
Image *flip_image;
ImageInfo *image_info;
const PixelPacket *pixels;
GLubyte *data = NULL;
unsigned int i;
unsigned int j;
int w, h;
// initialize the image info structure and read image
GetExceptionInfo (&exception);
image_info = CloneImageInfo ((ImageInfo *) NULL);
strncpy (image_info->filename, filename, MaxTextExtent);
image_info->filename[MaxTextExtent-1] = '\0';
image = ReadImage (image_info, &exception);
if (image == NULL) {
MagickError(exception.severity, exception.reason, exception.description);
return (NULL);
}
*width = w = image->columns;
*height = h = image->rows;
// flip image
flip_image = FlipImage (image, &exception);
DestroyImage (image);
if (!flip_image) {
MagickError (exception.severity, exception.reason, exception.description);
return (NULL);
}
image = flip_image;
// copy pixels to array
pixels = AcquireImagePixels (image, 0, 0, image->columns, image->rows, &exception);
if (!pixels) {
MagickError(exception.severity, exception.reason, exception.description);
return (NULL);
}
if (type == GRAYSCALE) {
data = new GLubyte[w * h];
for (j = 0; j < image->rows; j++) {
for (i = 0; i < image->columns; i++) {
data[j*w+i] = pixels[i + image->columns*j].red;
}
}
} else {
data = new GLubyte[w * h * 4];
for (j = 0; j < image->rows; j++) {
for (i = 0; i < image->columns; i++) {
int idx = (j * w +i ) * 4;
data[idx+0] = pixels[i + image->columns*j].red;
data[idx+1] = pixels[i + image->columns*j].green;
data[idx+2] = pixels[i + image->columns*j].blue;
data[idx+3] = pixels[i + image->columns*j].opacity;
}
}
}
DestroyImageInfo (image_info);
DestroyExceptionInfo (&exception);
DestroyImage (image);
return data;
}
/// writes data to RGB image file
int
write_image (const char *filename,
const unsigned char *data,
int width,
int height)
{
ExceptionInfo exception;
Image *image;
Image *flip_image;
ImageInfo *image_info;
// initialize the image
GetExceptionInfo(&exception);
image = ConstituteImage (width, height, "RGB", CharPixel, data, &exception);
if (image == NULL) {
MagickError (exception.severity,exception.reason,exception.description);
return (1);
}
// image is flipped vertically, with data coming from frame buffer
flip_image = FlipImage (image, &exception);
DestroyImage (image);
if (!flip_image) {
MagickError (exception.severity, exception.reason, exception.description);
return (1);
}
image = flip_image;
image_info = CloneImageInfo ((ImageInfo *) NULL);
image_info->compression = NoCompression;
SetImageDepth (image, 8);
strncpy (image->filename, filename, MaxTextExtent);
image->filename[MaxTextExtent-1] = '\0';
if (!WriteImage (image_info, image)) {
MagickError (exception.severity,exception.reason,exception.description);
return (1);
}
DestroyImageInfo (image_info);
DestroyImage (image);
return (0);
}
/// glut idle callback
void
idle ()
{
glutPostRedisplay ();
}
/// glut display callback
void
display ()
{
bool display_fps = fps_counter.update ();
glClear (GL_COLOR_BUFFER_BIT);
br->SetBayer (bayer); // bayer is ptr to bayer image
br->Bind ();
br->EnableTextureTarget ();
glBegin (GL_QUADS);
glTexCoord2i (0, 0); glVertex2i (0, 0);
glTexCoord2i (width, 0); glVertex2i (width, 0);
glTexCoord2i (width, height); glVertex2i (width, height);
glTexCoord2i (0, height); glVertex2i (0, height);
glEnd();
br->DisableTextureTarget ();
glutSwapBuffers ();
// print FPS
if (display_fps) {
printf ("FPS: %3.2f\n", fps_counter.get_fps ());
}
}
/// glut reshape callback
void
reshape (int w,
int h)
{
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0, w, 0, h);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glutPostRedisplay ();
}
/// takes screenshot of rectangle with origin (x,y)
void
screenshot (int x,
int y)
{
unsigned char *p = NULL;
glReadBuffer (GL_FRONT);
p = new unsigned char[width * height * 3];
glReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, p);
if (write_image (SCREENSHOT_FILENAME, p, width, height) != 0) {
std::cerr << "ERROR: unable to write file '" << SCREENSHOT_FILENAME << "'" << std::endl;
}
delete [] p;
glReadBuffer (GL_BACK);
}
/// menu handler
void
select_from_menu (int command)
{
switch (command) {
case MENU_SCREENSHOT:
screenshot (0, 0);
break;
case MENU_QUIT:
if (br != NULL) {
delete br;
}
exit (EXIT_SUCCESS);
break;
}
}
/// glut keyboard callback
void
on_key_press (unsigned char key,
int x,
int y)
{
switch (key)
{
case 'Q':
case 'q':
case 27:
select_from_menu (MENU_QUIT);
break;
case 'S':
case 's':
select_from_menu (MENU_SCREENSHOT);
break;
};
glutPostRedisplay();
}
/// builds glut menu
int
build_glut_menu ()
{
int menu;
menu = glutCreateMenu (select_from_menu);
glutAddMenuEntry ("Screenshot (s)", MENU_SCREENSHOT);
glutAddMenuEntry ("Quit", MENU_QUIT);
glutAttachMenu (GLUT_RIGHT_BUTTON);
return menu;
}
/// main function
int
main (int argc,
char *argv[])
{
char bayer_filename[MaxTextExtent];
int w, h;
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <bayer image filename>" << std::endl;
std::cerr << "Displays demosaiced Bayer pattern image." << std::endl;
return (EXIT_FAILURE);
}
// copy filenames from argv
strncpy (bayer_filename, argv[1], MaxTextExtent-1);
bayer_filename[MaxTextExtent-1] = '\0';
// read bayer image
if ((bayer = read_image (bayer_filename, GRAYSCALE, &w, &h)) == NULL) {
std::cerr << "ERROR: unable to read image '" << bayer_filename << "'" << std::endl;
return (EXIT_FAILURE);
}
// init glut
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize (width, height);
glutInitWindowPosition (0, 0);
glutCreateWindow ("Bayer Viewer");
// Initialize GLEW
int err = glewInit ();
if (GLEW_OK != err) {
std::cerr << "GLEW ERROR: " << glewGetErrorString (err) << std::endl;
return (EXIT_FAILURE);
}
// init glut callbacks
glutKeyboardFunc (on_key_press);
glutReshapeFunc (reshape);
glutDisplayFunc (display);
glutIdleFunc (idle);
build_glut_menu ();
glPixelStorei (GL_PACK_ALIGNMENT, 1);
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glClearColor (0.0, 0.0, 0.0, 1.0);
// init bayer renderer
br = new BayerRenderer ();
if (!(br->Initialize (width, height))) {
std::cerr << "ERROR: unable to initialize BayerRenderer" << std::endl;
return (EXIT_FAILURE);
}
br->Bind ();
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
fps_counter.start ();
glutMainLoop ();
return (EXIT_SUCCESS);
}