1 #define GL_GLEXT_PROTOTYPES 1
6 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
15 #include <SDL/SDL_opengl.h>
16 #include <SDL/SDL_image.h>
23 #include "texture_enum.h"
25 unsigned char result[WIDTH * HEIGHT * 4];
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;
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;
37 enum { FORMAT_RGB, FORMAT_RGBA } format;
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;
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;
47 // Mostly for internal use.
49 RGB_PRIMARIES_CONVERSION,
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);
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);
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);
76 void render(unsigned char *src, unsigned char *dst);
79 unsigned width, height;
80 ImageFormat input_format, output_format;
81 std::vector<Effect *> effects;
84 GLhandleARB read_shader(const char* filename, GLenum type)
86 static char buf[131072];
87 FILE *fp = fopen(filename, "r");
93 int len = fread(buf, 1, sizeof(buf), fp);
96 GLhandleARB obj = glCreateShaderObjectARB(type);
97 const GLchar* source[] = { buf };
98 const GLint length[] = { len };
99 glShaderSource(obj, 1, source, length);
100 glCompileShader(obj);
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);
109 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
110 if (status == GL_FALSE) {
117 void draw_picture_quad(GLint prog, int frame)
119 glUseProgramObjectARB(prog);
122 glActiveTexture(GL_TEXTURE0);
123 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
124 glUniform1i(glGetUniformLocation(prog, "tex"), 0);
126 glActiveTexture(GL_TEXTURE1);
127 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
128 glUniform1i(glGetUniformLocation(prog, "srgb_tex"), 1);
130 glActiveTexture(GL_TEXTURE2);
131 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
132 glUniform1i(glGetUniformLocation(prog, "srgb_reverse_tex"), 2);
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"),
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);
148 glDisable(GL_DEPTH_TEST);
150 glDepthMask(GL_FALSE);
153 glMatrixMode(GL_PROJECTION);
155 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
157 glMatrixMode(GL_MODELVIEW);
160 glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
161 // glClear(GL_COLOR_BUFFER_BIT);
166 glTexCoord2f(0.0f, 1.0f);
167 glVertex2f(0.0f, 0.0f);
169 glTexCoord2f(1.0f, 1.0f);
170 glVertex2f(1.0f, 0.0f);
172 glTexCoord2f(1.0f, 0.0f);
173 glVertex2f(1.0f, 1.0f);
175 glTexCoord2f(0.0f, 0.0f);
176 glVertex2f(0.0f, 1.0f);
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);
188 if (saturation < 0.0) {
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);
199 void mouse(int x, int y)
201 float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
202 float yf = (HEIGHT - y) / (float)HEIGHT;
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;
217 void load_texture(const char *filename)
219 SDL_Surface *img = IMG_Load(filename);
221 fprintf(stderr, "Load of '%s' failed\n", filename);
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;
233 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
235 temp = pixel & fmt->Rmask;
236 temp = temp >> fmt->Rshift;
237 temp = temp << fmt->Rloss;
240 temp = pixel & fmt->Gmask;
241 temp = temp >> fmt->Gshift;
242 temp = temp << fmt->Gloss;
245 temp = pixel & fmt->Bmask;
246 temp = temp >> fmt->Bshift;
247 temp = temp << fmt->Bloss;
250 dst_pixels[i * 3 + 0] = r;
251 dst_pixels[i * 3 + 1] = g;
252 dst_pixels[i * 3 + 2] = b;
254 SDL_UnlockSurface(img);
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);
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);
266 SDL_FreeSurface(img);
269 void write_ppm(const char *filename, unsigned char *screenbuf)
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);
284 int main(int argc, char **argv)
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);
296 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
297 glPixelStorei(GL_PACK_ALIGNMENT, 1);
299 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
301 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
304 load_texture("blg_wheels_woman_1.jpg");
305 //glGenerateMipmap(GL_TEXTURE_2D);
306 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
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");
315 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
317 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
318 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
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;
326 srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
329 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
333 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
335 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
336 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
339 for (unsigned i = 0; i < 256; ++i) {
342 srgb_tex[i] = x * (1.0f / 12.92f);
344 srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
347 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
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);
356 make_hsv_wheel_texture();
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);
364 glAttachObjectARB(prog, fs_obj);
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);
376 struct timespec start, now;
377 int frame = 0, screenshot = 0;
378 clock_gettime(CLOCK_MONOTONIC, &start);
382 while (SDL_PollEvent(&event)) {
383 if (event.type == SDL_QUIT) {
385 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
387 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
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);
398 draw_picture_quad(prog, frame);
400 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
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);
408 SDL_GL_SwapBuffers();
411 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
415 sprintf(filename, "frame%05d.ppm", frame);
416 write_ppm(filename, screenbuf);
417 printf("Screenshot: %s\n", filename);
420 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
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);