Hook up image writer to ImageFile

This commit is contained in:
Pavel Krajcevski 2012-10-20 19:01:07 -04:00
parent 0dbf5a08cc
commit e39b23d18c
5 changed files with 56 additions and 3 deletions

View file

@ -83,6 +83,8 @@ class ImageLoader {
const uint8 *GetImageData() const { return m_PixelData; } const uint8 *GetImageData() const { return m_PixelData; }
}; };
#ifndef PNG_FOUND
#cmakedefine PNG_FOUND #cmakedefine PNG_FOUND
#endif // PNG_FOUND
#endif // _IMAGE_LOADER_H_ #endif // _IMAGE_LOADER_H_

View file

@ -35,11 +35,13 @@ class ImageWriter {
uint32 GetWidth() const { return m_Width; } uint32 GetWidth() const { return m_Width; }
uint32 GetHeight() const { return m_Height; } uint32 GetHeight() const { return m_Height; }
uint32 GetImageDataSz() const { return m_Width * m_Height * 4; } uint32 GetImageDataSz() const { return m_Width * m_Height * 4; }
uint32 GetRawImageDataSz() const { return m_RawFileDataSz; } uint32 GetRawFileDataSz() const { return m_RawFileDataSz; }
uint8 *GetRawImageData() const { return m_RawFileData; } uint8 *GetRawFileData() const { return m_RawFileData; }
virtual bool WriteImage() = 0; virtual bool WriteImage() = 0;
}; };
#ifndef PNG_FOUND
#cmakedefine PNG_FOUND #cmakedefine PNG_FOUND
#endif // PNG_FOUND
#endif // _IMAGE_LOADER_H_ #endif // _IMAGE_LOADER_H_

View file

@ -39,6 +39,7 @@ public:
const EImageFileFormat m_FileFormat; const EImageFileFormat m_FileFormat;
static unsigned char *ReadFileData(const CHAR *filename); 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); static EImageFileFormat DetectFileFormat(const CHAR *filename);
Image *LoadImage(const unsigned char *rawImageData) const; Image *LoadImage(const unsigned char *rawImageData) const;

View file

@ -6,6 +6,7 @@
#include <limits.h> #include <limits.h>
#include <assert.h> #include <assert.h>
#include "ImageWriter.h"
#include "ImageLoader.h" #include "ImageLoader.h"
#include "CompressedImage.h" #include "CompressedImage.h"
#include "Image.h" #include "Image.h"
@ -13,6 +14,7 @@
#ifdef PNG_FOUND #ifdef PNG_FOUND
# include "ImageLoaderPNG.h" # include "ImageLoaderPNG.h"
# include "ImageWriterPNG.h"
#endif #endif
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
@ -84,6 +86,39 @@ bool ImageFile::Load() {
return m_Image != NULL; 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 { Image *ImageFile::LoadImage(const unsigned char *rawImageData) const {
ImageLoader *loader = NULL; ImageLoader *loader = NULL;
@ -175,3 +210,16 @@ unsigned char *ImageFile::ReadFileData(const CHAR *filename) {
return rawData; 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;
}

View file

@ -8,7 +8,7 @@ class Image;
class ImageWriterPNG : public ImageWriter { class ImageWriterPNG : public ImageWriter {
public: public:
ImageWriterPNG(const Image &); ImageWriterPNG(const Image &);
virtual ~ImageWriterPNG(); virtual ~ImageWriterPNG() { }
virtual bool WriteImage(); virtual bool WriteImage();
private: private: