Add new structure to support creating Images and ImageFiles from compressed images

This commit is contained in:
Pavel Krajcevski 2012-10-20 16:54:43 -04:00
parent d024ce73fb
commit 545a6f68e2
6 changed files with 47 additions and 13 deletions

View file

@ -110,8 +110,7 @@ int main(int argc, char **argv) {
} }
ImageFile file (argv[fileArg]); ImageFile file (argv[fileArg]);
const Image *img = file.GetImage(); if(!file.Load()) {
if(NULL == img) {
fprintf(stderr, "Error loading file: %s\n", argv[fileArg]); fprintf(stderr, "Error loading file: %s\n", argv[fileArg]);
return 1; return 1;
} }

View file

@ -28,6 +28,9 @@ class CompressedImage {
const unsigned char *data const unsigned char *data
); );
unsigned int GetHeight() const { return m_Height; }
unsigned int GetWidth() const { return m_Width; }
CompressedImage( const CompressedImage &other ); CompressedImage( const CompressedImage &other );
~CompressedImage(); ~CompressedImage();

View file

@ -11,6 +11,7 @@ class ImageLoader;
class Image { class Image {
public: public:
Image(const CompressedImage &);
Image(const ImageLoader &); Image(const ImageLoader &);
const uint8 *RawData() const { return m_PixelData; } const uint8 *RawData() const { return m_PixelData; }

View file

@ -12,6 +12,17 @@ static inline T sad( const T &a, const T &b ) {
return (a > b)? a - b : b - a; 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) Image::Image(const ImageLoader &loader)
: m_PixelData(0) : m_PixelData(0)
, m_Width(loader.GetWidth()) , m_Width(loader.GetWidth())

View file

@ -16,6 +16,7 @@ public:
ImageFile(const char *filename); ImageFile(const char *filename);
ImageFile(const char *filename, EImageFileFormat format); ImageFile(const char *filename, EImageFileFormat format);
ImageFile(const char *filename, EImageFileFormat format, const Image &);
~ImageFile(); ~ImageFile();
unsigned int GetWidth() const { return m_Width; } unsigned int GetWidth() const { return m_Width; }
@ -23,7 +24,12 @@ public:
CompressedImage *Compress(const SCompressionSettings &) const; CompressedImage *Compress(const SCompressionSettings &) const;
Image *GetImage() const { return m_Image; } Image *GetImage() const { return m_Image; }
bool Load();
bool Write();
private: private:
static const unsigned int kMaxFilenameSz = 256;
char m_Filename[kMaxFilenameSz];
unsigned int m_Handle; unsigned int m_Handle;
unsigned int m_Width; unsigned int m_Width;
unsigned int m_Height; unsigned int m_Height;

View file

@ -45,22 +45,21 @@ ImageFile::ImageFile(const CHAR *filename)
: m_FileFormat( DetectFileFormat(filename) ) : m_FileFormat( DetectFileFormat(filename) )
, m_Image(NULL) , m_Image(NULL)
{ {
unsigned char *rawData = ReadFileData(filename); strncpy(m_Filename, filename, kMaxFilenameSz);
if(rawData) {
m_Image = LoadImage(rawData);
delete [] rawData;
}
} }
ImageFile::ImageFile(const CHAR *filename, EImageFileFormat format) ImageFile::ImageFile(const CHAR *filename, EImageFileFormat format)
: m_FileFormat(format) : m_FileFormat(format)
, m_Image(NULL) , m_Image(NULL)
{ {
unsigned char *rawData = ReadFileData(filename); strncpy(m_Filename, filename, kMaxFilenameSz);
if(rawData) { }
m_Image = LoadImage(rawData);
delete [] rawData; ImageFile::ImageFile(const char *filename, EImageFileFormat format, const Image &image)
} : m_FileFormat(format)
, m_Image(new Image(image))
{
strncpy(m_Filename, filename, kMaxFilenameSz);
} }
ImageFile::~ImageFile() { 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 { Image *ImageFile::LoadImage(const unsigned char *rawImageData) const {
ImageLoader *loader = NULL; ImageLoader *loader = NULL;