| 1 | // SPDX-FileCopyrightText: 2011 Torsten Dreyer |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | // From the OpenSceneGraph distribution ReaderWriterRGB.cpp |
| 5 | // Reader for sgi's .rgb format. |
| 6 | // specification can be found at http://local.wasp.uwa.edu.au/~pbourke/dataformats/sgirgb/sgiversion.html |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | # include <config.h> |
| 10 | #endif |
| 11 | |
| 12 | #ifdef HAVE_WINDOWS_H |
| 13 | #include <windows.h> |
| 14 | #endif |
| 15 | |
| 16 | #if defined (SG_MAC) |
| 17 | #include <OpenGL/gl.h> |
| 18 | #include <GLUT/glut.h> |
| 19 | #elif defined (_GLES2) |
| 20 | #include <GLES2/gl2.h> |
| 21 | #else |
| 22 | #include <GL/glew.h> // Must be included before <GL/gl.h> |
| 23 | #include <GL/gl.h> |
| 24 | #include <GL/glut.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <cstdio> |
| 28 | #include <cstdlib> |
| 29 | #include <fstream> |
| 30 | #include <iostream> |
| 31 | |
| 32 | using std::ifstream; |
| 33 | using std::ios; |
| 34 | using std::istream; |
| 35 | using std::string; |
| 36 | |
| 37 | #include "FGRGBTextureLoader.hxx" |
| 38 | |
| 39 | typedef struct _rawImageRec { |
| 40 | unsigned short imagic; |
| 41 | unsigned short type; |
| 42 | unsigned short dim; |
| 43 | unsigned short sizeX, sizeY, sizeZ; |
| 44 | unsigned long min, max; |
| 45 | unsigned long wasteBytes; |
| 46 | char name[80]; |
| 47 | unsigned long colorMap; |
| 48 | istream *file; |
| 49 | unsigned char *tmp, *tmpR, *tmpG, *tmpB, *tmpA; |
| 50 | unsigned long rleEnd; |
| 51 | GLuint *rowStart; |
| 52 | GLint *rowSize; |
| 53 | GLenum swapFlag; |
| 54 | short bpc; |
| 55 | |
| 56 | typedef unsigned char *BytePtr; |
| 57 | |
| 58 | bool needsBytesSwapped () { |
| 59 | union { |
| 60 | int testWord; |
| 61 | char testByte[sizeof (int)]; |
| 62 | } endianTest; |
| 63 | endianTest.testWord = 1; |
| 64 | if (endianTest.testByte[0] == 1) { |
| 65 | return true; |
| 66 | } else { |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | template <class T> |
| 72 | inline void swapBytes (T &s) { |
| 73 | if (sizeof (T) == 1) { |
| 74 | return; |
| 75 | } |
| 76 | T d = s; |
| 77 | BytePtr sptr ((BytePtr) &s); |
| 78 | BytePtr dptr = &(((BytePtr) &d)[sizeof (T) - 1]); |
| 79 | |
| 80 | for (unsigned int i = 0; i < sizeof (T); i++) { |
| 81 | *(sptr++) = *(dptr--); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void swapBytes () { |
| 86 | swapBytes (imagic); |
| 87 | swapBytes (type); |
| 88 | swapBytes (dim); |
| 89 | swapBytes (sizeX); |
| 90 | swapBytes (sizeY); |
| 91 | swapBytes (sizeZ); |
| 92 | swapBytes (wasteBytes); |
| 93 | swapBytes (min); |
| 94 | swapBytes (max); |
| 95 | swapBytes (colorMap); |
| 96 | } |
| 97 | } rawImageRec; |
| 98 | |
| 99 | static void |
| 100 | ConvertShort (unsigned short *array, long length) { |
| 101 | unsigned long b1, b2; |
| 102 | unsigned char *ptr ((unsigned char *) array); |
| 103 | while (length--) { |
| 104 | b1 = *ptr++; |
| 105 | b2 = *ptr++; |
| 106 | *array++ = (unsigned short) ((b1 << 8) | (b2)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static void |
| 111 | ConvertLong (GLuint *array, long length) { |
| 112 | unsigned long b1, b2, b3, b4; |
| 113 | unsigned char *ptr ((unsigned char *) array); |
| 114 | while (length--) { |
| 115 | b1 = *ptr++; |
| 116 | b2 = *ptr++; |
| 117 | b3 = *ptr++; |
| 118 | b4 = *ptr++; |
| 119 | *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | RawImageClose (rawImageRec *raw) { |
| 125 | if (raw) { |
| 126 | if (raw->tmp) { |
| 127 | delete []raw->tmp; |
| 128 | } |
| 129 | if (raw->tmpR) { |
| 130 | delete []raw->tmpR; |
| 131 | } |
| 132 | if (raw->tmpG) { |
| 133 | delete []raw->tmpG; |
| 134 | } |
| 135 | if (raw->tmpB) { |
| 136 | delete []raw->tmpB; |
| 137 | } |
| 138 | if (raw->tmpA) { |
| 139 | delete []raw->tmpA; |
| 140 | } |
| 141 | if (raw->rowStart) { |
| 142 | delete []raw->rowStart; |
| 143 | } |
| 144 | if (raw->rowSize) { |
| 145 | delete []raw->rowSize; |
| 146 | } |
| 147 | delete raw; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static rawImageRec * |
| 152 | RawImageOpen (istream& fin) { |
| 153 | union { |
| 154 | int testWord; |
| 155 | char testByte[4]; |
| 156 | } endianTest; |
| 157 | |
| 158 | rawImageRec *raw = new rawImageRec; |
| 159 | if (raw == NULL) { |
| 160 | // notify(WARN)<< "Out of memory!"<< endl; |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | //Set istream pointer |
| 165 | raw->file = &fin; |
| 166 | |
| 167 | endianTest.testWord = 1; |
| 168 | if (endianTest.testByte[0] == 1) { |
| 169 | raw->swapFlag = GL_TRUE; |
| 170 | } else { |
| 171 | raw->swapFlag = GL_FALSE; |
| 172 | } |
| 173 | |
| 174 | fin.read ((char*) raw, 12); |
| 175 | if (!fin.good ()) { |
| 176 | return NULL; |
| 177 | } |
| 178 | if (raw->swapFlag) { |
| 179 | ConvertShort (&raw->imagic, 6); |
| 180 | } |
| 181 | |
| 182 | raw->tmp = raw->tmpR = raw->tmpG = raw->tmpB = raw->tmpA = 0L; |
| 183 | raw->rowStart = 0; |
| 184 | raw->rowSize = 0; |
| 185 | raw->bpc = (raw->type & 0x00FF); |
| 186 | |
| 187 | raw->tmp = new unsigned char[raw->sizeX * 256 * raw->bpc]; |
| 188 | if (raw->tmp == NULL ) { |
| 189 | // notify(FATAL)<< "Out of memory!"<< endl; |
| 190 | RawImageClose (raw); |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | if (raw->sizeZ >= 1) { |
| 195 | if ((raw->tmpR = new unsigned char[raw->sizeX * raw->bpc]) == NULL) { |
| 196 | // notify(FATAL)<< "Out of memory!"<< endl; |
| 197 | RawImageClose (raw); |
| 198 | return NULL; |
| 199 | } |
| 200 | } |
| 201 | if (raw->sizeZ >= 2) { |
| 202 | if ((raw->tmpG = new unsigned char[raw->sizeX * raw->bpc]) == NULL) { |
| 203 | // notify(FATAL)<< "Out of memory!"<< endl; |
| 204 | RawImageClose (raw); |
| 205 | return NULL; |
| 206 | } |
| 207 | } |
| 208 | if (raw->sizeZ >= 3) { |
| 209 | if ((raw->tmpB = new unsigned char[raw->sizeX*raw->bpc]) == NULL) { |
| 210 | // notify(FATAL)<< "Out of memory!"<< endl; |
| 211 | RawImageClose (raw); |
| 212 | return NULL; |
| 213 | } |
| 214 | } |
| 215 | if (raw->sizeZ >= 4) { |
| 216 | if ((raw->tmpA = new unsigned char[raw->sizeX * raw->bpc]) == NULL) { |
| 217 | // notify(FATAL)<< "Out of memory!"<< endl; |
| 218 | RawImageClose (raw); |
| 219 | return NULL; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if ((raw->type & 0xFF00) == 0x0100) { |
| 224 | unsigned int ybyz (raw->sizeY * raw->sizeZ); |
| 225 | if ((raw->rowStart = new GLuint[ybyz]) == NULL) { |
| 226 | // notify(FATAL)<< "Out of memory!"<< endl; |
| 227 | RawImageClose (raw); |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | if ((raw->rowSize = new GLint[ybyz]) == NULL) { |
| 232 | // notify(FATAL)<< "Out of memory!"<< endl; |
| 233 | RawImageClose (raw); |
| 234 | return NULL; |
| 235 | } |
| 236 | int x (ybyz * sizeof (GLuint)); |
| 237 | raw->rleEnd = 512 + (2 * x); |
| 238 | fin.seekg (512, ios::beg); |
| 239 | fin.read ((char*) raw->rowStart, x); |
| 240 | fin.read ((char*) raw->rowSize, x); |
| 241 | if (raw->swapFlag) { |
| 242 | ConvertLong (raw->rowStart, long (x / sizeof (GLuint))); |
| 243 | ConvertLong ((GLuint *) raw->rowSize, long (x /sizeof (GLint))); |
| 244 | } |
| 245 | } |
| 246 | return raw; |
| 247 | } |
| 248 | |
| 249 | static void |
| 250 | RawImageGetRow (rawImageRec *raw, unsigned char *buf, const int y, const int z) { |
| 251 | unsigned short pixel; |
| 252 | int done = 0; |
| 253 | unsigned short *tempShort; |
| 254 | |
| 255 | if ((raw->type & 0xFF00) == 0x0100) { |
| 256 | raw->file->seekg (long (raw->rowStart[y + z * raw->sizeY]), ios::beg); |
| 257 | raw->file->read ((char*) raw->tmp, (unsigned int) (raw->rowSize[y + z * raw->sizeY])); |
| 258 | |
| 259 | unsigned char *iPtr = raw->tmp; |
| 260 | unsigned char *oPtr = buf; |
| 261 | while (!done) { |
| 262 | if (raw->bpc == 1) { |
| 263 | pixel = *iPtr++; |
| 264 | } else { |
| 265 | tempShort = reinterpret_cast<unsigned short*> (iPtr); |
| 266 | pixel = *tempShort; |
| 267 | tempShort++; |
| 268 | iPtr = reinterpret_cast<unsigned char *> (tempShort); |
| 269 | } |
| 270 | |
| 271 | if (raw->bpc != 1) { |
| 272 | ConvertShort (&pixel, 1); |
| 273 | } |
| 274 | int count (int (pixel & 0x7F)); |
| 275 | |
| 276 | // limit the count value to the remiaing row size |
| 277 | if (oPtr + count * raw->bpc > buf + raw->sizeX * raw->bpc) { |
| 278 | count = ((buf + raw->sizeX * raw->bpc) - oPtr) / raw->bpc; |
| 279 | } |
| 280 | |
| 281 | if (count <= 0) { |
| 282 | done = 1; |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | if (pixel & 0x80) { |
| 287 | while (count--) { |
| 288 | if (raw->bpc == 1) { |
| 289 | *oPtr++ = *iPtr++; |
| 290 | } else { |
| 291 | tempShort = reinterpret_cast<unsigned short*> (iPtr); |
| 292 | pixel = *tempShort; |
| 293 | tempShort++; |
| 294 | iPtr = reinterpret_cast<unsigned char *> (tempShort); |
| 295 | |
| 296 | ConvertShort (&pixel, 1); |
| 297 | |
| 298 | tempShort = reinterpret_cast<unsigned short*> (oPtr); |
| 299 | *tempShort = pixel; |
| 300 | tempShort++; |
| 301 | oPtr = reinterpret_cast<unsigned char *> (tempShort); |
| 302 | } |
| 303 | } |
| 304 | } else { |
| 305 | if (raw->bpc == 1) { |
| 306 | pixel = *iPtr++; |
| 307 | } else { |
| 308 | tempShort = reinterpret_cast<unsigned short*> (iPtr); |
| 309 | pixel = *tempShort; |
| 310 | tempShort++; |
| 311 | iPtr = reinterpret_cast<unsigned char *> (tempShort); |
| 312 | } |
| 313 | if (raw->bpc != 1) { |
| 314 | ConvertShort (&pixel, 1); |
| 315 | } |
| 316 | while (count--) { |
| 317 | if (raw->bpc == 1) { |
| 318 | *oPtr++ = pixel; |
| 319 | } else { |
| 320 | tempShort = reinterpret_cast<unsigned short*> (oPtr); |
| 321 | *tempShort = pixel; |
| 322 | tempShort++; |
| 323 | oPtr = reinterpret_cast<unsigned char *> (tempShort); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | } else { |
| 329 | raw->file->seekg (512 + (y * raw->sizeX * raw->bpc) + (z * raw->sizeX * raw->sizeY * raw->bpc), ios::beg); |
| 330 | raw->file->read ((char*) buf, raw->sizeX * raw->bpc); |
| 331 | if (raw->swapFlag && raw->bpc != 1) { |
| 332 | ConvertShort (reinterpret_cast<unsigned short*> (buf), raw->sizeX); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | static void |
| 338 | RawImageGetData (rawImageRec *raw, unsigned char **data) { |
| 339 | |
| 340 | // // round the width to a factor 4 |
| 341 | // int width = (int)(floorf((float)raw->sizeX/4.0f)*4.0f); |
| 342 | // if (width!=raw->sizeX) width += 4; |
| 343 | |
| 344 | // byte aligned. |
| 345 | |
| 346 | // osg::notify(osg::INFO)<<"raw->sizeX = "<<raw->sizeX<<endl; |
| 347 | // osg::notify(osg::INFO)<<"raw->sizeY = "<<raw->sizeY<<endl; |
| 348 | // osg::notify(osg::INFO)<<"raw->sizeZ = "<<raw->sizeZ<<endl; |
| 349 | // osg::notify(osg::INFO)<<"raw->bpc = "<<raw->bpc<<endl; |
| 350 | |
| 351 | *data = new unsigned char [(raw->sizeX) * (raw->sizeY) * (raw->sizeZ) * (raw->bpc)]; |
| 352 | |
| 353 | unsigned char *ptr (*data); |
| 354 | for (int i = 0; i < int (raw->sizeY); i++) { |
| 355 | if (raw->sizeZ >= 1) { |
| 356 | RawImageGetRow (raw, raw->tmpR, i, 0); |
| 357 | } |
| 358 | if (raw->sizeZ >= 2) { |
| 359 | RawImageGetRow (raw, raw->tmpG, i, 1); |
| 360 | } |
| 361 | if (raw->sizeZ >= 3) { |
| 362 | RawImageGetRow (raw, raw->tmpB, i, 2); |
| 363 | } |
| 364 | if (raw->sizeZ >= 4) { |
| 365 | RawImageGetRow (raw, raw->tmpA, i, 3); |
| 366 | } |
| 367 | for (int j = 0; j < int (raw->sizeX); j++) { |
| 368 | if (raw->bpc == 1) { |
| 369 | if (raw->sizeZ >= 1) { |
| 370 | *ptr++ = *(raw->tmpR + j); |
| 371 | } |
| 372 | if (raw->sizeZ >= 2) { |
| 373 | *ptr++ = *(raw->tmpG + j); |
| 374 | } |
| 375 | if (raw->sizeZ >= 3) { |
| 376 | *ptr++ = *(raw->tmpB + j); |
| 377 | } |
| 378 | if (raw->sizeZ >= 4) { |
| 379 | *ptr++ = *(raw->tmpA + j); |
| 380 | } |
| 381 | } else { |
| 382 | unsigned short *tempShort; |
| 383 | if (raw->sizeZ >= 1) { |
| 384 | tempShort = reinterpret_cast<unsigned short*> (ptr); |
| 385 | *tempShort = *(reinterpret_cast<unsigned short*> (raw->tmpR) + j); |
| 386 | tempShort++; |
| 387 | ptr = reinterpret_cast<unsigned char *> (tempShort); |
| 388 | } |
| 389 | if (raw->sizeZ >= 2) { |
| 390 | tempShort = reinterpret_cast<unsigned short*> (ptr); |
| 391 | *tempShort = *(reinterpret_cast<unsigned short*> (raw->tmpG) + j); |
| 392 | tempShort++; |
| 393 | ptr = reinterpret_cast<unsigned char *> (tempShort); |
| 394 | } |
| 395 | if (raw->sizeZ >= 3) { |
| 396 | tempShort = reinterpret_cast<unsigned short*> (ptr); |
| 397 | *tempShort = *(reinterpret_cast<unsigned short*> (raw->tmpB) + j); |
| 398 | tempShort++; |
| 399 | ptr = reinterpret_cast<unsigned char *> (tempShort); |
| 400 | } |
| 401 | if (raw->sizeZ >= 4) { |
| 402 | tempShort = reinterpret_cast<unsigned short*> (ptr); |
| 403 | *tempShort = *(reinterpret_cast<unsigned short*> (raw->tmpA) + j); |
| 404 | tempShort++; |
| 405 | ptr = reinterpret_cast<unsigned char *> (tempShort); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | // // pad the image width with blanks to bring it up to the rounded width. |
| 410 | // for(;j<width;++j) *ptr++ = 0; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | |
| 415 | // supportsExtension("rgb","rgb image format"); |
| 416 | // supportsExtension("rgba","rgba image format"); |
| 417 | // supportsExtension("sgi","sgi image format"); |
| 418 | // supportsExtension("int","int image format"); |
| 419 | // supportsExtension("inta","inta image format"); |
| 420 | // supportsExtension("bw","bw image format"); |
| 421 | |
| 422 | GLuint |
| 423 | readRGBStream (istream &fin) { |
| 424 | rawImageRec * const raw (RawImageOpen (fin)); |
| 425 | |
| 426 | if (raw == NULL) { |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | const int s (raw->sizeX); |
| 431 | const int t (raw->sizeY); |
| 432 | // int r = 1; |
| 433 | |
| 434 | #if 0 |
| 435 | int internalFormat = raw->sizeZ == 3 ? GL_RGB5 : |
| 436 | raw->sizeZ == 4 ? GL_RGB5_A1 : GL_RGB; |
| 437 | #else |
| 438 | // int internalFormat = raw->sizeZ; |
| 439 | #endif |
| 440 | const unsigned int pixelFormat |
| 441 | (raw->sizeZ == 1 ? GL_LUMINANCE : |
| 442 | raw->sizeZ == 2 ? GL_LUMINANCE_ALPHA : |
| 443 | raw->sizeZ == 3 ? GL_RGB : |
| 444 | raw->sizeZ == 4 ? GL_RGBA : (GLenum) - 1); |
| 445 | |
| 446 | const unsigned int dataType (raw->bpc == 1 ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT); |
| 447 | |
| 448 | unsigned char *data; |
| 449 | RawImageGetData (raw, &data); |
| 450 | RawImageClose (raw); |
| 451 | |
| 452 | GLuint texture; |
| 453 | glGenTextures (1, &texture); |
| 454 | glBindTexture (GL_TEXTURE_2D, texture); |
| 455 | glTexImage2D (GL_TEXTURE_2D, 0, pixelFormat, s, t, 0, pixelFormat, dataType, (GLvoid*) data); |
| 456 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 457 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 458 | |
| 459 | delete []data; |
| 460 | return texture; |
| 461 | } |
| 462 | |
| 463 | GLuint |
| 464 | FGRGBTextureLoader::loadTexture (const string &filename) { |
| 465 | GLuint texture = NOTEXTURE; |
| 466 | ifstream istream (filename.c_str (), ios::in | ios::binary); |
| 467 | texture = readRGBStream (istream); |
| 468 | istream.close (); |
| 469 | return texture; |
| 470 | } |