gl_dev.c
5.28 KB
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
#include <stdio.h>
#include <string.h>
#ifdef __sgi__
#include <bstring.h>
#include <gl/gl.h>
#include <gl/device.h>
#endif
#include <PRimage.h>
#include "test_image.h"
/*
* save_cvg -- save 32b RGBA image, pseudo color version of coverage (alpha) channel
*
*
*/
void
save_cvg(char *name, Image *image)
{
IMAGE *img;
unsigned short *cb;
unsigned char *fb;
int x, xl, y, xsize, ysize;
xsize = image->xsize;
ysize = image->ysize;
img = iopen(name, "w", RLE(1), 3, xsize, ysize, 3);
if (img == (IMAGE *)0) {
fprintf(stderr, "error in creating image %s\n", name);
return;
}
cb = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
fb = image->base;
for (y = 0; y < ysize; y++, fb += image->lsize) {
for (xl = 0, x = 3; xl < xsize; x += 4, xl++) {
if(fb[x] & 0x80)
cb[xl] = 0xff;
else
cb[xl] = 0x00;
}
putrow(img, cb, y, 0);
for (xl = 0, x = 3; xl < xsize; x += 4, xl++) {
if(fb[x] == 0x47)
cb[xl] = 0xff;
else
cb[xl] = 0x00;
}
putrow(img, cb, y, 1);
for (xl = 0, x = 3; xl < xsize; x += 4, xl++) {
if((fb[x] & 7) < 0x7 && (fb[x] & 0x40) && !(fb[x] & 0x80))
cb[xl] = 0xff;
else
cb[xl] = 0x00;
}
putrow(img, cb, y, 2);
}
free(cb);
iclose(img);
}
/*
* save_image -- save 32b RGBA frame buffer in 24b .rgb format
*
*
*/
void
save_image(char *name, Image *image)
{
IMAGE *img;
unsigned short *cb;
unsigned char *fb;
int x, xl, y, xsize, ysize;
xsize = image->xsize;
ysize = image->ysize;
img = iopen(name, "w", RLE(1), 3, xsize, ysize, 3);
if (img == (IMAGE *)0) {
fprintf(stderr, "error in creating image %s\n", name);
return;
}
cb = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
fb = image->base;
for (y = 0; y < ysize; y++, fb += image->lsize) {
for (xl = 0, x = 0; xl < xsize; x += 4, xl++) cb[xl] = fb[x];
putrow(img, cb, ysize - y - 1, 0);
for (xl = 0, x = 1; xl < xsize; x += 4, xl++) cb[xl] = fb[x];
putrow(img, cb, ysize - y - 1, 1);
for (xl = 0, x = 2; xl < xsize; x += 4, xl++) cb[xl] = fb[x];
putrow(img, cb, ysize - y - 1, 2);
}
free(cb);
iclose(img);
}
/*
* dump_image_header
*
*/
void
dump_image_header(char *file)
{
IMAGE *img;
if ((img = iopen(file, "r")) == (IMAGE *)NULL) {
fprintf(stderr, "can't open texture file %s\n", file);
return;
}
printf("\n%s\n", file);
printf("imagic = 0x%04x\n", img->imagic);
printf("type = 0x%04x\n", img->type);
printf("dim = 0x%04x\n", img->dim);
printf("xsize = %d\n", img->xsize);
printf("ysize = %d\n", img->ysize);
printf("zsize = %d\n", img->zsize);
printf("min = %d\n", img->min);
printf("max = %d\n", img->max);
printf("waste = %d\n", img->wastebytes);
printf("name = %s\n", img->name);
printf("clrmap = 0x%08x\n", img->colormap);
printf("\n");
}
/*
* load_image -- recognizes input data in 24/32b RGB/A SGI format
* or y/u/v data in separate SGI format files. Imports these
* into either an RGBA image or UVYY image type with 8b per component.
* On first call make sure texture pointer is NULL.
*/
Image *
load_image(char *file, Image *texture)
{
IMAGE *img;
int x, xl, y;
unsigned short *cp;
unsigned char *sp;
if ((img = iopen(file, "r")) == (IMAGE *)NULL) {
fprintf(stderr, "can't open texture file %s\n", file);
exit(1);
}
if(texture == (Image *)NULL) {
texture = (Image *)malloc(sizeof(Image));
texture->xsize = img->xsize;
texture->ysize = img->ysize;
texture->type = 0;
texture->lsize = texture->xsize * 4;
texture->base = (unsigned char *)malloc(texture->ysize * texture->lsize);
bzero(texture->base, texture->lsize * texture->ysize);
}
cp = (unsigned short *)malloc(texture->xsize * sizeof(unsigned short));
sp = texture->base;
if((strstr(file, ".rgb")) != NULL)
{
/*
* read in 24b or 32b pixels
*/
for (y = 0; y < texture->ysize; y++, sp += texture->lsize) {
getrow(img, cp, y, 0);
for (xl = x = 0; xl < img->xsize; x += 4, xl++) sp[x] = cp[xl] & 0xff;
getrow(img, cp, y, 1);
for (xl = 0, x = 1; xl < img->xsize; x += 4, xl++) sp[x] = cp[xl] & 0xff;
getrow(img, cp, y, 2);
for (xl = 0, x = 2; xl < img->xsize; x += 4, xl++) sp[x] = cp[xl] & 0xff;
if (img->zsize > 3) {
getrow(img, cp, y, 3);
for (xl = 0, x = 3; xl < img->xsize; x += 4, xl++)
sp[x] = cp[xl] & 0xff;
}
else {
for (xl = 0, x = 3; xl < img->xsize; x += 4, xl++)
if (sp[x]) sp[x] = 0xff;
}
}
}
else if((strstr(file, ".y")) != NULL)
{
for (y = 0; y < texture->ysize; y++, sp += texture->lsize)
{
getrow(img, cp, y, 0);
for (xl = 0, x = 2; xl < img->xsize; x += 4, xl++)
{
sp[x] = cp[xl] & 0xff;
sp[x+1] = cp[xl] & 0xff;
}
}
}
else if((strstr(file,".u")) != NULL)
{
for (y = 0; y < texture->ysize; y++, sp += texture->lsize)
{
getrow(img, cp, y, 0);
for (xl = 0, x = 0; xl < img->xsize; x += 4, xl++)
sp[x] = cp[xl] & 0xff;
}
}
else if((strstr(file,".v")) != NULL)
{
for (y = 0; y < texture->ysize; y++, sp += texture->lsize)
{
getrow(img, cp, y, 0);
for (xl = 0, x = 1; xl < img->xsize; x += 4, xl++)
sp[x] = cp[xl] & 0xff;
}
}
else
{
fprintf(stderr,"Unrecognized file extension: %s\n", file);
exit(1);
}
free(cp);
iclose(img);
return(texture);
}