summaryrefslogtreecommitdiff
path: root/progs/glsl/convolutions.c
blob: 9b9ee532453ff52a92afea68cab5851fc9ca66cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#define GL_GLEXT_PROTOTYPES
#include "readtex.h"

#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>

enum Filter {
   GAUSSIAN_BLUR,
   SHARPEN,
   MEAN_REMOVAL,
   EMBOSS,
   EDGE_DETECT,
   NO_FILTER,
   LAST
};
#define QUIT LAST

struct BoundingBox {
   float minx, miny, minz;
   float maxx, maxy, maxz;
};
struct Texture {
   GLuint id;
   GLfloat x;
   GLfloat y;
   GLint width;
   GLint height;
   GLenum format;
};

static const char *textureLocation = "../images/girl2.rgb";

static GLfloat viewRotx = 0.0, viewRoty = 0.0, viewRotz = 0.0;
static struct BoundingBox box;
static struct Texture texture;
static GLuint program;
static GLint menuId;
static enum Filter filter = GAUSSIAN_BLUR;


static void checkError(int line)
{
   GLenum err = glGetError();
   if (err) {
      printf("GL Error %s (0x%x) at line %d\n",
             gluErrorString(err), (int) err, line);
   }
}

static void loadAndCompileShader(GLuint shader, const char *text)
{
   GLint stat;

   glShaderSource(shader, 1, (const GLchar **) &text, NULL);

   glCompileShader(shader);

   glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
   if (!stat) {
      GLchar log[1000];
      GLsizei len;
      glGetShaderInfoLog(shader, 1000, &len, log);
      fprintf(stderr, "Problem compiling shader: %s\n", log);
      exit(1);
   }
   else {
      printf("Shader compiled OK\n");
   }
}

static void readShader(GLuint shader, const char *filename)
{
   const int max = 100*1000;
   int n;
   char *buffer = (char*) malloc(max);
   FILE *f = fopen(filename, "r");
   if (!f) {
      fprintf(stderr, "Unable to open shader file %s\n", filename);
      exit(1);
   }

   n = fread(buffer, 1, max, f);
   printf("Read %d bytes from shader file %s\n", n, filename);
   if (n > 0) {
      buffer[n] = 0;
      loadAndCompileShader(shader, buffer);
   }

   fclose(f);
   free(buffer);
}


static void
checkLink(GLuint prog)
{
   GLint stat;
   glGetProgramiv(prog, GL_LINK_STATUS, &stat);
   if (!stat) {
      GLchar log[1000];
      GLsizei len;
      glGetProgramInfoLog(prog, 1000, &len, log);
      fprintf(stderr, "Linker error:\n%s\n", log);
   }
   else {
      fprintf(stderr, "Link success!\n");
   }
}

static void fillConvolution(GLint *k,
                            GLfloat *scale,
                            GLfloat *color)
{
   switch(filter) {
   case GAUSSIAN_BLUR:
      k[0] = 1; k[1] = 2; k[2] = 1;
      k[3] = 2; k[4] = 4; k[5] = 2;
      k[6] = 1; k[7] = 2; k[8] = 1;

      *scale = 1./16.;
      break;
   case SHARPEN:
      k[0] =  0; k[1] = -2; k[2] =  0;
      k[3] = -2; k[4] = 11; k[5] = -2;
      k[6] =  0; k[7] = -2; k[8] =  0;

      *scale = 1./3.;
      break;
   case MEAN_REMOVAL:
      k[0] = -1; k[1] = -1; k[2] = -1;
      k[3] = -1; k[4] =  9; k[5] = -1;
      k[6] = -1; k[7] = -1; k[8] = -1;

      *scale = 1./1.;
      break;
   case EMBOSS:
      k[0] = -1; k[1] =  0; k[2] = -1;
      k[3] =  0; k[4] =  4; k[5] =  0;
      k[6] = -1; k[7] =  0; k[8] = -1;

      *scale = 1./1.;
      color[0] = 0.5;
      color[1] = 0.5;
      color[2] = 0.5;
      color[3] = 0.5;
      break;
   case EDGE_DETECT:
      k[0] =  1; k[1] =  1; k[2] =  1;
      k[3] =  0; k[4] =  0; k[5] =  0;
      k[6] = -1; k[7] = -1; k[8] = -1;

      *scale = 1.;
      color[0] = 0.5;
      color[1] = 0.5;
      color[2] = 0.5;
      color[3] = 0.5;
      break;
   case NO_FILTER:
      k[0] =  0; k[1] =  0; k[2] =  0;
      k[3] =  0; k[4] =  1; k[5] =  0;
      k[6] =  0; k[7] =  0; k[8] =  0;

      *scale = 1.;
      break;
   default:
      assert(!"Unhandled switch value");
   }
}

