From Alberto Farre, fixes to the computation of sizes

This commit is contained in:
Robert Osfield
2004-05-24 19:50:13 +00:00
parent ab2b817e61
commit eac7fdff76

View File

@@ -164,45 +164,36 @@ typedef struct _DDSURFACEDESC2
#define FOURCC_DXT5 (MAKEFOURCC('D','X','T','5')) #define FOURCC_DXT5 (MAKEFOURCC('D','X','T','5'))
osg::Image* ReadDDSFile(const char *filename) osg::Image* ReadDDSFile(std::istream& _istream)
{ {
DDSURFACEDESC2 ddsd; DDSURFACEDESC2 ddsd;
char filecode[4]; char filecode[4];
FILE *fp;
// Open file. _istream.read(filecode, 4);
fp = fopen(filename, "rb");
if (fp == NULL)
return NULL;
// Verify that this is a DDS file.
fread(filecode, 1, 4, fp);
if (strncmp(filecode, "DDS ", 4) != 0) { if (strncmp(filecode, "DDS ", 4) != 0) {
fclose(fp);
return NULL; return NULL;
} }
// Get the surface desc. // Get the surface desc.
fread(&ddsd, sizeof(ddsd), 1, fp); _istream.read((char*)(&ddsd), sizeof(ddsd));
// Size of 2d images - 3d images don't set dwLinearSize // Size of 2d images - 3d images don't set dwLinearSize
unsigned int size = ddsd.dwMipMapCount > 1 ? ddsd.dwLinearSize * (ddsd.ddpfPixelFormat.dwFourCC==FOURCC_DXT1 ? 2: 4) : ddsd.dwLinearSize; //###[afarre_051904]
/*unsigned int size = ddsd.dwMipMapCount > 1 ? ddsd.dwLinearSize * (ddsd.ddpfPixelFormat.dwFourCC==FOURCC_DXT1 ? 2: 4) : ddsd.dwLinearSize;
if(size <= 0) if(size <= 0)
{ {
osg::notify(osg::WARN)<<"Warning:: dwLinearSize is not defined in dds file, image not loaded."<<std::endl; osg::notify(osg::WARN)<<"Warning:: dwLinearSize is not defined in dds file, image not loaded."<<std::endl;
return NULL; return NULL;
} }*/
osg::ref_ptr<osg::Image> osgImage = new osg::Image(); osg::ref_ptr<osg::Image> osgImage = new osg::Image();
osgImage->setFileName(filename);
//Check valid structure sizes //Check valid structure sizes
if(ddsd.dwSize != 124 && ddsd.ddpfPixelFormat.dwSize != 32) if(ddsd.dwSize != 124 && ddsd.ddpfPixelFormat.dwSize != 32)
{ {
fclose(fp);
return NULL; return NULL;
} }
@@ -287,28 +278,37 @@ osg::Image* ReadDDSFile(const char *filename)
return NULL; return NULL;
} }
if (is3dImage) //###[afarre_051904]
/*if (is3dImage)
size = osg::Image::computeNumComponents(pixelFormat) * ddsd.dwWidth * ddsd.dwHeight * depth; size = osg::Image::computeNumComponents(pixelFormat) * ddsd.dwWidth * ddsd.dwHeight * depth;
//delayed allocation og image data after all checks //delayed allocation og image data after all checks
unsigned char* imageData = new unsigned char [size]; unsigned char* imageData = new unsigned char [size];
if(!imageData)
{
return NULL;
}
// Read image data // Read image data
int bytesRead = fread(imageData, 1, size, fp); _istream.read((char*)imageData, size);
// Close the file.
fclose(fp);
// NOTE: We need to set the image data before setting the mipmap data, this
// NOTE: We need to set the image data before setting the mipmap data, this
// is because the setImage method clears the _mipmapdata vector in osg::Image. // is because the setImage method clears the _mipmapdata vector in osg::Image.
// Set image data and properties. // Set image data and properties.
osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, imageData, osg::Image::USE_NEW_DELETE); osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, imageData, osg::Image::USE_NEW_DELETE);
*/
// Now set mipmap data (offsets into image raw data)
//###[afarre_051904]
osg::Image::MipmapDataType mipmaps;
// Take care of mipmaps if any. // Take care of mipmaps if any.
if (ddsd.dwMipMapCount>1) if (ddsd.dwMipMapCount>1)
{ {
// Now set mipmap data (offsets into image raw data). // Now set mipmap data (offsets into image raw data).
osg::Image::MipmapDataType mipmaps; //###[afarre_051904]osg::Image::MipmapDataType mipmaps;
//This is to complete mipmap sequence until level Nx1 //This is to complete mipmap sequence until level Nx1
@@ -337,13 +337,12 @@ osg::Image* ReadDDSFile(const char *filename)
width = 1; width = 1;
if (height == 0) if (height == 0)
height = 1; height = 1;
size = ((width+3)/4) * ((height+3)/4) * blockSize; offset += (((width+3)/4) * ((height+3)/4) * blockSize);
offset += size;
mipmaps[k-1] = offset; mipmaps[k-1] = offset;
width >>= 1; width >>= 1;
height >>= 1; height >>= 1;
} }
osgImage->setMipmapData(mipmaps); //###[afarre_051904] osgImage->setMipmapData(mipmaps);
} }
// Handle uncompressed mipmaps // Handle uncompressed mipmaps
if(ddsd.ddpfPixelFormat.dwFlags & DDPF_RGB) if(ddsd.ddpfPixelFormat.dwFlags & DDPF_RGB)
@@ -357,20 +356,75 @@ osg::Image* ReadDDSFile(const char *filename)
width = 1; width = 1;
if (height == 0) if (height == 0)
height = 1; height = 1;
size = width*height*(ddsd.ddpfPixelFormat.dwRGBBitCount/8); offset += (width*height*(ddsd.ddpfPixelFormat.dwRGBBitCount/8));
offset += size;
mipmaps[k-1] = offset; mipmaps[k-1] = offset;
width >>= 1; width >>= 1;
height >>= 1; height >>= 1;
} }
osgImage->setMipmapData(mipmaps); //###[afarre_051904] osgImage->setMipmapData(mipmaps);
} }
} }
// Return Image.
//###[afarre_051904]
unsigned int size = 0;
if (is3dImage)
{
size = osg::Image::computeNumComponents(pixelFormat) * ddsd.dwWidth * ddsd.dwHeight * depth;
}
else
{
osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, 0, osg::Image::USE_NEW_DELETE);
if (mipmaps.size()>0) osgImage->setMipmapData(mipmaps);
size = osgImage->getTotalSizeInBytesIncludingMipmaps();
}
if(size <= 0)
{
return NULL;
}
unsigned char* imageData = new unsigned char [size];
if(!imageData)
{
return NULL;
}
// Read image data
_istream.read((char*)imageData, size);
osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, imageData, osg::Image::USE_NEW_DELETE);
if (mipmaps.size()>0) osgImage->setMipmapData(mipmaps);
// Return Image.
return osgImage.release(); return osgImage.release();
} }
/*
osg::Image::MipmapDataType mipmaps;
osgImage->setMipmapData(mipmaps);
osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, 0, osg::Image::USE_NEW_DELETE);
printf("INVENTO===> gtsibim:%d grsib:%d mi_size:%d lPitch%d\n",
osgImage->getTotalSizeInBytesIncludingMipmaps(),
osgImage->getRowSizeInBytes(), size, ddsd.lPitch);
printf("CORRECTO**> gtsibim:%d grsib:%d mi_size:%d lPitch%d\n",
osgImage->getTotalSizeInBytesIncludingMipmaps(),
osgImage->getRowSizeInBytes(), size, ddsd.lPitch);
*/
bool WriteDDSFile(const osg::Image *img, const char *filename) bool WriteDDSFile(const osg::Image *img, const char *filename)
{ {
@@ -614,7 +668,7 @@ public:
return osgDB::equalCaseInsensitive(extension,"dds"); return osgDB::equalCaseInsensitive(extension,"dds");
} }
virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options*) virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options* options)
{ {
std::string ext = osgDB::getLowerCaseFileExtension(file); std::string ext = osgDB::getLowerCaseFileExtension(file);
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
@@ -622,11 +676,19 @@ public:
std::string fileName = osgDB::findDataFile( file ); std::string fileName = osgDB::findDataFile( file );
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
osg::Image* osgImage = ReadDDSFile(fileName.c_str()); std::ifstream stream(fileName.c_str(), std::ios::in | std::ios::binary);
if(!stream) return ReadResult::FILE_NOT_HANDLED;
ReadResult rr = readImage(stream, options);
if(rr.validImage()) rr.getImage()->setFileName(file);
return rr;
}
virtual ReadResult readImage(std::istream& fin, const Options* options)
{
osg::Image* osgImage = ReadDDSFile(fin);
if (osgImage==NULL) return ReadResult::FILE_NOT_HANDLED; if (osgImage==NULL) return ReadResult::FILE_NOT_HANDLED;
return osgImage; return osgImage;
}
}
virtual WriteResult writeImage(const osg::Image &img,const std::string& file, const osgDB::ReaderWriter::Options*) virtual WriteResult writeImage(const osg::Image &img,const std::string& file, const osgDB::ReaderWriter::Options*)
{ {