1 #define GL_GLEXT_PROTOTYPES 1
18 #include <SDL/SDL_opengl.h>
19 #include <SDL/SDL_image.h>
25 #include "effect_chain.h"
29 unsigned char result[WIDTH * HEIGHT * 4];
31 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
32 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
33 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
34 float saturation = 1.0f;
37 float inner_radius = 0.3f;
38 float blur_radius = 3.0f;
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);
79 } else if (yf >= 0.75f && yf < 0.77f && xf < 0.2f) {
80 blur_radius = (xf / 0.2f) * 100.0f;
84 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
86 SDL_Surface *img = IMG_Load(filename);
88 fprintf(stderr, "Load of '%s' failed\n", filename);
92 SDL_PixelFormat rgba_fmt;
93 rgba_fmt.palette = NULL;
94 rgba_fmt.BitsPerPixel = 32;
95 rgba_fmt.BytesPerPixel = 8;
96 rgba_fmt.Rloss = rgba_fmt.Gloss = rgba_fmt.Bloss = rgba_fmt.Aloss = 0;
98 // NOTE: Assumes little endian.
99 rgba_fmt.Rmask = 0x00ff0000;
100 rgba_fmt.Gmask = 0x0000ff00;
101 rgba_fmt.Bmask = 0x000000ff;
102 rgba_fmt.Amask = 0xff000000;
104 rgba_fmt.Rshift = 16;
107 rgba_fmt.Ashift = 24;
109 rgba_fmt.colorkey = 0;
110 rgba_fmt.alpha = 255;
112 SDL_Surface *converted = SDL_ConvertSurface(img, &rgba_fmt, SDL_SWSURFACE);
117 SDL_FreeSurface(img);
119 return (unsigned char *)converted->pixels;
122 void write_ppm(const char *filename, unsigned char *screenbuf)
124 FILE *fp = fopen(filename, "w");
125 fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
126 for (unsigned y = 0; y < HEIGHT; ++y) {
127 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
128 for (unsigned x = 0; x < WIDTH; ++x) {
129 fputc(srcptr[x * 4 + 2], fp);
130 fputc(srcptr[x * 4 + 1], fp);
131 fputc(srcptr[x * 4 + 0], fp);
137 int main(int argc, char **argv)
141 SDL_Init(SDL_INIT_EVERYTHING);
142 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
143 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
144 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
145 SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
146 SDL_WM_SetCaption("OpenGL window", NULL);
149 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
150 glPixelStorei(GL_PACK_ALIGNMENT, 1);
152 unsigned img_w, img_h;
153 unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
155 EffectChain chain(WIDTH, HEIGHT);
157 ImageFormat inout_format;
158 inout_format.pixel_format = FORMAT_BGRA;
159 inout_format.color_space = COLORSPACE_sRGB;
160 inout_format.gamma_curve = GAMMA_sRGB;
162 chain.add_input(inout_format);
163 Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN);
164 Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION);
165 Effect *blur_effect = chain.add_effect(EFFECT_BLUR);
166 Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE);
167 //Effect *sandbox_effect = chain.add_effect(EFFECT_SANDBOX);
168 //sandbox_effect->set_float("parm", 42.0f);
169 //chain.add_effect(EFFECT_MIRROR);
170 chain.add_output(inout_format);
173 // generate a PDO to hold the data we read back with glReadPixels()
174 // (Intel/DRI goes into a slow path if we don't read to PDO)
175 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
176 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
178 make_hsv_wheel_texture();
180 int frame = 0, screenshot = 0;
181 #if _POSIX_C_SOURCE >= 199309L
182 struct timespec start, now;
183 clock_gettime(CLOCK_MONOTONIC, &start);
185 struct timeval start, now;
186 gettimeofday(&start, NULL);
191 while (SDL_PollEvent(&event)) {
192 if (event.type == SDL_QUIT) {
194 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
196 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
198 } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
199 mouse(event.button.x, event.button.y);
200 } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
201 mouse(event.motion.x, event.motion.y);
207 update_hsv(lift_gamma_gain_effect, saturation_effect);
208 vignette_effect->set_float("radius", radius);
209 vignette_effect->set_float("inner_radius", inner_radius);
210 //vignette_effect->set_vec2("center", (float[]){ 0.7f, 0.5f });
212 blur_effect->set_float("radius", blur_radius);
214 chain.render_to_screen(src_img);
216 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
218 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
220 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
224 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
225 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
226 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
227 draw_saturation_bar(0.6f, saturation / 4.0f);
228 draw_saturation_bar(0.65f, radius);
229 draw_saturation_bar(0.70f, inner_radius);
230 draw_saturation_bar(0.75f, blur_radius / 100.0f);
232 SDL_GL_SwapBuffers();
235 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
237 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
241 sprintf(filename, "frame%05d.ppm", frame);
242 write_ppm(filename, screenbuf);
243 printf("Screenshot: %s\n", filename);
246 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
248 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
252 #if _POSIX_C_SOURCE >= 199309L
253 clock_gettime(CLOCK_MONOTONIC, &now);
254 double elapsed = now.tv_sec - start.tv_sec +
255 1e-9 * (now.tv_nsec - start.tv_nsec);
257 gettimeofday(&now, NULL);
258 double elapsed = now.tv_sec - start.tv_sec +
259 1e-6 * (now.tv_usec - start.tv_usec);
261 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
262 frame, elapsed, frame / elapsed,
263 1e3 * elapsed / frame);