File: texture.C

package info (click to toggle)
boinc 5.4.11-4%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 21,440 kB
  • ctags: 16,986
  • sloc: cpp: 70,682; ansic: 45,747; php: 35,513; xml: 10,487; sh: 9,324; python: 4,291; makefile: 1,958; asm: 1,258; perl: 914; sql: 395; csh: 126; pascal: 124
file content (371 lines) | stat: -rw-r--r-- 9,408 bytes parent folder | download | duplicates (2)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "config.h"
#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

void bwtorgba(unsigned char *b,unsigned char *l,int n) {
    while(n--) {
		l[0] = *b;
		l[1] = *b;
		l[2] = *b;
		l[3] = 0xff;
		l += 4; 
		b++;
    }
}

void latorgba(unsigned char *b, unsigned char *a,unsigned char *l,int n) {
    while(n--) {
		l[0] = *b;
		l[1] = *b;
		l[2] = *b;
		l[3] = *a;
		l += 4; 
		b++; 
		a++;
	}
}

void rgbtorgba(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *l,int n) {
    while(n--) {
		l[0] = r[0];
		l[1] = g[0];
		l[2] = b[0];
		l[3] = 0xff;
		l += 4; 
		r++; 
		g++; 
		b++;
	}
}

void rgbatorgba(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *a,unsigned char *l,int n) {
    while(n--) {
		l[0] = r[0];
		l[1] = g[0];
		l[2] = b[0];
		l[3] = a[0];
		l += 4; 
		r++; 
		g++; 
		b++; 
		a++;
    }
}

typedef struct _ImageRec {
    unsigned short imagic;
    unsigned short type;
    unsigned short dim;
    unsigned short xsize, ysize, zsize;
    unsigned int min, max;
    unsigned int wasteBytes;
    char name[80];
    unsigned long colorMap;
    FILE *file;
    unsigned char *tmp, *tmpR, *tmpG, *tmpB;
    unsigned long rleEnd;
    unsigned int *rowStart;
    int *rowSize;
} ImageRec;

static void ConvertShort(unsigned short *array, long length) {
    unsigned b1, b2;
    unsigned char *ptr;
    ptr = (unsigned char *) array;
    while (length--) {
		b1 = *ptr++;
		b2 = *ptr++;
		*array++ = (b1 << 8) | (b2);
	}
}

static void ConvertLong(unsigned *array, long length) {
    unsigned long b1, b2, b3, b4;
    unsigned char *ptr;
    ptr = (unsigned char *)array;
    while (length--) {
		b1 = *ptr++;
		b2 = *ptr++;
		b3 = *ptr++;
		b4 = *ptr++;
		*array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
	}
}

static ImageRec *ImageOpen(const char *fileName){
    union {
		int testWord;
		char testByte[4];
	} 
	endianTest;
    ImageRec *image;
    int swapFlag;
    int x;
    endianTest.testWord = 1;
    if (endianTest.testByte[0] == 1) {
		swapFlag = 1;
    } 
	else {
		swapFlag = 0;
    }
    image = (ImageRec *)malloc(sizeof(ImageRec));
    if (image == NULL) {
		fprintf(stderr, "Out of memory!\n");
		return NULL;
    }
    if ((image->file = fopen(fileName, "rb")) == NULL) {
		perror(fileName);
		return NULL;
    }
    fread(image, 1, 12, image->file);
    if (swapFlag) {
		ConvertShort(&image->imagic, 6);
    }

    image->tmp  = (unsigned char *)malloc(image->xsize*256);
    image->tmpR = (unsigned char *)malloc(image->xsize*256);
    image->tmpG = (unsigned char *)malloc(image->xsize*256);
    image->tmpB = (unsigned char *)malloc(image->xsize*256);
    if (image->tmp == NULL || image->tmpR == NULL || image->tmpG == NULL ||image->tmpB == NULL) {
		fprintf(stderr, "Out of memory!\n");
		return NULL;
    }

    if ((image->type & 0xFF00) == 0x0100) {
		x = image->ysize * image->zsize * sizeof(unsigned);
		image->rowStart = (unsigned *)malloc(x);
		image->rowSize = (int *)malloc(x);
		if (image->rowStart == NULL || image->rowSize == NULL) {
			fprintf(stderr, "Out of memory!\n");
			return NULL;
		}
		image->rleEnd = 512 + (2 * x);
		fseek(image->file, 512, SEEK_SET);
		fread(image->rowStart, 1, x, image->file);
		fread(image->rowSize, 1, x, image->file);
		if (swapFlag) {
			ConvertLong(image->rowStart, x/sizeof(unsigned));
			ConvertLong((unsigned *)image->rowSize, x/sizeof(int));
		}
    }
    return image;
}

