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