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
|
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "texture.h"
static char defaultFile[] = "data/mandrill256.rgb";
GLuint *img;
GLsizei w, h;
GLint comp;
GLfloat kernScale;
/* a few filters... */
GLfloat horizontalEdge3x3[] = {
-1, -1, -1,
2, 2, 2,
-1, -1, -1,
};
GLfloat verticalEdge3x3[] = {
-1, 2, -1,
-1, 2, -1,
-1, 2, -1,
};
GLfloat allEdge3x3[] = {
-2, 1, -2,
1, 4, 1,
-2, 1, -2,
};
GLfloat smooth3x3[] = {
1, 2, 1,
2, 4, 2,
1, 2, 1,
};
GLfloat highpass3x3[] = {
-1, -1, -1,
-1, 9, -1,
-1, -1, -1,
};
GLfloat laplacian5x5[] = {
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 24, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
};
GLfloat box3x3[] = {
1, 1, 1,
1, 1, 1,
1, 1, 1,
};
GLfloat box5x5[] = {
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
};
struct Kernel {
const char *name;
GLfloat *kern;
GLsizei w, h;
} filters[] = {
{"horizontal edge detect", horizontalEdge3x3, 3, 3},
{"vertical edge detect", verticalEdge3x3, 3, 3},
{"all edge detect", allEdge3x3, 3, 3},
{"3x3 smooth", smooth3x3, 3, 3},
{"3x3 highpass", highpass3x3, 3, 3},
{"5x5 laplacian", laplacian5x5, 5, 5},
{"3x3 box", box3x3, 3, 3},
{"5x5 box", box5x5, 5, 5},
};
struct Kernel *kern;
void kern_normalize(void)
{
GLfloat total, scale, *k;
int i;
k = kern->kern;
total = 0;
for (i = 0; i < kern->w*kern->h; i++) {
total += *k++;
}
if (!total) {
/* kernel sums to 0... */
return;
} else {
scale = 1. / total;
k = kern->kern;
for (i = 0; i < kern->w * kern->h; i++) {
*k *= scale;
k++;
}
}
}
GLfloat acc_kern_scale(void)
{
GLfloat minPossible = 0, maxPossible = 1;
GLfloat *k;
int i;
k = kern->kern;
for (i = 0; i < kern->w*kern->h; i++) {
if (*k < 0) {
minPossible += *k;
} else {
maxPossible += *k;
}
k++;
}
return(1. / ((-minPossible > maxPossible) ? -minPossible : maxPossible));
}
void init(void)
{
kern = &filters[4];
kern_normalize();
kernScale = acc_kern_scale();
}
void load_img(const char *fname)
{
img = read_texture(fname, &w, &h, &comp);
if (!img) {
fprintf(stderr, "Could not open %s\n", fname);
exit(1);
}
}
void reshape(GLsizei winW, GLsizei winH)
{
glViewport(0, 0, w, h);
glLoadIdentity();
glOrtho(0, winW, 0, winH, 0, 5);
}
void acc_convolve(void)
{
int x, y;
for (y = 0; y < kern->h; y++) {
for (x = 0; x < kern->w; x++) {
glRasterPos2i(0, 0);
glBitmap(0, 0, 0, 0, -x, -y, 0);
glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, img);
glAccum(GL_ACCUM, kern->kern[y*kern->w + x]*kernScale);
}
}
glAccum(GL_RETURN, 1./kernScale);
}
void draw(void)
{
GLenum err;
glutSetCursor(GLUT_CURSOR_WAIT);
glClear(GL_COLOR_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);
acc_convolve();
err = glGetError();
if (err != GL_NO_ERROR) printf("Error: %s\n", gluErrorString(err));
glutSetCursor(GLUT_CURSOR_INHERIT);
}
/* ARGSUSED1 */
void key(unsigned char key, int x, int y)
{
if (key == 27) exit(0);
}
void menu(int val)
{
kern = &filters[val];
kern_normalize();
kernScale= acc_kern_scale();
draw();
}
main(int argc, char *argv[])
{
int i;
glutInit(&argc, argv);
if (argc > 1) {
load_img(argv[1]);
} else {
load_img(defaultFile);
}
glutInitWindowSize(w, h);
glutInitWindowPosition(0, 0);
glutInitDisplayMode(GLUT_RGB | GLUT_ACCUM);
glutCreateWindow(argv[0]);
glutDisplayFunc(draw);
glutKeyboardFunc(key);
glutReshapeFunc(reshape);
glutCreateMenu(menu);
for (i = 0; i < sizeof(filters) / sizeof(filters[0]); i++) {
glutAddMenuEntry(filters[i].name, i);
}
glutAttachMenu(GLUT_RIGHT_BUTTON);
init();
glutMainLoop();
return 0;
}
|