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