Restructed the ref counting of the rawImageRec structure so that it's done with a separate refImageRec struct so it doesn't interfer with the writing of the rawImageRec as a header when writing to .rgb

This commit is contained in:
Robert Osfield 2017-11-11 11:17:34 +00:00
parent eb061d9acc
commit ee118c872f

View File

@ -39,7 +39,7 @@ using namespace osg;
typedef unsigned int size_pos; typedef unsigned int size_pos;
struct rawImageRec : public osg::Referenced struct rawImageRec
{ {
rawImageRec(): rawImageRec():
imagic(0), imagic(0),
@ -59,7 +59,7 @@ struct rawImageRec : public osg::Referenced
{ {
} }
virtual ~rawImageRec() ~rawImageRec()
{ {
if (tmp) delete [] tmp; if (tmp) delete [] tmp;
if (tmpR) delete [] tmpR; if (tmpR) delete [] tmpR;
@ -131,6 +131,11 @@ struct rawImageRec : public osg::Referenced
} }
}; };
struct refImageRec : public rawImageRec, public osg::Referenced
{
};
static void ConvertShort(unsigned short *array, long length) static void ConvertShort(unsigned short *array, long length)
{ {
unsigned long b1, b2; unsigned long b1, b2;
@ -163,7 +168,7 @@ static void ConvertLong(GLuint *array, long length)
static osg::ref_ptr<rawImageRec> RawImageOpen(std::istream& fin) static osg::ref_ptr<refImageRec> RawImageOpen(std::istream& fin)
{ {
union union
{ {
@ -173,7 +178,7 @@ static osg::ref_ptr<rawImageRec> RawImageOpen(std::istream& fin)
int x; int x;
osg::ref_ptr<rawImageRec> raw = new rawImageRec; osg::ref_ptr<refImageRec> raw = new refImageRec;
if (raw == NULL) if (raw == NULL)
{ {
OSG_WARN<< "Out of memory!"<< std::endl; OSG_WARN<< "Out of memory!"<< std::endl;
@ -278,27 +283,27 @@ static osg::ref_ptr<rawImageRec> RawImageOpen(std::istream& fin)
} }
static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z) static void RawImageGetRow(rawImageRec& raw, unsigned char *buf, int y, int z)
{ {
unsigned char *iPtr, *oPtr; unsigned char *iPtr, *oPtr;
unsigned short pixel; unsigned short pixel;
int count, done = 0; int count, done = 0;
unsigned short *tempShort; unsigned short *tempShort;
if ((raw->type & 0xFF00) == 0x0100) if ((raw.type & 0xFF00) == 0x0100)
{ {
size_pos pos = raw->rowStart[static_cast<size_pos>(y)+static_cast<size_pos>(z)*static_cast<size_pos>(raw->sizeY)]; size_pos pos = raw.rowStart[static_cast<size_pos>(y)+static_cast<size_pos>(z)*static_cast<size_pos>(raw.sizeY)];
size_pos amount = raw->rowSize[static_cast<size_pos>(y)+static_cast<size_pos>(z)*static_cast<size_pos>(raw->sizeY)]; size_pos amount = raw.rowSize[static_cast<size_pos>(y)+static_cast<size_pos>(z)*static_cast<size_pos>(raw.sizeY)];
raw->file->seekg(pos, std::ios::beg); raw.file->seekg(pos, std::ios::beg);
raw->file->read((char*)raw->tmp, amount); raw.file->read((char*)raw.tmp, amount);
iPtr = raw->tmp; iPtr = raw.tmp;
oPtr = buf; oPtr = buf;
while (!done) while (!done)
{ {
if (raw->bpc == 1) if (raw.bpc == 1)
pixel = *iPtr++; pixel = *iPtr++;
else else
{ {
@ -308,15 +313,15 @@ static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
iPtr = reinterpret_cast<unsigned char *>(tempShort); iPtr = reinterpret_cast<unsigned char *>(tempShort);
} }
if(raw->bpc != 1) if(raw.bpc != 1)
ConvertShort(&pixel, 1); ConvertShort(&pixel, 1);
count = (int)(pixel & 0x7F); count = (int)(pixel & 0x7F);
// limit the count value to the remiaing row size // limit the count value to the remiaing row size
if ((static_cast<size_pos>(raw->sizeX)*static_cast<size_pos>(raw->bpc)) <= (oPtr - buf)) if ((static_cast<size_pos>(raw.sizeX)*static_cast<size_pos>(raw.bpc)) <= (oPtr - buf))
{ {
count = static_cast<size_pos>(raw->sizeX) - (oPtr - buf) / static_cast<size_pos>(raw->bpc); count = static_cast<size_pos>(raw.sizeX) - (oPtr - buf) / static_cast<size_pos>(raw.bpc);
} }
if (count<=0) if (count<=0)
@ -329,7 +334,7 @@ static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
{ {
while (count--) while (count--)
{ {
if(raw->bpc == 1) if(raw.bpc == 1)
*oPtr++ = *iPtr++; *oPtr++ = *iPtr++;
else{ else{
tempShort = reinterpret_cast<unsigned short*>(iPtr); tempShort = reinterpret_cast<unsigned short*>(iPtr);
@ -348,7 +353,7 @@ static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
} }
else else
{ {
if (raw->bpc == 1) if (raw.bpc == 1)
{ {
pixel = *iPtr++; pixel = *iPtr++;
} }
@ -359,11 +364,11 @@ static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
tempShort++; tempShort++;
iPtr = reinterpret_cast<unsigned char *>(tempShort); iPtr = reinterpret_cast<unsigned char *>(tempShort);
} }
if(raw->bpc != 1) if(raw.bpc != 1)
ConvertShort(&pixel, 1); ConvertShort(&pixel, 1);
while (count--) while (count--)
{ {
if(raw->bpc == 1) if(raw.bpc == 1)
*oPtr++ = pixel; *oPtr++ = pixel;
else else
{ {
@ -379,87 +384,87 @@ static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
else else
{ {
size_pos pos = static_cast<size_pos>(512)+ size_pos pos = static_cast<size_pos>(512)+
(static_cast<size_pos>(y)*static_cast<size_pos>(raw->sizeX)*static_cast<size_pos>(raw->bpc))+ (static_cast<size_pos>(y)*static_cast<size_pos>(raw.sizeX)*static_cast<size_pos>(raw.bpc))+
(static_cast<size_pos>(z)*static_cast<size_pos>(raw->sizeX)*static_cast<size_pos>(raw->sizeY)*static_cast<size_pos>(raw->bpc)); (static_cast<size_pos>(z)*static_cast<size_pos>(raw.sizeX)*static_cast<size_pos>(raw.sizeY)*static_cast<size_pos>(raw.bpc));
size_pos amount = static_cast<size_pos>(raw->sizeX)*static_cast<size_pos>(raw->bpc); size_pos amount = static_cast<size_pos>(raw.sizeX)*static_cast<size_pos>(raw.bpc);
raw->file->seekg(pos,std::ios::beg); raw.file->seekg(pos,std::ios::beg);
raw->file->read((char*)buf, amount); raw.file->read((char*)buf, amount);
if(raw->swapFlag && raw->bpc != 1){ if(raw.swapFlag && raw.bpc != 1){
ConvertShort(reinterpret_cast<unsigned short*>(buf), raw->sizeX); ConvertShort(reinterpret_cast<unsigned short*>(buf), raw.sizeX);
} }
} }
} }
static void RawImageGetData(rawImageRec *raw, unsigned char **data ) static void RawImageGetData(rawImageRec& raw, unsigned char **data )
{ {
unsigned char *ptr; unsigned char *ptr;
int i, j; int i, j;
unsigned short *tempShort; unsigned short *tempShort;
// // round the width to a factor 4 // // round the width to a factor 4
// int width = (int)(floorf((float)raw->sizeX/4.0f)*4.0f); // int width = (int)(floorf((float)raw.sizeX/4.0f)*4.0f);
// if (width!=raw->sizeX) width += 4; // if (width!=raw.sizeX) width += 4;
// byte aligned. // byte aligned.
OSG_INFO<<"raw->sizeX = "<<raw->sizeX<<std::endl; OSG_INFO<<"raw.sizeX = "<<raw.sizeX<<std::endl;
OSG_INFO<<"raw->sizeY = "<<raw->sizeY<<std::endl; OSG_INFO<<"raw.sizeY = "<<raw.sizeY<<std::endl;
OSG_INFO<<"raw->sizeZ = "<<raw->sizeZ<<std::endl; OSG_INFO<<"raw.sizeZ = "<<raw.sizeZ<<std::endl;
OSG_INFO<<"raw->bpc = "<<raw->bpc<<std::endl; OSG_INFO<<"raw.bpc = "<<raw.bpc<<std::endl;
*data = new unsigned char [(raw->sizeX)*(raw->sizeY)*(raw->sizeZ)*(raw->bpc)]; *data = new unsigned char [(raw.sizeX)*(raw.sizeY)*(raw.sizeZ)*(raw.bpc)];
ptr = *data; ptr = *data;
for (i = 0; i < (int)(raw->sizeY); i++) for (i = 0; i < (int)(raw.sizeY); i++)
{ {
if( raw->sizeZ >= 1 ) if( raw.sizeZ >= 1 )
RawImageGetRow(raw, raw->tmpR, i, 0); RawImageGetRow(raw, raw.tmpR, i, 0);
if( raw->sizeZ >= 2 ) if( raw.sizeZ >= 2 )
RawImageGetRow(raw, raw->tmpG, i, 1); RawImageGetRow(raw, raw.tmpG, i, 1);
if( raw->sizeZ >= 3 ) if( raw.sizeZ >= 3 )
RawImageGetRow(raw, raw->tmpB, i, 2); RawImageGetRow(raw, raw.tmpB, i, 2);
if( raw->sizeZ >= 4 ) if( raw.sizeZ >= 4 )
RawImageGetRow(raw, raw->tmpA, i, 3); RawImageGetRow(raw, raw.tmpA, i, 3);
for (j = 0; j < (int)(raw->sizeX); j++) for (j = 0; j < (int)(raw.sizeX); j++)
{ {
if(raw->bpc == 1){ if(raw.bpc == 1){
if( raw->sizeZ >= 1 ) if( raw.sizeZ >= 1 )
*ptr++ = *(raw->tmpR + j); *ptr++ = *(raw.tmpR + j);
if( raw->sizeZ >= 2 ) if( raw.sizeZ >= 2 )
*ptr++ = *(raw->tmpG + j); *ptr++ = *(raw.tmpG + j);
if( raw->sizeZ >= 3 ) if( raw.sizeZ >= 3 )
*ptr++ = *(raw->tmpB + j); *ptr++ = *(raw.tmpB + j);
if( raw->sizeZ >= 4 ) if( raw.sizeZ >= 4 )
*ptr++ = *(raw->tmpA + j); *ptr++ = *(raw.tmpA + j);
}else{ }else{
if( raw->sizeZ >= 1 ) if( raw.sizeZ >= 1 )
{ {
tempShort = reinterpret_cast<unsigned short*>(ptr); tempShort = reinterpret_cast<unsigned short*>(ptr);
*tempShort = *(reinterpret_cast<unsigned short*>(raw->tmpR) + j); *tempShort = *(reinterpret_cast<unsigned short*>(raw.tmpR) + j);
tempShort++; tempShort++;
ptr = reinterpret_cast<unsigned char *>(tempShort); ptr = reinterpret_cast<unsigned char *>(tempShort);
} }
if( raw->sizeZ >= 2 ) if( raw.sizeZ >= 2 )
{ {
tempShort = reinterpret_cast<unsigned short*>(ptr); tempShort = reinterpret_cast<unsigned short*>(ptr);
*tempShort = *(reinterpret_cast<unsigned short*>(raw->tmpG) + j); *tempShort = *(reinterpret_cast<unsigned short*>(raw.tmpG) + j);
tempShort++; tempShort++;
ptr = reinterpret_cast<unsigned char *>(tempShort); ptr = reinterpret_cast<unsigned char *>(tempShort);
} }
if( raw->sizeZ >= 3 ) if( raw.sizeZ >= 3 )
{ {
tempShort = reinterpret_cast<unsigned short*>(ptr); tempShort = reinterpret_cast<unsigned short*>(ptr);
*tempShort = *(reinterpret_cast<unsigned short*>(raw->tmpB) + j); *tempShort = *(reinterpret_cast<unsigned short*>(raw.tmpB) + j);
tempShort++; tempShort++;
ptr = reinterpret_cast<unsigned char *>(tempShort); ptr = reinterpret_cast<unsigned char *>(tempShort);
} }
if( raw->sizeZ >= 4 ) if( raw.sizeZ >= 4 )
{ {
tempShort = reinterpret_cast<unsigned short*>(ptr); tempShort = reinterpret_cast<unsigned short*>(ptr);
*tempShort = *(reinterpret_cast<unsigned short*>(raw->tmpA) + j); *tempShort = *(reinterpret_cast<unsigned short*>(raw.tmpA) + j);
tempShort++; tempShort++;
ptr = reinterpret_cast<unsigned char *>(tempShort); ptr = reinterpret_cast<unsigned char *>(tempShort);
} }
@ -489,7 +494,7 @@ class ReaderWriterRGB : public osgDB::ReaderWriter
ReadResult readRGBStream(std::istream& fin) const ReadResult readRGBStream(std::istream& fin) const
{ {
osg::ref_ptr<rawImageRec> raw; osg::ref_ptr<refImageRec> raw;
if( (raw = RawImageOpen(fin)) == NULL ) if( (raw = RawImageOpen(fin)) == NULL )
{ {
@ -512,7 +517,7 @@ class ReaderWriterRGB : public osgDB::ReaderWriter
GL_UNSIGNED_SHORT; GL_UNSIGNED_SHORT;
unsigned char *data; unsigned char *data;
RawImageGetData(raw.get(), &data); RawImageGetData(*raw, &data);
Image* image = new Image(); Image* image = new Image();
image->setImage(s,t,r, image->setImage(s,t,r,