cd6cf88a0286045e55782d8ca2d6a59714c8bb8b
[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 <SDL/SDL.h>
18 #include <SDL/SDL_opengl.h>
19 #include <SDL/SDL_image.h>
20
21 #include "init.h"
22 #include "effect.h"
23 #include "effect_chain.h"
24 #include "util.h"
25 #include "opengl.h"
26 #include "widgets.h"
27
28 #include "flat_input.h"
29 #include "lift_gamma_gain_effect.h"
30 #include "saturation_effect.h"
31 #include "diffusion_effect.h"
32
33 unsigned char result[WIDTH * HEIGHT * 4];
34
35 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
36 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
37 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
38 float saturation = 1.0f;
39
40 //float radius = 0.3f;
41 // float inner_radius = 0.3f;
42 float blur_radius = 20.0f;
43 float blurred_mix_amount = 0.5f;
44         
45 void update_hsv(Effect *lift_gamma_gain_effect, Effect *saturation_effect)
46 {
47         RGBTriplet lift(0.0f, 0.0f, 0.0f);
48         RGBTriplet gamma(1.0f, 1.0f, 1.0f);
49         RGBTriplet gain(1.0f, 1.0f, 1.0f);
50
51         hsv2rgb_normalized(lift_theta, lift_rad, lift_v, &lift.r, &lift.g, &lift.b);
52         hsv2rgb_normalized(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b);
53         hsv2rgb_normalized(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b);
54
55         bool ok = lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
56         ok = ok && lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
57         ok = ok && lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
58         assert(ok);
59
60         if (saturation < 0.0) {
61                 saturation = 0.0;
62         }
63         ok = saturation_effect->set_float("saturation", saturation);
64         assert(ok);
65 }
66
67 void mouse(int x, int y)
68 {
69         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
70         float yf = (HEIGHT - y) / (float)HEIGHT;
71
72         if (yf < 0.2f) {
73                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
74         } else if (yf >= 0.2f && yf < 0.4f) {
75                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
76         } else if (yf >= 0.4f && yf < 0.6f) {
77                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
78         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
79                 saturation = (xf / 0.2f) * 4.0f;
80 #if 0
81         } else if (yf >= 0.65f && yf < 0.67f && xf < 0.2f) {
82                 radius = (xf / 0.2f);
83         } else if (yf >= 0.70f && yf < 0.72f && xf < 0.2f) {
84                 inner_radius = (xf / 0.2f);
85 #endif
86         } else if (yf >= 0.75f && yf < 0.77f && xf < 0.2f) {
87                 blur_radius = (xf / 0.2f) * 100.0f;
88         } else if (yf >= 0.80f && yf < 0.82f && xf < 0.2f) {
89                 blurred_mix_amount = (xf / 0.2f);
90         }
91 }
92
93 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
94 {
95         SDL_Surface *img = IMG_Load(filename);
96         if (img == NULL) {
97                 fprintf(stderr, "Load of '%s' failed\n", filename);
98                 exit(1);
99         }
100
101         SDL_PixelFormat rgba_fmt;
102         rgba_fmt.palette = NULL;
103         rgba_fmt.BitsPerPixel = 32;
104         rgba_fmt.BytesPerPixel = 8;
105         rgba_fmt.Rloss = rgba_fmt.Gloss = rgba_fmt.Bloss = rgba_fmt.Aloss = 0;
106
107         // NOTE: Assumes little endian.
108         rgba_fmt.Rmask = 0x00ff0000;
109         rgba_fmt.Gmask = 0x0000ff00;
110         rgba_fmt.Bmask = 0x000000ff;
111         rgba_fmt.Amask = 0xff000000;
112
113         rgba_fmt.Rshift = 16;
114         rgba_fmt.Gshift = 8;
115         rgba_fmt.Bshift = 0;
116         rgba_fmt.Ashift = 24;
117         
118         rgba_fmt.colorkey = 0;
119         rgba_fmt.alpha = 255;
120
121         SDL_Surface *converted = SDL_ConvertSurface(img, &rgba_fmt, SDL_SWSURFACE);
122
123         *w = img->w;
124         *h = img->h;
125
126         SDL_FreeSurface(img);
127
128         return (unsigned char *)converted->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         bool quit = false;
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         init_movit();
158         printf("GPU texture subpixel precision: about %.1f bits\n",
159                 log2(1.0f / movit_texel_subpixel_precision));
160         
161         unsigned img_w, img_h;
162         unsigned char *src_img = load_image(argc > 1 ? argv[1] : "blg_wheels_woman_1.jpg", &img_w, &img_h);
163
164         EffectChain chain(WIDTH, HEIGHT);
165         glViewport(0, 0, WIDTH, HEIGHT);
166
167         ImageFormat inout_format;
168         inout_format.color_space = COLORSPACE_sRGB;
169         inout_format.gamma_curve = GAMMA_sRGB;
170
171         FlatInput *input = new FlatInput(inout_format, FORMAT_BGRA, GL_UNSIGNED_BYTE, img_w, img_h);
172         chain.add_input(input);
173         Effect *lift_gamma_gain_effect = chain.add_effect(new LiftGammaGainEffect());
174         Effect *saturation_effect = chain.add_effect(new SaturationEffect());
175         Effect *diffusion_effect = chain.add_effect(new DiffusionEffect());
176         //Effect *vignette_effect = chain.add_effect(new VignetteEffect());
177         //Effect *sandbox_effect = chain.add_effect(new SandboxEffect());
178         //sandbox_effect->set_float("parm", 42.0f);
179         //chain.add_effect(new MirrorEffect());
180         chain.add_output(inout_format);
181         chain.set_dither_bits(8);
182         chain.finalize();
183
184         // generate a PBO to hold the data we read back with glReadPixels()
185         // (Intel/DRI goes into a slow path if we don't read to PBO)
186         GLuint pbo;
187         glGenBuffers(1, &pbo);
188         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
189         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
190
191         make_hsv_wheel_texture();
192
193         int frame = 0;
194         bool screenshot = false;
195 #if _POSIX_C_SOURCE >= 199309L
196         struct timespec start, now;
197         clock_gettime(CLOCK_MONOTONIC, &start);
198 #else
199         struct timeval start, now;
200         gettimeofday(&start, NULL);
201 #endif
202
203         while (!quit) {
204                 SDL_Event event;
205                 while (SDL_PollEvent(&event)) {
206                         if (event.type == SDL_QUIT) {
207                                 quit = true;
208                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
209                                 quit = true;
210                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
211                                 screenshot = true;
212                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
213                                 mouse(event.button.x, event.button.y);
214                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
215                                 mouse(event.motion.x, event.motion.y);
216                         }
217                 }
218
219                 ++frame;
220
221                 update_hsv(lift_gamma_gain_effect, saturation_effect);
222                 //vignette_effect->set_float("radius", radius);
223                 //vignette_effect->set_float("inner_radius", inner_radius);
224                 //vignette_effect->set_vec2("center", (float[]){ 0.7f, 0.5f });
225
226                 CHECK(diffusion_effect->set_float("radius", blur_radius));
227                 CHECK(diffusion_effect->set_float("blurred_mix_amount", blurred_mix_amount));
228
229                 input->set_pixel_data(src_img);
230                 chain.render_to_screen();
231                 
232                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
233                 check_error();
234                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
235                 check_error();
236                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
237                 check_error();
238
239                 glLoadIdentity();
240                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
241                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
242                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
243                 draw_saturation_bar(0.6f, saturation / 4.0f);
244 #if 0
245                 draw_saturation_bar(0.65f, radius);
246                 draw_saturation_bar(0.70f, inner_radius);
247 #endif
248                 draw_saturation_bar(0.75f, blur_radius / 100.0f);
249                 draw_saturation_bar(0.80f, blurred_mix_amount);
250
251                 SDL_GL_SwapBuffers();
252                 check_error();
253
254                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
255                 check_error();
256                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
257                 check_error();
258                 if (screenshot) {
259                         char filename[256];
260                         sprintf(filename, "frame%05d.ppm", frame);
261                         write_ppm(filename, screenbuf);
262                         printf("Screenshot: %s\n", filename);
263                         screenshot = false;
264                 }
265                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
266                 check_error();
267                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
268                 check_error();
269
270 #if 1
271 #if _POSIX_C_SOURCE >= 199309L
272                 clock_gettime(CLOCK_MONOTONIC, &now);
273                 double elapsed = now.tv_sec - start.tv_sec +
274                         1e-9 * (now.tv_nsec - start.tv_nsec);
275 #else
276                 gettimeofday(&now, NULL);
277                 double elapsed = now.tv_sec - start.tv_sec +
278                         1e-6 * (now.tv_usec - start.tv_usec);
279 #endif
280                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
281                         frame, elapsed, frame / elapsed,
282                         1e3 * elapsed / frame);
283 #endif
284         }
285         return 0; 
286 }