4
* Read an SGI .rgb image file and generate a mipmap texture set.
5
* Much of this code was borrowed from SGI's tk OpenGL toolkit.
24
** RGB Image Structure
27
typedef struct _TK_RGBImageRec {
35
/******************************************************************************/
37
typedef struct _rawImageRec {
38
unsigned short imagic;
41
unsigned short sizeX, sizeY, sizeZ;
42
unsigned long min, max;
43
unsigned long wasteBytes;
45
unsigned long colorMap;
47
unsigned char *tmp, *tmpR, *tmpG, *tmpB, *tmpA;
53
/******************************************************************************/
55
static void ConvertShort(unsigned short *array, long length)
60
ptr = (unsigned char *)array;
64
*array++ = (unsigned short) ((b1 << 8) | (b2));
68
static void ConvertLong(GLuint *array, long length)
70
unsigned long b1, b2, b3, b4;
73
ptr = (unsigned char *)array;
79
*array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
83
static rawImageRec *RawImageOpen(const char *fileName)
93
endianTest.testWord = 1;
94
if (endianTest.testByte[0] == 1) {
100
raw = (rawImageRec *)calloc(1, sizeof(rawImageRec));
102
fprintf(stderr, "Out of memory!\n");
105
if ((raw->file = fopen(fileName, "rb")) == NULL) {
110
fread(raw, 1, 12, raw->file);
113
ConvertShort(&raw->imagic, 6);
116
raw->tmp = (unsigned char *)malloc(raw->sizeX*256);
117
raw->tmpR = (unsigned char *)malloc(raw->sizeX*256);
118
raw->tmpG = (unsigned char *)malloc(raw->sizeX*256);
119
raw->tmpB = (unsigned char *)malloc(raw->sizeX*256);
121
raw->tmpA = (unsigned char *)malloc(raw->sizeX*256);
123
if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL ||
125
fprintf(stderr, "Out of memory!\n");
129
if ((raw->type & 0xFF00) == 0x0100) {
130
x = raw->sizeY * raw->sizeZ * sizeof(GLuint);
131
raw->rowStart = (GLuint *)malloc(x);
132
raw->rowSize = (GLint *)malloc(x);
133
if (raw->rowStart == NULL || raw->rowSize == NULL) {
134
fprintf(stderr, "Out of memory!\n");
137
raw->rleEnd = 512 + (2 * x);
138
fseek(raw->file, 512, SEEK_SET);
139
fread(raw->rowStart, 1, x, raw->file);
140
fread(raw->rowSize, 1, x, raw->file);
142
ConvertLong(raw->rowStart, (long) (x/sizeof(GLuint)));
143
ConvertLong((GLuint *)raw->rowSize, (long) (x/sizeof(GLint)));
149
static void RawImageClose(rawImageRec *raw)
166
static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
168
unsigned char *iPtr, *oPtr, pixel;
171
if ((raw->type & 0xFF00) == 0x0100) {
172
fseek(raw->file, (long) raw->rowStart[y+z*raw->sizeY], SEEK_SET);
173
fread(raw->tmp, 1, (unsigned int)raw->rowSize[y+z*raw->sizeY],
180
count = (int)(pixel & 0x7F);
197
fseek(raw->file, 512+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY),
199
fread(buf, 1, raw->sizeX, raw->file);
204
static void RawImageGetData(rawImageRec *raw, TK_RGBImageRec *final)
209
final->data = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4);
210
if (final->data == NULL) {
211
fprintf(stderr, "Out of memory!\n");
215
for (i = 0; i < (int)(raw->sizeY); i++) {
216
RawImageGetRow(raw, raw->tmpR, i, 0);
217
RawImageGetRow(raw, raw->tmpG, i, 1);
218
RawImageGetRow(raw, raw->tmpB, i, 2);
220
RawImageGetRow(raw, raw->tmpA, i, 3);
222
for (j = 0; j < (int)(raw->sizeX); j++) {
223
*ptr++ = *(raw->tmpR + j);
224
*ptr++ = *(raw->tmpG + j);
225
*ptr++ = *(raw->tmpB + j);
227
*ptr++ = *(raw->tmpA + j);
234
static TK_RGBImageRec *tkRGBImageLoad(const char *fileName)
237
TK_RGBImageRec *final;
239
raw = RawImageOpen(fileName);
241
fprintf(stderr, "File not found\n");
244
final = (TK_RGBImageRec *)malloc(sizeof(TK_RGBImageRec));
246
fprintf(stderr, "Out of memory!\n");
249
final->sizeX = raw->sizeX;
250
final->sizeY = raw->sizeY;
251
final->components = raw->sizeZ;
252
RawImageGetData(raw, final);
258
static void FreeImage( TK_RGBImageRec *image )
266
* Load an SGI .rgb file and generate a set of 2-D mipmaps from it.
267
* Input: imageFile - name of .rgb to read
268
* intFormat - internal texture format to use, or number of components
269
* Return: GL_TRUE if success, GL_FALSE if error.
271
GLboolean LoadRGBMipmaps( const char *imageFile, GLint intFormat )
274
return LoadRGBMipmaps2( imageFile, GL_TEXTURE_2D, intFormat, &w, &h );
279
GLboolean LoadRGBMipmaps2( const char *imageFile, GLenum target,
280
GLint intFormat, GLint *width, GLint *height )
284
TK_RGBImageRec *image;
286
image = tkRGBImageLoad( imageFile );
291
if (image->components==3) {
294
else if (image->components==4) {
298
/* not implemented */
300
"Error in LoadRGBMipmaps %d-component images not implemented\n",
305
error = gluBuild2DMipmaps( target,
307
image->sizeX, image->sizeY,
312
*width = image->sizeX;
313
*height = image->sizeY;
317
return error ? GL_FALSE : GL_TRUE;
323
* Load an SGI .rgb file and return a pointer to the image data.
324
* Input: imageFile - name of .rgb to read
325
* Output: width - width of image
326
* height - height of image
327
* format - format of image (GL_RGB or GL_RGBA)
328
* Return: pointer to image data or NULL if error
330
GLubyte *LoadRGBImage( const char *imageFile, GLint *width, GLint *height,
333
TK_RGBImageRec *image;
337
image = tkRGBImageLoad( imageFile );
342
if (image->components==3) {
345
else if (image->components==4) {
349
/* not implemented */
351
"Error in LoadRGBImage %d-component images not implemented\n",
356
*width = image->sizeX;
357
*height = image->sizeY;
359
bytes = image->sizeX * image->sizeY * image->components;
360
buffer = (GLubyte *) malloc(bytes);
364
memcpy( (void *) buffer, (void *) image->data, bytes );
371
#define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
374
static void ConvertRGBtoYUV(GLint w, GLint h, GLint texel_bytes,
380
for (i = 0; i < h; i++) {
381
for (j = 0; j < w; j++) {
382
const GLfloat r = (src[0]) / 255.0;
383
const GLfloat g = (src[1]) / 255.0;
384
const GLfloat b = (src[2]) / 255.0;
388
y = r * 65.481 + g * 128.553 + b * 24.966 + 16;
389
cb = r * -37.797 + g * -74.203 + b * 112.0 + 128;
390
cr = r * 112.0 + g * -93.786 + b * -18.214 + 128;
391
/*printf("%f %f %f -> %f %f %f\n", r, g, b, y, cb, cr);*/
392
iy = (GLint) CLAMP(y, 0, 254);
393
icb = (GLint) CLAMP(cb, 0, 254);
394
icr = (GLint) CLAMP(cr, 0, 254);
398
*dest = (iy << 8) | icr;
402
*dest = (iy << 8) | icb;
412
* Load an SGI .rgb file and return a pointer to the image data, converted
415
* Input: imageFile - name of .rgb to read
416
* Output: width - width of image
417
* height - height of image
418
* Return: pointer to image data or NULL if error
420
GLushort *LoadYUVImage( const char *imageFile, GLint *width, GLint *height )
422
TK_RGBImageRec *image;
425
image = tkRGBImageLoad( imageFile );
430
if (image->components != 3 && image->components !=4 ) {
431
/* not implemented */
433
"Error in LoadYUVImage %d-component images not implemented\n",
438
*width = image->sizeX;
439
*height = image->sizeY;
441
buffer = (GLushort *) malloc( image->sizeX * image->sizeY * 2 );
444
ConvertRGBtoYUV( image->sizeX,