static void ImageClose(ImageRec *image) {
    fclose(image->file);
    free(image->tmp);
    free(image->tmpR);
    free(image->tmpG);
    free(image->tmpB);
    free(image);
}

static void ImageGetRow(ImageRec *image, unsigned char *buf, int y, int z) {
    unsigned char *iPtr, *oPtr, pixel;
    int count;
    if ((image->type & 0xFF00) == 0x0100) {
		fseek(image->file, image->rowStart[y+z*image->ysize], SEEK_SET);
		fread(image->tmp, 1, (unsigned int)image->rowSize[y+z*image->ysize],image->file);
		iPtr = image->tmp;
		oPtr = buf;
		while (1) {
			pixel = *iPtr++;
			count = (int)(pixel & 0x7F);
			if (!count) {
				return;
			}
			if (pixel & 0x80) {
				while (count--) {
					*oPtr++ = *iPtr++;
				}
			} 
			else {
				pixel = *iPtr++;
				while (count--) {
					*oPtr++ = pixel;
				}
			}
		}
    } 
	else {
		fseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),SEEK_SET);
		fread(buf, 1, image->xsize, image->file);
    }
}

//-------------------------TGA LOADER-------------------------

//	=============
//	checkSize
//	Make sure its a power of 2.
//	=============
int checkSize (int x){
	return 1;
	if (x == 2	 || x == 4   || 
        x == 8	 || x == 16  || 
        x == 32  || x == 64  ||
        x == 128 || x == 256 || x == 512)
		return 1;
    else 
		return 0;
}


unsigned int texFormat;

unsigned char* getRGBA (FILE *s, int size){
    unsigned char *rgba;
    unsigned char temp;
    int bread;
    int i;
    rgba=(unsigned char*)malloc(size * 4); 
    if (rgba == NULL) return 0;
    bread = fread (rgba, sizeof (unsigned char), size * 4, s); 
    // TGA is stored in BGRA, make it RGBA 
    if (bread != size * 4) {
        free (rgba);
        return 0;
    }
    for (i = 0; i < size * 4; i += 4 ){
        temp = rgba[i];
        rgba[i] = rgba[i + 2];
        rgba[i + 2] = temp;
    }
    return rgba;
}


//	=============
//	getRGB
//	Reads in RGB data for a 24bit image. 
//	=============
unsigned char* getRGB (FILE *s, int size){
    unsigned char *rgb;
    unsigned char temp;
    int bread;
    int i;
    rgb=(unsigned char*)malloc(size * 3); 
    if (rgb == NULL) return 0;
    bread = fread (rgb, sizeof (unsigned char), size * 3, s);
    if (bread != size * 3){
        free (rgb);
        return 0;
    }
    // TGA is stored in BGR, make it RGB 
    for (i = 0; i < size * 3; i += 3){
        temp = rgb[i];
        rgb[i] = rgb[i + 2];
        rgb[i + 2] = temp;
    }
    return rgb;
}


