]> git.sesse.net Git - movit/blob - main.cpp
Kill the hard-coded texture enums (yay).
[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         
39 void update_hsv(Effect *lift_gamma_gain_effect, Effect *saturation_effect)
40 {
41         RGBTriplet lift(0.0f, 0.0f, 0.0f);
42         RGBTriplet gamma(1.0f, 1.0f, 1.0f);
43         RGBTriplet gain(1.0f, 1.0f, 1.0f);
44
45         hsv2rgb(lift_theta, lift_rad, lift_v, &lift.r, &lift.g, &lift.b);
46         hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b);
47         hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b);
48
49         bool ok = lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
50         ok = ok && lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
51         ok = ok && lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
52         assert(ok);
53
54         if (saturation < 0.0) {
55                 saturation = 0.0;
56         }
57         ok = saturation_effect->set_float("saturation", saturation);
58         assert(ok);
59 }
60
61 void mouse(int x, int y)
62 {
63         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
64         float yf = (HEIGHT - y) / (float)HEIGHT;
65
66         if (yf < 0.2f) {
67                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
68         } else if (yf >= 0.2f && yf < 0.4f) {
69                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
70         } else if (yf >= 0.4f && yf < 0.6f) {
71                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
72         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
73                 saturation = (xf / 0.2f) * 4.0f;
74         } else if (yf >= 0.65f && yf < 0.67f && xf < 0.2f) {
75                 radius = (xf / 0.2f);
76         } else if (yf >= 0.70f && yf < 0.72f && xf < 0.2f) {
77                 inner_radius = (xf / 0.2f);
78         }
79 }
80
81 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
82 {
83         SDL_Surface *img = IMG_Load(filename);
84         if (img == NULL) {
85                 fprintf(stderr, "Load of '%s' failed\n", filename);
86                 exit(1);
87         }
88
89         // Convert to RGB.
90         SDL_PixelFormat *fmt = img->format;
91         SDL_LockSurface(img);
92         unsigned char *src_pixels = (unsigned char *)img->pixels;
93         unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 4);
94         for (int i = 0; i < img->w * img->h; ++i) {
95                 unsigned char r, g, b;
96                 unsigned int temp;
97                 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
98
99                 temp = pixel & fmt->Rmask;
100                 temp = temp >> fmt->Rshift;
101                 temp = temp << fmt->Rloss;
102                 r = temp;
103
104                 temp = pixel & fmt->Gmask;
105                 temp = temp >> fmt->Gshift;
106                 temp = temp << fmt->Gloss;
107                 g = temp;
108
109                 temp = pixel & fmt->Bmask;
110                 temp = temp >> fmt->Bshift;
111                 temp = temp << fmt->Bloss;
112                 b = temp;
113
114                 dst_pixels[i * 4 + 0] = b;
115                 dst_pixels[i * 4 + 1] = g;
116                 dst_pixels[i * 4 + 2] = r;
117                 dst_pixels[i * 4 + 3] = 255;
118         }
119         SDL_UnlockSurface(img);
120
121         *w = img->w;
122         *h = img->h;
123
124         SDL_FreeSurface(img);
125
126         return dst_pixels;
127 }
128
129 void write_ppm(const char *filename, unsigned char *screenbuf)
130 {
131         FILE *fp = fopen(filename, "w");
132         fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
133         for (unsigned y = 0; y < HEIGHT; ++y) {
134                 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
135                 for (unsigned x = 0; x < WIDTH; ++x) {
136                         fputc(srcptr[x * 4 + 2], fp);
137                         fputc(srcptr[x * 4 + 1], fp);
138                         fputc(srcptr[x * 4 + 0], fp);
139                 }
140         }
141         fclose(fp);
142 }
143
144 int main(int argc, char **argv)
145 {
146         int quit = 0;
147
148         SDL_Init(SDL_INIT_EVERYTHING);
149         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
150         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
151         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
152         SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
153         SDL_WM_SetCaption("OpenGL window", NULL);
154         
155         // geez 
156         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
157         glPixelStorei(GL_PACK_ALIGNMENT, 1);
158
159         unsigned img_w, img_h;
160         unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
161
162         EffectChain chain(WIDTH, HEIGHT);
163
164         ImageFormat inout_format;
165         inout_format.pixel_format = FORMAT_BGRA;
166         inout_format.color_space = COLORSPACE_sRGB;
167         inout_format.gamma_curve = GAMMA_sRGB;
168
169         chain.add_input(inout_format);
170         Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN);
171         Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION);
172         Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE);
173         //chain.add_effect(EFFECT_MIRROR);
174         chain.add_output(inout_format);
175         chain.finalize();
176
177         //glGenerateMipmap(GL_TEXTURE_2D);
178         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
179         //check_error();
180
181 #if 0
182         // sRGB reverse LUT
183         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
184         check_error();
185         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
186         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
187         check_error();
188         float srgb_reverse_tex[4096];
189         for (unsigned i = 0; i < 4096; ++i) {
190                 float x = i / 4095.0;
191                 if (x < 0.0031308f) {
192                         srgb_reverse_tex[i] = 12.92f * x;
193                 } else {
194                         srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
195                 }
196         }
197         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
198         check_error();
199
200         // sRGB LUT
201         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
202         check_error();
203         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
204         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
205         check_error();
206         float srgb_tex[256];
207         for (unsigned i = 0; i < 256; ++i) {
208                 float x = i / 255.0;
209                 if (x < 0.04045f) {
210                         srgb_tex[i] = x * (1.0f / 12.92f);
211                 } else {
212                         srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
213                 }
214         }
215         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
216         check_error();
217 #endif
218
219         // generate a PDO to hold the data we read back with glReadPixels()
220         // (Intel/DRI goes into a slow path if we don't read to PDO)
221         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
222         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
223
224         make_hsv_wheel_texture();
225
226         int frame = 0, screenshot = 0;
227 #if _POSIX_C_SOURCE >= 199309L
228         struct timespec start, now;
229         clock_gettime(CLOCK_MONOTONIC, &start);
230 #else
231         struct timeval start, now;
232         gettimeofday(&start, NULL);
233 #endif
234
235         while (!quit) {
236                 SDL_Event event;
237                 while (SDL_PollEvent(&event)) {
238                         if (event.type == SDL_QUIT) {
239                                 quit = 1;
240                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
241                                 quit = 1;
242                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
243                                 screenshot = 1;
244                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
245                                 mouse(event.button.x, event.button.y);
246                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
247                                 mouse(event.motion.x, event.motion.y);
248                         }
249                 }
250
251                 ++frame;
252
253                 update_hsv(lift_gamma_gain_effect, saturation_effect);
254                 vignette_effect->set_float("radius", radius);
255                 vignette_effect->set_float("inner_radius", inner_radius);
256                 //vignette_effect->set_vec2("center", (float[]){ 0.7f, 0.5f });
257                 chain.render_to_screen(src_img);
258                 
259                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
260                 check_error();
261                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
262                 check_error();
263                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
264                 check_error();
265
266                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
267                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
268                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
269                 draw_saturation_bar(0.6f, saturation / 4.0f);
270                 draw_saturation_bar(0.65f, radius);
271                 draw_saturation_bar(0.70f, inner_radius);
272
273                 SDL_GL_SwapBuffers();
274                 check_error();
275
276                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
277                 check_error();
278                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
279                 check_error();
280                 if (screenshot) {
281                         char filename[256];
282                         sprintf(filename, "frame%05d.ppm", frame);
283                         write_ppm(filename, screenbuf);
284                         printf("Screenshot: %s\n", filename);
285                         screenshot = 0;
286                 }
287                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
288                 check_error();
289                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
290                 check_error();
291
292 #if 1
293 #if _POSIX_C_SOURCE >= 199309L
294                 clock_gettime(CLOCK_MONOTONIC, &now);
295                 double elapsed = now.tv_sec - start.tv_sec +
296                         1e-9 * (now.tv_nsec - start.tv_nsec);
297 #else
298                 gettimeofday(&now, NULL);
299                 double elapsed = now.tv_sec - start.tv_sec +
300                         1e-6 * (now.tv_usec - start.tv_usec);
301 #endif
302                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
303                         frame, elapsed, frame / elapsed,
304                         1e3 * elapsed / frame);
305 #endif
306         }
307         return 0; 
308 }