Add fallbacks for systems without proper monotonic clocks (e.g. OS X).
[movit] / main.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 #include <string.h>
9 #include <math.h>
10 #include <time.h>
11 #include <assert.h>
12
13 #include <string>
14 #include <vector>
15 #include <map>
16
17 #include <SDL/SDL.h>
18 #include <SDL/SDL_opengl.h>
19 #include <SDL/SDL_image.h>
20
21 #include <GL/gl.h>
22 #include <GL/glext.h>
23
24 #include "effect.h"
25 #include "effect_chain.h"
26 #include "util.h"
27 #include "widgets.h"
28 #include "texture_enum.h"
29
30 unsigned char result[WIDTH * HEIGHT * 4];
31
32 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
33 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
34 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
35 float saturation = 1.0f;
36
37 float radius = 0.3f;
38 float inner_radius = 0.3f;
39
40 void update_hsv(Effect *lift_gamma_gain_effect, Effect *saturation_effect)
41 {
42         RGBTriplet lift(0.0f, 0.0f, 0.0f);
43         RGBTriplet gamma(1.0f, 1.0f, 1.0f);
44         RGBTriplet gain(1.0f, 1.0f, 1.0f);
45
46         hsv2rgb(lift_theta, lift_rad, lift_v, &lift.r, &lift.g, &lift.b);
47         hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b);
48         hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b);
49
50         bool ok = lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
51         ok = ok && lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
52         ok = ok && lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
53         assert(ok);
54
55         if (saturation < 0.0) {
56                 saturation = 0.0;
57         }
58         ok = saturation_effect->set_float("saturation", saturation);
59         assert(ok);
60 }
61
62 void mouse(int x, int y)
63 {
64         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
65         float yf = (HEIGHT - y) / (float)HEIGHT;
66
67         if (yf < 0.2f) {
68                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
69         } else if (yf >= 0.2f && yf < 0.4f) {
70                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
71         } else if (yf >= 0.4f && yf < 0.6f) {
72                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
73         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
74                 saturation = (xf / 0.2f) * 4.0f;
75         } else if (yf >= 0.65f && yf < 0.67f && xf < 0.2f) {
76                 radius = (xf / 0.2f);
77         } else if (yf >= 0.70f && yf < 0.72f && xf < 0.2f) {
78                 inner_radius = (xf / 0.2f);
79         }
80 }
81
82 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
83 {
84         SDL_Surface *img = IMG_Load(filename);
85         if (img == NULL) {
86                 fprintf(stderr, "Load of '%s' failed\n", filename);
87                 exit(1);
88         }
89
90         // Convert to RGB.
91         SDL_PixelFormat *fmt = img->format;
92         SDL_LockSurface(img);
93         unsigned char *src_pixels = (unsigned char *)img->pixels;
94         unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 3);
95         for (int i = 0; i < img->w * img->h; ++i) {
96                 unsigned char r, g, b;
97                 unsigned int temp;
98                 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
99
100                 temp = pixel & fmt->Rmask;
101                 temp = temp >> fmt->Rshift;
102                 temp = temp << fmt->Rloss;
103                 r = temp;
104
105                 temp = pixel & fmt->Gmask;
106                 temp = temp >> fmt->Gshift;
107                 temp = temp << fmt->Gloss;
108                 g = temp;
109
110                 temp = pixel & fmt->Bmask;
111                 temp = temp >> fmt->Bshift;
112                 temp = temp << fmt->Bloss;
113                 b = temp;
114
115                 dst_pixels[i * 3 + 0] = r;
116                 dst_pixels[i * 3 + 1] = g;
117                 dst_pixels[i * 3 + 2] = b;
118         }
119         SDL_UnlockSurface(img);
120
121         *w = img->w;
122         *h = img->h;
123
124         SDL_FreeSurface(img);
125
126         return dst_pixels;
127 }
128
129 void write_ppm(const char *filename, unsigned char *screenbuf)
130 {
131         FILE *fp = fopen(filename, "w");
132         fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
133         for (unsigned y = 0; y < HEIGHT; ++y) {
134                 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
135                 for (unsigned x = 0; x < WIDTH; ++x) {
136                         fputc(srcptr[x * 4 + 2], fp);
137                         fputc(srcptr[x * 4 + 1], fp);
138                         fputc(srcptr[x * 4 + 0], fp);
139                 }
140         }
141         fclose(fp);
142 }
143
144 int main(int argc, char **argv)
145 {
146         int quit = 0;
147
148         SDL_Init(SDL_INIT_EVERYTHING);
149         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
150         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
151         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
152         SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
153         SDL_WM_SetCaption("OpenGL window", NULL);
154         
155         // geez 
156         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
157         glPixelStorei(GL_PACK_ALIGNMENT, 1);
158
159         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
160         check_error();
161         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
162         check_error();
163
164         unsigned img_w, img_h;
165         unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
166
167         EffectChain chain(WIDTH, HEIGHT);
168
169         ImageFormat inout_format;
170         inout_format.pixel_format = FORMAT_RGB;
171         inout_format.color_space = COLORSPACE_sRGB;
172         inout_format.gamma_curve = GAMMA_sRGB;
173
174         chain.add_input(inout_format);
175         Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN);
176         Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION);
177         Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE);
178         chain.add_output(inout_format);
179         chain.finalize();
180
181         //glGenerateMipmap(GL_TEXTURE_2D);
182         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
183         //check_error();
184
185 #if 0
186         // sRGB reverse LUT
187         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
188         check_error();
189         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
190         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
191         check_error();
192         float srgb_reverse_tex[4096];
193         for (unsigned i = 0; i < 4096; ++i) {
194                 float x = i / 4095.0;
195                 if (x < 0.0031308f) {
196                         srgb_reverse_tex[i] = 12.92f * x;
197                 } else {
198                         srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
199                 }
200         }
201         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
202         check_error();
203
204         // sRGB LUT
205         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
206         check_error();
207         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
208         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
209         check_error();
210         float srgb_tex[256];
211         for (unsigned i = 0; i < 256; ++i) {
212                 float x = i / 255.0;
213                 if (x < 0.04045f) {
214                         srgb_tex[i] = x * (1.0f / 12.92f);
215                 } else {
216                         srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
217                 }
218         }
219         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
220         check_error();
221 #endif
222
223         // generate a PDO to hold the data we read back with glReadPixels()
224         // (Intel/DRI goes into a slow path if we don't read to PDO)
225         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
226         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
227
228         make_hsv_wheel_texture();
229
230         int frame = 0, screenshot = 0;
231 #if _POSIX_C_SOURCE >= 199309L
232         struct timespec start, now;
233         clock_gettime(CLOCK_MONOTONIC, &start);
234 #else
235         struct timeval start, now;
236         gettimeofday(&start, NULL);
237 #endif
238
239         while (!quit) {
240                 SDL_Event event;
241                 while (SDL_PollEvent(&event)) {
242                         if (event.type == SDL_QUIT) {
243                                 quit = 1;
244                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
245                                 quit = 1;
246                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
247                                 screenshot = 1;
248                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
249                                 mouse(event.button.x, event.button.y);
250                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
251                                 mouse(event.motion.x, event.motion.y);
252                         }
253                 }
254
255                 ++frame;
256
257                 update_hsv(lift_gamma_gain_effect, saturation_effect);
258                 vignette_effect->set_float("radius", radius);
259                 vignette_effect->set_float("inner_radius", inner_radius);
260                 chain.render_to_screen(src_img);
261                 
262                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
263                 check_error();
264
265                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
266                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
267                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
268                 draw_saturation_bar(0.6f, saturation / 4.0f);
269                 draw_saturation_bar(0.65f, radius);
270                 draw_saturation_bar(0.70f, inner_radius);
271
272                 SDL_GL_SwapBuffers();
273                 check_error();
274
275                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
276                 check_error();
277                 if (screenshot) {
278                         char filename[256];
279                         sprintf(filename, "frame%05d.ppm", frame);
280                         write_ppm(filename, screenbuf);
281                         printf("Screenshot: %s\n", filename);
282                         screenshot = 0;
283                 }
284                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
285                 check_error();
286
287 #if 1
288 #if _POSIX_C_SOURCE >= 199309L
289                 clock_gettime(CLOCK_MONOTONIC, &now);
290                 double elapsed = now.tv_sec - start.tv_sec +
291                         1e-9 * (now.tv_nsec - start.tv_nsec);
292 #else
293                 gettimeofday(&now, NULL);
294                 double elapsed = now.tv_sec - start.tv_sec +
295                         1e-6 * (now.tv_usec - start.tv_usec);
296 #endif
297                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
298                         frame, elapsed, frame / elapsed,
299                         1e3 * elapsed / frame);
300 #endif
301         }
302         return 0; 
303 }