//	=============
//	getGray
//	Gets the grayscale image data.  Used as an alpha channel.
//	=============
unsigned char* getGray (FILE *s, int size){
    unsigned char *grayData;
    int bread;
    grayData=(unsigned char*)malloc (size);
    if (grayData == NULL) return 0;
    bread = fread (grayData, sizeof (unsigned char), size, s);
    if (bread != size){
        free (grayData);
        return 0;
    }
    return grayData;
}


//	=============
//	getData
//	Gets the image data for the specified bit depth.
//	=============
unsigned char* getData (FILE *s, int sz, int iBits){
	switch (iBits){
	case 32 : return getRGBA (s, sz); break;
	case 24 : return getRGB  (s, sz); break;
	case 8  : return getGray (s, sz); break;
	default : return NULL;
	}
}


unsigned * read_tga_texture(char *name, int *width, int *height, int*) {   
    unsigned char type[4];
    unsigned char info[7];    
    unsigned *base;
	int imageBits, size;
    FILE *s;
	if (!(s = fopen (name, "r+bt"))) return NULL;
	fread (&type, sizeof (char), 3, s);  // read in colormap info and image type, byte 0 ignored
    fseek (s, 12, SEEK_SET);			 // seek past the header and useless info
    fread (&info, sizeof (char), 6, s);
	if (type[1] != 0 || (type[2] != 2 && type[2] != 3)) return NULL;
	(*width)  = info[0] + info[1] * 256; 
    (*height) = info[2] + info[3] * 256;
    imageBits =	info[4]; 
    size = (*width) * (*height); 
	// make sure dimension is a power of 2 
    if (!checkSize ((*width)) || !checkSize ((*height) )) return NULL;
	// make sure we are loading a supported type 
    if (imageBits != 32 && imageBits != 24 && imageBits != 8) return NULL;
	base = (unsigned int *)getData (s, size, imageBits);
	return (unsigned *) base;
	fclose(s);
}

unsigned * read_rgb_texture(const char *name, int *width, int *height, int *components) {
    unsigned *base, *lptr;
    unsigned char *rbuf, *gbuf, *bbuf, *abuf;
    ImageRec *image;
    int y;
    image = ImageOpen(name);
    if(!image) return NULL;
    (*width)=image->xsize;
    (*height)=image->ysize;
    (*components)=image->zsize;
    base = (unsigned *)malloc(image->xsize*image->ysize*sizeof(unsigned));
    rbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
    gbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
    bbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
    abuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
    if(!base || !rbuf || !gbuf || !bbuf) return NULL;
    lptr = base;
	for(y=0; y<image->ysize; y++) {
		if(image->zsize>=4) {
			ImageGetRow(image,rbuf,y,0);
			ImageGetRow(image,gbuf,y,1);
			ImageGetRow(image,bbuf,y,2);
			ImageGetRow(image,abuf,y,3);
			rgbatorgba(rbuf,gbuf,bbuf,abuf,(unsigned char *)lptr,image->xsize);
			lptr += image->xsize;
		} else if(image->zsize==3) {
			ImageGetRow(image,rbuf,y,0);
			ImageGetRow(image,gbuf,y,1);
			ImageGetRow(image,bbuf,y,2);
			rgbtorgba(rbuf,gbuf,bbuf,(unsigned char *)lptr,image->xsize);
			lptr += image->xsize;
		} else if(image->zsize==2) {
			ImageGetRow(image,rbuf,y,0);
			ImageGetRow(image,abuf,y,1);
			latorgba(rbuf,abuf,(unsigned char *)lptr,image->xsize);
			lptr += image->xsize;
		} else {
			ImageGetRow(image,rbuf,y,0);
			bwtorgba(rbuf,(unsigned char *)lptr,image->xsize);
			lptr += image->xsize;
		}
    }
    ImageClose(image);
    free(rbuf);
    free(gbuf);
    free(bbuf);
    free(abuf);
    return (unsigned *) base;
}

const char *BOINC_RCSID_97d4f29d84="$Id: texture.C,v 1.6 2005/11/21 18:34:15 korpela Exp $";