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