File: paint.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 (266 lines) | stat: -rw-r--r-- 5,730 bytes parent folder | download | duplicates (3)
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
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "texture.h"

/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#ifndef __sgi
#define trunc(x) ((double)((int)(x)))
#endif

#define RW 0.3086
#define GW 0.6094
#define BW 0.0820

static char defaultFile0[] = "data/mandrill256.rgb";
static char defaultFile1[] = "data/weave.bw";
static char defaultBrushFile[] = "data/brush3.rgb";
GLuint *img0, *img1, *brush;
GLsizei w0, w1, wbrush, h0, h1, hbrush;
GLsizei w, h;

GLint comp;

void init(void)
{
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}

GLuint *load_img(const char *fname, GLsizei *imgW, GLsizei *imgH)
{
  GLuint *img;

  img = read_texture(fname, imgW, imgH, &comp);
  if (!img) {
    fprintf(stderr, "Could not open %s\n", fname);
    exit(1);
  }

  return img;
}

GLuint *
resize_img(GLuint *img, GLsizei curW, GLsizei curH)
{
  /* save & set buffer settings */
  glPushAttrib(GL_COLOR_BUFFER_BIT | GL_PIXEL_MODE_BIT);
  glDrawBuffer(GL_BACK);
  glReadBuffer(GL_BACK);

  glPixelZoom((float)w / (float)curW, (float)h / (float)curH);
  glRasterPos2i(0, 0);
  glDrawPixels(curW, curH, GL_RGBA, GL_UNSIGNED_BYTE, img);
  free(img);
  img = (GLuint *)malloc(w * h * sizeof(GLuint));
  if (!img) {
    fprintf(stderr, "Malloc of %d bytes failed.\n", 
	    curW * curH * sizeof(GLuint));
    exit(1);
  }
  glPixelZoom(1, 1);
  glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, img);

  glPopAttrib();

  return img;
}

GLuint *
convert_to_luminance(GLuint *img, GLsizei w, GLsizei h)
{
  GLubyte *newImg, *src, *dst;
  GLfloat val;
  int i;

  newImg = (GLubyte *)malloc(w * h);
  if (!newImg) {
    fprintf(stderr, "malloc of %d bytes failed\n", w*h);
    exit(1);
  }

  src = (GLubyte *)img;
  dst = newImg;
  for (i = 0; i < w*h; i++) {
    val = ((float)(*src++) * RW + 
	   (float)(*src++) * GW +
	   (float)(*src++) * BW);
    src++;
    if (val > 255) val = 255;
    *dst++ = val;
  }
  free(img);

  /* casting a ubyte ptr to a uint pointer is sloppy since it can
   * lead to alignment errors, but since the pointer came from
   * malloc we know it's legal in this case... */
  return (GLuint *)newImg;
}

void reshape(GLsizei winW, GLsizei winH) 
{
    glViewport(0, 0, w, h);
    glLoadIdentity();
    glOrtho(0, winW, 0, winH, 0, 5);
}

void draw(void)
{
  static int first = 1;
  GLenum err;

  if (first) {
    printf("Scaling images to %d by %d\n", w, h);

    if (w0 != w || h0 != h) {
      img0 = resize_img(img0, w0, h0);

    }
    if (w1 != w || h1 != h) {
      img1 = resize_img(img1, w1, h1);
    }

    first = 0;
  }
  
  glClear(GL_COLOR_BUFFER_BIT);
  glRasterPos2i(0, 0);
  glDrawBuffer(GL_BACK);
  glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, img1);
  glDrawBuffer(GL_FRONT);
  glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, img0);
  
  err = glGetError();
  if (err != GL_NO_ERROR) printf("Error:  %s\n", gluErrorString(err));
}

int lastX, lastY, curX, curY;

int get_msecs(void)
{
  return glutGet(GLUT_ELAPSED_TIME) / 1000.0;
}

void idle(void)
{
  int x = curX - (wbrush/2);
  int y = h - (curY + (hbrush/2));
  int msecs;
  static int last_msecs = -1;

  /* do not do this more than 60 times a second.  Otherwise it's
   * to fast for use on high-end systems */
  msecs = get_msecs();
  if (fabs(last_msecs - msecs) < 1000./60.) {
    return;
  }
  last_msecs = msecs;

  /* we draw the brush using a drawpixels command.  on systems with
   * hardware-accelerated texture mapping it would be better to use
   * that. 
   * 
   * we use the bitmap hack to set the rasterpos because we don't
   * know that the position will be within the window. 
   */
  glRasterPos2i(0, 0);
  glBitmap(0, 0, 0, 0, x, y, 0);
  glColorMask(0, 0, 0, 1);
  glDrawBuffer(GL_BACK);
  glDrawPixels(wbrush, hbrush, GL_ALPHA, GL_UNSIGNED_BYTE, brush);
  glColorMask(1, 1, 1, 1);
  
  glReadBuffer(GL_BACK);
  glDrawBuffer(GL_FRONT);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glEnable(GL_BLEND);
  glCopyPixels(x, y, wbrush, hbrush, GL_COLOR);
  glDisable(GL_BLEND);
  
  glColorMask(1, 1, 1, 1);
}

void motion(int xpos, int ypos)
{
  curX = xpos;
  curY = ypos;
}

/* ARGSUSED */
void button(int button, int state, int xpos, int ypos)
{
  if (state == GLUT_DOWN) {
    glutIdleFunc(idle); 
    lastX = lastY = -1;
    curX = xpos;
    curY = ypos;
    return;
  } else {
    glutIdleFunc(0);
  }
}

/* ARGSUSED1 */
void key(unsigned char key, int x, int y)
{
  if (key == 27) exit(0);
}

void 
show_usage(void)
{
  fprintf(stderr, "Usage:\n");
  fprintf(stderr, "paint [imagefile0] [imagefile1] [brush]\n");
}

main(int argc, char *argv[])
{
  const char *fileName0 = defaultFile0, *fileName1 = defaultFile1,
  *brushName = defaultBrushFile;
  
  glutInit(&argc, argv);
  if (argc > 1) {
    fileName0 = argv[1];
  } 
  if (argc > 2) {
    fileName1 = argv[2];
  } 
  if (argc > 3) {
    brushName = argv[3];
  } 
  if (argc > 4) {
    show_usage();
    exit(1);
  }
  printf("Image file 1 is %s\n", fileName0);
  printf("Image file 2 is %s\n", fileName1);
  printf("Brush file is %s\n", brushName);

  img0 = load_img(fileName0, &w0, &h0);
  img1 = load_img(fileName1, &w1, &h1);
  brush = load_img(brushName, &wbrush, &hbrush);
  brush = convert_to_luminance(brush, wbrush, hbrush);

#define MAX(a, b) ((a) > (b) ? (a) : (b))
  w = MAX(w0, w1);
  h = MAX(h0, h1);

  glutInitWindowSize(w, h);
  glutInitWindowPosition(0, 0);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA);
  glutCreateWindow(argv[0]);
  glutDisplayFunc(draw);
  glutKeyboardFunc(key);
  glutReshapeFunc(reshape);
  glutMouseFunc(button);
  glutMotionFunc(motion);
  init();

  glutMainLoop();
  return 0;
}