File: imgproc.c

package info (click to toggle)
glut 3.7-14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,556 kB
  • ctags: 45,170
  • sloc: ansic: 148,716; makefile: 35,208; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (288 lines) | stat: -rw-r--r-- 5,988 bytes parent folder | download | duplicates (5)
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

/* imgproc.c - by David Blythe, SGI */

/* Examples of various image processing operations coded as OpenGL
   accumulation buffer operations.  This allows extremely fast   
   image processing on machines with hardware accumulation buffers
   (RealityEngine, InfiniteReality, VGX). */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <GL/glut.h>
#include "texture.h"

static unsigned *image, *null;
static int width, height, components;
static void (*func) (void);
static float alpha = 1.;
static float luma = .5;
static int reset = 1;
static int format = GL_RGBA;

void
brighten(void)
{
  if (reset) {
    memset(null, 0, width * height * sizeof *null);
    reset = 0;
  }
  glDrawPixels(width, height, format, GL_UNSIGNED_BYTE, image);
  glAccum(GL_LOAD, alpha / 2.);
  glDrawPixels(width, height, format, GL_UNSIGNED_BYTE, null);
  glAccum(GL_ACCUM, (1 - alpha) / 2.);
  glAccum(GL_RETURN, 2.0);
}

void
saturate(void)
{
  if (reset) {
    memset(null, 0xff, width * height * sizeof *null);
    reset = 0;
  }
  glDrawPixels(width, height, format, GL_UNSIGNED_BYTE, image);
  glAccum(GL_LOAD, alpha / 2.);
  glDrawPixels(width, height, format, GL_UNSIGNED_BYTE, null);
  glAccum(GL_ACCUM, (1 - alpha) / 2.);
  glAccum(GL_RETURN, 2.0);
}

void
contrast(void)
{
  if (reset) {
    memset(null, luma, width * height * sizeof *null);
    reset = 0;
  }
  glDrawPixels(width, height, format, GL_UNSIGNED_BYTE, image);
  glAccum(GL_LOAD, alpha / 2.);
  glDrawPixels(width, height, format, GL_UNSIGNED_BYTE, null);
  glAccum(GL_ACCUM, (1 - alpha) / 2.);
  glAccum(GL_RETURN, 2.0);
}

void
balance(void)
{
  if (reset) {
    memset(null, luma, width * height * sizeof *null);
    reset = 0;
  }
  glDrawPixels(width, height, format, GL_UNSIGNED_BYTE, image);
  glAccum(GL_LOAD, alpha / 2.);
  glDrawPixels(width, height, format, GL_UNSIGNED_BYTE, null);
  glAccum(GL_ACCUM, (1 - alpha) / 2.);
  glAccum(GL_RETURN, 2.0);
}

void
sharpen(void)
{
  if (reset) {
    gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
      width / 4, height / 4, GL_UNSIGNED_BYTE, null);
    gluScaleImage(format, width / 4, height / 4, GL_UNSIGNED_BYTE, null,
      width, height, GL_UNSIGNED_BYTE, null);
    reset = 0;
  }
  glDrawPixels(width, height, format, GL_UNSIGNED_BYTE, image);
  glAccum(GL_LOAD, alpha / 2.);
  glDrawPixels(width, height, format, GL_UNSIGNED_BYTE, null);
  glAccum(GL_ACCUM, (1 - alpha) / 2.);
  glAccum(GL_RETURN, 2.0);
}

void 
set_brighten(void)
{
  func = brighten;
  reset = 1;
  printf("brighten\n");
}

void 
set_saturate(void)
{
  func = saturate;
  reset = 1;
  printf("saturate\n");
}

void 
set_contrast(void)
{
  func = contrast;
  reset = 1;
  printf("contrast\n");
}

void 
set_balance(void)
{
  func = balance;
  reset = 1;
  printf("balance\n");
}

void 
set_sharpen(void)
{
  func = sharpen;
  reset = 1;
  printf("sharpen\n");
}

void
help(void)
{
  printf("'h'   - help\n");
  printf("'b'   - brighten\n");
  printf("'s'   - saturate\n");
  printf("'c'   - contrast\n");
  printf("'z'   - sharpen\n");
  printf("'a'   - color balance\n");
  printf("left mouse     - increase alpha\n");
  printf("middle mouse   - decrease alpha\n");
}

void
init(char *filename)
{
  double l = 0;
  int i;

  func = brighten;

  if (filename) {
    image = read_texture(filename, &width, &height, &components);
    if (image == NULL) {
      fprintf(stderr, "Error: Can't load image file \"%s\".\n",
        filename);
      exit(1);
    } else {
      printf("%d x %d image loaded\n", width, height);
    }
    if (components < 3 || components > 4) {
      printf("must be RGB or RGBA image\n");
      exit(1);
    }
  } else {
    int i, j;
    components = 4;
    width = height = 512;
    image = (unsigned *) malloc(width * height * sizeof(unsigned));
    for (j = 0; j < height; j++)
      for (i = 0; i < width; i++) {
        if (i & 1)
          image[i + j * width] = 0xff;
        else
          image[i + j * width] = 0xff00;
        if (j & 1)
          image[i + j * width] |= 0xff0000;
      }

  }
  null = (unsigned *) malloc(width * height * sizeof *image);

  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glClearColor(.25, .25, .25, .25);

  /* compute luminance */
  for (i = 0; i < width * height; i++) {
    GLubyte *p = (GLubyte *) (image + i);
    double r = p[0] / 255.;
    double g = p[1] / 255.;
    double b = p[2] / 255.;
    l += r * .3086 + g * .0820 + b * .114;
  }
  luma = l / (width * height);
  printf("average luminance = %f\n", luma);
}

void
display(void)
{
  printf("alpha = %f\n", alpha);
  glClear(GL_COLOR_BUFFER_BIT);
  (*func) ();
  glutSwapBuffers();
}

void
reshape(int w, int h)
{
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0., (GLdouble) width, 0., (GLdouble) height, -1., 1.);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

/* ARGSUSED1 */
void
key(unsigned char key, int x, int y)
{
  switch (key) {
  case 'b':
    set_brighten();
    break;
  case 's':
    set_saturate();
    break;
  case 'c':
    set_contrast();
    break;
  case 'z':
    set_sharpen();
    break;
  case 'a':
    set_balance();
    break;
  case 'h':
    help();
    break;
  case '\033':
    exit(0);
    break;
  default:
    return;
  }
  glutPostRedisplay();
}

/* ARGSUSED2 */
void
mouse(int button, int state, int x, int y)
{
  if (state == GLUT_DOWN) {
    switch (button) {
    case GLUT_LEFT_BUTTON:
      alpha += .1;
      break;
    case GLUT_MIDDLE_BUTTON:
      alpha -= .1;
      break;
    case GLUT_RIGHT_BUTTON:
      break;
    }
  }
  glutPostRedisplay();
}

int
main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitWindowSize(512, 512);
  glutInitDisplayMode(GLUT_RGBA | GLUT_ACCUM | GLUT_DOUBLE);
  (void) glutCreateWindow("imgproc");
  init(argv[1]);
  glutDisplayFunc(display);
  glutKeyboardFunc(key);
  glutReshapeFunc(reshape);
  glutMouseFunc(mouse);
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}