]> git.sesse.net Git - movit/blob - test.cpp
Add a Makefile.
[movit] / test.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2 #define NO_SDL_GLEXT 1
3
4 #define WIDTH 1280
5 #define HEIGHT 720
6 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
7
8 #define HSV_WHEEL_SIZE 128
9
10 #include <math.h>
11 #include <time.h>
12
13 #include <SDL/SDL.h>
14 #include <SDL/SDL_opengl.h>
15 #include <SDL/SDL_image.h>
16
17 #include <GL/gl.h>
18 #include <GL/glext.h>
19
20 #ifdef NDEBUG
21 #define check_error()
22 #else
23 #define check_error() { int err = glGetError(); if (err != GL_NO_ERROR) { printf("GL error 0x%x at line %d\n", err, __LINE__); exit(1); } }
24 #endif
25
26 unsigned char result[WIDTH * HEIGHT * 4];
27
28 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
29 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
30 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
31 float saturation = 1.0f;
32
33 float lift_r = 0.0f, lift_g = 0.0f, lift_b = 0.0f;
34 float gamma_r = 1.0f, gamma_g = 1.0f, gamma_b = 1.0f;
35 float gain_r = 1.0f, gain_g = 1.0f, gain_b = 1.0f;
36
37 enum textures {
38         SOURCE_IMAGE = 1,
39         SRGB_LUT = 2,
40         SRGB_REVERSE_LUT = 3,
41         HSV_WHEEL = 4,
42 };
43
44 // assumes h in [0, 2pi> or [-pi, pi>
45 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
46 {
47         if (h < 0.0f) {
48                 h += 2.0f * M_PI;
49         }
50         float c = v * s;
51         float hp = (h * 180.0 / M_PI) / 60.0;
52         float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
53
54         if (hp >= 0 && hp < 1) {
55                 *r = c;
56                 *g = x;
57                 *b = 0.0f;
58         } else if (hp >= 1 && hp < 2) {
59                 *r = x;
60                 *g = c;
61                 *b = 0.0f;
62         } else if (hp >= 2 && hp < 3) {
63                 *r = 0.0f;
64                 *g = c;
65                 *b = x;
66         } else if (hp >= 3 && hp < 4) {
67                 *r = 0.0f;
68                 *g = x;
69                 *b = c;
70         } else if (hp >= 4 && hp < 5) {
71                 *r = x;
72                 *g = 0.0f;
73                 *b = c;
74         } else {
75                 *r = c;
76                 *g = 0.0f;
77                 *b = x;
78         }
79
80         float m = v - c;
81         *r += m;
82         *g += m;
83         *b += m;
84 }
85
86 GLhandleARB read_shader(const char* filename, GLenum type)
87 {
88         static char buf[131072];
89         FILE *fp = fopen(filename, "r");
90         if (fp == NULL) {
91                 perror(filename);
92                 exit(1);
93         }
94
95         int len = fread(buf, 1, sizeof(buf), fp);
96         fclose(fp);
97
98         GLhandleARB obj = glCreateShaderObjectARB(type);
99         const GLchar* source[] = { buf };
100         const GLint length[] = { len };
101         glShaderSource(obj, 1, source, length);
102         glCompileShader(obj);
103
104         GLchar info_log[4096];
105         GLsizei log_length = sizeof(info_log) - 1;
106         glGetShaderInfoLog(obj, log_length, &log_length, info_log);
107         info_log[log_length] = 0; 
108         printf("shader compile log: %s\n", info_log);
109
110         GLint status;
111         glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
112         if (status == GL_FALSE) {
113                 exit(1);
114         }
115
116         return obj;
117 }
118
119 void draw_picture_quad(GLint prog, int frame)
120 {
121         glUseProgramObjectARB(prog);
122         check_error();
123
124         glActiveTexture(GL_TEXTURE0);
125         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
126         glUniform1i(glGetUniformLocation(prog, "tex"), 0);
127
128         glActiveTexture(GL_TEXTURE1);
129         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
130         glUniform1i(glGetUniformLocation(prog, "srgb_tex"), 1);
131
132         glActiveTexture(GL_TEXTURE2);
133         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
134         glUniform1i(glGetUniformLocation(prog, "srgb_reverse_tex"), 2);
135
136         glUniform3f(glGetUniformLocation(prog, "lift"), lift_r, lift_g, lift_b);
137         //glUniform3f(glGetUniformLocation(prog, "gamma"), gamma_r, gamma_g, gamma_b);
138         glUniform3f(glGetUniformLocation(prog, "inv_gamma_22"),
139                     2.2f / gamma_r,
140                     2.2f / gamma_g,
141                     2.2f / gamma_b);
142         glUniform3f(glGetUniformLocation(prog, "gain_pow_inv_gamma"),
143                     pow(gain_r, 1.0f / gamma_r),
144                     pow(gain_g, 1.0f / gamma_g),
145                     pow(gain_b, 1.0f / gamma_b));
146         glUniform1f(glGetUniformLocation(prog, "saturation"), saturation);
147
148         glDisable(GL_BLEND);
149         check_error();
150         glDisable(GL_DEPTH_TEST);
151         check_error();
152         glDepthMask(GL_FALSE);
153         check_error();
154
155         glMatrixMode(GL_PROJECTION);
156         glLoadIdentity();
157         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
158
159         glMatrixMode(GL_MODELVIEW);
160         glLoadIdentity();
161
162         glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
163 //      glClear(GL_COLOR_BUFFER_BIT);
164         check_error();
165
166         glBegin(GL_QUADS);
167
168         glTexCoord2f(0.0f, 1.0f);
169         glVertex2f(0.0f, 0.0f);
170
171         glTexCoord2f(1.0f, 1.0f);
172         glVertex2f(1.0f, 0.0f);
173
174         glTexCoord2f(1.0f, 0.0f);
175         glVertex2f(1.0f, 1.0f);
176
177         glTexCoord2f(0.0f, 0.0f);
178         glVertex2f(0.0f, 1.0f);
179
180         glEnd();
181         check_error();
182 }
183
184 void draw_hsv_wheel(float y, float rad, float theta, float value)
185 {
186         glUseProgramObjectARB(0);
187         check_error();
188         glActiveTexture(GL_TEXTURE0);
189         check_error();
190         glEnable(GL_TEXTURE_2D);
191         check_error();
192         glBindTexture(GL_TEXTURE_2D, HSV_WHEEL);
193         check_error();
194         glActiveTexture(GL_TEXTURE1);
195         check_error();
196         glBindTexture(GL_TEXTURE_2D, 0);
197         check_error();
198         glActiveTexture(GL_TEXTURE0);
199         glEnable(GL_BLEND);
200         check_error();
201         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
202         check_error();
203
204         // wheel
205         glBegin(GL_QUADS);
206
207         glTexCoord2f(0.0f, 1.0f);
208         glVertex2f(0.0f, y);
209
210         glTexCoord2f(1.0f, 1.0f);
211         glVertex2f(0.2f * 9.0f / 16.0f, y);
212
213         glTexCoord2f(1.0f, 0.0f);
214         glVertex2f(0.2f * 9.0f / 16.0f, y + 0.2f);
215
216         glTexCoord2f(0.0f, 0.0f);
217         glVertex2f(0.0f, y + 0.2f);
218
219         glEnd();
220
221         // wheel selector
222         glDisable(GL_TEXTURE_2D);
223         glColor3f(0.0f, 0.0f, 0.0f);
224         glPointSize(5.0f);
225         glBegin(GL_POINTS);
226         glVertex2f((0.1f + rad * cos(theta) * 0.1f) * 9.0f / 16.0f, y + 0.1f - rad * sin(theta) * 0.1f);
227         glEnd();
228         
229         // value slider
230         glDisable(GL_TEXTURE_2D);
231         glBegin(GL_QUADS);
232
233         glColor3f(0.0f, 0.0f, 0.0f);
234         glVertex2f(0.22f * 9.0f / 16.0f, y);
235         glVertex2f(0.24f * 9.0f / 16.0f, y);
236
237         glColor3f(1.0f, 1.0f, 1.0f);
238         glVertex2f(0.24f * 9.0f / 16.0f, y + 0.2f);
239         glVertex2f(0.22f * 9.0f / 16.0f, y + 0.2f);
240
241         glEnd();
242
243         // value selector
244         glColor3f(0.0f, 0.0f, 0.0f);
245         glPointSize(5.0f);
246         glBegin(GL_POINTS);
247         glVertex2f(0.23f * 9.0f / 16.0f, y + value * 0.2f);
248         glEnd();
249
250         glColor3f(1.0f, 1.0f, 1.0f);
251 }
252
253 void draw_saturation_bar(float y)
254 {
255         glUseProgramObjectARB(0);
256         check_error();
257
258         // value slider
259         glDisable(GL_TEXTURE_2D);
260         glBegin(GL_QUADS);
261
262         glColor3f(0.0f, 0.0f, 0.0f);
263         glVertex2f(0.0f * 9.0f / 16.0f, y + 0.02f);
264         glVertex2f(0.0f * 9.0f / 16.0f, y);
265
266         glColor3f(1.0f, 1.0f, 1.0f);
267         glVertex2f(0.2f * 9.0f / 16.0f, y);
268         glVertex2f(0.2f * 9.0f / 16.0f, y + 0.02f);
269
270         glEnd();
271
272         // value selector
273         glColor3f(0.0f, 0.0f, 0.0f);
274         glPointSize(5.0f);
275         glBegin(GL_POINTS);
276         glVertex2f(0.2f * (saturation / 4.0f) * 9.0f / 16.0f, y + 0.01f);
277         glEnd();
278
279         glColor3f(1.0f, 1.0f, 1.0f);
280 }
281
282 void make_hsv_wheel_texture()
283 {
284         static unsigned char hsv_pix[HSV_WHEEL_SIZE * HSV_WHEEL_SIZE * 4];
285         for (int y = 0; y < HSV_WHEEL_SIZE; ++y) {
286                 for (int x = 0; x < HSV_WHEEL_SIZE; ++x) {
287                         float yf = 2.0f * y / (float)(HSV_WHEEL_SIZE) - 1.0f;
288                         float xf = 2.0f * x / (float)(HSV_WHEEL_SIZE) - 1.0f;
289                         float rad = hypot(xf, yf);
290                         float theta = atan2(yf, xf);
291
292                         float r, g, b;
293                         hsv2rgb(theta, rad, 1.0f, &r, &g, &b);
294                         hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 0] = lrintf(r * 255.0f);
295                         hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 1] = lrintf(g * 255.0f);
296                         hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 2] = lrintf(b * 255.0f);
297
298                         if (rad > 1.0f) {
299                                 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 0;
300                         } else {
301                                 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 255;
302                         }
303                 }
304                 printf("\n");
305         }
306
307         glBindTexture(GL_TEXTURE_2D, HSV_WHEEL);
308         check_error();
309         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
310         check_error();
311         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, HSV_WHEEL_SIZE, HSV_WHEEL_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, hsv_pix);
312         check_error();
313 }
314
315 void update_hsv()
316 {
317         hsv2rgb(lift_theta, lift_rad, lift_v, &lift_r, &lift_g, &lift_b);
318         hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma_r, &gamma_g, &gamma_b);
319         hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain_r, &gain_g, &gain_b);
320
321         if (saturation < 0.0) {
322                 saturation = 0.0;
323         }
324
325         printf("lift: %f %f %f\n", lift_r, lift_g, lift_b);
326         printf("gamma: %f %f %f\n", gamma_r, gamma_g, gamma_b);
327         printf("gain: %f %f %f\n", gain_r, gain_g, gain_b);
328         printf("saturation: %f\n", saturation);
329         printf("\n");
330 }
331
332 void read_colorwheel(float xf, float yf, float *rad, float *theta, float *value)
333 {
334         if (xf < 0.2f && yf < 0.2f) {
335                 float xp = 2.0f * xf / 0.2f - 1.0f;
336                 float yp = -(2.0f * yf / 0.2f - 1.0f);
337                 *rad = hypot(xp, yp);
338                 *theta = atan2(yp, xp);
339                 if (*rad > 1.0) {
340                         *rad = 1.0;
341                 }
342         } else if (xf >= 0.22f && xf <= 0.24f) {
343                 *value = yf / 0.2f;
344         }
345 }
346
347 void mouse(int x, int y)
348 {
349         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
350         float yf = (HEIGHT - y) / (float)HEIGHT;
351
352         if (yf < 0.2f) {
353                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
354         } else if (yf >= 0.2f && yf < 0.4f) {
355                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
356         } else if (yf >= 0.4f && yf < 0.6f) {
357                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
358         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
359                 saturation = (xf / 0.2f) * 4.0f;
360         }
361
362         update_hsv();
363 }
364
365 void load_texture(const char *filename)
366 {
367         SDL_Surface *img = IMG_Load(filename);
368         if (img == NULL) {
369                 fprintf(stderr, "Load of '%s' failed\n", filename);
370                 exit(1);
371         }
372
373         // Convert to RGB.
374         SDL_PixelFormat *fmt = img->format;
375         SDL_LockSurface(img);
376         unsigned char *src_pixels = (unsigned char *)img->pixels;
377         unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 3);
378         for (unsigned i = 0; i < img->w * img->h; ++i) {
379                 unsigned char r, g, b;
380                 unsigned int temp;
381                 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
382
383                 temp = pixel & fmt->Rmask;
384                 temp = temp >> fmt->Rshift;
385                 temp = temp << fmt->Rloss;
386                 r = temp;
387
388                 temp = pixel & fmt->Gmask;
389                 temp = temp >> fmt->Gshift;
390                 temp = temp << fmt->Gloss;
391                 g = temp;
392
393                 temp = pixel & fmt->Bmask;
394                 temp = temp >> fmt->Bshift;
395                 temp = temp << fmt->Bloss;
396                 b = temp;
397
398                 dst_pixels[i * 3 + 0] = r;
399                 dst_pixels[i * 3 + 1] = g;
400                 dst_pixels[i * 3 + 2] = b;
401         }
402         SDL_UnlockSurface(img);
403
404 #if 1
405         // we will convert to sRGB in the shader
406         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
407         check_error();
408 #else
409         // implicit sRGB conversion in hardware
410         glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
411         check_error();
412 #endif
413         free(dst_pixels);
414         SDL_FreeSurface(img);
415 }
416
417 void write_ppm(const char *filename, unsigned char *screenbuf)
418 {
419         FILE *fp = fopen(filename, "w");
420         fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
421         for (unsigned y = 0; y < HEIGHT; ++y) {
422                 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
423                 for (unsigned x = 0; x < WIDTH; ++x) {
424                         fputc(srcptr[x * 4 + 2], fp);
425                         fputc(srcptr[x * 4 + 1], fp);
426                         fputc(srcptr[x * 4 + 0], fp);
427                 }
428         }
429         fclose(fp);
430 }
431
432 int main(int argc, char **argv)
433 {
434         int quit = 0;
435
436         SDL_Init(SDL_INIT_EVERYTHING);
437         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
438         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
439         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
440         SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
441         SDL_WM_SetCaption("OpenGL window", NULL);
442         
443         // geez 
444         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
445         glPixelStorei(GL_PACK_ALIGNMENT, 1);
446
447         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
448         check_error();
449         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
450         check_error();
451
452         load_texture("blg_wheels_woman_1.jpg");
453         //glGenerateMipmap(GL_TEXTURE_2D);
454         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
455         //check_error();
456
457         //load_texture("maserati_gts_wallpaper_1280x720_01.jpg");
458         //load_texture("90630d1295075297-console-games-wallpapers-wallpaper_need_for_speed_prostreet_09_1920x1080.jpg");
459         //load_texture("glacier-lake-1280-720-4087.jpg");
460
461 #if 0
462         // sRGB reverse LUT
463         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
464         check_error();
465         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
466         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
467         check_error();
468         float srgb_reverse_tex[4096];
469         for (unsigned i = 0; i < 4096; ++i) {
470                 float x = i / 4095.0;
471                 if (x < 0.0031308f) {
472                         srgb_reverse_tex[i] = 12.92f * x;
473                 } else {
474                         srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
475                 }
476         }
477         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
478         check_error();
479
480         // sRGB LUT
481         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
482         check_error();
483         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
484         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
485         check_error();
486         float srgb_tex[256];
487         for (unsigned i = 0; i < 256; ++i) {
488                 float x = i / 255.0;
489                 if (x < 0.04045f) {
490                         srgb_tex[i] = x * (1.0f / 12.92f);
491                 } else {
492                         srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
493                 }
494         }
495         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
496         check_error();
497 #endif
498
499         // generate a PDO to hold the data we read back with glReadPixels()
500         // (Intel/DRI goes into a slow path if we don't read to PDO)
501         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
502         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
503
504         make_hsv_wheel_texture();
505         update_hsv();
506
507         int prog = glCreateProgram();
508         GLhandleARB vs_obj = read_shader("vs.glsl", GL_VERTEX_SHADER);
509         GLhandleARB fs_obj = read_shader("fs.glsl", GL_FRAGMENT_SHADER);
510         glAttachObjectARB(prog, vs_obj);
511         check_error();
512         glAttachObjectARB(prog, fs_obj);
513         check_error();
514         glLinkProgram(prog);
515         check_error();
516
517         GLchar info_log[4096];
518         GLsizei log_length = sizeof(info_log) - 1;
519         log_length = sizeof(info_log) - 1;
520         glGetProgramInfoLog(prog, log_length, &log_length, info_log);
521         info_log[log_length] = 0; 
522         printf("link: %s\n", info_log);
523
524         struct timespec start, now;
525         int frame = 0, screenshot = 0;
526         clock_gettime(CLOCK_MONOTONIC, &start);
527
528         while (!quit) {
529                 SDL_Event event;
530                 while (SDL_PollEvent(&event)) {
531                         if (event.type == SDL_QUIT) {
532                                 quit = 1;
533                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
534                                 quit = 1;
535                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
536                                 screenshot = 1;
537                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
538                                 mouse(event.button.x, event.button.y);
539                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
540                                 mouse(event.motion.x, event.motion.y);
541                         }
542                 }
543
544                 ++frame;
545
546                 draw_picture_quad(prog, frame);
547                 
548                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
549                 check_error();
550
551                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
552                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
553                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
554                 draw_saturation_bar(0.6f);
555
556                 SDL_GL_SwapBuffers();
557                 check_error();
558
559                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
560                 check_error();
561                 if (screenshot) {
562                         char filename[256];
563                         sprintf(filename, "frame%05d.ppm", frame);
564                         write_ppm(filename, screenbuf);
565                         printf("Screenshot: %s\n", filename);
566                         screenshot = 0;
567                 }
568                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
569                 check_error();
570
571 #if 1
572                 clock_gettime(CLOCK_MONOTONIC, &now);
573                 double elapsed = now.tv_sec - start.tv_sec +
574                         1e-9 * (now.tv_nsec - start.tv_nsec);
575                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
576                         frame, elapsed, frame / elapsed,
577                         1e3 * elapsed / frame);
578 #endif
579         }
580         return 0; 
581 }