diff --git a/CLTool/src/clunix.cpp b/CLTool/src/clunix.cpp index 064857b..4869398 100644 --- a/CLTool/src/clunix.cpp +++ b/CLTool/src/clunix.cpp @@ -110,8 +110,7 @@ int main(int argc, char **argv) { } ImageFile file (argv[fileArg]); - const Image *img = file.GetImage(); - if(NULL == img) { + if(!file.Load()) { fprintf(stderr, "Error loading file: %s\n", argv[fileArg]); return 1; } diff --git a/Core/include/CompressedImage.h b/Core/include/CompressedImage.h index b0246ad..68b7feb 100644 --- a/Core/include/CompressedImage.h +++ b/Core/include/CompressedImage.h @@ -28,6 +28,9 @@ class CompressedImage { const unsigned char *data ); + unsigned int GetHeight() const { return m_Height; } + unsigned int GetWidth() const { return m_Width; } + CompressedImage( const CompressedImage &other ); ~CompressedImage(); diff --git a/Core/include/Image.h b/Core/include/Image.h index 8aaaef3..1afe510 100644 --- a/Core/include/Image.h +++ b/Core/include/Image.h @@ -11,6 +11,7 @@ class ImageLoader; class Image { public: + Image(const CompressedImage &); Image(const ImageLoader &); const uint8 *RawData() const { return m_PixelData; } diff --git a/Core/src/Image.cpp b/Core/src/Image.cpp index 27c2382..e876a7a 100644 --- a/Core/src/Image.cpp +++ b/Core/src/Image.cpp @@ -12,6 +12,17 @@ static inline T sad( const T &a, const T &b ) { return (a > b)? a - b : b - a; } +Image::Image(const CompressedImage &ci) { + unsigned int bufSz = ci.GetWidth() * ci.GetHeight() * 4; + m_PixelData = new uint8[ bufSz ]; + if(!m_PixelData) { fprintf(stderr, "%s\n", "Out of memory!"); return; } + + if(!ci.DecompressImage(m_PixelData, bufSz)) { + fprintf(stderr, "Error decompressing image!\n"); + return; + } +} + Image::Image(const ImageLoader &loader) : m_PixelData(0) , m_Width(loader.GetWidth()) diff --git a/IO/include/ImageFile.h b/IO/include/ImageFile.h index 485f93e..01ef7d0 100644 --- a/IO/include/ImageFile.h +++ b/IO/include/ImageFile.h @@ -16,6 +16,7 @@ public: ImageFile(const char *filename); ImageFile(const char *filename, EImageFileFormat format); + ImageFile(const char *filename, EImageFileFormat format, const Image &); ~ImageFile(); unsigned int GetWidth() const { return m_Width; } @@ -23,7 +24,12 @@ public: CompressedImage *Compress(const SCompressionSettings &) const; Image *GetImage() const { return m_Image; } + bool Load(); + bool Write(); + private: + static const unsigned int kMaxFilenameSz = 256; + char m_Filename[kMaxFilenameSz]; unsigned int m_Handle; unsigned int m_Width; unsigned int m_Height; diff --git a/IO/src/ImageFile.cpp b/IO/src/ImageFile.cpp index d286991..6f07ab2 100644 --- a/IO/src/ImageFile.cpp +++ b/IO/src/ImageFile.cpp @@ -44,23 +44,22 @@ static inline T min(const T &a, const T &b) { ImageFile::ImageFile(const CHAR *filename) : m_FileFormat( DetectFileFormat(filename) ) , m_Image(NULL) -{ - unsigned char *rawData = ReadFileData(filename); - if(rawData) { - m_Image = LoadImage(rawData); - delete [] rawData; - } +{ + strncpy(m_Filename, filename, kMaxFilenameSz); } ImageFile::ImageFile(const CHAR *filename, EImageFileFormat format) : m_FileFormat(format) , m_Image(NULL) +{ + strncpy(m_Filename, filename, kMaxFilenameSz); +} + +ImageFile::ImageFile(const char *filename, EImageFileFormat format, const Image &image) + : m_FileFormat(format) + , m_Image(new Image(image)) { - unsigned char *rawData = ReadFileData(filename); - if(rawData) { - m_Image = LoadImage(rawData); - delete [] rawData; - } + strncpy(m_Filename, filename, kMaxFilenameSz); } ImageFile::~ImageFile() { @@ -70,6 +69,21 @@ ImageFile::~ImageFile() { } } +bool ImageFile::Load() { + + if(m_Image) { + delete m_Image; + m_Image = NULL; + } + + unsigned char *rawData = ReadFileData(m_Filename); + if(rawData) { + m_Image = LoadImage(rawData); + delete [] rawData; + } + + return m_Image != NULL; +} Image *ImageFile::LoadImage(const unsigned char *rawImageData) const { ImageLoader *loader = NULL;