Rename test.cpp to main.cpp.
[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 <math.h>
9 #include <time.h>
10
11 #include <string>
12 #include <vector>
13
14 #include <SDL/SDL.h>
15 #include <SDL/SDL_opengl.h>
16 #include <SDL/SDL_image.h>
17
18 #include <GL/gl.h>
19 #include <GL/glext.h>
20
21 #include "util.h"
22 #include "widgets.h"
23 #include "texture_enum.h"
24
25 unsigned char result[WIDTH * HEIGHT * 4];
26
27 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
28 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
29 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
30 float saturation = 1.0f;
31
32 float lift_r = 0.0f, lift_g = 0.0f, lift_b = 0.0f;
33 float gamma_r = 1.0f, gamma_g = 1.0f, gamma_b = 1.0f;
34 float gain_r = 1.0f, gain_g = 1.0f, gain_b = 1.0f;
35
36 struct ImageFormat {
37         enum { FORMAT_RGB, FORMAT_RGBA } format;
38
39         // Note: sRGB and 709 use the same colorspace primaries.
40         enum { COLORSPACE_sRGB, COLORSPACE_REC_601_525, COLORSPACE_REC_601_625, COLORSPACE_REC_709 } color_space;
41
42         // Note: Rec. 601 and 709 use the same gamma curve.
43         enum { LINEAR_LIGHT, GAMMA_sRGB, GAMMA_REC_601, GAMMA_REC_709 } gamma_curve;
44 };
45
46 enum EffectId {
47         // Mostly for internal use.
48         GAMMA_CONVERSION = 0,
49         RGB_PRIMARIES_CONVERSION,
50
51         // Color.
52         LIFT_GAMMA_GAIN,
53 };
54
55 class Effect {
56 public: 
57         virtual bool needs_linear_light() { return true; }
58         virtual bool needs_srgb_primaries() { return true; }
59         virtual bool needs_many_samples() { return false; }
60         virtual bool needs_mipmaps() { return false; }
61         bool set_float(const std::string& key, float value);
62         bool set_float_array(const std::string&, const float *values, size_t num_values);
63
64 private:
65         bool register_float(const std::string& key, float value);
66         bool register_float_array(const std::string& key, float *values, size_t num_values);
67 };      
68
69 class EffectChain {
70 public:
71         void set_size(unsigned width, unsigned height);
72         void add_input(const ImageFormat &format);
73         Effect *add_effect(EffectId effect);
74         void add_output(const ImageFormat &format);
75
76         void render(unsigned char *src, unsigned char *dst);
77
78 private:
79         unsigned width, height;
80         ImageFormat input_format, output_format;
81         std::vector<Effect *> effects;
82 };
83
84 GLhandleARB read_shader(const char* filename, GLenum type)
85 {
86         static char buf[131072];
87         FILE *fp = fopen(filename, "r");
88         if (fp == NULL) {
89                 perror(filename);
90                 exit(1);
91         }
92
93         int len = fread(buf, 1, sizeof(buf), fp);
94         fclose(fp);
95
96         GLhandleARB obj = glCreateShaderObjectARB(type);
97         const GLchar* source[] = { buf };
98         const GLint length[] = { len };
99         glShaderSource(obj, 1, source, length);
100         glCompileShader(obj);
101
102         GLchar info_log[4096];
103         GLsizei log_length = sizeof(info_log) - 1;
104         glGetShaderInfoLog(obj, log_length, &log_length, info_log);
105         info_log[log_length] = 0; 
106         printf("shader compile log: %s\n", info_log);
107
108         GLint status;
109         glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
110         if (status == GL_FALSE) {
111                 exit(1);
112         }
113
114         return obj;
115 }
116
117 void draw_picture_quad(GLint prog, int frame)
118 {
119         glUseProgramObjectARB(prog);
120         check_error();
121
122         glActiveTexture(GL_TEXTURE0);
123         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
124         glUniform1i(glGetUniformLocation(prog, "tex"), 0);
125
126         glActiveTexture(GL_TEXTURE1);
127         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
128         glUniform1i(glGetUniformLocation(prog, "srgb_tex"), 1);
129
130         glActiveTexture(GL_TEXTURE2);
131         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
132         glUniform1i(glGetUniformLocation(prog, "srgb_reverse_tex"), 2);
133
134         glUniform3f(glGetUniformLocation(prog, "lift"), lift_r, lift_g, lift_b);
135         //glUniform3f(glGetUniformLocation(prog, "gamma"), gamma_r, gamma_g, gamma_b);
136         glUniform3f(glGetUniformLocation(prog, "inv_gamma_22"),
137                     2.2f / gamma_r,
138                     2.2f / gamma_g,
139                     2.2f / gamma_b);
140         glUniform3f(glGetUniformLocation(prog, "gain_pow_inv_gamma"),
141                     pow(gain_r, 1.0f / gamma_r),
142                     pow(gain_g, 1.0f / gamma_g),
143                     pow(gain_b, 1.0f / gamma_b));
144         glUniform1f(glGetUniformLocation(prog, "saturation"), saturation);
145
146         glDisable(GL_BLEND);
147         check_error();
148         glDisable(GL_DEPTH_TEST);
149         check_error();
150         glDepthMask(GL_FALSE);
151         check_error();
152
153         glMatrixMode(GL_PROJECTION);
154         glLoadIdentity();
155         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
156
157         glMatrixMode(GL_MODELVIEW);
158         glLoadIdentity();
159
160         glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
161 //      glClear(GL_COLOR_BUFFER_BIT);
162         check_error();
163
164         glBegin(GL_QUADS);
165
166         glTexCoord2f(0.0f, 1.0f);
167         glVertex2f(0.0f, 0.0f);
168
169         glTexCoord2f(1.0f, 1.0f);
170         glVertex2f(1.0f, 0.0f);
171
172         glTexCoord2f(1.0f, 0.0f);
173         glVertex2f(1.0f, 1.0f);
174
175         glTexCoord2f(0.0f, 0.0f);
176         glVertex2f(0.0f, 1.0f);
177
178         glEnd();
179         check_error();
180 }
181
182 void update_hsv()
183 {
184         hsv2rgb(lift_theta, lift_rad, lift_v, &lift_r, &lift_g, &lift_b);
185         hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma_r, &gamma_g, &gamma_b);
186         hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain_r, &gain_g, &gain_b);
187
188         if (saturation < 0.0) {
189                 saturation = 0.0;
190         }
191
192         printf("lift: %f %f %f\n", lift_r, lift_g, lift_b);
193         printf("gamma: %f %f %f\n", gamma_r, gamma_g, gamma_b);
194         printf("gain: %f %f %f\n", gain_r, gain_g, gain_b);
195         printf("saturation: %f\n", saturation);
196         printf("\n");
197 }
198
199 void mouse(int x, int y)
200 {
201         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
202         float yf = (HEIGHT - y) / (float)HEIGHT;
203
204         if (yf < 0.2f) {
205                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
206         } else if (yf >= 0.2f && yf < 0.4f) {
207                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
208         } else if (yf >= 0.4f && yf < 0.6f) {
209                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
210         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
211                 saturation = (xf / 0.2f) * 4.0f;
212         }
213
214         update_hsv();
215 }
216
217 void load_texture(const char *filename)
218 {
219         SDL_Surface *img = IMG_Load(filename);
220         if (img == NULL) {
221                 fprintf(stderr, "Load of '%s' failed\n", filename);
222                 exit(1);
223         }
224
225         // Convert to RGB.
226         SDL_PixelFormat *fmt = img->format;
227         SDL_LockSurface(img);
228         unsigned char *src_pixels = (unsigned char *)img->pixels;
229         unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 3);
230         for (unsigned i = 0; i < img->w * img->h; ++i) {
231                 unsigned char r, g, b;
232                 unsigned int temp;
233                 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
234
235                 temp = pixel & fmt->Rmask;
236                 temp = temp >> fmt->Rshift;
237                 temp = temp << fmt->Rloss;
238                 r = temp;
239
240                 temp = pixel & fmt->Gmask;
241                 temp = temp >> fmt->Gshift;
242                 temp = temp << fmt->Gloss;
243                 g = temp;
244
245                 temp = pixel & fmt->Bmask;
246                 temp = temp >> fmt->Bshift;
247                 temp = temp << fmt->Bloss;
248                 b = temp;
249
250                 dst_pixels[i * 3 + 0] = r;
251                 dst_pixels[i * 3 + 1] = g;
252                 dst_pixels[i * 3 + 2] = b;
253         }
254         SDL_UnlockSurface(img);
255
256 #if 1
257         // we will convert to sRGB in the shader
258         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
259         check_error();
260 #else
261         // implicit sRGB conversion in hardware
262         glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
263         check_error();
264 #endif
265         free(dst_pixels);
266         SDL_FreeSurface(img);
267 }
268
269 void write_ppm(const char *filename, unsigned char *screenbuf)
270 {
271         FILE *fp = fopen(filename, "w");
272         fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
273         for (unsigned y = 0; y < HEIGHT; ++y) {
274                 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
275                 for (unsigned x = 0; x < WIDTH; ++x) {
276                         fputc(srcptr[x * 4 + 2], fp);
277                         fputc(srcptr[x * 4 + 1], fp);
278                         fputc(srcptr[x * 4 + 0], fp);
279                 }
280         }
281         fclose(fp);
282 }
283
284 int main(int argc, char **argv)
285 {
286         int quit = 0;
287
288         SDL_Init(SDL_INIT_EVERYTHING);
289         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
290         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
291         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
292         SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
293         SDL_WM_SetCaption("OpenGL window", NULL);
294         
295         // geez 
296         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
297         glPixelStorei(GL_PACK_ALIGNMENT, 1);
298
299         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
300         check_error();
301         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
302         check_error();
303
304         load_texture("blg_wheels_woman_1.jpg");
305         //glGenerateMipmap(GL_TEXTURE_2D);
306         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
307         //check_error();
308
309         //load_texture("maserati_gts_wallpaper_1280x720_01.jpg");
310         //load_texture("90630d1295075297-console-games-wallpapers-wallpaper_need_for_speed_prostreet_09_1920x1080.jpg");
311         //load_texture("glacier-lake-1280-720-4087.jpg");
312
313 #if 0
314         // sRGB reverse LUT
315         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
316         check_error();
317         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
318         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
319         check_error();
320         float srgb_reverse_tex[4096];
321         for (unsigned i = 0; i < 4096; ++i) {
322                 float x = i / 4095.0;
323                 if (x < 0.0031308f) {
324                         srgb_reverse_tex[i] = 12.92f * x;
325                 } else {
326                         srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
327                 }
328         }
329         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
330         check_error();
331
332         // sRGB LUT
333         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
334         check_error();
335         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
336         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
337         check_error();
338         float srgb_tex[256];
339         for (unsigned i = 0; i < 256; ++i) {
340                 float x = i / 255.0;
341                 if (x < 0.04045f) {
342                         srgb_tex[i] = x * (1.0f / 12.92f);
343                 } else {
344                         srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
345                 }
346         }
347         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
348         check_error();
349 #endif
350
351         // generate a PDO to hold the data we read back with glReadPixels()
352         // (Intel/DRI goes into a slow path if we don't read to PDO)
353         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
354         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
355
356         make_hsv_wheel_texture();
357         update_hsv();
358
359         int prog = glCreateProgram();
360         GLhandleARB vs_obj = read_shader("vs.glsl", GL_VERTEX_SHADER);
361         GLhandleARB fs_obj = read_shader("fs.glsl", GL_FRAGMENT_SHADER);
362         glAttachObjectARB(prog, vs_obj);
363         check_error();
364         glAttachObjectARB(prog, fs_obj);
365         check_error();
366         glLinkProgram(prog);
367         check_error();
368
369         GLchar info_log[4096];
370         GLsizei log_length = sizeof(info_log) - 1;
371         log_length = sizeof(info_log) - 1;
372         glGetProgramInfoLog(prog, log_length, &log_length, info_log);
373         info_log[log_length] = 0; 
374         printf("link: %s\n", info_log);
375
376         struct timespec start, now;
377         int frame = 0, screenshot = 0;
378         clock_gettime(CLOCK_MONOTONIC, &start);
379
380         while (!quit) {
381                 SDL_Event event;
382                 while (SDL_PollEvent(&event)) {
383                         if (event.type == SDL_QUIT) {
384                                 quit = 1;
385                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
386                                 quit = 1;
387                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
388                                 screenshot = 1;
389                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
390                                 mouse(event.button.x, event.button.y);
391                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
392                                 mouse(event.motion.x, event.motion.y);
393                         }
394                 }
395
396                 ++frame;
397
398                 draw_picture_quad(prog, frame);
399                 
400                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
401                 check_error();
402
403                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
404                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
405                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
406                 draw_saturation_bar(0.6f, saturation);
407
408                 SDL_GL_SwapBuffers();
409                 check_error();
410
411                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
412                 check_error();
413                 if (screenshot) {
414                         char filename[256];
415                         sprintf(filename, "frame%05d.ppm", frame);
416                         write_ppm(filename, screenbuf);
417                         printf("Screenshot: %s\n", filename);
418                         screenshot = 0;
419                 }
420                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
421                 check_error();
422
423 #if 1
424                 clock_gettime(CLOCK_MONOTONIC, &now);
425                 double elapsed = now.tv_sec - start.tv_sec +
426                         1e-9 * (now.tv_nsec - start.tv_nsec);
427                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
428                         frame, elapsed, frame / elapsed,
429                         1e3 * elapsed / frame);
430 #endif
431         }
432         return 0; 
433 }