2 /* texture.c - by David Blythe, SGI */
4 /* texload is a simplistic routine for reading an SGI .rgb image file. */
12 typedef struct _ImageRec
{
13 unsigned short imagic
;
16 unsigned short xsize
, ysize
, zsize
;
17 unsigned int min
, max
;
18 unsigned int wasteBytes
;
20 unsigned long colorMap
;
24 unsigned int *rowStart
;
29 rgbtorgb(unsigned char *r
,unsigned char *g
,unsigned char *b
,unsigned char *l
,int n
) {
34 l
+= 3; r
++; g
++; b
++;
39 ConvertShort(unsigned short *array
, unsigned int length
) {
40 unsigned short b1
, b2
;
43 ptr
= (unsigned char *)array
;
47 *array
++ = (b1
<< 8) | (b2
);
52 ConvertUint(unsigned *array
, unsigned int length
) {
53 unsigned int b1
, b2
, b3
, b4
;
56 ptr
= (unsigned char *)array
;
62 *array
++ = (b1
<< 24) | (b2
<< 16) | (b3
<< 8) | (b4
);
66 static ImageRec
*ImageOpen(char *fileName
)
76 endianTest
.testWord
= 1;
77 if (endianTest
.testByte
[0] == 1) {
83 image
= (ImageRec
*)malloc(sizeof(ImageRec
));
85 fprintf(stderr
, "Out of memory!\n");
88 if ((image
->file
= fopen(fileName
, "rb")) == NULL
) {
92 fread(image
, 1, 12, image
->file
);
95 ConvertShort(&image
->imagic
, 6);
98 image
->tmp
= (unsigned char *)malloc(image
->xsize
*256);
99 if (image
->tmp
== NULL
) {
100 fprintf(stderr
, "\nOut of memory!\n");
104 if ((image
->type
& 0xFF00) == 0x0100) {
105 x
= image
->ysize
* image
->zsize
* (int) sizeof(unsigned);
106 image
->rowStart
= (unsigned *)malloc(x
);
107 image
->rowSize
= (int *)malloc(x
);
108 if (image
->rowStart
== NULL
|| image
->rowSize
== NULL
) {
109 fprintf(stderr
, "\nOut of memory!\n");
112 image
->rleEnd
= 512 + (2 * x
);
113 fseek(image
->file
, 512, SEEK_SET
);
114 fread(image
->rowStart
, 1, x
, image
->file
);
115 fread(image
->rowSize
, 1, x
, image
->file
);
117 ConvertUint(image
->rowStart
, x
/(int) sizeof(unsigned));
118 ConvertUint((unsigned *)image
->rowSize
, x
/(int) sizeof(int));
125 ImageClose(ImageRec
*image
) {
132 ImageGetRow(ImageRec
*image
, unsigned char *buf
, int y
, int z
) {
133 unsigned char *iPtr
, *oPtr
, pixel
;
136 if ((image
->type
& 0xFF00) == 0x0100) {
137 fseek(image
->file
, (long) image
->rowStart
[y
+z
*image
->ysize
], SEEK_SET
);
138 fread(image
->tmp
, 1, (unsigned int)image
->rowSize
[y
+z
*image
->ysize
],
145 count
= (int)(pixel
& 0x7F);
161 fseek(image
->file
, 512+(y
*image
->xsize
)+(z
*image
->xsize
*image
->ysize
),
163 fread(buf
, 1, image
->xsize
, image
->file
);
168 read_alpha_texture(char *name
, int *width
, int *height
)
170 unsigned char *base
, *lptr
;
174 image
= ImageOpen(name
);
179 (*width
)=image
->xsize
;
180 (*height
)=image
->ysize
;
181 if (image
->zsize
!= 1) {
186 base
= (unsigned char *)malloc(image
->xsize
*image
->ysize
*sizeof(unsigned char));
188 for(y
=0; y
<image
->ysize
; y
++) {
189 ImageGetRow(image
,lptr
,y
,0);
190 lptr
+= image
->xsize
;
194 return (unsigned char *) base
;
198 read_rgb_texture(char *name
, int *width
, int *height
)
200 unsigned char *base
, *ptr
;
201 unsigned char *rbuf
, *gbuf
, *bbuf
, *abuf
;
205 image
= ImageOpen(name
);
209 (*width
)=image
->xsize
;
210 (*height
)=image
->ysize
;
211 if (image
->zsize
!= 3 && image
->zsize
!= 4) {
216 base
= (unsigned char*)malloc(image
->xsize
*image
->ysize
*sizeof(unsigned int)*3);
217 rbuf
= (unsigned char *)malloc(image
->xsize
*sizeof(unsigned char));
218 gbuf
= (unsigned char *)malloc(image
->xsize
*sizeof(unsigned char));
219 bbuf
= (unsigned char *)malloc(image
->xsize
*sizeof(unsigned char));
220 abuf
= (unsigned char *)malloc(image
->xsize
*sizeof(unsigned char));
221 if(!base
|| !rbuf
|| !gbuf
|| !bbuf
|| !abuf
) {
222 if (base
) free(base
);
223 if (rbuf
) free(rbuf
);
224 if (gbuf
) free(gbuf
);
225 if (bbuf
) free(bbuf
);
226 if (abuf
) free(abuf
);
230 for(y
=0; y
<image
->ysize
; y
++) {
231 if(image
->zsize
== 4) {
232 ImageGetRow(image
,rbuf
,y
,0);
233 ImageGetRow(image
,gbuf
,y
,1);
234 ImageGetRow(image
,bbuf
,y
,2);
235 ImageGetRow(image
,abuf
,y
,3); /* Discard. */
236 rgbtorgb(rbuf
,gbuf
,bbuf
,ptr
,image
->xsize
);
237 ptr
+= (image
->xsize
* 3);
239 ImageGetRow(image
,rbuf
,y
,0);
240 ImageGetRow(image
,gbuf
,y
,1);
241 ImageGetRow(image
,bbuf
,y
,2);
242 rgbtorgb(rbuf
,gbuf
,bbuf
,ptr
,image
->xsize
);
243 ptr
+= (image
->xsize
* 3);
252 return (GLubyte
*) base
;
255 int main(int argc
, char **argv
)
265 fprintf(stderr
, "usage: %s <infile.rgb> <outfile.p6>\n", argv
[0]);
269 data
= read_rgb_texture(argv
[1], &width
, &height
);
271 n
= sprintf(buff
, "P6\n%d %d\n255\n", width
, height
);
273 /* [dBorca] avoid LF to CRLF conversion */
274 if ((fo
= fopen(argv
[2], "wb")) == NULL
) {
275 fprintf(stderr
, "Cannot open output file!\n");
279 fwrite(buff
, n
, 1, fo
);
280 fwrite(data
, width
* 3, height
, fo
);