1 #define GL_GLEXT_PROTOTYPES 1
6 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
19 #include <SDL/SDL_opengl.h>
20 #include <SDL/SDL_image.h>
26 #include "effect_chain.h"
29 #include "texture_enum.h"
31 unsigned char result[WIDTH * HEIGHT * 4];
33 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
34 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
35 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
36 float saturation = 1.0f;
39 float inner_radius = 0.3f;
41 void update_hsv(Effect *lift_gamma_gain_effect, Effect *saturation_effect)
43 RGBTriplet lift(0.0f, 0.0f, 0.0f);
44 RGBTriplet gamma(1.0f, 1.0f, 1.0f);
45 RGBTriplet gain(1.0f, 1.0f, 1.0f);
47 hsv2rgb(lift_theta, lift_rad, lift_v, &lift.r, &lift.g, &lift.b);
48 hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b);
49 hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b);
51 bool ok = lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
52 ok = ok && lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
53 ok = ok && lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
56 if (saturation < 0.0) {
59 ok = saturation_effect->set_float("saturation", saturation);
63 void mouse(int x, int y)
65 float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
66 float yf = (HEIGHT - y) / (float)HEIGHT;
69 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
70 } else if (yf >= 0.2f && yf < 0.4f) {
71 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
72 } else if (yf >= 0.4f && yf < 0.6f) {
73 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
74 } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
75 saturation = (xf / 0.2f) * 4.0f;
76 } else if (yf >= 0.65f && yf < 0.67f && xf < 0.2f) {
78 } else if (yf >= 0.70f && yf < 0.72f && xf < 0.2f) {
79 inner_radius = (xf / 0.2f);
83 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
85 SDL_Surface *img = IMG_Load(filename);
87 fprintf(stderr, "Load of '%s' failed\n", filename);
92 SDL_PixelFormat *fmt = img->format;
94 unsigned char *src_pixels = (unsigned char *)img->pixels;
95 unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 4);
96 for (int i = 0; i < img->w * img->h; ++i) {
97 unsigned char r, g, b;
99 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
101 temp = pixel & fmt->Rmask;
102 temp = temp >> fmt->Rshift;
103 temp = temp << fmt->Rloss;
106 temp = pixel & fmt->Gmask;
107 temp = temp >> fmt->Gshift;
108 temp = temp << fmt->Gloss;
111 temp = pixel & fmt->Bmask;
112 temp = temp >> fmt->Bshift;
113 temp = temp << fmt->Bloss;
116 dst_pixels[i * 4 + 0] = b;
117 dst_pixels[i * 4 + 1] = g;
118 dst_pixels[i * 4 + 2] = r;
119 dst_pixels[i * 4 + 3] = 255;
121 SDL_UnlockSurface(img);
126 SDL_FreeSurface(img);
131 void write_ppm(const char *filename, unsigned char *screenbuf)
133 FILE *fp = fopen(filename, "w");
134 fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
135 for (unsigned y = 0; y < HEIGHT; ++y) {
136 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
137 for (unsigned x = 0; x < WIDTH; ++x) {
138 fputc(srcptr[x * 4 + 2], fp);
139 fputc(srcptr[x * 4 + 1], fp);
140 fputc(srcptr[x * 4 + 0], fp);
146 int main(int argc, char **argv)
150 SDL_Init(SDL_INIT_EVERYTHING);
151 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
152 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
153 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
154 SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
155 SDL_WM_SetCaption("OpenGL window", NULL);
158 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
159 glPixelStorei(GL_PACK_ALIGNMENT, 1);
161 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
163 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
166 unsigned img_w, img_h;
167 unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
169 EffectChain chain(WIDTH, HEIGHT);
171 ImageFormat inout_format;
172 inout_format.pixel_format = FORMAT_BGRA;
173 inout_format.color_space = COLORSPACE_sRGB;
174 inout_format.gamma_curve = GAMMA_sRGB;
176 chain.add_input(inout_format);
177 Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN);
178 Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION);
179 Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE);
180 chain.add_output(inout_format);
183 //glGenerateMipmap(GL_TEXTURE_2D);
184 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
189 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
191 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
192 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
194 float srgb_reverse_tex[4096];
195 for (unsigned i = 0; i < 4096; ++i) {
196 float x = i / 4095.0;
197 if (x < 0.0031308f) {
198 srgb_reverse_tex[i] = 12.92f * x;
200 srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
203 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
207 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
209 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
210 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
213 for (unsigned i = 0; i < 256; ++i) {
216 srgb_tex[i] = x * (1.0f / 12.92f);
218 srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
221 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
225 // generate a PDO to hold the data we read back with glReadPixels()
226 // (Intel/DRI goes into a slow path if we don't read to PDO)
227 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
228 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
230 make_hsv_wheel_texture();
232 int frame = 0, screenshot = 0;
233 #if _POSIX_C_SOURCE >= 199309L
234 struct timespec start, now;
235 clock_gettime(CLOCK_MONOTONIC, &start);
237 struct timeval start, now;
238 gettimeofday(&start, NULL);
243 while (SDL_PollEvent(&event)) {
244 if (event.type == SDL_QUIT) {
246 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
248 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
250 } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
251 mouse(event.button.x, event.button.y);
252 } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
253 mouse(event.motion.x, event.motion.y);
259 update_hsv(lift_gamma_gain_effect, saturation_effect);
260 vignette_effect->set_float("radius", radius);
261 vignette_effect->set_float("inner_radius", inner_radius);
262 chain.render_to_screen(src_img);
264 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
267 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
268 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
269 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
270 draw_saturation_bar(0.6f, saturation / 4.0f);
271 draw_saturation_bar(0.65f, radius);
272 draw_saturation_bar(0.70f, inner_radius);
274 SDL_GL_SwapBuffers();
277 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
281 sprintf(filename, "frame%05d.ppm", frame);
282 write_ppm(filename, screenbuf);
283 printf("Screenshot: %s\n", filename);
286 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
290 #if _POSIX_C_SOURCE >= 199309L
291 clock_gettime(CLOCK_MONOTONIC, &now);
292 double elapsed = now.tv_sec - start.tv_sec +
293 1e-9 * (now.tv_nsec - start.tv_nsec);
295 gettimeofday(&now, NULL);
296 double elapsed = now.tv_sec - start.tv_sec +
297 1e-6 * (now.tv_usec - start.tv_usec);
299 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
300 frame, elapsed, frame / elapsed,
301 1e3 * elapsed / frame);