]> git.sesse.net Git - movit/blob - main.cpp
Slight cleanup in texture upload format selection.
[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 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
7
8 #include <string.h>
9 #include <math.h>
10 #include <time.h>
11 #include <sys/time.h>
12 #include <assert.h>
13
14 #include <string>
15 #include <vector>
16 #include <map>
17
18 #include <SDL/SDL.h>
19 #include <SDL/SDL_opengl.h>
20 #include <SDL/SDL_image.h>
21
22 #include <GL/gl.h>
23 #include <GL/glext.h>
24
25 #include "effect.h"
26 #include "effect_chain.h"
27 #include "util.h"
28 #include "widgets.h"
29 #include "texture_enum.h"
30
31 unsigned char result[WIDTH * HEIGHT * 4];
32
33 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
34 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
35 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
36 float saturation = 1.0f;
37
38 float radius = 0.3f;
39 float inner_radius = 0.3f;
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         } else if (yf >= 0.65f && yf < 0.67f && xf < 0.2f) {
77                 radius = (xf / 0.2f);
78         } else if (yf >= 0.70f && yf < 0.72f && xf < 0.2f) {
79                 inner_radius = (xf / 0.2f);
80         }
81 }
82
83 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
84 {
85         SDL_Surface *img = IMG_Load(filename);
86         if (img == NULL) {
87                 fprintf(stderr, "Load of '%s' failed\n", filename);
88                 exit(1);
89         }
90
91         // Convert to RGB.
92         SDL_PixelFormat *fmt = img->format;
93         SDL_LockSurface(img);
94         unsigned char *src_pixels = (unsigned char *)img->pixels;
95         unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 3);
96         for (int i = 0; i < img->w * img->h; ++i) {
97                 unsigned char r, g, b;
98                 unsigned int temp;
99                 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
100
101                 temp = pixel & fmt->Rmask;
102                 temp = temp >> fmt->Rshift;
103                 temp = temp << fmt->Rloss;
104                 r = temp;
105
106                 temp = pixel & fmt->Gmask;
107                 temp = temp >> fmt->Gshift;
108                 temp = temp << fmt->Gloss;
109                 g = temp;
110
111                 temp = pixel & fmt->Bmask;
112                 temp = temp >> fmt->Bshift;
113                 temp = temp << fmt->Bloss;
114                 b = temp;
115
116                 dst_pixels[i * 3 + 0] = r;
117                 dst_pixels[i * 3 + 1] = g;
118                 dst_pixels[i * 3 + 2] = b;
119         }
120         SDL_UnlockSurface(img);
121
122         *w = img->w;
123         *h = img->h;
124
125         SDL_FreeSurface(img);
126
127         return dst_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         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
161         check_error();
162         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
163         check_error();
164
165         unsigned img_w, img_h;
166         unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
167
168         EffectChain chain(WIDTH, HEIGHT);
169
170         ImageFormat inout_format;
171         inout_format.pixel_format = FORMAT_RGB;
172         inout_format.color_space = COLORSPACE_sRGB;
173         inout_format.gamma_curve = GAMMA_sRGB;
174
175         chain.add_input(inout_format);
176         Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN);
177         Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION);
178         Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE);
179         chain.add_output(inout_format);
180         chain.finalize();
181
182         //glGenerateMipmap(GL_TEXTURE_2D);
183         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
184         //check_error();
185
186 #if 0
187         // sRGB reverse LUT
188         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
189         check_error();
190         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
191         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
192         check_error();
193         float srgb_reverse_tex[4096];
194         for (unsigned i = 0; i < 4096; ++i) {
195                 float x = i / 4095.0;
196                 if (x < 0.0031308f) {
197                         srgb_reverse_tex[i] = 12.92f * x;
198                 } else {
199                         srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
200                 }
201         }
202         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
203         check_error();
204
205         // sRGB LUT
206         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
207         check_error();
208         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
209         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
210         check_error();
211         float srgb_tex[256];
212         for (unsigned i = 0; i < 256; ++i) {
213                 float x = i / 255.0;
214                 if (x < 0.04045f) {
215                         srgb_tex[i] = x * (1.0f / 12.92f);
216                 } else {
217                         srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
218                 }
219         }
220         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
221         check_error();
222 #endif
223
224         // generate a PDO to hold the data we read back with glReadPixels()
225         // (Intel/DRI goes into a slow path if we don't read to PDO)
226         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
227         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
228
229         make_hsv_wheel_texture();
230
231         int frame = 0, screenshot = 0;
232 #if _POSIX_C_SOURCE >= 199309L
233         struct timespec start, now;
234         clock_gettime(CLOCK_MONOTONIC, &start);
235 #else
236         struct timeval start, now;
237         gettimeofday(&start, NULL);
238 #endif
239
240         while (!quit) {
241                 SDL_Event event;
242                 while (SDL_PollEvent(&event)) {
243                         if (event.type == SDL_QUIT) {
244                                 quit = 1;
245                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
246                                 quit = 1;
247                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
248                                 screenshot = 1;
249                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
250                                 mouse(event.button.x, event.button.y);
251                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
252                                 mouse(event.motion.x, event.motion.y);
253                         }
254                 }
255
256                 ++frame;
257
258                 update_hsv(lift_gamma_gain_effect, saturation_effect);
259                 vignette_effect->set_float("radius", radius);
260                 vignette_effect->set_float("inner_radius", inner_radius);
261                 chain.render_to_screen(src_img);
262                 
263                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(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                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
277                 check_error();
278                 if (screenshot) {
279                         char filename[256];
280                         sprintf(filename, "frame%05d.ppm", frame);
281                         write_ppm(filename, screenbuf);
282                         printf("Screenshot: %s\n", filename);
283                         screenshot = 0;
284                 }
285                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
286                 check_error();
287
288 #if 1
289 #if _POSIX_C_SOURCE >= 199309L
290                 clock_gettime(CLOCK_MONOTONIC, &now);
291                 double elapsed = now.tv_sec - start.tv_sec +
292                         1e-9 * (now.tv_nsec - start.tv_nsec);
293 #else
294                 gettimeofday(&now, NULL);
295                 double elapsed = now.tv_sec - start.tv_sec +
296                         1e-6 * (now.tv_usec - start.tv_usec);
297 #endif
298                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
299                         frame, elapsed, frame / elapsed,
300                         1e3 * elapsed / frame);
301 #endif
302         }
303         return 0; 
304 }