static void setupConvolution()
{
   GLint *kernel = (GLint*)malloc(sizeof(GLint) * 9);
   GLfloat scale;
   GLfloat *vecKer = (GLfloat*)malloc(sizeof(GLfloat) * 9 * 4);
   GLuint loc;
   GLuint i;
   GLfloat baseColor[4];
   baseColor[0] = 0;
   baseColor[1] = 0;
   baseColor[2] = 0;
   baseColor[3] = 0;

   fillConvolution(kernel, &scale, baseColor);
   /*vector of 4*/
   for (i = 0; i < 9; ++i) {
      vecKer[i*4 + 0] = kernel[i];
      vecKer[i*4 + 1] = kernel[i];
      vecKer[i*4 + 2] = kernel[i];
      vecKer[i*4 + 3] = kernel[i];
   }

   loc = glGetUniformLocationARB(program, "KernelValue");
   glUniform4fv(loc, 9, vecKer);
   loc = glGetUniformLocationARB(program, "ScaleFactor");
   glUniform4f(loc, scale, scale, scale, scale);
   loc = glGetUniformLocationARB(program, "BaseColor");
   glUniform4f(loc, baseColor[0], baseColor[1],
               baseColor[2], baseColor[3]);

   free(vecKer);
   free(kernel);
}

static void createProgram(const char *vertProgFile,
                          const char *fragProgFile)
{
   GLuint fragShader = 0, vertShader = 0;

   program = glCreateProgram();
   if (vertProgFile) {
      vertShader = glCreateShader(GL_VERTEX_SHADER);
      readShader(vertShader, vertProgFile);
      glAttachShader(program, vertShader);
   }

   if (fragProgFile) {
      fragShader = glCreateShader(GL_FRAGMENT_SHADER);
      readShader(fragShader, fragProgFile);
      glAttachShader(program, fragShader);
   }

   glLinkProgram(program);
   checkLink(program);

   glUseProgram(program);

   assert(glIsProgram(program));
   assert(glIsShader(fragShader));
   assert(glIsShader(vertShader));

   checkError(__LINE__);
   {/*texture*/
      GLuint texLoc = glGetUniformLocationARB(program, "srcTex");
      glUniform1iARB(texLoc, 0);
   }
   {/*setup offsets */
      float offsets[] = { 1.0 / texture.width,  1.0 / texture.height,
                          0.0                ,  1.0 / texture.height,
                          -1.0 / texture.width,  1.0 / texture.height,
                          1.0 / texture.width,  0.0,
                          0.0                ,  0.0,
                          -1.0 / texture.width,  0.0,
                          1.0 / texture.width, -1.0 / texture.height,
                          0.0                , -1.0 / texture.height,
                          -1.0 / texture.width, -1.0 / texture.height };
      GLuint offsetLoc = glGetUniformLocationARB(program, "Offset");
      glUniform2fv(offsetLoc, 9, offsets);
   }
   setupConvolution();

   checkError(__LINE__);
}


static void readTexture(const char *filename)
{
   GLubyte *data;

   texture.x = 0;
   texture.y = 0;

   glGenTextures(1, &texture.id);
   glBindTexture(GL_TEXTURE_2D, texture.id);
   glTexParameteri(GL_TEXTURE_2D,
                   GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D,
                   GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   data = LoadRGBImage(filename, &texture.width, &texture.height,
                       &texture.format);
   if (!data) {
      printf("Error: couldn't load texture image '%s'\n", filename);
      exit(1);
   }
   printf("Texture %s (%d x %d)\n",
          filename, texture.width, texture.height);
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
                texture.width, texture.height, 0, texture.format,
                GL_UNSIGNED_BYTE, data);
}

static void menuSelected(int entry)
{
   switch (entry) {
   case QUIT:
      exit(0);
      break;
   default:
      filter = (enum Filter)entry;
   }
   setupConvolution();

   glutPostRedisplay();
}

