Remove the now obsolete load_texture().
[movit] / main.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2 #define NO_SDL_GLEXT 1
3
4 #define WIDTH 1280
5 #define HEIGHT 720
6 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
7
8 #include <string.h>
9 #include <math.h>
10 #include <time.h>
11 #include <assert.h>
12
13 #include <string>
14 #include <vector>
15 #include <map>
16
17 #include <SDL/SDL.h>
18 #include <SDL/SDL_opengl.h>
19 #include <SDL/SDL_image.h>
20
21 #include <GL/gl.h>
22 #include <GL/glext.h>
23
24 #include "effect.h"
25 #include "effect_chain.h"
26 #include "util.h"
27 #include "widgets.h"
28 #include "texture_enum.h"
29
30 unsigned char result[WIDTH * HEIGHT * 4];
31
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;
36
37 void update_hsv(Effect *lift_gamma_gain_effect, Effect *saturation_effect)
38 {
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);
42
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);
46
47         bool ok = lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
48         ok = ok && lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
49         ok = ok && lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
50         assert(ok);
51
52         if (saturation < 0.0) {
53                 saturation = 0.0;
54         }
55         ok = saturation_effect->set_float("saturation", saturation);
56         assert(ok);
57 }
58
59 void mouse(int x, int y)
60 {
61         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
62         float yf = (HEIGHT - y) / (float)HEIGHT;
63
64         if (yf < 0.2f) {
65                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
66         } else if (yf >= 0.2f && yf < 0.4f) {
67                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
68         } else if (yf >= 0.4f && yf < 0.6f) {
69                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
70         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
71                 saturation = (xf / 0.2f) * 4.0f;
72         }
73 }
74
75 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
76 {
77         SDL_Surface *img = IMG_Load(filename);
78         if (img == NULL) {
79                 fprintf(stderr, "Load of '%s' failed\n", filename);
80                 exit(1);
81         }
82
83         // Convert to RGB.
84         SDL_PixelFormat *fmt = img->format;
85         SDL_LockSurface(img);
86         unsigned char *src_pixels = (unsigned char *)img->pixels;
87         unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 3);
88         for (unsigned i = 0; i < img->w * img->h; ++i) {
89                 unsigned char r, g, b;
90                 unsigned int temp;
91                 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
92
93                 temp = pixel & fmt->Rmask;
94                 temp = temp >> fmt->Rshift;
95                 temp = temp << fmt->Rloss;
96                 r = temp;
97
98                 temp = pixel & fmt->Gmask;
99                 temp = temp >> fmt->Gshift;
100                 temp = temp << fmt->Gloss;
101                 g = temp;
102
103                 temp = pixel & fmt->Bmask;
104                 temp = temp >> fmt->Bshift;
105                 temp = temp << fmt->Bloss;
106                 b = temp;
107
108                 dst_pixels[i * 3 + 0] = r;
109                 dst_pixels[i * 3 + 1] = g;
110                 dst_pixels[i * 3 + 2] = b;
111         }
112         SDL_UnlockSurface(img);
113
114         *w = img->w;
115         *h = img->h;
116
117         SDL_FreeSurface(img);
118
119         return dst_pixels;
120 }
121
122 void write_ppm(const char *filename, unsigned char *screenbuf)
123 {
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);
132                 }
133         }
134         fclose(fp);
135 }
136
137 int main(int argc, char **argv)
138 {
139         int quit = 0;
140
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);
147         
148         // geez 
149         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
150         glPixelStorei(GL_PACK_ALIGNMENT, 1);
151
152         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
153         check_error();
154         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
155         check_error();
156
157         unsigned img_w, img_h;
158         unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
159
160         EffectChain chain(WIDTH, HEIGHT);
161
162         ImageFormat inout_format;
163         inout_format.pixel_format = FORMAT_RGB;
164         inout_format.color_space = COLORSPACE_sRGB;
165         inout_format.gamma_curve = GAMMA_sRGB;
166
167         chain.add_input(inout_format);
168         Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN);
169         Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION);
170         chain.add_output(inout_format);
171         chain.finalize();
172
173         //glGenerateMipmap(GL_TEXTURE_2D);
174         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
175         //check_error();
176
177 #if 0
178         // sRGB reverse LUT
179         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
180         check_error();
181         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
182         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
183         check_error();
184         float srgb_reverse_tex[4096];
185         for (unsigned i = 0; i < 4096; ++i) {
186                 float x = i / 4095.0;
187                 if (x < 0.0031308f) {
188                         srgb_reverse_tex[i] = 12.92f * x;
189                 } else {
190                         srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
191                 }
192         }
193         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
194         check_error();
195
196         // sRGB LUT
197         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
198         check_error();
199         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
200         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
201         check_error();
202         float srgb_tex[256];
203         for (unsigned i = 0; i < 256; ++i) {
204                 float x = i / 255.0;
205                 if (x < 0.04045f) {
206                         srgb_tex[i] = x * (1.0f / 12.92f);
207                 } else {
208                         srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
209                 }
210         }
211         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
212         check_error();
213 #endif
214
215         // generate a PDO to hold the data we read back with glReadPixels()
216         // (Intel/DRI goes into a slow path if we don't read to PDO)
217         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
218         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
219
220         make_hsv_wheel_texture();
221
222         struct timespec start, now;
223         int frame = 0, screenshot = 0;
224         clock_gettime(CLOCK_MONOTONIC, &start);
225
226         while (!quit) {
227                 SDL_Event event;
228                 while (SDL_PollEvent(&event)) {
229                         if (event.type == SDL_QUIT) {
230                                 quit = 1;
231                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
232                                 quit = 1;
233                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
234                                 screenshot = 1;
235                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
236                                 mouse(event.button.x, event.button.y);
237                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
238                                 mouse(event.motion.x, event.motion.y);
239                         }
240                 }
241
242                 ++frame;
243
244                 update_hsv(lift_gamma_gain_effect, saturation_effect);
245                 chain.render_to_screen(src_img);
246                 
247                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
248                 check_error();
249
250                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
251                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
252                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
253                 draw_saturation_bar(0.6f, saturation);
254
255                 SDL_GL_SwapBuffers();
256                 check_error();
257
258                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
259                 check_error();
260                 if (screenshot) {
261                         char filename[256];
262                         sprintf(filename, "frame%05d.ppm", frame);
263                         write_ppm(filename, screenbuf);
264                         printf("Screenshot: %s\n", filename);
265                         screenshot = 0;
266                 }
267                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
268                 check_error();
269
270 #if 1
271                 clock_gettime(CLOCK_MONOTONIC, &now);
272                 double elapsed = now.tv_sec - start.tv_sec +
273                         1e-9 * (now.tv_nsec - start.tv_nsec);
274                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
275                         frame, elapsed, frame / elapsed,
276                         1e3 * elapsed / frame);
277 #endif
278         }
279         return 0; 
280 }