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 void update_hsv(Effect *lift_gamma_gain_effect)
39 RGBTriplet lift(0.0f, 0.0f, 0.0f);
40 RGBTriplet gamma(1.0f, 1.0f, 1.0f);
41 RGBTriplet gain(1.0f, 1.0f, 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 lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
48 lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
49 lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
51 if (saturation < 0.0) {
56 void mouse(int x, int y)
58 float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
59 float yf = (HEIGHT - y) / (float)HEIGHT;
62 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
63 } else if (yf >= 0.2f && yf < 0.4f) {
64 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
65 } else if (yf >= 0.4f && yf < 0.6f) {
66 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
67 } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
68 saturation = (xf / 0.2f) * 4.0f;
72 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
74 SDL_Surface *img = IMG_Load(filename);
76 fprintf(stderr, "Load of '%s' failed\n", filename);
81 SDL_PixelFormat *fmt = img->format;
83 unsigned char *src_pixels = (unsigned char *)img->pixels;
84 unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 3);
85 for (unsigned i = 0; i < img->w * img->h; ++i) {
86 unsigned char r, g, b;
88 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
90 temp = pixel & fmt->Rmask;
91 temp = temp >> fmt->Rshift;
92 temp = temp << fmt->Rloss;
95 temp = pixel & fmt->Gmask;
96 temp = temp >> fmt->Gshift;
97 temp = temp << fmt->Gloss;
100 temp = pixel & fmt->Bmask;
101 temp = temp >> fmt->Bshift;
102 temp = temp << fmt->Bloss;
105 dst_pixels[i * 3 + 0] = r;
106 dst_pixels[i * 3 + 1] = g;
107 dst_pixels[i * 3 + 2] = b;
109 SDL_UnlockSurface(img);
114 SDL_FreeSurface(img);
119 void load_texture(const char *filename)
122 unsigned char *pixels = load_image(filename, &w, &h);
125 // we will convert to sRGB in the shader
126 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
129 // implicit sRGB conversion in hardware
130 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
137 void write_ppm(const char *filename, unsigned char *screenbuf)
139 FILE *fp = fopen(filename, "w");
140 fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
141 for (unsigned y = 0; y < HEIGHT; ++y) {
142 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
143 for (unsigned x = 0; x < WIDTH; ++x) {
144 fputc(srcptr[x * 4 + 2], fp);
145 fputc(srcptr[x * 4 + 1], fp);
146 fputc(srcptr[x * 4 + 0], fp);
152 int main(int argc, char **argv)
156 SDL_Init(SDL_INIT_EVERYTHING);
157 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
158 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
159 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
160 SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
161 SDL_WM_SetCaption("OpenGL window", NULL);
164 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
165 glPixelStorei(GL_PACK_ALIGNMENT, 1);
167 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
169 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
172 unsigned img_w, img_h;
173 unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
175 EffectChain chain(WIDTH, HEIGHT);
177 ImageFormat inout_format;
178 inout_format.pixel_format = FORMAT_RGB;
179 inout_format.color_space = COLORSPACE_sRGB;
180 inout_format.gamma_curve = GAMMA_sRGB;
182 chain.add_input(inout_format);
183 Effect *lift_gamma_gain_effect = chain.add_effect(LIFT_GAMMA_GAIN);
184 chain.add_output(inout_format);
187 //glGenerateMipmap(GL_TEXTURE_2D);
188 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
191 //load_texture("maserati_gts_wallpaper_1280x720_01.jpg");
192 //load_texture("90630d1295075297-console-games-wallpapers-wallpaper_need_for_speed_prostreet_09_1920x1080.jpg");
193 //load_texture("glacier-lake-1280-720-4087.jpg");
197 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
199 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
200 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
202 float srgb_reverse_tex[4096];
203 for (unsigned i = 0; i < 4096; ++i) {
204 float x = i / 4095.0;
205 if (x < 0.0031308f) {
206 srgb_reverse_tex[i] = 12.92f * x;
208 srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
211 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
215 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
217 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
218 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
221 for (unsigned i = 0; i < 256; ++i) {
224 srgb_tex[i] = x * (1.0f / 12.92f);
226 srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
229 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
233 // generate a PDO to hold the data we read back with glReadPixels()
234 // (Intel/DRI goes into a slow path if we don't read to PDO)
235 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
236 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
238 make_hsv_wheel_texture();
240 int prog = glCreateProgram();
241 GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
242 GLhandleARB fs_obj = compile_shader(read_file("fs.glsl"), GL_FRAGMENT_SHADER);
243 glAttachObjectARB(prog, vs_obj);
245 glAttachObjectARB(prog, fs_obj);
250 GLchar info_log[4096];
251 GLsizei log_length = sizeof(info_log) - 1;
252 log_length = sizeof(info_log) - 1;
253 glGetProgramInfoLog(prog, log_length, &log_length, info_log);
254 info_log[log_length] = 0;
255 printf("link: %s\n", info_log);
257 struct timespec start, now;
258 int frame = 0, screenshot = 0;
259 clock_gettime(CLOCK_MONOTONIC, &start);
263 while (SDL_PollEvent(&event)) {
264 if (event.type == SDL_QUIT) {
266 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
268 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
270 } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
271 mouse(event.button.x, event.button.y);
272 } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
273 mouse(event.motion.x, event.motion.y);
279 update_hsv(lift_gamma_gain_effect);
280 chain.render_to_screen(src_img);
282 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
285 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
286 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
287 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
288 draw_saturation_bar(0.6f, saturation);
290 SDL_GL_SwapBuffers();
293 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
297 sprintf(filename, "frame%05d.ppm", frame);
298 write_ppm(filename, screenbuf);
299 printf("Screenshot: %s\n", filename);
302 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
306 clock_gettime(CLOCK_MONOTONIC, &now);
307 double elapsed = now.tv_sec - start.tv_sec +
308 1e-9 * (now.tv_nsec - start.tv_nsec);
309 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
310 frame, elapsed, frame / elapsed,
311 1e3 * elapsed / frame);