static void menuInit()
{
   menuId = glutCreateMenu(menuSelected);

   glutAddMenuEntry("Gaussian blur", GAUSSIAN_BLUR);
   glutAddMenuEntry("Sharpen", SHARPEN);
   glutAddMenuEntry("Mean removal", MEAN_REMOVAL);
   glutAddMenuEntry("Emboss", EMBOSS);
   glutAddMenuEntry("Edge detect", EDGE_DETECT);
   glutAddMenuEntry("None", NO_FILTER);

   glutAddMenuEntry("Quit", QUIT);

   glutAttachMenu(GLUT_RIGHT_BUTTON);
}

static void init()
{
   fprintf(stderr, "GL_RENDERER   = %s\n", (char *) glGetString(GL_RENDERER));
   fprintf(stderr, "GL_VERSION    = %s\n", (char *) glGetString(GL_VERSION));
   fprintf(stderr, "GL_VENDOR     = %s\n", (char *) glGetString(GL_VENDOR));

   menuInit();
   readTexture(textureLocation);
   createProgram("convolution.vert", "convolution.frag");

   glEnable(GL_TEXTURE_2D);
   glClearColor(1.0, 1.0, 1.0, 1.0);
   /*glShadeModel(GL_SMOOTH);*/
   glShadeModel(GL_FLAT);
}

static void reshape(int width, int height)
{
   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   box.minx = 0;
   box.maxx = width;
   box.miny = 0;
   box.maxy = height;
   box.minz = 0;
   box.maxz = 1;
   glOrtho(box.minx, box.maxx, box.miny, box.maxy, -999999, 999999);
   glMatrixMode(GL_MODELVIEW);
}

static void keyPress(unsigned char key, int x, int y)
{
   switch(key) {
   case 27:
      exit(0);
   default:
      return;
   }
   glutPostRedisplay();
}

static void
special(int k, int x, int y)
{
   switch (k) {
   case GLUT_KEY_UP:
      viewRotx += 2.0;
      break;
   case GLUT_KEY_DOWN:
      viewRotx -= 2.0;
      break;
   case GLUT_KEY_LEFT:
      viewRoty += 2.0;
      break;
   case GLUT_KEY_RIGHT:
      viewRoty -= 2.0;
      break;
   default:
      return;
   }
   glutPostRedisplay();
}


static void draw()
{
   GLfloat center[2];
   GLfloat anchor[2];

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   glLoadIdentity();
   glPushMatrix();

   center[0] = box.maxx/2;
   center[1] = box.maxy/2;
   anchor[0] = center[0] - texture.width/2;
   anchor[1] = center[1] - texture.height/2;

   glTranslatef(center[0], center[1], 0);
   glRotatef(viewRotx, 1.0, 0.0, 0.0);
   glRotatef(viewRoty, 0.0, 1.0, 0.0);
   glRotatef(viewRotz, 0.0, 0.0, 1.0);
   glTranslatef(-center[0], -center[1], 0);

   glTranslatef(anchor[0], anchor[1], 0);
   glBegin(GL_TRIANGLE_STRIP);
   {
      glColor3f(1., 0., 0.);
      glTexCoord2f(0, 0);
      glVertex3f(0, 0, 0);

      glColor3f(0., 1., 0.);
      glTexCoord2f(0, 1.0);
      glVertex3f(0, texture.height, 0);

      glColor3f(1., 0., 0.);
      glTexCoord2f(1.0, 0);
      glVertex3f(texture.width, 0, 0);

      glColor3f(0., 1., 0.);
      glTexCoord2f(1, 1);
      glVertex3f(texture.width, texture.height, 0);
   }
   glEnd();

   glPopMatrix();

   glFlush();

   glutSwapBuffers();
}

int main(int argc, char **argv)
{
   glutInit(&argc, argv);

   glutInitWindowPosition(0, 0);
   glutInitWindowSize(400, 400);
   glutInitDisplayMode(GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE);

   if (!glutCreateWindow("Image Convolutions")) {
      fprintf(stderr, "Couldn't create window!\n");
      exit(1);
   }

   init();

   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyPress);
   glutSpecialFunc(special);
   glutDisplayFunc(draw);


   glutMainLoop();
   return 0;
}