1 #define GL_GLEXT_PROTOTYPES 1
6 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
8 #define HSV_WHEEL_SIZE 128
17 #include <SDL/SDL_opengl.h>
18 #include <SDL/SDL_image.h>
28 #define check_error() { int err = glGetError(); if (err != GL_NO_ERROR) { printf("GL error 0x%x at line %d\n", err, __LINE__); exit(1); } }
31 unsigned char result[WIDTH * HEIGHT * 4];
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;
38 float lift_r = 0.0f, lift_g = 0.0f, lift_b = 0.0f;
39 float gamma_r = 1.0f, gamma_g = 1.0f, gamma_b = 1.0f;
40 float gain_r = 1.0f, gain_g = 1.0f, gain_b = 1.0f;
43 enum { FORMAT_RGB, FORMAT_RGBA } format;
45 // Note: sRGB and 709 use the same colorspace primaries.
46 enum { COLORSPACE_sRGB, COLORSPACE_REC_601_525, COLORSPACE_REC_601_625, COLORSPACE_REC_709 } color_space;
48 // Note: Rec. 601 and 709 use the same gamma curve.
49 enum { LINEAR_LIGHT, GAMMA_sRGB, GAMMA_REC_601, GAMMA_REC_709 } gamma_curve;
53 // Mostly for internal use.
55 RGB_PRIMARIES_CONVERSION,
63 virtual bool needs_linear_light() { return true; }
64 virtual bool needs_srgb_primaries() { return true; }
65 virtual bool needs_many_samples() { return false; }
66 virtual bool needs_mipmaps() { return false; }
67 bool set_float(const std::string& key, float value);
68 bool set_float_array(const std::string&, const float *values, size_t num_values);
71 bool register_float(const std::string& key, float value);
72 bool register_float_array(const std::string& key, float *values, size_t num_values);
77 void set_size(unsigned width, unsigned height);
78 void add_input(const ImageFormat &format);
79 Effect *add_effect(EffectId effect);
80 void add_output(const ImageFormat &format);
82 void render(unsigned char *src, unsigned char *dst);
85 unsigned width, height;
86 ImageFormat input_format, output_format;
87 std::vector<Effect *> effects;
97 GLhandleARB read_shader(const char* filename, GLenum type)
99 static char buf[131072];
100 FILE *fp = fopen(filename, "r");
106 int len = fread(buf, 1, sizeof(buf), fp);
109 GLhandleARB obj = glCreateShaderObjectARB(type);
110 const GLchar* source[] = { buf };
111 const GLint length[] = { len };
112 glShaderSource(obj, 1, source, length);
113 glCompileShader(obj);
115 GLchar info_log[4096];
116 GLsizei log_length = sizeof(info_log) - 1;
117 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
118 info_log[log_length] = 0;
119 printf("shader compile log: %s\n", info_log);
122 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
123 if (status == GL_FALSE) {
130 void draw_picture_quad(GLint prog, int frame)
132 glUseProgramObjectARB(prog);
135 glActiveTexture(GL_TEXTURE0);
136 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
137 glUniform1i(glGetUniformLocation(prog, "tex"), 0);
139 glActiveTexture(GL_TEXTURE1);
140 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
141 glUniform1i(glGetUniformLocation(prog, "srgb_tex"), 1);
143 glActiveTexture(GL_TEXTURE2);
144 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
145 glUniform1i(glGetUniformLocation(prog, "srgb_reverse_tex"), 2);
147 glUniform3f(glGetUniformLocation(prog, "lift"), lift_r, lift_g, lift_b);
148 //glUniform3f(glGetUniformLocation(prog, "gamma"), gamma_r, gamma_g, gamma_b);
149 glUniform3f(glGetUniformLocation(prog, "inv_gamma_22"),
153 glUniform3f(glGetUniformLocation(prog, "gain_pow_inv_gamma"),
154 pow(gain_r, 1.0f / gamma_r),
155 pow(gain_g, 1.0f / gamma_g),
156 pow(gain_b, 1.0f / gamma_b));
157 glUniform1f(glGetUniformLocation(prog, "saturation"), saturation);
161 glDisable(GL_DEPTH_TEST);
163 glDepthMask(GL_FALSE);
166 glMatrixMode(GL_PROJECTION);
168 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
170 glMatrixMode(GL_MODELVIEW);
173 glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
174 // glClear(GL_COLOR_BUFFER_BIT);
179 glTexCoord2f(0.0f, 1.0f);
180 glVertex2f(0.0f, 0.0f);
182 glTexCoord2f(1.0f, 1.0f);
183 glVertex2f(1.0f, 0.0f);
185 glTexCoord2f(1.0f, 0.0f);
186 glVertex2f(1.0f, 1.0f);
188 glTexCoord2f(0.0f, 0.0f);
189 glVertex2f(0.0f, 1.0f);
195 void draw_hsv_wheel(float y, float rad, float theta, float value)
197 glUseProgramObjectARB(0);
199 glActiveTexture(GL_TEXTURE0);
201 glEnable(GL_TEXTURE_2D);
203 glBindTexture(GL_TEXTURE_2D, HSV_WHEEL);
205 glActiveTexture(GL_TEXTURE1);
207 glBindTexture(GL_TEXTURE_2D, 0);
209 glActiveTexture(GL_TEXTURE0);
212 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
218 glTexCoord2f(0.0f, 1.0f);
221 glTexCoord2f(1.0f, 1.0f);
222 glVertex2f(0.2f * 9.0f / 16.0f, y);
224 glTexCoord2f(1.0f, 0.0f);
225 glVertex2f(0.2f * 9.0f / 16.0f, y + 0.2f);
227 glTexCoord2f(0.0f, 0.0f);
228 glVertex2f(0.0f, y + 0.2f);
233 glDisable(GL_TEXTURE_2D);
234 glColor3f(0.0f, 0.0f, 0.0f);
237 glVertex2f((0.1f + rad * cos(theta) * 0.1f) * 9.0f / 16.0f, y + 0.1f - rad * sin(theta) * 0.1f);
241 glDisable(GL_TEXTURE_2D);
244 glColor3f(0.0f, 0.0f, 0.0f);
245 glVertex2f(0.22f * 9.0f / 16.0f, y);
246 glVertex2f(0.24f * 9.0f / 16.0f, y);
248 glColor3f(1.0f, 1.0f, 1.0f);
249 glVertex2f(0.24f * 9.0f / 16.0f, y + 0.2f);
250 glVertex2f(0.22f * 9.0f / 16.0f, y + 0.2f);
255 glColor3f(0.0f, 0.0f, 0.0f);
258 glVertex2f(0.23f * 9.0f / 16.0f, y + value * 0.2f);
261 glColor3f(1.0f, 1.0f, 1.0f);
264 void draw_saturation_bar(float y)
266 glUseProgramObjectARB(0);
270 glDisable(GL_TEXTURE_2D);
273 glColor3f(0.0f, 0.0f, 0.0f);
274 glVertex2f(0.0f * 9.0f / 16.0f, y + 0.02f);
275 glVertex2f(0.0f * 9.0f / 16.0f, y);
277 glColor3f(1.0f, 1.0f, 1.0f);
278 glVertex2f(0.2f * 9.0f / 16.0f, y);
279 glVertex2f(0.2f * 9.0f / 16.0f, y + 0.02f);
284 glColor3f(0.0f, 0.0f, 0.0f);
287 glVertex2f(0.2f * (saturation / 4.0f) * 9.0f / 16.0f, y + 0.01f);
290 glColor3f(1.0f, 1.0f, 1.0f);
293 void make_hsv_wheel_texture()
295 static unsigned char hsv_pix[HSV_WHEEL_SIZE * HSV_WHEEL_SIZE * 4];
296 for (int y = 0; y < HSV_WHEEL_SIZE; ++y) {
297 for (int x = 0; x < HSV_WHEEL_SIZE; ++x) {
298 float yf = 2.0f * y / (float)(HSV_WHEEL_SIZE) - 1.0f;
299 float xf = 2.0f * x / (float)(HSV_WHEEL_SIZE) - 1.0f;
300 float rad = hypot(xf, yf);
301 float theta = atan2(yf, xf);
304 hsv2rgb(theta, rad, 1.0f, &r, &g, &b);
305 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 0] = lrintf(r * 255.0f);
306 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 1] = lrintf(g * 255.0f);
307 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 2] = lrintf(b * 255.0f);
310 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 0;
312 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 255;
318 glBindTexture(GL_TEXTURE_2D, HSV_WHEEL);
320 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
322 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, HSV_WHEEL_SIZE, HSV_WHEEL_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, hsv_pix);
328 hsv2rgb(lift_theta, lift_rad, lift_v, &lift_r, &lift_g, &lift_b);
329 hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma_r, &gamma_g, &gamma_b);
330 hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain_r, &gain_g, &gain_b);
332 if (saturation < 0.0) {
336 printf("lift: %f %f %f\n", lift_r, lift_g, lift_b);
337 printf("gamma: %f %f %f\n", gamma_r, gamma_g, gamma_b);
338 printf("gain: %f %f %f\n", gain_r, gain_g, gain_b);
339 printf("saturation: %f\n", saturation);
343 void read_colorwheel(float xf, float yf, float *rad, float *theta, float *value)
345 if (xf < 0.2f && yf < 0.2f) {
346 float xp = 2.0f * xf / 0.2f - 1.0f;
347 float yp = -(2.0f * yf / 0.2f - 1.0f);
348 *rad = hypot(xp, yp);
349 *theta = atan2(yp, xp);
353 } else if (xf >= 0.22f && xf <= 0.24f) {
358 void mouse(int x, int y)
360 float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
361 float yf = (HEIGHT - y) / (float)HEIGHT;
364 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
365 } else if (yf >= 0.2f && yf < 0.4f) {
366 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
367 } else if (yf >= 0.4f && yf < 0.6f) {
368 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
369 } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
370 saturation = (xf / 0.2f) * 4.0f;
376 void load_texture(const char *filename)
378 SDL_Surface *img = IMG_Load(filename);
380 fprintf(stderr, "Load of '%s' failed\n", filename);
385 SDL_PixelFormat *fmt = img->format;
386 SDL_LockSurface(img);
387 unsigned char *src_pixels = (unsigned char *)img->pixels;
388 unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 3);
389 for (unsigned i = 0; i < img->w * img->h; ++i) {
390 unsigned char r, g, b;
392 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
394 temp = pixel & fmt->Rmask;
395 temp = temp >> fmt->Rshift;
396 temp = temp << fmt->Rloss;
399 temp = pixel & fmt->Gmask;
400 temp = temp >> fmt->Gshift;
401 temp = temp << fmt->Gloss;
404 temp = pixel & fmt->Bmask;
405 temp = temp >> fmt->Bshift;
406 temp = temp << fmt->Bloss;
409 dst_pixels[i * 3 + 0] = r;
410 dst_pixels[i * 3 + 1] = g;
411 dst_pixels[i * 3 + 2] = b;
413 SDL_UnlockSurface(img);
416 // we will convert to sRGB in the shader
417 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
420 // implicit sRGB conversion in hardware
421 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
425 SDL_FreeSurface(img);
428 void write_ppm(const char *filename, unsigned char *screenbuf)
430 FILE *fp = fopen(filename, "w");
431 fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
432 for (unsigned y = 0; y < HEIGHT; ++y) {
433 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
434 for (unsigned x = 0; x < WIDTH; ++x) {
435 fputc(srcptr[x * 4 + 2], fp);
436 fputc(srcptr[x * 4 + 1], fp);
437 fputc(srcptr[x * 4 + 0], fp);
443 int main(int argc, char **argv)
447 SDL_Init(SDL_INIT_EVERYTHING);
448 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
449 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
450 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
451 SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
452 SDL_WM_SetCaption("OpenGL window", NULL);
455 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
456 glPixelStorei(GL_PACK_ALIGNMENT, 1);
458 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
460 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
463 load_texture("blg_wheels_woman_1.jpg");
464 //glGenerateMipmap(GL_TEXTURE_2D);
465 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
468 //load_texture("maserati_gts_wallpaper_1280x720_01.jpg");
469 //load_texture("90630d1295075297-console-games-wallpapers-wallpaper_need_for_speed_prostreet_09_1920x1080.jpg");
470 //load_texture("glacier-lake-1280-720-4087.jpg");
474 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
476 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
477 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
479 float srgb_reverse_tex[4096];
480 for (unsigned i = 0; i < 4096; ++i) {
481 float x = i / 4095.0;
482 if (x < 0.0031308f) {
483 srgb_reverse_tex[i] = 12.92f * x;
485 srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
488 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
492 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
494 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
495 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
498 for (unsigned i = 0; i < 256; ++i) {
501 srgb_tex[i] = x * (1.0f / 12.92f);
503 srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
506 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
510 // generate a PDO to hold the data we read back with glReadPixels()
511 // (Intel/DRI goes into a slow path if we don't read to PDO)
512 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
513 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
515 make_hsv_wheel_texture();
518 int prog = glCreateProgram();
519 GLhandleARB vs_obj = read_shader("vs.glsl", GL_VERTEX_SHADER);
520 GLhandleARB fs_obj = read_shader("fs.glsl", GL_FRAGMENT_SHADER);
521 glAttachObjectARB(prog, vs_obj);
523 glAttachObjectARB(prog, fs_obj);
528 GLchar info_log[4096];
529 GLsizei log_length = sizeof(info_log) - 1;
530 log_length = sizeof(info_log) - 1;
531 glGetProgramInfoLog(prog, log_length, &log_length, info_log);
532 info_log[log_length] = 0;
533 printf("link: %s\n", info_log);
535 struct timespec start, now;
536 int frame = 0, screenshot = 0;
537 clock_gettime(CLOCK_MONOTONIC, &start);
541 while (SDL_PollEvent(&event)) {
542 if (event.type == SDL_QUIT) {
544 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
546 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
548 } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
549 mouse(event.button.x, event.button.y);
550 } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
551 mouse(event.motion.x, event.motion.y);
557 draw_picture_quad(prog, frame);
559 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
562 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
563 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
564 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
565 draw_saturation_bar(0.6f);
567 SDL_GL_SwapBuffers();
570 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
574 sprintf(filename, "frame%05d.ppm", frame);
575 write_ppm(filename, screenbuf);
576 printf("Screenshot: %s\n", filename);
579 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
583 clock_gettime(CLOCK_MONOTONIC, &now);
584 double elapsed = now.tv_sec - start.tv_sec +
585 1e-9 * (now.tv_nsec - start.tv_nsec);
586 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
587 frame, elapsed, frame / elapsed,
588 1e3 * elapsed / frame);