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>
25 #include "effect_chain.h"
28 #include "texture_enum.h"
30 unsigned char result[WIDTH * HEIGHT * 4];
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;
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;
41 void draw_picture_quad(GLint prog, int frame)
43 glUseProgramObjectARB(prog);
46 glActiveTexture(GL_TEXTURE0);
47 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
48 glUniform1i(glGetUniformLocation(prog, "tex"), 0);
50 glActiveTexture(GL_TEXTURE1);
51 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
52 glUniform1i(glGetUniformLocation(prog, "srgb_tex"), 1);
54 glActiveTexture(GL_TEXTURE2);
55 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
56 glUniform1i(glGetUniformLocation(prog, "srgb_reverse_tex"), 2);
58 glUniform3f(glGetUniformLocation(prog, "lift"), lift_r, lift_g, lift_b);
59 //glUniform3f(glGetUniformLocation(prog, "gamma"), gamma_r, gamma_g, gamma_b);
60 glUniform3f(glGetUniformLocation(prog, "inv_gamma_22"),
64 glUniform3f(glGetUniformLocation(prog, "gain_pow_inv_gamma"),
65 pow(gain_r, 1.0f / gamma_r),
66 pow(gain_g, 1.0f / gamma_g),
67 pow(gain_b, 1.0f / gamma_b));
68 glUniform1f(glGetUniformLocation(prog, "saturation"), saturation);
72 glDisable(GL_DEPTH_TEST);
74 glDepthMask(GL_FALSE);
77 glMatrixMode(GL_PROJECTION);
79 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
81 glMatrixMode(GL_MODELVIEW);
84 glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
85 // glClear(GL_COLOR_BUFFER_BIT);
90 glTexCoord2f(0.0f, 1.0f);
91 glVertex2f(0.0f, 0.0f);
93 glTexCoord2f(1.0f, 1.0f);
94 glVertex2f(1.0f, 0.0f);
96 glTexCoord2f(1.0f, 0.0f);
97 glVertex2f(1.0f, 1.0f);
99 glTexCoord2f(0.0f, 0.0f);
100 glVertex2f(0.0f, 1.0f);
108 hsv2rgb(lift_theta, lift_rad, lift_v, &lift_r, &lift_g, &lift_b);
109 hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma_r, &gamma_g, &gamma_b);
110 hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain_r, &gain_g, &gain_b);
112 if (saturation < 0.0) {
116 printf("lift: %f %f %f\n", lift_r, lift_g, lift_b);
117 printf("gamma: %f %f %f\n", gamma_r, gamma_g, gamma_b);
118 printf("gain: %f %f %f\n", gain_r, gain_g, gain_b);
119 printf("saturation: %f\n", saturation);
123 void mouse(int x, int y)
125 float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
126 float yf = (HEIGHT - y) / (float)HEIGHT;
129 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
130 } else if (yf >= 0.2f && yf < 0.4f) {
131 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
132 } else if (yf >= 0.4f && yf < 0.6f) {
133 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
134 } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
135 saturation = (xf / 0.2f) * 4.0f;
141 void load_texture(const char *filename)
143 SDL_Surface *img = IMG_Load(filename);
145 fprintf(stderr, "Load of '%s' failed\n", filename);
150 SDL_PixelFormat *fmt = img->format;
151 SDL_LockSurface(img);
152 unsigned char *src_pixels = (unsigned char *)img->pixels;
153 unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 3);
154 for (unsigned i = 0; i < img->w * img->h; ++i) {
155 unsigned char r, g, b;
157 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
159 temp = pixel & fmt->Rmask;
160 temp = temp >> fmt->Rshift;
161 temp = temp << fmt->Rloss;
164 temp = pixel & fmt->Gmask;
165 temp = temp >> fmt->Gshift;
166 temp = temp << fmt->Gloss;
169 temp = pixel & fmt->Bmask;
170 temp = temp >> fmt->Bshift;
171 temp = temp << fmt->Bloss;
174 dst_pixels[i * 3 + 0] = r;
175 dst_pixels[i * 3 + 1] = g;
176 dst_pixels[i * 3 + 2] = b;
178 SDL_UnlockSurface(img);
181 // we will convert to sRGB in the shader
182 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
185 // implicit sRGB conversion in hardware
186 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
190 SDL_FreeSurface(img);
193 void write_ppm(const char *filename, unsigned char *screenbuf)
195 FILE *fp = fopen(filename, "w");
196 fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
197 for (unsigned y = 0; y < HEIGHT; ++y) {
198 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
199 for (unsigned x = 0; x < WIDTH; ++x) {
200 fputc(srcptr[x * 4 + 2], fp);
201 fputc(srcptr[x * 4 + 1], fp);
202 fputc(srcptr[x * 4 + 0], fp);
208 int main(int argc, char **argv)
212 SDL_Init(SDL_INIT_EVERYTHING);
213 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
214 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
215 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
216 SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
217 SDL_WM_SetCaption("OpenGL window", NULL);
220 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
221 glPixelStorei(GL_PACK_ALIGNMENT, 1);
223 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
225 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
228 load_texture("blg_wheels_woman_1.jpg");
230 EffectChain chain(WIDTH, HEIGHT);
232 ImageFormat inout_format;
233 inout_format.pixel_format = FORMAT_RGB;
234 inout_format.color_space = COLORSPACE_sRGB;
235 inout_format.gamma_curve = GAMMA_sRGB;
237 chain.add_input(inout_format);
238 Effect *lift_gamma_gain_effect = chain.add_effect(LIFT_GAMMA_GAIN);
239 chain.add_output(inout_format);
242 //glGenerateMipmap(GL_TEXTURE_2D);
243 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
246 //load_texture("maserati_gts_wallpaper_1280x720_01.jpg");
247 //load_texture("90630d1295075297-console-games-wallpapers-wallpaper_need_for_speed_prostreet_09_1920x1080.jpg");
248 //load_texture("glacier-lake-1280-720-4087.jpg");
252 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
254 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
255 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
257 float srgb_reverse_tex[4096];
258 for (unsigned i = 0; i < 4096; ++i) {
259 float x = i / 4095.0;
260 if (x < 0.0031308f) {
261 srgb_reverse_tex[i] = 12.92f * x;
263 srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
266 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
270 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
272 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
273 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
276 for (unsigned i = 0; i < 256; ++i) {
279 srgb_tex[i] = x * (1.0f / 12.92f);
281 srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
284 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
288 // generate a PDO to hold the data we read back with glReadPixels()
289 // (Intel/DRI goes into a slow path if we don't read to PDO)
290 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
291 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
293 make_hsv_wheel_texture();
296 int prog = glCreateProgram();
297 GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
298 GLhandleARB fs_obj = compile_shader(read_file("fs.glsl"), GL_FRAGMENT_SHADER);
299 glAttachObjectARB(prog, vs_obj);
301 glAttachObjectARB(prog, fs_obj);
306 GLchar info_log[4096];
307 GLsizei log_length = sizeof(info_log) - 1;
308 log_length = sizeof(info_log) - 1;
309 glGetProgramInfoLog(prog, log_length, &log_length, info_log);
310 info_log[log_length] = 0;
311 printf("link: %s\n", info_log);
313 struct timespec start, now;
314 int frame = 0, screenshot = 0;
315 clock_gettime(CLOCK_MONOTONIC, &start);
319 while (SDL_PollEvent(&event)) {
320 if (event.type == SDL_QUIT) {
322 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
324 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
326 } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
327 mouse(event.button.x, event.button.y);
328 } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
329 mouse(event.motion.x, event.motion.y);
336 draw_picture_quad(prog, frame);
338 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
341 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
342 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
343 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
344 draw_saturation_bar(0.6f, saturation);
346 SDL_GL_SwapBuffers();
349 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
353 sprintf(filename, "frame%05d.ppm", frame);
354 write_ppm(filename, screenbuf);
355 printf("Screenshot: %s\n", filename);
358 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
362 clock_gettime(CLOCK_MONOTONIC, &now);
363 double elapsed = now.tv_sec - start.tv_sec +
364 1e-9 * (now.tv_nsec - start.tv_nsec);
365 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
366 frame, elapsed, frame / elapsed,
367 1e3 * elapsed / frame);