Add a diffusion effect, and hook it up in the GUI.
[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
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 <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
29 unsigned char result[WIDTH * HEIGHT * 4];
30
31 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
32 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
33 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
34 float saturation = 1.0f;
35
36 //float radius = 0.3f;
37 // float inner_radius = 0.3f;
38 float blur_radius = 20.0f;
39 float blurred_mix_amount = 0.5f;
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 #if 0
77         } else if (yf >= 0.65f && yf < 0.67f && xf < 0.2f) {
78                 radius = (xf / 0.2f);
79         } else if (yf >= 0.70f && yf < 0.72f && xf < 0.2f) {
80                 inner_radius = (xf / 0.2f);
81 #endif
82         } else if (yf >= 0.75f && yf < 0.77f && xf < 0.2f) {
83                 blur_radius = (xf / 0.2f) * 100.0f;
84         } else if (yf >= 0.80f && yf < 0.82f && xf < 0.2f) {
85                 blurred_mix_amount = (xf / 0.2f);
86         }
87 }
88
89 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
90 {
91         SDL_Surface *img = IMG_Load(filename);
92         if (img == NULL) {
93                 fprintf(stderr, "Load of '%s' failed\n", filename);
94                 exit(1);
95         }
96
97         SDL_PixelFormat rgba_fmt;
98         rgba_fmt.palette = NULL;
99         rgba_fmt.BitsPerPixel = 32;
100         rgba_fmt.BytesPerPixel = 8;
101         rgba_fmt.Rloss = rgba_fmt.Gloss = rgba_fmt.Bloss = rgba_fmt.Aloss = 0;
102
103         // NOTE: Assumes little endian.
104         rgba_fmt.Rmask = 0x00ff0000;
105         rgba_fmt.Gmask = 0x0000ff00;
106         rgba_fmt.Bmask = 0x000000ff;
107         rgba_fmt.Amask = 0xff000000;
108
109         rgba_fmt.Rshift = 16;
110         rgba_fmt.Gshift = 8;
111         rgba_fmt.Bshift = 0;
112         rgba_fmt.Ashift = 24;
113         
114         rgba_fmt.colorkey = 0;
115         rgba_fmt.alpha = 255;
116
117         SDL_Surface *converted = SDL_ConvertSurface(img, &rgba_fmt, SDL_SWSURFACE);
118
119         *w = img->w;
120         *h = img->h;
121
122         SDL_FreeSurface(img);
123
124         return (unsigned char *)converted->pixels;
125 }
126
127 void write_ppm(const char *filename, unsigned char *screenbuf)
128 {
129         FILE *fp = fopen(filename, "w");
130         fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
131         for (unsigned y = 0; y < HEIGHT; ++y) {
132                 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
133                 for (unsigned x = 0; x < WIDTH; ++x) {
134                         fputc(srcptr[x * 4 + 2], fp);
135                         fputc(srcptr[x * 4 + 1], fp);
136                         fputc(srcptr[x * 4 + 0], fp);
137                 }
138         }
139         fclose(fp);
140 }
141
142 int main(int argc, char **argv)
143 {
144         int quit = 0;
145
146         SDL_Init(SDL_INIT_EVERYTHING);
147         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
148         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
149         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
150         SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
151         SDL_WM_SetCaption("OpenGL window", NULL);
152         
153         // geez 
154         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
155         glPixelStorei(GL_PACK_ALIGNMENT, 1);
156
157         unsigned img_w, img_h;
158         unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
159
160         EffectChain chain(WIDTH, HEIGHT);
161
162         ImageFormat inout_format;
163         inout_format.pixel_format = FORMAT_BGRA;
164         inout_format.color_space = COLORSPACE_sRGB;
165         inout_format.gamma_curve = GAMMA_sRGB;
166
167         chain.add_input(inout_format);
168         Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN);
169         Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION);
170         Effect *diffusion_effect = chain.add_effect(EFFECT_DIFFUSION);
171         //Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE);
172         //Effect *sandbox_effect = chain.add_effect(EFFECT_SANDBOX);
173         //sandbox_effect->set_float("parm", 42.0f);
174         //chain.add_effect(EFFECT_MIRROR);
175         chain.add_output(inout_format);
176         chain.finalize();
177
178         // generate a PDO to hold the data we read back with glReadPixels()
179         // (Intel/DRI goes into a slow path if we don't read to PDO)
180         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
181         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
182
183         make_hsv_wheel_texture();
184
185         int frame = 0, screenshot = 0;
186 #if _POSIX_C_SOURCE >= 199309L
187         struct timespec start, now;
188         clock_gettime(CLOCK_MONOTONIC, &start);
189 #else
190         struct timeval start, now;
191         gettimeofday(&start, NULL);
192 #endif
193
194         while (!quit) {
195                 SDL_Event event;
196                 while (SDL_PollEvent(&event)) {
197                         if (event.type == SDL_QUIT) {
198                                 quit = 1;
199                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
200                                 quit = 1;
201                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
202                                 screenshot = 1;
203                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
204                                 mouse(event.button.x, event.button.y);
205                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
206                                 mouse(event.motion.x, event.motion.y);
207                         }
208                 }
209
210                 ++frame;
211
212                 update_hsv(lift_gamma_gain_effect, saturation_effect);
213                 //vignette_effect->set_float("radius", radius);
214                 //vignette_effect->set_float("inner_radius", inner_radius);
215                 //vignette_effect->set_vec2("center", (float[]){ 0.7f, 0.5f });
216
217                 diffusion_effect->set_float("radius", blur_radius);
218                 diffusion_effect->set_float("blurred_mix_amount", blurred_mix_amount);
219
220                 chain.render_to_screen(src_img);
221                 
222                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
223                 check_error();
224                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
225                 check_error();
226                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
227                 check_error();
228
229                 glLoadIdentity();
230                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
231                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
232                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
233                 draw_saturation_bar(0.6f, saturation / 4.0f);
234 #if 0
235                 draw_saturation_bar(0.65f, radius);
236                 draw_saturation_bar(0.70f, inner_radius);
237 #endif
238                 draw_saturation_bar(0.75f, blur_radius / 100.0f);
239                 draw_saturation_bar(0.80f, blurred_mix_amount);
240
241                 SDL_GL_SwapBuffers();
242                 check_error();
243
244                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
245                 check_error();
246                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
247                 check_error();
248                 if (screenshot) {
249                         char filename[256];
250                         sprintf(filename, "frame%05d.ppm", frame);
251                         write_ppm(filename, screenbuf);
252                         printf("Screenshot: %s\n", filename);
253                         screenshot = 0;
254                 }
255                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
256                 check_error();
257                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
258                 check_error();
259
260 #if 1
261 #if _POSIX_C_SOURCE >= 199309L
262                 clock_gettime(CLOCK_MONOTONIC, &now);
263                 double elapsed = now.tv_sec - start.tv_sec +
264                         1e-9 * (now.tv_nsec - start.tv_nsec);
265 #else
266                 gettimeofday(&now, NULL);
267                 double elapsed = now.tv_sec - start.tv_sec +
268                         1e-6 * (now.tv_usec - start.tv_usec);
269 #endif
270                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
271                         frame, elapsed, frame / elapsed,
272                         1e3 * elapsed / frame);
273 #endif
274         }
275         return 0; 
276 }