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