2
* Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
4
* Permission to use, copy, modify, distribute, and sell this software and
5
* its documentation for any purpose is hereby granted without fee, provided
6
* that (i) the above copyright notices and this permission notice appear in
7
* all copies of the software and related documentation, and (ii) the name of
8
* Silicon Graphics may not be used in any advertising or
9
* publicity relating to the software without the specific, prior written
10
* permission of Silicon Graphics.
12
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
14
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
18
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
35
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
36
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
47
typedef struct _cRec {
51
typedef struct _vertexRec {
61
int imageSizeX, imageSizeY;
66
int cCount, cIndex[2], cStep;
70
static void DrawImage(void)
74
glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
83
glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
87
static void DrawPoint(void)
91
glColor3f(1.0, 0.0, 1.0);
94
for (i = 0; i < cCount; i++) {
95
glVertex2f(cList[i].x, cList[i].y);
105
static void InitVList(void)
115
vList[1].x = (float)imageSizeX;
122
vList[2].x = (float)imageSizeX;
123
vList[2].y = (float)imageSizeY;
130
vList[3].y = (float)imageSizeY;
136
vList[4].x = cList[0].x;
137
vList[4].y = cList[0].y;
138
vList[4].dX = (cList[1].x - cList[0].x) / STEPCOUNT;
139
vList[4].dY = (cList[1].y - cList[0].y) / STEPCOUNT;
140
vList[4].tX = cList[0].x / (float)imageSizeX;
141
vList[4].tY = cList[0].y / (float)imageSizeY;
144
static void ScaleImage(int sizeX, int sizeY)
148
buf = (GLubyte *)malloc(3*sizeX*sizeY);
149
gluScaleImage(GL_RGB, image->sizeX, image->sizeY, GL_UNSIGNED_BYTE,
150
image->data, sizeX, sizeY, GL_UNSIGNED_BYTE, buf);
153
image->sizeX = sizeX;
154
image->sizeY = sizeY;
157
static void SetPoint(int x, int y)
160
cList[cCount].x = (float)x;
161
cList[cCount].y = (float)y;
165
static void Stretch(void)
168
glBegin(GL_TRIANGLES);
169
glTexCoord2f(vList[0].tX, vList[0].tY);
170
glVertex2f(vList[0].x, vList[0].y);
171
glTexCoord2f(vList[1].tX, vList[1].tY);
172
glVertex2f(vList[1].x, vList[1].y);
173
glTexCoord2f(vList[4].tX, vList[4].tY);
174
glVertex2f(vList[4].x, vList[4].y);
177
glBegin(GL_TRIANGLES);
178
glTexCoord2f(vList[1].tX, vList[1].tY);
179
glVertex2f(vList[1].x, vList[1].y);
180
glTexCoord2f(vList[2].tX, vList[2].tY);
181
glVertex2f(vList[2].x, vList[2].y);
182
glTexCoord2f(vList[4].tX, vList[4].tY);
183
glVertex2f(vList[4].x, vList[4].y);
186
glBegin(GL_TRIANGLES);
187
glTexCoord2f(vList[2].tX, vList[2].tY);
188
glVertex2f(vList[2].x, vList[2].y);
189
glTexCoord2f(vList[3].tX, vList[3].tY);
190
glVertex2f(vList[3].x, vList[3].y);
191
glTexCoord2f(vList[4].tX, vList[4].tY);
192
glVertex2f(vList[4].x, vList[4].y);
195
glBegin(GL_TRIANGLES);
196
glTexCoord2f(vList[3].tX, vList[3].tY);
197
glVertex2f(vList[3].x, vList[3].y);
198
glTexCoord2f(vList[0].tX, vList[0].tY);
199
glVertex2f(vList[0].x, vList[0].y);
200
glTexCoord2f(vList[4].tX, vList[4].tY);
201
glVertex2f(vList[4].x, vList[4].y);
209
if (++cStep < STEPCOUNT) {
210
vList[4].x += vList[4].dX;
211
vList[4].y += vList[4].dY;
213
cIndex[0] = cIndex[1];
214
cIndex[1] = cIndex[1] + 1;
215
if (cIndex[1] == cCount) {
218
vList[4].dX = (cList[cIndex[1]].x - cList[cIndex[0]].x) / STEPCOUNT;
219
vList[4].dY = (cList[cIndex[1]].y - cList[cIndex[0]].y) / STEPCOUNT;
224
static void Key(unsigned char key, int x, int y)
237
glEnable(GL_TEXTURE_2D);
248
static void Mouse(int button, int state, int mouseX, int mouseY)
251
if (state != GLUT_DOWN)
254
if (op == OP_STRETCH) {
255
glDisable(GL_TEXTURE_2D);
259
SetPoint(mouseX, imageSizeY-mouseY);
266
static void Animate(void)
268
static double t0 = -1.;
270
t = glutGet(GLUT_ELAPSED_TIME) / 1000.;
295
static GLenum Args(int argc, char **argv)
299
doubleBuffer = GL_TRUE;
301
for (i = 1; i < argc; i++) {
302
if (strcmp(argv[i], "-sb") == 0) {
303
doubleBuffer = GL_FALSE;
304
} else if (strcmp(argv[i], "-db") == 0) {
305
doubleBuffer = GL_TRUE;
306
} else if (strcmp(argv[i], "-f") == 0) {
307
if (i+1 >= argc || argv[i+1][0] == '-') {
308
printf("-f (No file name).\n");
311
fileName = argv[++i];
314
printf("%s (Bad option).\n", argv[i]);
321
#if !defined(GLUTCALLBACK)
325
static void GLUTCALLBACK glut_post_redisplay_p(void)
330
int main(int argc, char **argv)
334
glutInit(&argc, argv);
336
if (Args(argc, argv) == GL_FALSE) {
341
printf("No image file.\n");
345
image = LoadPPM(fileName);
347
/* changed powf and logf to pow and log -Brian */
348
imageSizeX = (int)pow(2.0, (float)((int)(log(image->sizeX)/log(2.0))));
349
imageSizeY = (int)pow(2.0, (float)((int)(log(image->sizeY)/log(2.0))));
351
glutInitWindowPosition(0, 0); glutInitWindowSize( imageSizeX, imageSizeY);
354
type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
355
glutInitDisplayMode(type);
357
if (glutCreateWindow("Stretch") == GL_FALSE) {
361
glViewport(0, 0, imageSizeX, imageSizeY);
362
gluOrtho2D(0, imageSizeX, 0, imageSizeY);
363
glClearColor(0.0, 0.0, 0.0, 0.0);
365
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
366
glPixelStorei(GL_PACK_ALIGNMENT, 1);
368
ScaleImage(imageSizeX, imageSizeY);
370
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
371
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
372
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
373
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
374
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
375
glTexImage2D(GL_TEXTURE_2D, 0, 3, image->sizeX, image->sizeY, 0,
376
GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *)image->data);
384
glutKeyboardFunc(Key);
385
glutMouseFunc(Mouse);
386
glutDisplayFunc(Animate);
387
glutIdleFunc(glut_post_redisplay_p);