// rgbimage.cpp -- image data from rgb file // // Written by Frederic Bouvier, started May 2003. // Code taken from the OpenSceneGraph (http://www.openscenegraph.org/) project // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // // $Id: rgbimage.cpp,v 1.2 2005/05/09 07:02:13 fredb Exp $ #include "rgbimage.hpp" #include typedef unsigned char * BytePtr; template inline void swapBytes( T &s ) { if( sizeof( T ) == 1 ) return; T d = s; BytePtr sptr = (BytePtr)&s; BytePtr dptr = &(((BytePtr)&d)[sizeof(T)-1]); for( unsigned int i = 0; i < sizeof(T); i++ ) *(sptr++) = *(dptr--); } struct rawImageRec { unsigned short imagic; unsigned short type; unsigned short dim; unsigned short sizeX, sizeY, sizeZ; unsigned long min, max; unsigned long wasteBytes; char name[80]; unsigned long colorMap; FILE *file; unsigned char *tmp, *tmpR, *tmpG, *tmpB, *tmpA; unsigned long rleEnd; unsigned int *rowStart; int *rowSize; }; static void ConvertShort(unsigned short *array, long length) { unsigned long b1, b2; unsigned char *ptr; ptr = (unsigned char *)array; while (length--) { b1 = *ptr++; b2 = *ptr++; *array++ = (unsigned short) ((b1 << 8) | (b2)); } } static void ConvertLong(unsigned int *array, long length) { unsigned long b1, b2, b3, b4; unsigned char *ptr; ptr = (unsigned char *)array; while (length--) { b1 = *ptr++; b2 = *ptr++; b3 = *ptr++; b4 = *ptr++; *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4); } } static void RawImageClose(rawImageRec *raw) { if (raw) { fclose(raw->file); if (raw->tmp) delete [] raw->tmp; if (raw->tmpR) delete [] raw->tmpR; if (raw->tmpG) delete [] raw->tmpG; if (raw->tmpB) delete [] raw->tmpB; if (raw->tmpA) delete [] raw->tmpA; if (raw->rowStart) delete [] raw->rowStart; if (raw->rowSize) delete [] raw->rowSize; delete raw; } } static rawImageRec *RawImageOpen(const char *fileName) { union { int testWord; char testByte[4]; } endianTest; rawImageRec *raw; bool swapFlag; int x; endianTest.testWord = 1; if (endianTest.testByte[0] == 1) { swapFlag = true; } else { swapFlag = false; } raw = new rawImageRec; if (raw == NULL) { // notify(WARN)<< "Out of memory!"<< std::endl; return NULL; } if ((raw->file = fopen(fileName, "rb")) == NULL) { delete raw; perror(fileName); return NULL; } fread(raw, 1, 12, raw->file); if (swapFlag) { ConvertShort(&raw->imagic, 6); } raw->tmp = raw->tmpR = raw->tmpG = raw->tmpB = raw->tmpA = 0L; raw->rowStart = 0; raw->rowSize = 0; raw->tmp = new unsigned char [raw->sizeX*256]; if (raw->tmp == NULL ) { // notify(FATAL)<< "Out of memory!"<< std::endl; RawImageClose(raw); return NULL; } if( raw->sizeZ >= 1 ) { if( (raw->tmpR = new unsigned char [raw->sizeX]) == NULL ) { // notify(FATAL)<< "Out of memory!"<< std::endl; RawImageClose(raw); return NULL; } } if( raw->sizeZ >= 2 ) { if( (raw->tmpG = new unsigned char [raw->sizeX]) == NULL ) { // notify(FATAL)<< "Out of memory!"<< std::endl; RawImageClose(raw); return NULL; } } if( raw->sizeZ >= 3 ) { if( (raw->tmpB = new unsigned char [raw->sizeX]) == NULL ) { // notify(FATAL)<< "Out of memory!"<< std::endl; RawImageClose(raw); return NULL; } } if (raw->sizeZ >= 4) { if( (raw->tmpA = new unsigned char [raw->sizeX]) == NULL ) { // notify(FATAL)<< "Out of memory!"<< std::endl; RawImageClose(raw); return NULL; } } if ((raw->type & 0xFF00) == 0x0100) { unsigned int ybyz = raw->sizeY * raw->sizeZ; if ( (raw->rowStart = new unsigned int [ybyz]) == NULL ) { // notify(FATAL)<< "Out of memory!"<< std::endl; RawImageClose(raw); return NULL; } if ( (raw->rowSize = new int [ybyz]) == NULL ) { // notify(FATAL)<< "Out of memory!"<< std::endl; RawImageClose(raw); return NULL; } x = ybyz * sizeof(unsigned int); raw->rleEnd = 512 + (2 * x); fseek(raw->file, 512, SEEK_SET); fread(raw->rowStart, 1, x, raw->file); fread(raw->rowSize, 1, x, raw->file); if (swapFlag) { ConvertLong(raw->rowStart, (long) (x/sizeof(unsigned int))); ConvertLong((unsigned int *)raw->rowSize, (long) (x/sizeof(int))); } } return raw; } static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z) { unsigned char *iPtr, *oPtr, pixel; int count, done = 0; if ((raw->type & 0xFF00) == 0x0100) { fseek(raw->file, (long) raw->rowStart[y+z*raw->sizeY], SEEK_SET); fread(raw->tmp, 1, (unsigned int)raw->rowSize[y+z*raw->sizeY], raw->file); iPtr = raw->tmp; oPtr = buf; while (!done) { pixel = *iPtr++; count = (int)(pixel & 0x7F); if (!count) { done = 1; return; } if (pixel & 0x80) { while (count--) { *oPtr++ = *iPtr++; } } else { pixel = *iPtr++; while (count--) { *oPtr++ = pixel; } } } } else { fseek(raw->file, 512+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY), SEEK_SET); fread(buf, 1, raw->sizeX, raw->file); } } static void RawImageGetData(rawImageRec *raw, unsigned char *data ) { unsigned char *ptr; int i, j; // // round the width to a factor 4 // int width = (int)(floorf((float)raw->sizeX/4.0f)*4.0f); // if (width!=raw->sizeX) width += 4; ptr = data; for (i = 0; i < (int)(raw->sizeY); i++) { if( raw->sizeZ >= 1 ) RawImageGetRow(raw, raw->tmpR, i, 0); if( raw->sizeZ >= 2 ) RawImageGetRow(raw, raw->tmpG, i, 1); if( raw->sizeZ >= 3 ) RawImageGetRow(raw, raw->tmpB, i, 2); if( raw->sizeZ >= 4 ) RawImageGetRow(raw, raw->tmpA, i, 3); for (j = 0; j < (int)(raw->sizeX); j++) { if( raw->sizeZ >= 1 ) *ptr++ = *(raw->tmpR + j); if( raw->sizeZ >= 2 ) *ptr++ = *(raw->tmpG + j); if( raw->sizeZ >= 3 ) *ptr++ = *(raw->tmpB + j); if( raw->sizeZ >= 4 ) *ptr++ = *(raw->tmpA + j); } // // pad the image width with blanks to bring it up to the rounded width. // for(;jsizeX; int t = raw->sizeY; int r = 1; allocImage( raw->sizeX, raw->sizeY, raw->sizeZ ); RawImageGetData(raw, _buffer); RawImageClose(raw); return true; } bool FGSD_RGBImage::save (const char *__fileName) { FILE *fp = fopen(__fileName, "wb"); if (!fp) return false; rawImageRec raw; raw.imagic = 0732; raw.type = 1; raw.dim = _depth; raw.sizeX = _width; raw.sizeY = _height; raw.sizeZ = raw.dim; raw.min = 0; raw.max = 0xFF; raw.wasteBytes = 0; strcpy( raw.name, __fileName ); raw.colorMap = 0; int isize = _height * _rowStride; unsigned char *buffer = new unsigned char[isize]; unsigned char *dptr = buffer; int i, j; for( i = 0; i < raw.sizeZ; i++ ) { const unsigned char *ptr = _buffer; ptr += i; for( j = 0; j < isize/raw.sizeZ; j++ ) { *(dptr++) = *ptr; ptr += raw.sizeZ; } } swapBytes( raw.imagic ); swapBytes( raw.type ); swapBytes( raw.dim ); swapBytes( raw.sizeX ); swapBytes( raw.sizeY ); swapBytes( raw.sizeZ ); swapBytes( raw.min ); swapBytes( raw.max ); swapBytes( raw.colorMap ); char pad[512 - sizeof(rawImageRec)]; memset( pad, 0, sizeof(pad)); fwrite( &raw, sizeof( rawImageRec), 1, fp ); fwrite( pad, sizeof(pad), 1, fp ); fwrite( buffer, isize, 1, fp ); fclose(fp); return true; }