]> git.sesse.net Git - movit/blob - demo.cpp
Add the rest of the files for the premultiplied alpha commit.
[movit] / demo.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2 #define NO_SDL_GLEXT 1
3
4 #define WIDTH 1280
5 #define HEIGHT 720
6
7 #include <string.h>
8 #include <math.h>
9 #include <time.h>
10 #include <sys/time.h>
11 #include <assert.h>
12
13 #include <string>
14 #include <vector>
15 #include <map>
16
17 #include <GL/glew.h>
18
19 #include <SDL/SDL.h>
20 #include <SDL/SDL_opengl.h>
21 #include <SDL/SDL_image.h>
22
23 #include "init.h"
24 #include "effect.h"
25 #include "effect_chain.h"
26 #include "util.h"
27 #include "widgets.h"
28
29 #include "flat_input.h"
30 #include "lift_gamma_gain_effect.h"
31 #include "saturation_effect.h"
32 #include "diffusion_effect.h"
33 #include "overlay_effect.h"
34 #include "resample_effect.h"
35 #include "resize_effect.h"
36
37 unsigned char result[WIDTH * HEIGHT * 4];
38
39 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
40 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
41 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
42 float saturation = 1.0f;
43
44 //float radius = 0.3f;
45 // float inner_radius = 0.3f;
46 float blur_radius = 20.0f;
47 float blurred_mix_amount = 0.5f;
48         
49 void update_hsv(Effect *lift_gamma_gain_effect, Effect *saturation_effect)
50 {
51         RGBTriplet lift(0.0f, 0.0f, 0.0f);
52         RGBTriplet gamma(1.0f, 1.0f, 1.0f);
53         RGBTriplet gain(1.0f, 1.0f, 1.0f);
54
55         hsv2rgb_normalized(lift_theta, lift_rad, lift_v, &lift.r, &lift.g, &lift.b);
56         hsv2rgb_normalized(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b);
57         hsv2rgb_normalized(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b);
58
59         bool ok = lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
60         ok = ok && lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
61         ok = ok && lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
62         assert(ok);
63
64         if (saturation < 0.0) {
65                 saturation = 0.0;
66         }
67         ok = saturation_effect->set_float("saturation", saturation);
68         assert(ok);
69 }
70
71 void mouse(int x, int y)
72 {
73         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
74         float yf = (HEIGHT - y) / (float)HEIGHT;
75
76         if (yf < 0.2f) {
77                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
78         } else if (yf >= 0.2f && yf < 0.4f) {
79                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
80         } else if (yf >= 0.4f && yf < 0.6f) {
81                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
82         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
83                 saturation = (xf / 0.2f) * 4.0f;
84 #if 0
85         } else if (yf >= 0.65f && yf < 0.67f && xf < 0.2f) {
86                 radius = (xf / 0.2f);
87         } else if (yf >= 0.70f && yf < 0.72f && xf < 0.2f) {
88                 inner_radius = (xf / 0.2f);
89 #endif
90         } else if (yf >= 0.75f && yf < 0.77f && xf < 0.2f) {
91                 blur_radius = (xf / 0.2f) * 100.0f;
92         } else if (yf >= 0.80f && yf < 0.82f && xf < 0.2f) {
93                 blurred_mix_amount = (xf / 0.2f);
94         }
95 }
96
97 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
98 {
99         SDL_Surface *img = IMG_Load(filename);
100         if (img == NULL) {
101                 fprintf(stderr, "Load of '%s' failed\n", filename);
102                 exit(1);
103         }
104
105         SDL_PixelFormat rgba_fmt;
106         rgba_fmt.palette = NULL;
107         rgba_fmt.BitsPerPixel = 32;
108         rgba_fmt.BytesPerPixel = 8;
109         rgba_fmt.Rloss = rgba_fmt.Gloss = rgba_fmt.Bloss = rgba_fmt.Aloss = 0;
110
111         // NOTE: Assumes little endian.
112         rgba_fmt.Rmask = 0x00ff0000;
113         rgba_fmt.Gmask = 0x0000ff00;
114         rgba_fmt.Bmask = 0x000000ff;
115         rgba_fmt.Amask = 0xff000000;
116
117         rgba_fmt.Rshift = 16;
118         rgba_fmt.Gshift = 8;
119         rgba_fmt.Bshift = 0;
120         rgba_fmt.Ashift = 24;
121         
122         rgba_fmt.colorkey = 0;
123         rgba_fmt.alpha = 255;
124
125         SDL_Surface *converted = SDL_ConvertSurface(img, &rgba_fmt, SDL_SWSURFACE);
126
127         *w = img->w;
128         *h = img->h;
129
130         SDL_FreeSurface(img);
131
132         unsigned char *x = (unsigned char *)converted->pixels;
133         for (int i = 0; i < img->w * img->h; ++i) {
134                 if (x[i * 4 + 3] == 0) {
135                         x[i * 4 + 0] = 255;
136                         x[i * 4 + 1] = 255;
137                         x[i * 4 + 2] = 255;
138                 }
139         }
140
141         return (unsigned char *)converted->pixels;
142 }
143
144 void write_ppm(const char *filename, unsigned char *screenbuf)
145 {
146         FILE *fp = fopen(filename, "w");
147         fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
148         for (unsigned y = 0; y < HEIGHT; ++y) {
149                 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
150                 for (unsigned x = 0; x < WIDTH; ++x) {
151                         fputc(srcptr[x * 4 + 2], fp);
152                         fputc(srcptr[x * 4 + 1], fp);
153                         fputc(srcptr[x * 4 + 0], fp);
154                 }
155         }
156         fclose(fp);
157 }
158
159 int main(int argc, char **argv)
160 {
161         bool quit = false;
162
163         SDL_Init(SDL_INIT_EVERYTHING);
164         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
165         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
166         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
167         SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
168         SDL_WM_SetCaption("OpenGL window", NULL);
169
170         init_movit(".", MOVIT_DEBUG_ON);
171         printf("GPU texture subpixel precision: about %.1f bits\n",
172                 log2(1.0f / movit_texel_subpixel_precision));
173         
174         unsigned img_w, img_h;
175         unsigned char *src_img = load_image(argc > 1 ? argv[1] : "blg_wheels_woman_1.jpg", &img_w, &img_h);
176
177         EffectChain chain(WIDTH, HEIGHT);
178         glViewport(0, 0, WIDTH, HEIGHT);
179
180         ImageFormat inout_format;
181         inout_format.color_space = COLORSPACE_sRGB;
182         inout_format.gamma_curve = GAMMA_LINEAR;
183
184         FlatInput *input = new FlatInput(inout_format, FORMAT_BGRA_POSTMULTIPLIED_ALPHA, GL_UNSIGNED_BYTE, img_w, img_h);
185         chain.add_input(input);
186
187         unsigned char *src_overlay1 = load_image("overlay1.png", &img_w, &img_h);
188 #if 0
189         float *src_bleh = new float[img_w * img_h * 4];
190         for (int i = 0; i < img_w * img_h; ++i) {
191                 float r = src_overlay1[i * 4 + 0] / 255.0f;
192                 float g = src_overlay1[i * 4 + 1] / 255.0f;
193                 float b = src_overlay1[i * 4 + 2] / 255.0f;
194                 float a = src_overlay1[i * 4 + 3] / 255.0f;
195         //      src_bleh[i * 4 + 0] = r * a;
196         //      src_bleh[i * 4 + 1] = g * a;
197         //      src_bleh[i * 4 + 2] = b * a;
198                 src_bleh[i * 4 + 0] = r;
199                 src_bleh[i * 4 + 1] = g;
200                 src_bleh[i * 4 + 2] = b;
201                 src_bleh[i * 4 + 3] = a;
202         }
203         FlatInput *overlay1 = new FlatInput(inout_format, FORMAT_BGRA_POSTMULTIPLIED_ALPHA, GL_FLOAT, img_w, img_h);
204 #endif
205         FlatInput *overlay1 = new FlatInput(inout_format, FORMAT_BGRA_POSTMULTIPLIED_ALPHA, GL_UNSIGNED_BYTE, img_w, img_h);
206         chain.add_input(overlay1);
207
208         unsigned char *src_overlay2 = load_image("overlay2.png", &img_w, &img_h);
209         FlatInput *overlay2 = new FlatInput(inout_format, FORMAT_BGRA_POSTMULTIPLIED_ALPHA, GL_UNSIGNED_BYTE, img_w, img_h);
210         chain.add_input(overlay2);
211
212         Effect *mix1 = chain.add_effect(new OverlayEffect(), overlay2, overlay1);
213         //Effect *mix1_resized = chain.add_effect(new ResampleEffect(), mix1);
214         Effect *mix1_resized = chain.add_effect(new ResizeEffect(), mix1);
215         CHECK(mix1_resized->set_int("width", 1280));
216         CHECK(mix1_resized->set_int("height", 720));
217         Effect *mix2 = chain.add_effect(new OverlayEffect(), input, mix1_resized);
218 //      Effect *mix2 = chain.add_effect(new OverlayEffect(), input, overlay1);
219
220         //Effect *lift_gamma_gain_effect = chain.add_effect(new LiftGammaGainEffect());
221         //Effect *saturation_effect = chain.add_effect(new SaturationEffect());
222         //Effect *diffusion_effect = chain.add_effect(new DiffusionEffect());
223         //Effect *vignette_effect = chain.add_effect(new VignetteEffect());
224         //Effect *sandbox_effect = chain.add_effect(new SandboxEffect());
225         //sandbox_effect->set_float("parm", 42.0f);
226         //chain.add_effect(new MirrorEffect());
227         chain.add_output(inout_format, OUTPUT_ALPHA_POSTMULTIPLIED);
228         chain.set_dither_bits(8);
229         chain.finalize();
230
231         // generate a PBO to hold the data we read back with glReadPixels()
232         // (Intel/DRI goes into a slow path if we don't read to PBO)
233         GLuint pbo;
234         glGenBuffers(1, &pbo);
235         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
236         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
237
238         make_hsv_wheel_texture();
239
240         int frame = 0;
241         bool screenshot = false;
242 #if _POSIX_C_SOURCE >= 199309L
243         struct timespec start, now;
244         clock_gettime(CLOCK_MONOTONIC, &start);
245 #else
246         struct timeval start, now;
247         gettimeofday(&start, NULL);
248 #endif
249
250         while (!quit) {
251                 SDL_Event event;
252                 while (SDL_PollEvent(&event)) {
253                         if (event.type == SDL_QUIT) {
254                                 quit = true;
255                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
256                                 quit = true;
257                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
258                                 screenshot = true;
259                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
260                                 mouse(event.button.x, event.button.y);
261                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
262                                 mouse(event.motion.x, event.motion.y);
263                         }
264                 }
265
266                 ++frame;
267
268                 //update_hsv(lift_gamma_gain_effect, saturation_effect);
269                 //vignette_effect->set_float("radius", radius);
270                 //vignette_effect->set_float("inner_radius", inner_radius);
271                 //vignette_effect->set_vec2("center", (float[]){ 0.7f, 0.5f });
272
273                 //CHECK(diffusion_effect->set_float("radius", blur_radius));
274                 //CHECK(diffusion_effect->set_float("blurred_mix_amount", blurred_mix_amount));
275
276                 input->set_pixel_data(src_img);
277                 overlay1->set_pixel_data(src_overlay1);
278                 //overlay1->set_pixel_data(src_bleh);
279                 overlay2->set_pixel_data(src_overlay2);
280                 chain.render_to_screen();
281                 
282                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
283                 check_error();
284                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
285                 check_error();
286                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
287                 check_error();
288
289                 glLoadIdentity();
290                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
291                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
292                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
293                 draw_saturation_bar(0.6f, saturation / 4.0f);
294 #if 0
295                 draw_saturation_bar(0.65f, radius);
296                 draw_saturation_bar(0.70f, inner_radius);
297 #endif
298                 draw_saturation_bar(0.75f, blur_radius / 100.0f);
299                 draw_saturation_bar(0.80f, blurred_mix_amount);
300
301                 SDL_GL_SwapBuffers();
302                 check_error();
303
304                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
305                 check_error();
306                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
307                 check_error();
308                 if (screenshot) {
309                         char filename[256];
310                         sprintf(filename, "frame%05d.ppm", frame);
311                         write_ppm(filename, screenbuf);
312                         printf("Screenshot: %s\n", filename);
313                         screenshot = false;
314                 }
315                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
316                 check_error();
317                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
318                 check_error();
319
320 #if 1
321 #if _POSIX_C_SOURCE >= 199309L
322                 clock_gettime(CLOCK_MONOTONIC, &now);
323                 double elapsed = now.tv_sec - start.tv_sec +
324                         1e-9 * (now.tv_nsec - start.tv_nsec);
325 #else
326                 gettimeofday(&now, NULL);
327                 double elapsed = now.tv_sec - start.tv_sec +
328                         1e-6 * (now.tv_usec - start.tv_usec);
329 #endif
330                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
331                         frame, elapsed, frame / elapsed,
332                         1e3 * elapsed / frame);
333 #endif
334         }
335         return 0; 
336 }