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;
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);
47 if (saturation < 0.0) {
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);
58 void mouse(int x, int y)
60 float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
61 float yf = (HEIGHT - y) / (float)HEIGHT;
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;
76 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
78 SDL_Surface *img = IMG_Load(filename);
80 fprintf(stderr, "Load of '%s' failed\n", filename);
85 SDL_PixelFormat *fmt = img->format;
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;
92 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
94 temp = pixel & fmt->Rmask;
95 temp = temp >> fmt->Rshift;
96 temp = temp << fmt->Rloss;
99 temp = pixel & fmt->Gmask;
100 temp = temp >> fmt->Gshift;
101 temp = temp << fmt->Gloss;
104 temp = pixel & fmt->Bmask;
105 temp = temp >> fmt->Bshift;
106 temp = temp << fmt->Bloss;
109 dst_pixels[i * 3 + 0] = r;
110 dst_pixels[i * 3 + 1] = g;
111 dst_pixels[i * 3 + 2] = b;
113 SDL_UnlockSurface(img);
118 SDL_FreeSurface(img);
123 void load_texture(const char *filename)
126 unsigned char *pixels = load_image(filename, &w, &h);
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);
133 // implicit sRGB conversion in hardware
134 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
141 void write_ppm(const char *filename, unsigned char *screenbuf)
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);
156 int main(int argc, char **argv)
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);
168 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
169 glPixelStorei(GL_PACK_ALIGNMENT, 1);
171 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
173 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
176 unsigned img_w, img_h;
177 unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
179 EffectChain chain(WIDTH, HEIGHT);
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;
186 chain.add_input(inout_format);
187 Effect *lift_gamma_gain_effect = chain.add_effect(LIFT_GAMMA_GAIN);
188 chain.add_output(inout_format);
191 //glGenerateMipmap(GL_TEXTURE_2D);
192 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
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");
201 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
203 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
204 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
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;
212 srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
215 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
219 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
221 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
222 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
225 for (unsigned i = 0; i < 256; ++i) {
228 srgb_tex[i] = x * (1.0f / 12.92f);
230 srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
233 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
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);
242 make_hsv_wheel_texture();
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);
250 glAttachObjectARB(prog, fs_obj);
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);
262 struct timespec start, now;
263 int frame = 0, screenshot = 0;
264 clock_gettime(CLOCK_MONOTONIC, &start);
268 while (SDL_PollEvent(&event)) {
269 if (event.type == SDL_QUIT) {
271 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
273 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
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);
284 chain.render_to_screen(src_img);
286 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
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);
294 SDL_GL_SwapBuffers();
297 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
301 sprintf(filename, "frame%05d.ppm", frame);
302 write_ppm(filename, screenbuf);
303 printf("Screenshot: %s\n", filename);
306 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
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);