Hold on to the raw file data once its been loaded.

This commit is contained in:
Pavel Krajcevski 2013-11-19 13:55:21 -05:00
parent 56259e2861
commit eeb4a995fc
2 changed files with 32 additions and 15 deletions

View file

@ -91,12 +91,15 @@ public:
const EImageFileFormat m_FileFormat; const EImageFileFormat m_FileFormat;
uint8 *m_FileData;
int32 m_FileDataSz;
FasTC::Image<> *m_Image; FasTC::Image<> *m_Image;
static unsigned char *ReadFileData(const CHAR *filename); bool ReadFileData(const CHAR *filename);
static bool WriteImageDataToFile(const uint8 *data, const uint32 dataSz, const CHAR *filename); static bool WriteImageDataToFile(const uint8 *data, const uint32 dataSz, const CHAR *filename);
static EImageFileFormat DetectFileFormat(const CHAR *filename); static EImageFileFormat DetectFileFormat(const CHAR *filename);
FasTC::Image<> *LoadImage(const unsigned char *rawImageData) const; FasTC::Image<> *LoadImage() const;
}; };
#endif // _IMAGE_FILE_H_ #endif // _IMAGE_FILE_H_

View file

@ -93,6 +93,8 @@ static inline T abs(const T &a) {
ImageFile::ImageFile(const CHAR *filename) ImageFile::ImageFile(const CHAR *filename)
: m_FileFormat( DetectFileFormat(filename) ) : m_FileFormat( DetectFileFormat(filename) )
, m_FileData(NULL)
, m_FileDataSz(-1)
, m_Image(NULL) , m_Image(NULL)
{ {
strncpy(m_Filename, filename, kMaxFilenameSz); strncpy(m_Filename, filename, kMaxFilenameSz);
@ -100,6 +102,8 @@ ImageFile::ImageFile(const CHAR *filename)
ImageFile::ImageFile(const CHAR *filename, EImageFileFormat format) ImageFile::ImageFile(const CHAR *filename, EImageFileFormat format)
: m_FileFormat(format) : m_FileFormat(format)
, m_FileData(NULL)
, m_FileDataSz(-1)
, m_Image(NULL) , m_Image(NULL)
{ {
strncpy(m_Filename, filename, kMaxFilenameSz); strncpy(m_Filename, filename, kMaxFilenameSz);
@ -107,6 +111,8 @@ ImageFile::ImageFile(const CHAR *filename, EImageFileFormat format)
ImageFile::ImageFile(const char *filename, EImageFileFormat format, const FasTC::Image<> &image) ImageFile::ImageFile(const char *filename, EImageFileFormat format, const FasTC::Image<> &image)
: m_FileFormat(format) : m_FileFormat(format)
, m_FileData(NULL)
, m_FileDataSz(-1)
, m_Image(image.Clone()) , m_Image(image.Clone())
{ {
strncpy(m_Filename, filename, kMaxFilenameSz); strncpy(m_Filename, filename, kMaxFilenameSz);
@ -117,6 +123,11 @@ ImageFile::~ImageFile() {
delete m_Image; delete m_Image;
m_Image = NULL; m_Image = NULL;
} }
if(m_FileData) {
delete m_FileData;
m_FileData = NULL;
}
} }
bool ImageFile::Load() { bool ImageFile::Load() {
@ -126,10 +137,13 @@ bool ImageFile::Load() {
m_Image = NULL; m_Image = NULL;
} }
unsigned char *rawData = ReadFileData(m_Filename); if(m_FileData) {
if(rawData) { delete m_FileData;
m_Image = LoadImage(rawData); m_FileData = NULL;
delete [] rawData; }
if(ReadFileData(m_Filename)) {
m_Image = LoadImage();
} }
return m_Image != NULL; return m_Image != NULL;
@ -165,20 +179,20 @@ bool ImageFile::Write() {
return true; return true;
} }
FasTC::Image<> *ImageFile::LoadImage(const unsigned char *rawImageData) const { FasTC::Image<> *ImageFile::LoadImage() const {
ImageLoader *loader = NULL; ImageLoader *loader = NULL;
switch(m_FileFormat) { switch(m_FileFormat) {
#ifdef PNG_FOUND #ifdef PNG_FOUND
case eFileFormat_PNG: case eFileFormat_PNG:
loader = new ImageLoaderPNG(rawImageData); loader = new ImageLoaderPNG(m_FileData);
break; break;
#endif // PNG_FOUND #endif // PNG_FOUND
#ifdef PVRTEXLIB_FOUND #ifdef PVRTEXLIB_FOUND
case eFileFormat_PVR: case eFileFormat_PVR:
loader = new ImageLoaderPVR(rawImageData); loader = new ImageLoaderPVR(m_FileData);
break; break;
#endif // PVRTEXLIB_FOUND #endif // PVRTEXLIB_FOUND
@ -246,7 +260,7 @@ EImageFileFormat ImageFile::DetectFileFormat(const CHAR *filename) {
return kNumImageFileFormats; return kNumImageFileFormats;
} }
unsigned char *ImageFile::ReadFileData(const CHAR *filename) { bool ImageFile::ReadFileData(const CHAR *filename) {
FileStream fstr (filename, eFileMode_ReadBinary); FileStream fstr (filename, eFileMode_ReadBinary);
if(fstr.Tell() < 0) { if(fstr.Tell() < 0) {
fprintf(stderr, "Error opening file for reading: %s\n", filename); fprintf(stderr, "Error opening file for reading: %s\n", filename);
@ -258,7 +272,7 @@ unsigned char *ImageFile::ReadFileData(const CHAR *filename) {
uint32 fileSize = fstr.Tell(); uint32 fileSize = fstr.Tell();
// Allocate data for file contents // Allocate data for file contents
unsigned char *rawData = new unsigned char[fileSize]; m_FileData = new unsigned char[fileSize];
// Return stream to beginning of file // Return stream to beginning of file
fstr.Seek(0, FileStream::eSeekPosition_Beginning); fstr.Seek(0, FileStream::eSeekPosition_Beginning);
@ -268,7 +282,7 @@ unsigned char *ImageFile::ReadFileData(const CHAR *filename) {
uint64 totalBytesRead = 0; uint64 totalBytesRead = 0;
uint64 totalBytesLeft = fileSize; uint64 totalBytesLeft = fileSize;
int32 bytesRead; int32 bytesRead;
while((bytesRead = fstr.Read(rawData, uint32(fileSize))) > 0) { while((bytesRead = fstr.Read(m_FileData, uint32(fileSize))) > 0) {
totalBytesRead += bytesRead; totalBytesRead += bytesRead;
totalBytesLeft -= bytesRead; totalBytesLeft -= bytesRead;
} }
@ -276,11 +290,11 @@ unsigned char *ImageFile::ReadFileData(const CHAR *filename) {
if(totalBytesRead != fileSize) { if(totalBytesRead != fileSize) {
assert(!"We didn't read as much data as we thought we had!"); assert(!"We didn't read as much data as we thought we had!");
fprintf(stderr, "Internal error: Incorrect file size assumption\n"); fprintf(stderr, "Internal error: Incorrect file size assumption\n");
return 0; return false;
} }
// Return the data.. m_FileDataSz = fileSize;
return rawData; return true;
} }
bool ImageFile::WriteImageDataToFile(const uint8 *data, bool ImageFile::WriteImageDataToFile(const uint8 *data,