1 #define GL_GLEXT_PROTOTYPES 1
6 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
18 #include <SDL/SDL_opengl.h>
19 #include <SDL/SDL_image.h>
26 #include "texture_enum.h"
28 unsigned char result[WIDTH * HEIGHT * 4];
30 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
31 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
32 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
33 float saturation = 1.0f;
35 float lift_r = 0.0f, lift_g = 0.0f, lift_b = 0.0f;
36 float gamma_r = 1.0f, gamma_g = 1.0f, gamma_b = 1.0f;
37 float gain_r = 1.0f, gain_g = 1.0f, gain_b = 1.0f;
39 enum PixelFormat { FORMAT_RGB, FORMAT_RGBA };
43 COLORSPACE_REC_709 = 0, // Same as sRGB.
44 COLORSPACE_REC_601_525 = 1,
45 COLORSPACE_REC_601_625 = 2,
52 GAMMA_REC_709 = 2, // Same as Rec. 601.
56 PixelFormat pixel_format;
57 ColorSpace color_space;
58 GammaCurve gamma_curve;
62 // Mostly for internal use.
64 RGB_PRIMARIES_CONVERSION,
72 virtual bool needs_linear_light() { return true; }
73 virtual bool needs_srgb_primaries() { return true; }
74 virtual bool needs_many_samples() { return false; }
75 virtual bool needs_mipmaps() { return false; }
77 // Neither of these take ownership.
78 bool set_int(const std::string&, int value);
79 bool set_float(const std::string &key, float value);
80 bool set_vec3(const std::string &key, const float *values);
83 // Neither of these take ownership.
84 void register_int(const std::string &key, int *value);
85 void register_float(const std::string &key, float *value);
86 void register_vec3(const std::string &key, float *values);
89 std::map<std::string, int *> params_int;
90 std::map<std::string, float *> params_float;
91 std::map<std::string, float *> params_vec3;
94 bool Effect::set_int(const std::string &key, int value)
96 if (params_int.count(key) == 0) {
99 *params_int[key] = value;
103 bool Effect::set_float(const std::string &key, float value)
105 if (params_float.count(key) == 0) {
108 *params_float[key] = value;
112 bool Effect::set_vec3(const std::string &key, const float *values)
114 if (params_vec3.count(key) == 0) {
117 memcpy(params_vec3[key], values, sizeof(float) * 3);
121 void Effect::register_int(const std::string &key, int *value)
123 assert(params_int.count(key) == 0);
124 params_int[key] = value;
127 void Effect::register_float(const std::string &key, float *value)
129 assert(params_float.count(key) == 0);
130 params_float[key] = value;
133 void Effect::register_vec3(const std::string &key, float *values)
135 assert(params_vec3.count(key) == 0);
136 params_vec3[key] = values;
139 // Can alias on a float[3].
141 RGBTriplet(float r, float g, float b)
142 : r(r), g(g), b(b) {}
147 class GammaExpansionEffect : public Effect {
149 GammaExpansionEffect()
150 : source_curve(GAMMA_LINEAR)
152 register_int("source_curve", (int *)&source_curve);
156 GammaCurve source_curve;
159 class ColorSpaceConversionEffect : public Effect {
161 ColorSpaceConversionEffect()
162 : source_space(COLORSPACE_sRGB),
163 destination_space(COLORSPACE_sRGB)
165 register_int("source_space", (int *)&source_space);
166 register_int("destination_space", (int *)&destination_space);
170 ColorSpace source_space, destination_space;
173 class ColorSpaceConversionEffect;
175 class LiftGammaGainEffect : public Effect {
177 LiftGammaGainEffect()
178 : lift(0.0f, 0.0f, 0.0f),
179 gamma(1.0f, 1.0f, 1.0f),
180 gain(1.0f, 1.0f, 1.0f),
183 register_vec3("lift", (float *)&lift);
184 register_vec3("gamma", (float *)&gamma);
185 register_vec3("gain", (float *)&gain);
186 register_float("saturation", &saturation);
190 RGBTriplet lift, gamma, gain;
196 EffectChain(unsigned width, unsigned height);
197 void add_input(const ImageFormat &format);
199 // The pointer is owned by EffectChain.
200 Effect *add_effect(EffectId effect);
202 void add_output(const ImageFormat &format);
204 void render(unsigned char *src, unsigned char *dst);
207 unsigned width, height;
208 ImageFormat input_format, output_format;
209 std::vector<Effect *> effects;
211 ColorSpace current_color_space;
212 GammaCurve current_gamma_curve;
215 EffectChain::EffectChain(unsigned width, unsigned height)
216 : width(width), height(height) {}
218 void EffectChain::add_input(const ImageFormat &format)
220 input_format = format;
221 current_color_space = format.color_space;
222 current_gamma_curve = format.gamma_curve;
225 void EffectChain::add_output(const ImageFormat &format)
227 output_format = format;
230 Effect *instantiate_effect(EffectId effect)
233 case GAMMA_CONVERSION:
234 return new GammaExpansionEffect();
235 case RGB_PRIMARIES_CONVERSION:
236 return new GammaExpansionEffect();
237 case LIFT_GAMMA_GAIN:
238 return new LiftGammaGainEffect();
243 Effect *EffectChain::add_effect(EffectId effect_id)
245 Effect *effect = instantiate_effect(effect_id);
247 if (effect->needs_linear_light() && current_gamma_curve != GAMMA_LINEAR) {
248 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
249 gamma_conversion->set_int("source_curve", current_gamma_curve);
250 effects.push_back(gamma_conversion);
251 current_gamma_curve = GAMMA_LINEAR;
254 if (effect->needs_srgb_primaries() && current_color_space != COLORSPACE_sRGB) {
255 assert(current_gamma_curve == GAMMA_LINEAR);
256 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
257 colorspace_conversion->set_int("source_space", current_color_space);
258 colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
259 effects.push_back(colorspace_conversion);
260 current_color_space = COLORSPACE_sRGB;
263 effects.push_back(effect);
267 GLhandleARB read_shader(const char* filename, GLenum type)
269 static char buf[131072];
270 FILE *fp = fopen(filename, "r");
276 int len = fread(buf, 1, sizeof(buf), fp);
279 GLhandleARB obj = glCreateShaderObjectARB(type);
280 const GLchar* source[] = { buf };
281 const GLint length[] = { len };
282 glShaderSource(obj, 1, source, length);
283 glCompileShader(obj);
285 GLchar info_log[4096];
286 GLsizei log_length = sizeof(info_log) - 1;
287 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
288 info_log[log_length] = 0;
289 printf("shader compile log: %s\n", info_log);
292 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
293 if (status == GL_FALSE) {
300 void draw_picture_quad(GLint prog, int frame)
302 glUseProgramObjectARB(prog);
305 glActiveTexture(GL_TEXTURE0);
306 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
307 glUniform1i(glGetUniformLocation(prog, "tex"), 0);
309 glActiveTexture(GL_TEXTURE1);
310 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
311 glUniform1i(glGetUniformLocation(prog, "srgb_tex"), 1);
313 glActiveTexture(GL_TEXTURE2);
314 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
315 glUniform1i(glGetUniformLocation(prog, "srgb_reverse_tex"), 2);
317 glUniform3f(glGetUniformLocation(prog, "lift"), lift_r, lift_g, lift_b);
318 //glUniform3f(glGetUniformLocation(prog, "gamma"), gamma_r, gamma_g, gamma_b);
319 glUniform3f(glGetUniformLocation(prog, "inv_gamma_22"),
323 glUniform3f(glGetUniformLocation(prog, "gain_pow_inv_gamma"),
324 pow(gain_r, 1.0f / gamma_r),
325 pow(gain_g, 1.0f / gamma_g),
326 pow(gain_b, 1.0f / gamma_b));
327 glUniform1f(glGetUniformLocation(prog, "saturation"), saturation);
331 glDisable(GL_DEPTH_TEST);
333 glDepthMask(GL_FALSE);
336 glMatrixMode(GL_PROJECTION);
338 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
340 glMatrixMode(GL_MODELVIEW);
343 glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
344 // glClear(GL_COLOR_BUFFER_BIT);
349 glTexCoord2f(0.0f, 1.0f);
350 glVertex2f(0.0f, 0.0f);
352 glTexCoord2f(1.0f, 1.0f);
353 glVertex2f(1.0f, 0.0f);
355 glTexCoord2f(1.0f, 0.0f);
356 glVertex2f(1.0f, 1.0f);
358 glTexCoord2f(0.0f, 0.0f);
359 glVertex2f(0.0f, 1.0f);
367 hsv2rgb(lift_theta, lift_rad, lift_v, &lift_r, &lift_g, &lift_b);
368 hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma_r, &gamma_g, &gamma_b);
369 hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain_r, &gain_g, &gain_b);
371 if (saturation < 0.0) {
375 printf("lift: %f %f %f\n", lift_r, lift_g, lift_b);
376 printf("gamma: %f %f %f\n", gamma_r, gamma_g, gamma_b);
377 printf("gain: %f %f %f\n", gain_r, gain_g, gain_b);
378 printf("saturation: %f\n", saturation);
382 void mouse(int x, int y)
384 float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
385 float yf = (HEIGHT - y) / (float)HEIGHT;
388 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
389 } else if (yf >= 0.2f && yf < 0.4f) {
390 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
391 } else if (yf >= 0.4f && yf < 0.6f) {
392 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
393 } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
394 saturation = (xf / 0.2f) * 4.0f;
400 void load_texture(const char *filename)
402 SDL_Surface *img = IMG_Load(filename);
404 fprintf(stderr, "Load of '%s' failed\n", filename);
409 SDL_PixelFormat *fmt = img->format;
410 SDL_LockSurface(img);
411 unsigned char *src_pixels = (unsigned char *)img->pixels;
412 unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 3);
413 for (unsigned i = 0; i < img->w * img->h; ++i) {
414 unsigned char r, g, b;
416 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
418 temp = pixel & fmt->Rmask;
419 temp = temp >> fmt->Rshift;
420 temp = temp << fmt->Rloss;
423 temp = pixel & fmt->Gmask;
424 temp = temp >> fmt->Gshift;
425 temp = temp << fmt->Gloss;
428 temp = pixel & fmt->Bmask;
429 temp = temp >> fmt->Bshift;
430 temp = temp << fmt->Bloss;
433 dst_pixels[i * 3 + 0] = r;
434 dst_pixels[i * 3 + 1] = g;
435 dst_pixels[i * 3 + 2] = b;
437 SDL_UnlockSurface(img);
440 // we will convert to sRGB in the shader
441 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
444 // implicit sRGB conversion in hardware
445 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
449 SDL_FreeSurface(img);
452 void write_ppm(const char *filename, unsigned char *screenbuf)
454 FILE *fp = fopen(filename, "w");
455 fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
456 for (unsigned y = 0; y < HEIGHT; ++y) {
457 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
458 for (unsigned x = 0; x < WIDTH; ++x) {
459 fputc(srcptr[x * 4 + 2], fp);
460 fputc(srcptr[x * 4 + 1], fp);
461 fputc(srcptr[x * 4 + 0], fp);
467 int main(int argc, char **argv)
471 SDL_Init(SDL_INIT_EVERYTHING);
472 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
473 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
474 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
475 SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
476 SDL_WM_SetCaption("OpenGL window", NULL);
479 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
480 glPixelStorei(GL_PACK_ALIGNMENT, 1);
482 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
484 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
487 load_texture("blg_wheels_woman_1.jpg");
488 //glGenerateMipmap(GL_TEXTURE_2D);
489 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
492 //load_texture("maserati_gts_wallpaper_1280x720_01.jpg");
493 //load_texture("90630d1295075297-console-games-wallpapers-wallpaper_need_for_speed_prostreet_09_1920x1080.jpg");
494 //load_texture("glacier-lake-1280-720-4087.jpg");
498 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
500 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
501 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
503 float srgb_reverse_tex[4096];
504 for (unsigned i = 0; i < 4096; ++i) {
505 float x = i / 4095.0;
506 if (x < 0.0031308f) {
507 srgb_reverse_tex[i] = 12.92f * x;
509 srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
512 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
516 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
518 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
519 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
522 for (unsigned i = 0; i < 256; ++i) {
525 srgb_tex[i] = x * (1.0f / 12.92f);
527 srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
530 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
534 // generate a PDO to hold the data we read back with glReadPixels()
535 // (Intel/DRI goes into a slow path if we don't read to PDO)
536 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
537 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
539 make_hsv_wheel_texture();
542 int prog = glCreateProgram();
543 GLhandleARB vs_obj = read_shader("vs.glsl", GL_VERTEX_SHADER);
544 GLhandleARB fs_obj = read_shader("fs.glsl", GL_FRAGMENT_SHADER);
545 glAttachObjectARB(prog, vs_obj);
547 glAttachObjectARB(prog, fs_obj);
552 GLchar info_log[4096];
553 GLsizei log_length = sizeof(info_log) - 1;
554 log_length = sizeof(info_log) - 1;
555 glGetProgramInfoLog(prog, log_length, &log_length, info_log);
556 info_log[log_length] = 0;
557 printf("link: %s\n", info_log);
559 struct timespec start, now;
560 int frame = 0, screenshot = 0;
561 clock_gettime(CLOCK_MONOTONIC, &start);
565 while (SDL_PollEvent(&event)) {
566 if (event.type == SDL_QUIT) {
568 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
570 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
572 } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
573 mouse(event.button.x, event.button.y);
574 } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
575 mouse(event.motion.x, event.motion.y);
581 draw_picture_quad(prog, frame);
583 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
586 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
587 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
588 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
589 draw_saturation_bar(0.6f, saturation);
591 SDL_GL_SwapBuffers();
594 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
598 sprintf(filename, "frame%05d.ppm", frame);
599 write_ppm(filename, screenbuf);
600 printf("Screenshot: %s\n", filename);
603 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
607 clock_gettime(CLOCK_MONOTONIC, &now);
608 double elapsed = now.tv_sec - start.tv_sec +
609 1e-9 * (now.tv_nsec - start.tv_nsec);
610 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
611 frame, elapsed, frame / elapsed,
612 1e3 * elapsed / frame);