1 #define GL_GLEXT_PROTOTYPES 1
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;
38 float inner_radius = 0.3f;
40 void update_hsv(Effect *lift_gamma_gain_effect, Effect *saturation_effect)
42 RGBTriplet lift(0.0f, 0.0f, 0.0f);
43 RGBTriplet gamma(1.0f, 1.0f, 1.0f);
44 RGBTriplet gain(1.0f, 1.0f, 1.0f);
46 hsv2rgb(lift_theta, lift_rad, lift_v, &lift.r, &lift.g, &lift.b);
47 hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b);
48 hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b);
50 bool ok = lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
51 ok = ok && lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
52 ok = ok && lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
55 if (saturation < 0.0) {
58 ok = saturation_effect->set_float("saturation", saturation);
62 void mouse(int x, int y)
64 float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
65 float yf = (HEIGHT - y) / (float)HEIGHT;
68 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
69 } else if (yf >= 0.2f && yf < 0.4f) {
70 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
71 } else if (yf >= 0.4f && yf < 0.6f) {
72 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
73 } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
74 saturation = (xf / 0.2f) * 4.0f;
75 } else if (yf >= 0.65f && yf < 0.67f && xf < 0.2f) {
77 } else if (yf >= 0.70f && yf < 0.72f && xf < 0.2f) {
78 inner_radius = (xf / 0.2f);
82 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
84 SDL_Surface *img = IMG_Load(filename);
86 fprintf(stderr, "Load of '%s' failed\n", filename);
91 SDL_PixelFormat *fmt = img->format;
93 unsigned char *src_pixels = (unsigned char *)img->pixels;
94 unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 4);
95 for (int i = 0; i < img->w * img->h; ++i) {
96 unsigned char r, g, b;
98 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
100 temp = pixel & fmt->Rmask;
101 temp = temp >> fmt->Rshift;
102 temp = temp << fmt->Rloss;
105 temp = pixel & fmt->Gmask;
106 temp = temp >> fmt->Gshift;
107 temp = temp << fmt->Gloss;
110 temp = pixel & fmt->Bmask;
111 temp = temp >> fmt->Bshift;
112 temp = temp << fmt->Bloss;
115 dst_pixels[i * 4 + 0] = b;
116 dst_pixels[i * 4 + 1] = g;
117 dst_pixels[i * 4 + 2] = r;
118 dst_pixels[i * 4 + 3] = 255;
120 SDL_UnlockSurface(img);
125 SDL_FreeSurface(img);
130 void write_ppm(const char *filename, unsigned char *screenbuf)
132 FILE *fp = fopen(filename, "w");
133 fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
134 for (unsigned y = 0; y < HEIGHT; ++y) {
135 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
136 for (unsigned x = 0; x < WIDTH; ++x) {
137 fputc(srcptr[x * 4 + 2], fp);
138 fputc(srcptr[x * 4 + 1], fp);
139 fputc(srcptr[x * 4 + 0], fp);
145 int main(int argc, char **argv)
149 SDL_Init(SDL_INIT_EVERYTHING);
150 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
151 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
152 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
153 SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
154 SDL_WM_SetCaption("OpenGL window", NULL);
157 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
158 glPixelStorei(GL_PACK_ALIGNMENT, 1);
160 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
165 unsigned img_w, img_h;
166 unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
168 EffectChain chain(WIDTH, HEIGHT);
170 ImageFormat inout_format;
171 inout_format.pixel_format = FORMAT_BGRA;
172 inout_format.color_space = COLORSPACE_sRGB;
173 inout_format.gamma_curve = GAMMA_sRGB;
175 chain.add_input(inout_format);
176 Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN);
177 Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION);
178 Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE);
179 chain.add_output(inout_format);
182 //glGenerateMipmap(GL_TEXTURE_2D);
183 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
188 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
190 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
191 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
193 float srgb_reverse_tex[4096];
194 for (unsigned i = 0; i < 4096; ++i) {
195 float x = i / 4095.0;
196 if (x < 0.0031308f) {
197 srgb_reverse_tex[i] = 12.92f * x;
199 srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
202 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
206 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
208 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
209 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
212 for (unsigned i = 0; i < 256; ++i) {
215 srgb_tex[i] = x * (1.0f / 12.92f);
217 srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
220 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
224 // generate a PDO to hold the data we read back with glReadPixels()
225 // (Intel/DRI goes into a slow path if we don't read to PDO)
226 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
227 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
229 make_hsv_wheel_texture();
231 int frame = 0, screenshot = 0;
232 #if _POSIX_C_SOURCE >= 199309L
233 struct timespec start, now;
234 clock_gettime(CLOCK_MONOTONIC, &start);
236 struct timeval start, now;
237 gettimeofday(&start, NULL);
242 while (SDL_PollEvent(&event)) {
243 if (event.type == SDL_QUIT) {
245 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
247 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
249 } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
250 mouse(event.button.x, event.button.y);
251 } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
252 mouse(event.motion.x, event.motion.y);
258 update_hsv(lift_gamma_gain_effect, saturation_effect);
259 vignette_effect->set_float("radius", radius);
260 vignette_effect->set_float("inner_radius", inner_radius);
261 chain.render_to_screen(src_img);
263 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
265 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
267 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
270 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
271 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
272 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
273 draw_saturation_bar(0.6f, saturation / 4.0f);
274 draw_saturation_bar(0.65f, radius);
275 draw_saturation_bar(0.70f, inner_radius);
277 SDL_GL_SwapBuffers();
280 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
282 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
286 sprintf(filename, "frame%05d.ppm", frame);
287 write_ppm(filename, screenbuf);
288 printf("Screenshot: %s\n", filename);
291 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
293 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
297 #if _POSIX_C_SOURCE >= 199309L
298 clock_gettime(CLOCK_MONOTONIC, &now);
299 double elapsed = now.tv_sec - start.tv_sec +
300 1e-9 * (now.tv_nsec - start.tv_nsec);
302 gettimeofday(&now, NULL);
303 double elapsed = now.tv_sec - start.tv_sec +
304 1e-6 * (now.tv_usec - start.tv_usec);
306 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
307 frame, elapsed, frame / elapsed,
308 1e3 * elapsed / frame);