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