Do not send uninitialized memory as a texture.
[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 = 3.0f;
39         
40 void update_hsv(Effect *lift_gamma_gain_effect, Effect *saturation_effect)
41 {
42         RGBTriplet lift(0.0f, 0.0f, 0.0f);
43         RGBTriplet gamma(1.0f, 1.0f, 1.0f);
44         RGBTriplet gain(1.0f, 1.0f, 1.0f);
45
46         hsv2rgb(lift_theta, lift_rad, lift_v, &lift.r, &lift.g, &lift.b);
47         hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b);
48         hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b);
49
50         bool ok = lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
51         ok = ok && lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
52         ok = ok && lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
53         assert(ok);
54
55         if (saturation < 0.0) {
56                 saturation = 0.0;
57         }
58         ok = saturation_effect->set_float("saturation", saturation);
59         assert(ok);
60 }
61
62 void mouse(int x, int y)
63 {
64         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
65         float yf = (HEIGHT - y) / (float)HEIGHT;
66
67         if (yf < 0.2f) {
68                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
69         } else if (yf >= 0.2f && yf < 0.4f) {
70                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
71         } else if (yf >= 0.4f && yf < 0.6f) {
72                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
73         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
74                 saturation = (xf / 0.2f) * 4.0f;
75         } else if (yf >= 0.65f && yf < 0.67f && xf < 0.2f) {
76                 radius = (xf / 0.2f);
77         } else if (yf >= 0.70f && yf < 0.72f && xf < 0.2f) {
78                 inner_radius = (xf / 0.2f);
79         } else if (yf >= 0.75f && yf < 0.77f && xf < 0.2f) {
80                 blur_radius = (xf / 0.2f) * 100.0f;
81         }
82 }
83
84 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
85 {
86         SDL_Surface *img = IMG_Load(filename);
87         if (img == NULL) {
88                 fprintf(stderr, "Load of '%s' failed\n", filename);
89                 exit(1);
90         }
91
92         // Convert to RGB.
93         SDL_PixelFormat *fmt = img->format;
94         SDL_LockSurface(img);
95         unsigned char *src_pixels = (unsigned char *)img->pixels;
96         unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 4);
97         for (int i = 0; i < img->w * img->h; ++i) {
98                 unsigned char r, g, b;
99                 unsigned int temp;
100                 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
101
102                 temp = pixel & fmt->Rmask;
103                 temp = temp >> fmt->Rshift;
104                 temp = temp << fmt->Rloss;
105                 r = temp;
106
107                 temp = pixel & fmt->Gmask;
108                 temp = temp >> fmt->Gshift;
109                 temp = temp << fmt->Gloss;
110                 g = temp;
111
112                 temp = pixel & fmt->Bmask;
113                 temp = temp >> fmt->Bshift;
114                 temp = temp << fmt->Bloss;
115                 b = temp;
116
117                 dst_pixels[i * 4 + 0] = b;
118                 dst_pixels[i * 4 + 1] = g;
119                 dst_pixels[i * 4 + 2] = r;
120                 dst_pixels[i * 4 + 3] = 255;
121         }
122         SDL_UnlockSurface(img);
123
124         *w = img->w;
125         *h = img->h;
126
127         SDL_FreeSurface(img);
128
129         return dst_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         int quit = 0;
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         // geez 
159         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
160         glPixelStorei(GL_PACK_ALIGNMENT, 1);
161
162         unsigned img_w, img_h;
163         unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
164
165         EffectChain chain(WIDTH, HEIGHT);
166
167         ImageFormat inout_format;
168         inout_format.pixel_format = FORMAT_BGRA;
169         inout_format.color_space = COLORSPACE_sRGB;
170         inout_format.gamma_curve = GAMMA_sRGB;
171
172         chain.add_input(inout_format);
173         Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN);
174         Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION);
175         Effect *hblur_effect = chain.add_effect(EFFECT_BLUR);
176         Effect *vblur_effect = chain.add_effect(EFFECT_BLUR);
177         Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE);
178         //chain.add_effect(EFFECT_MIRROR);
179         chain.add_output(inout_format);
180         chain.finalize();
181
182         // generate a PDO to hold the data we read back with glReadPixels()
183         // (Intel/DRI goes into a slow path if we don't read to PDO)
184         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
185         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
186
187         make_hsv_wheel_texture();
188
189         int frame = 0, screenshot = 0;
190 #if _POSIX_C_SOURCE >= 199309L
191         struct timespec start, now;
192         clock_gettime(CLOCK_MONOTONIC, &start);
193 #else
194         struct timeval start, now;
195         gettimeofday(&start, NULL);
196 #endif
197
198         while (!quit) {
199                 SDL_Event event;
200                 while (SDL_PollEvent(&event)) {
201                         if (event.type == SDL_QUIT) {
202                                 quit = 1;
203                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
204                                 quit = 1;
205                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
206                                 screenshot = 1;
207                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
208                                 mouse(event.button.x, event.button.y);
209                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
210                                 mouse(event.motion.x, event.motion.y);
211                         }
212                 }
213
214                 ++frame;
215
216                 update_hsv(lift_gamma_gain_effect, saturation_effect);
217                 vignette_effect->set_float("radius", radius);
218                 vignette_effect->set_float("inner_radius", inner_radius);
219                 //vignette_effect->set_vec2("center", (float[]){ 0.7f, 0.5f });
220
221                 hblur_effect->set_int("direction", 0);
222                 hblur_effect->set_float("radius", blur_radius);
223
224                 vblur_effect->set_int("direction", 1);
225                 vblur_effect->set_float("radius", blur_radius);
226
227                 chain.render_to_screen(src_img);
228                 
229                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
230                 check_error();
231                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
232                 check_error();
233                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
234                 check_error();
235
236                 glLoadIdentity();
237                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
238                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
239                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
240                 draw_saturation_bar(0.6f, saturation / 4.0f);
241                 draw_saturation_bar(0.65f, radius);
242                 draw_saturation_bar(0.70f, inner_radius);
243                 draw_saturation_bar(0.75f, blur_radius / 100.0f);
244
245                 SDL_GL_SwapBuffers();
246                 check_error();
247
248                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
249                 check_error();
250                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
251                 check_error();
252                 if (screenshot) {
253                         char filename[256];
254                         sprintf(filename, "frame%05d.ppm", frame);
255                         write_ppm(filename, screenbuf);
256                         printf("Screenshot: %s\n", filename);
257                         screenshot = 0;
258                 }
259                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
260                 check_error();
261                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
262                 check_error();
263
264 #if 1
265 #if _POSIX_C_SOURCE >= 199309L
266                 clock_gettime(CLOCK_MONOTONIC, &now);
267                 double elapsed = now.tv_sec - start.tv_sec +
268                         1e-9 * (now.tv_nsec - start.tv_nsec);
269 #else
270                 gettimeofday(&now, NULL);
271                 double elapsed = now.tv_sec - start.tv_sec +
272                         1e-6 * (now.tv_usec - start.tv_usec);
273 #endif
274                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
275                         frame, elapsed, frame / elapsed,
276                         1e3 * elapsed / frame);
277 #endif
278         }
279         return 0; 
280 }