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