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