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
/
Bayer.cpp
executable file
·414 lines (373 loc) · 9.72 KB
/
Bayer.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**
* @file bayer.cpp
* @author Max Smolens
* @brief Bayer pattern renderer.
*/
#include <iostream>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <GL/glew.h>
#ifdef WIN32
# include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glext.h>
#include <Cg/cg.h>
#include <Cg/cgGL.h>
#include "Image.hpp"
#include "BayerRenderer.hpp"
#include "GLUTFPSCounter.hpp"
#include "trackball.h"
// Frames per second counter.
static GLUTFPSCounter fps_counter;
// Cg context.
CGcontext context;
// Handles to vertex profiles and programs.
CGprofile vertexProfile, fragmentProfile;
CGprogram vertexProgram, fragmentProgram;
// Window dimensions.
static int w;
static int h;
// Image dimensions.
static int texw;
static int texh;
// Image options.
static float brightness = 1.0;
static float contrast = 1.0;
static bool grayscale = false;
// Pointer to bayer images.
#define STREAM
#ifdef STREAM
static GLubyte **data = NULL;
#else
static GLubyte *data = NULL;
#endif
static int start_time;
static int end_time;
static int num_frames = 0;
// Bayer renderer.
static BayerRenderer *br;
static void mouse (int button, int state, int x, int y);
static void motion (int x, int y);
static void keyboard (unsigned char key, int x, int y);
static void display ();
static void reshape (int width, int height);
static void idle ();
static void handleCgError ();
static void load_cg_programs ();
static void load_profiles ();
static void read_images (const char *prefix, int num_frames);
int
main (int argc,
char *argv[])
{
const int LEN = 20;
char prefix[LEN];
if (argc < 2) {
std::cerr << "USAGE: " << argv[0] << " <prefix> <num_frames>" << std::endl;
exit (EXIT_FAILURE);
}
snprintf (prefix, LEN, "%s", argv[1]);
prefix[LEN-1] = '\0';
// Read image stream or single image.
if (argc > 2) {
num_frames = strtol (argv[2], NULL, 10);
read_images (prefix, num_frames);
} else {
num_frames = 1;
data = new GLubyte*[1];
if ((data[0] = read_image (argv[1], texw, texh)) == NULL) {
std::cerr << "ERROR: cannot open file '" << argv[1] << "'." << std::endl;
exit (EXIT_FAILURE);
}
}
w = texw;
h = texh;
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (w, h);
glutCreateWindow ("Cg Bayer Renderer");
// Initialize GLEW
int err = glewInit ();
if (GLEW_OK != err) {
std::cerr << "GLEW ERROR: " << glewGetErrorString (err) << std::endl;
return (EXIT_FAILURE);
}
std::cerr << "Cg Bayer Renderer\nUsing GLEW " << glewGetString (GLEW_VERSION) << std::endl;
glutKeyboardFunc (keyboard);
glutMouseFunc (mouse);
glutMotionFunc (motion);
glutReshapeFunc (reshape);
glutDisplayFunc (display);
glutIdleFunc (idle);
cgSetErrorCallback (handleCgError);
context = cgCreateContext ();
load_profiles ();
load_cg_programs ();
glClearColor (0.0, 0.0, 0.0, 1.0);
// Initialize bayer renderer
br = new BayerRenderer ();
if (!(br->Initialize (texw, texh, context, vertexProfile, fragmentProfile))) {
std::cerr << "ERROR: unable to initialize BayerRenderer" << std::endl;
return (EXIT_FAILURE);
}
br->Bind ();
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
tbInit (GLUT_LEFT_BUTTON);
tbAnimate (true);
fps_counter.start ();
start_time = glutGet (GLUT_ELAPSED_TIME);
glutMainLoop ();
return (EXIT_SUCCESS);
}
// Glut idle callback.
static void
idle ()
{
glutPostRedisplay ();
}
// Glut display callback.
static void
display ()
{
static const GLdouble znear = 1.0;
static const GLdouble zfar = 10.0;
static const GLdouble zcenter = -0.5 * (zfar - znear);
static const GLdouble p = zcenter/znear;
static int count = 0;
static int n = 1;
static int increment = 1;
bool display_fps = fps_counter.update ();
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, znear, zfar);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.0, 0.0, p); // translate so image initially fills viewport
tbMatrix (); // trackball rotation
glTranslatef (0.0, 0.0, -zcenter); // translate so rotation is around zcenter
// Bind the vertex and fragment programs.
cgGLBindProgram (vertexProgram);
cgGLEnableProfile (vertexProfile);
cgGLBindProgram (fragmentProgram);
cgGLEnableProfile (fragmentProfile);
// Bind uniform parameters to the vertex shader.
cgGLSetStateMatrixParameter(cgGetNamedParameter(vertexProgram, "ModelViewProj"),
CG_GL_MODELVIEW_PROJECTION_MATRIX,
CG_GL_MATRIX_IDENTITY);
// Bind uniform parameters to the fragment shader.
cgGLSetParameter1f (cgGetNamedParameter (fragmentProgram, "brightness"), brightness);
cgGLSetParameter1f (cgGetNamedParameter (fragmentProgram, "contrast"), contrast);
cgGLSetParameter1f (cgGetNamedParameter (fragmentProgram, "grayscale"), grayscale);
// Enable texture for the fragment shader.
cgGLEnableTextureParameter (cgGetNamedParameter (fragmentProgram, "decal"));
// Render Bayer data.
br->SetBayer (data[n-1]);
br->Bind ();
br->EnableTextureTarget ();
glBegin (GL_QUADS);
glTexCoord2i (0, 0); glVertex3f (p, p, zcenter);
glTexCoord2i (texw, 0); glVertex3f (-p, p, zcenter);
glTexCoord2i (texw, texh); glVertex3f (-p, -p, zcenter);
glTexCoord2i (0, texh); glVertex3f (p, -p, zcenter);
glEnd();
br->DisableTextureTarget ();
// Disable texture for the fragment shader.
cgGLDisableTextureParameter (cgGetNamedParameter (fragmentProgram, "decal"));
cgGLDisableProfile (vertexProfile);
cgGLDisableProfile (fragmentProfile);
// Display frames per second.
if (display_fps) {
fprintf (stderr, "FPS: %3.2f\n", fps_counter.get_fps ());
}
#ifdef SAVEFRAMES
// Save frame to disk.
GLubyte *pixelbuffer = new GLubyte[w*h*3];
glReadPixels (0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixelbuffer);
char filename[20];
sprintf (filename, "%s%05d.png", "out/out", count);
write_image (filename, pixelbuffer, w, h);
delete [] pixelbuffer;
std::cerr<< "readpixels" << std::endl;
#endif
glutSwapBuffers ();
// Increment frame if enough time has elapsed.
static const float NTSC_FRAMERATE = 1.0/29.97;
end_time = glutGet (GLUT_ELAPSED_TIME);
float elapsed = (end_time - start_time) / 1e3;
if (elapsed > NTSC_FRAMERATE) {
count++;
n += increment;
if (n < 1) {
n = 1;
increment = 1;
} else if (n > (num_frames - 1)) {
n = num_frames;
increment = -1;
#ifdef SAVEFRAMES
// exit (0);
#endif
}
start_time = glutGet (GLUT_ELAPSED_TIME);
}
}
// Glut reshape callback.
static void
reshape (int width,
int height)
{
w = width;
h = height;
tbReshape (w, h);
glViewport (0, 0, w, h);
}
// Glut mouse callback.
static void
mouse (int button,
int state,
int x,
int y)
{
tbMouse (button, state, x, y);
}
// Glut mouse motion callback.
static void
motion (int x,
int y)
{
tbMotion (x, y);
// glutPostRedisplay ();
}
// Glut keyboard callback.
static void
keyboard (unsigned char key,
int x,
int y)
{
static const float MAXVAL = 10.0;
switch (key) {
case 27:
case 'Q':
case 'q':
cgDestroyContext (context);
exit (EXIT_SUCCESS);
break;
case 'G':
case 'g':
grayscale = !grayscale;
break;
case '<':
brightness -= 0.1;
if (brightness < 0.0) {
brightness = 0.0;
}
break;
case '>':
brightness += 0.1;
if (brightness > MAXVAL) {
brightness = MAXVAL;
}
break;
case '[':
contrast -= 0.1;
if (contrast < 0.0) {
contrast = 0.0;
}
break;
case ']':
contrast += 0.1;
if (contrast > MAXVAL) {
contrast = MAXVAL;
}
break;
case 'F':
case 'f':
glutFullScreen ();
break;
default:
break;
}
glutPostRedisplay ();
}
// Cg error callback.
static void
handleCgError ()
{
CGerror error = cgGetError ();
if (error) {
std::cerr << cgGetErrorString (error) << std::endl;
std::cerr << cgGetLastListing (context) << std::endl;
exit (EXIT_FAILURE);
}
}
// Loads the vertex and fragment profiles.
static void
load_profiles ()
{
if ((vertexProfile = cgGLGetLatestProfile (CG_GL_VERTEX)) == CG_PROFILE_UNKNOWN) {
std::cerr << "ERROR: cannot load vertex profile." << std::endl;
exit (EXIT_FAILURE);
}
cgGLSetOptimalOptions (vertexProfile);
if ((fragmentProfile = cgGLGetLatestProfile (CG_GL_FRAGMENT)) == CG_PROFILE_UNKNOWN) {
std::cerr << "ERROR: cannot load fragment profile." << std::endl;
exit (EXIT_FAILURE);
}
cgGLSetOptimalOptions (fragmentProfile);
}
// Compiles and loads vertex and fragment programs.
static void
load_cg_programs ()
{
if (!cgIsContext (context)) {
std::cerr << "ERROR: invalid Cg context" << std::endl;
exit (EXIT_FAILURE);
}
// Compile and load the vertex program.
vertexProgram = cgCreateProgramFromFile (
context,
CG_SOURCE,
"imagev.cg",
vertexProfile,
"imagev", // entry point
NULL); // arguments
if (!cgIsProgramCompiled (vertexProgram)) {
cgCompileProgram (vertexProgram);
}
// Enable the appropriate vertex profile and load the vertex program.
cgGLLoadProgram (vertexProgram);
// Compile and load the fragment program.
fragmentProgram = cgCreateProgramFromFile (
context,
CG_SOURCE,
"imagef.cg",
fragmentProfile,
"imagef", // entry point
NULL); // arguments
if (!cgIsProgramCompiled (fragmentProgram)) {
cgCompileProgram (fragmentProgram);
}
// Enable the appropriate vertex profile and load the vertex program.
cgGLLoadProgram (fragmentProgram);
}
// Reads a stream of images from the disk.
static void
read_images (const char *prefix,
int num_frames)
{
char filename[40];
int prefix_len = strlen (prefix);
sprintf (filename, "%s", prefix);
data = new GLubyte*[num_frames];
for (int i = 1; i <= num_frames; i++) {
sprintf (filename+prefix_len, "%08d.png.png", i);
if ((data[i-1] = read_image (filename, texw, texh)) == NULL) {
std::cerr << "ERROR: cannot open file '" << filename << "'." << std::endl;
}
std::cerr << filename << std::endl;
}
}