Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 1 | /* readtex.c */ |
| 2 | |
| 3 | /* |
| 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. |
| 6 | */ |
| 7 | |
| 8 | #include <GL/gl.h> |
| 9 | #include <GL/glu.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include "../util/readtex.h" |
| 14 | |
| 15 | |
| 16 | #ifndef SEEK_SET |
| 17 | # define SEEK_SET 0 |
| 18 | #endif |
| 19 | |
| 20 | |
| 21 | /* |
| 22 | ** RGB Image Structure |
| 23 | */ |
| 24 | |
| 25 | typedef struct _TK_RGBImageRec { |
| 26 | GLint sizeX, sizeY; |
| 27 | GLint components; |
| 28 | unsigned char *data; |
| 29 | } TK_RGBImageRec; |
| 30 | |
| 31 | |
| 32 | |
| 33 | /******************************************************************************/ |
| 34 | |
| 35 | typedef struct _rawImageRec { |
| 36 | unsigned short imagic; |
| 37 | unsigned short type; |
| 38 | unsigned short dim; |
| 39 | unsigned short sizeX, sizeY, sizeZ; |
| 40 | unsigned long min, max; |
| 41 | unsigned long wasteBytes; |
| 42 | char name[80]; |
| 43 | unsigned long colorMap; |
| 44 | FILE *file; |
| 45 | unsigned char *tmp, *tmpR, *tmpG, *tmpB, *tmpA; |
| 46 | unsigned long rleEnd; |
| 47 | GLuint *rowStart; |
| 48 | GLint *rowSize; |
| 49 | } rawImageRec; |
| 50 | |
| 51 | /******************************************************************************/ |
| 52 | |
| 53 | static void ConvertShort(unsigned short *array, long length) |
| 54 | { |
| 55 | unsigned long b1, b2; |
| 56 | unsigned char *ptr; |
| 57 | |
| 58 | ptr = (unsigned char *)array; |
| 59 | while (length--) { |
| 60 | b1 = *ptr++; |
| 61 | b2 = *ptr++; |
| 62 | *array++ = (unsigned short) ((b1 << 8) | (b2)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static void ConvertLong(GLuint *array, long length) |
| 67 | { |
| 68 | unsigned long b1, b2, b3, b4; |
| 69 | unsigned char *ptr; |
| 70 | |
| 71 | ptr = (unsigned char *)array; |
| 72 | while (length--) { |
| 73 | b1 = *ptr++; |
| 74 | b2 = *ptr++; |
| 75 | b3 = *ptr++; |
| 76 | b4 = *ptr++; |
| 77 | *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static rawImageRec *RawImageOpen(const char *fileName) |
| 82 | { |
| 83 | union { |
| 84 | int testWord; |
| 85 | char testByte[4]; |
| 86 | } endianTest; |
| 87 | rawImageRec *raw; |
| 88 | GLenum swapFlag; |
| 89 | int x; |
| 90 | |
| 91 | endianTest.testWord = 1; |
| 92 | if (endianTest.testByte[0] == 1) { |
| 93 | swapFlag = GL_TRUE; |
| 94 | } else { |
| 95 | swapFlag = GL_FALSE; |
| 96 | } |
| 97 | |
| 98 | raw = (rawImageRec *)malloc(sizeof(rawImageRec)); |
| 99 | if (raw == NULL) { |
| 100 | fprintf(stderr, "Out of memory!\n"); |
| 101 | return NULL; |
| 102 | } |
| 103 | if ((raw->file = fopen(fileName, "rb")) == NULL) { |
| 104 | perror(fileName); |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | fread(raw, 1, 12, raw->file); |
| 109 | |
| 110 | if (swapFlag) { |
| 111 | ConvertShort(&raw->imagic, 6); |
| 112 | } |
| 113 | |
| 114 | raw->tmp = (unsigned char *)malloc(raw->sizeX*256); |
| 115 | raw->tmpR = (unsigned char *)malloc(raw->sizeX*256); |
| 116 | raw->tmpG = (unsigned char *)malloc(raw->sizeX*256); |
| 117 | raw->tmpB = (unsigned char *)malloc(raw->sizeX*256); |
| 118 | if (raw->sizeZ==4) { |
| 119 | raw->tmpA = (unsigned char *)malloc(raw->sizeX*256); |
| 120 | } |
| 121 | if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL || |
| 122 | raw->tmpB == NULL) { |
| 123 | fprintf(stderr, "Out of memory!\n"); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | if ((raw->type & 0xFF00) == 0x0100) { |
| 128 | x = raw->sizeY * raw->sizeZ * sizeof(GLuint); |
| 129 | raw->rowStart = (GLuint *)malloc(x); |
| 130 | raw->rowSize = (GLint *)malloc(x); |
| 131 | if (raw->rowStart == NULL || raw->rowSize == NULL) { |
| 132 | fprintf(stderr, "Out of memory!\n"); |
| 133 | return NULL; |
| 134 | } |
| 135 | raw->rleEnd = 512 + (2 * x); |
| 136 | fseek(raw->file, 512, SEEK_SET); |
| 137 | fread(raw->rowStart, 1, x, raw->file); |
| 138 | fread(raw->rowSize, 1, x, raw->file); |
| 139 | if (swapFlag) { |
| 140 | ConvertLong(raw->rowStart, (long) (x/sizeof(GLuint))); |
| 141 | ConvertLong((GLuint *)raw->rowSize, (long) (x/sizeof(GLint))); |
| 142 | } |
| 143 | } |
| 144 | return raw; |
| 145 | } |
| 146 | |
| 147 | static void RawImageClose(rawImageRec *raw) |
| 148 | { |
| 149 | |
| 150 | fclose(raw->file); |
| 151 | free(raw->tmp); |
| 152 | free(raw->tmpR); |
| 153 | free(raw->tmpG); |
| 154 | free(raw->tmpB); |
| 155 | if (raw->sizeZ>3) { |
| 156 | free(raw->tmpA); |
| 157 | } |
| 158 | free(raw); |
| 159 | } |
| 160 | |
| 161 | static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z) |
| 162 | { |
| 163 | unsigned char *iPtr, *oPtr, pixel; |
| 164 | int count, done = 0; |
| 165 | |
| 166 | if ((raw->type & 0xFF00) == 0x0100) { |
| 167 | fseek(raw->file, (long) raw->rowStart[y+z*raw->sizeY], SEEK_SET); |
| 168 | fread(raw->tmp, 1, (unsigned int)raw->rowSize[y+z*raw->sizeY], |
| 169 | raw->file); |
| 170 | |
| 171 | iPtr = raw->tmp; |
| 172 | oPtr = buf; |
| 173 | while (!done) { |
| 174 | pixel = *iPtr++; |
| 175 | count = (int)(pixel & 0x7F); |
| 176 | if (!count) { |
| 177 | done = 1; |
| 178 | return; |
| 179 | } |
| 180 | if (pixel & 0x80) { |
| 181 | while (count--) { |
| 182 | *oPtr++ = *iPtr++; |
| 183 | } |
| 184 | } else { |
| 185 | pixel = *iPtr++; |
| 186 | while (count--) { |
| 187 | *oPtr++ = pixel; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } else { |
| 192 | fseek(raw->file, 512+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY), |
| 193 | SEEK_SET); |
| 194 | fread(buf, 1, raw->sizeX, raw->file); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | |
| 199 | static void RawImageGetData(rawImageRec *raw, TK_RGBImageRec *final) |
| 200 | { |
| 201 | unsigned char *ptr; |
| 202 | int i, j; |
| 203 | |
| 204 | final->data = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4); |
| 205 | if (final->data == NULL) { |
| 206 | fprintf(stderr, "Out of memory!\n"); |
| 207 | } |
| 208 | |
| 209 | ptr = final->data; |
| 210 | for (i = 0; i < (int)(raw->sizeY); i++) { |
| 211 | RawImageGetRow(raw, raw->tmpR, i, 0); |
| 212 | RawImageGetRow(raw, raw->tmpG, i, 1); |
| 213 | RawImageGetRow(raw, raw->tmpB, i, 2); |
| 214 | if (raw->sizeZ>3) { |
| 215 | RawImageGetRow(raw, raw->tmpA, i, 3); |
| 216 | } |
| 217 | for (j = 0; j < (int)(raw->sizeX); j++) { |
| 218 | *ptr++ = *(raw->tmpR + j); |
| 219 | *ptr++ = *(raw->tmpG + j); |
| 220 | *ptr++ = *(raw->tmpB + j); |
| 221 | if (raw->sizeZ>3) { |
| 222 | *ptr++ = *(raw->tmpA + j); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
| 229 | static TK_RGBImageRec *tkRGBImageLoad(const char *fileName) |
| 230 | { |
| 231 | rawImageRec *raw; |
| 232 | TK_RGBImageRec *final; |
| 233 | |
| 234 | raw = RawImageOpen(fileName); |
| 235 | if (!raw) { |
| 236 | fprintf(stderr, "File not found\n"); |
| 237 | return NULL; |
| 238 | } |
| 239 | final = (TK_RGBImageRec *)malloc(sizeof(TK_RGBImageRec)); |
| 240 | if (final == NULL) { |
| 241 | fprintf(stderr, "Out of memory!\n"); |
| 242 | return NULL; |
| 243 | } |
| 244 | final->sizeX = raw->sizeX; |
| 245 | final->sizeY = raw->sizeY; |
| 246 | final->components = raw->sizeZ; |
| 247 | RawImageGetData(raw, final); |
| 248 | RawImageClose(raw); |
| 249 | return final; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | static void FreeImage( TK_RGBImageRec *image ) |
| 254 | { |
| 255 | free(image->data); |
| 256 | free(image); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /* |
| 261 | * Load an SGI .rgb file and generate a set of 2-D mipmaps from it. |
| 262 | * Input: imageFile - name of .rgb to read |
| 263 | * intFormat - internal texture format to use, or number of components |
| 264 | * Return: GL_TRUE if success, GL_FALSE if error. |
| 265 | */ |
| 266 | GLboolean LoadRGBMipmaps( const char *imageFile, GLint intFormat ) |
| 267 | { |
| 268 | GLint w, h; |
| 269 | return LoadRGBMipmaps2( imageFile, GL_TEXTURE_2D, intFormat, &w, &h ); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | |
| 274 | GLboolean LoadRGBMipmaps2( const char *imageFile, GLenum target, |
| 275 | GLint intFormat, GLint *width, GLint *height ) |
| 276 | { |
| 277 | GLint error; |
| 278 | GLenum format; |
| 279 | TK_RGBImageRec *image; |
| 280 | |
| 281 | image = tkRGBImageLoad( imageFile ); |
| 282 | if (!image) { |
| 283 | return GL_FALSE; |
| 284 | } |
| 285 | |
| 286 | if (image->components==3) { |
| 287 | format = GL_RGB; |
| 288 | } |
| 289 | else if (image->components==4) { |
| 290 | format = GL_RGBA; |
| 291 | } |
| 292 | else { |
| 293 | /* not implemented */ |
| 294 | fprintf(stderr, |
| 295 | "Error in LoadRGBMipmaps %d-component images not implemented\n", |
| 296 | image->components ); |
| 297 | return GL_FALSE; |
| 298 | } |
| 299 | |
| 300 | error = gluBuild2DMipmaps( target, |
| 301 | intFormat, |
| 302 | image->sizeX, image->sizeY, |
| 303 | format, |
| 304 | GL_UNSIGNED_BYTE, |
| 305 | image->data ); |
| 306 | |
| 307 | *width = image->sizeX; |
| 308 | *height = image->sizeY; |
| 309 | |
| 310 | FreeImage(image); |
| 311 | |
| 312 | return error ? GL_FALSE : GL_TRUE; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | |
| 317 | /* |
| 318 | * Load an SGI .rgb file and return a pointer to the image data. |
| 319 | * Input: imageFile - name of .rgb to read |
| 320 | * Output: width - width of image |
| 321 | * height - height of image |
| 322 | * format - format of image (GL_RGB or GL_RGBA) |
| 323 | * Return: pointer to image data or NULL if error |
| 324 | */ |
| 325 | GLubyte *LoadRGBImage( const char *imageFile, GLint *width, GLint *height, |
| 326 | GLenum *format ) |
| 327 | { |
| 328 | TK_RGBImageRec *image; |
| 329 | GLint bytes; |
| 330 | GLubyte *buffer; |
| 331 | |
| 332 | image = tkRGBImageLoad( imageFile ); |
| 333 | if (!image) { |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | if (image->components==3) { |
| 338 | *format = GL_RGB; |
| 339 | } |
| 340 | else if (image->components==4) { |
| 341 | *format = GL_RGBA; |
| 342 | } |
| 343 | else { |
| 344 | /* not implemented */ |
| 345 | fprintf(stderr, |
| 346 | "Error in LoadRGBImage %d-component images not implemented\n", |
| 347 | image->components ); |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | *width = image->sizeX; |
| 352 | *height = image->sizeY; |
| 353 | |
| 354 | bytes = image->sizeX * image->sizeY * image->components; |
| 355 | buffer = (GLubyte *) malloc(bytes); |
| 356 | if (!buffer) |
| 357 | return NULL; |
| 358 | |
| 359 | memcpy( (void *) buffer, (void *) image->data, bytes ); |
| 360 | |
| 361 | FreeImage(image); |
| 362 | |
| 363 | return buffer; |
| 364 | } |
| 365 | |