diff --git a/IO/config/ImageLoader.h.in b/IO/config/ImageLoader.h.in index fdc4402..20c102f 100644 --- a/IO/config/ImageLoader.h.in +++ b/IO/config/ImageLoader.h.in @@ -83,6 +83,8 @@ class ImageLoader { const uint8 *GetImageData() const { return m_PixelData; } }; +#ifndef PNG_FOUND #cmakedefine PNG_FOUND +#endif // PNG_FOUND #endif // _IMAGE_LOADER_H_ diff --git a/IO/config/ImageWriter.h.in b/IO/config/ImageWriter.h.in index f0e9102..7253442 100644 --- a/IO/config/ImageWriter.h.in +++ b/IO/config/ImageWriter.h.in @@ -35,11 +35,13 @@ class ImageWriter { uint32 GetWidth() const { return m_Width; } uint32 GetHeight() const { return m_Height; } uint32 GetImageDataSz() const { return m_Width * m_Height * 4; } - uint32 GetRawImageDataSz() const { return m_RawFileDataSz; } - uint8 *GetRawImageData() const { return m_RawFileData; } + uint32 GetRawFileDataSz() const { return m_RawFileDataSz; } + uint8 *GetRawFileData() const { return m_RawFileData; } virtual bool WriteImage() = 0; }; +#ifndef PNG_FOUND #cmakedefine PNG_FOUND +#endif // PNG_FOUND #endif // _IMAGE_LOADER_H_ diff --git a/IO/include/ImageFile.h b/IO/include/ImageFile.h index 01ef7d0..94d3113 100644 --- a/IO/include/ImageFile.h +++ b/IO/include/ImageFile.h @@ -39,6 +39,7 @@ public: const EImageFileFormat m_FileFormat; static unsigned char *ReadFileData(const CHAR *filename); + static bool WriteImageDataToFile(const uint8 *data, const uint32 dataSz, const CHAR *filename); static EImageFileFormat DetectFileFormat(const CHAR *filename); Image *LoadImage(const unsigned char *rawImageData) const; diff --git a/IO/src/ImageFile.cpp b/IO/src/ImageFile.cpp index 6f07ab2..6451bfc 100644 --- a/IO/src/ImageFile.cpp +++ b/IO/src/ImageFile.cpp @@ -6,6 +6,7 @@ #include #include +#include "ImageWriter.h" #include "ImageLoader.h" #include "CompressedImage.h" #include "Image.h" @@ -13,6 +14,7 @@ #ifdef PNG_FOUND # include "ImageLoaderPNG.h" +# include "ImageWriterPNG.h" #endif ////////////////////////////////////////////////////////////////////////////////////////// @@ -84,6 +86,39 @@ bool ImageFile::Load() { return m_Image != NULL; } + +bool ImageFile::Write() { + + ImageWriter *writer = NULL; + switch(m_FileFormat) { + +#ifdef PNG_FOUND + case eFileFormat_PNG: + { + writer = new ImageWriterPNG(*m_Image); + } + break; +#endif // PNG_FOUND + + default: + fprintf(stderr, "Unable to write image: unknown file format.\n"); + return false; + } + + if(NULL == writer) + return false; + + if(!writer->WriteImage()) { + delete writer; + return false; + } + + WriteImageDataToFile(writer->GetRawFileData(), writer->GetRawFileDataSz(), m_Filename); + + delete writer; + return true; +} + Image *ImageFile::LoadImage(const unsigned char *rawImageData) const { ImageLoader *loader = NULL; @@ -175,3 +210,16 @@ unsigned char *ImageFile::ReadFileData(const CHAR *filename) { return rawData; } +bool ImageFile::WriteImageDataToFile(const uint8 *data, const uint32 dataSz, const CHAR *filename) { + + // Open a file stream and write out the data... + FileStream fstr (filename, eFileMode_WriteBinary); + if(fstr.Tell() < 0) { + fprintf(stderr, "Error opening file for reading: %s\n", filename); + return 0; + } + + fstr.Write(data, dataSz); + fstr.Flush(); + return true; +} diff --git a/IO/src/ImageWriterPNG.h b/IO/src/ImageWriterPNG.h index 2167c00..c5dda26 100644 --- a/IO/src/ImageWriterPNG.h +++ b/IO/src/ImageWriterPNG.h @@ -8,7 +8,7 @@ class Image; class ImageWriterPNG : public ImageWriter { public: ImageWriterPNG(const Image &); - virtual ~ImageWriterPNG(); + virtual ~ImageWriterPNG() { } virtual bool WriteImage(); private: