2 /* texture.c - by David Blythe, SGI */
4 /* texload is a simplistic routine for reading an SGI .rgb image file. */
18 #include <Include/fg_zlib.h>
22 typedef struct _ImageRec {
23 unsigned short imagic;
26 unsigned short xsize, ysize, zsize;
27 unsigned int min, max;
28 unsigned int wasteBytes;
30 unsigned long colorMap;
34 unsigned int *rowStart;
39 rgbtorgb(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *l,int n) {
44 l += 3; r++; g++; b++;
49 ConvertShort(unsigned short *array, unsigned int length) {
50 unsigned short b1, b2;
53 ptr = (unsigned char *)array;
57 *array++ = (b1 << 8) | (b2);
62 ConvertUint(unsigned *array, unsigned int length) {
63 unsigned int b1, b2, b3, b4;
66 ptr = (unsigned char *)array;
72 *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
76 static ImageRec *ImageOpen(const char *fileName)
87 endianTest.testWord = 1;
88 if (endianTest.testByte[0] == 1) {
94 image = (ImageRec *)malloc(sizeof(ImageRec));
96 fprintf(stderr, "Out of memory!\n");
99 if ((image->file = fgopen(fileName, "rb")) == NULL) {
103 // fread(image, 1, 12, image->file);
104 fgread(image->file, image, 12);
107 ConvertShort(&image->imagic, 6);
110 image->tmp = (unsigned char *)malloc(image->xsize*256);
111 if (image->tmp == NULL) {
112 fprintf(stderr, "\nOut of memory!\n");
116 if ((image->type & 0xFF00) == 0x0100) {
117 x = image->ysize * image->zsize * (int) sizeof(unsigned);
118 image->rowStart = (unsigned *)malloc(x);
119 image->rowSize = (int *)malloc(x);
120 if (image->rowStart == NULL || image->rowSize == NULL) {
121 fprintf(stderr, "\nOut of memory!\n");
124 image->rleEnd = 512 + (2 * x);
125 fgseek(image->file, 512, SEEK_SET);
126 // fread(image->rowStart, 1, x, image->file);
127 fgread(image->file, image->rowStart, x);
128 // fread(image->rowSize, 1, x, image->file);
129 fgread(image->file, image->rowSize, x);
131 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
132 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
139 ImageClose(ImageRec *image) {
140 fgclose(image->file);
146 ImageGetRow(ImageRec *image, unsigned char *buf, int y, int z) {
147 unsigned char *iPtr, *oPtr, pixel;
150 if ((image->type & 0xFF00) == 0x0100) {
151 fgseek(image->file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
152 // fread(image->tmp, 1, (unsigned int)image->rowSize[y+z*image->ysize],
154 fgread(image->file, image->tmp,
155 (unsigned int)image->rowSize[y+z*image->ysize]);
161 count = (int)(pixel & 0x7F);
177 fgseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
179 // fread(buf, 1, image->xsize, image->file);
180 fgread(image->file, buf, image->xsize);
185 read_alpha_texture(const char *name, int *width, int *height)
187 unsigned char *base, *lptr;
191 image = ImageOpen(name);
196 (*width)=image->xsize;
197 (*height)=image->ysize;
199 printf("image->zsize = %d\n", image->zsize);
201 if (image->zsize != 1) {
206 base = (unsigned char *)malloc(image->xsize*image->ysize*sizeof(unsigned char));
208 for(y=0; y<image->ysize; y++) {
209 ImageGetRow(image,lptr,y,0);
210 lptr += image->xsize;
214 return (unsigned char *) base;
218 read_rgb_texture(const char *name, int *width, int *height)
220 unsigned char *base, *ptr;
221 unsigned char *rbuf, *gbuf, *bbuf, *abuf;
225 image = ImageOpen(name);
229 (*width)=image->xsize;
230 (*height)=image->ysize;
231 if (image->zsize != 3 && image->zsize != 4) {
236 base = (unsigned char*)malloc(image->xsize*image->ysize*sizeof(unsigned int)*3);
237 rbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
238 gbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
239 bbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
240 abuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
241 if(!base || !rbuf || !gbuf || !bbuf || !abuf) {
242 if (base) free(base);
243 if (rbuf) free(rbuf);
244 if (gbuf) free(gbuf);
245 if (bbuf) free(bbuf);
246 if (abuf) free(abuf);
250 for(y=0; y<image->ysize; y++) {
251 if(image->zsize == 4) {
252 ImageGetRow(image,rbuf,y,0);
253 ImageGetRow(image,gbuf,y,1);
254 ImageGetRow(image,bbuf,y,2);
255 ImageGetRow(image,abuf,y,3); /* Discard. */
256 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
257 ptr += (image->xsize * 3);
259 ImageGetRow(image,rbuf,y,0);
260 ImageGetRow(image,gbuf,y,1);
261 ImageGetRow(image,bbuf,y,2);
262 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
263 ptr += (image->xsize * 3);
272 return (GLubyte *) base;