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]);
const Image *img = file.GetImage();
if(NULL == img) {
if(!file.Load()) {
fprintf(stderr, "Error loading file: %s\n", argv[fileArg]);
return 1;
}

View file

@ -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();

View file

@ -11,6 +11,7 @@ class ImageLoader;
class Image {
public:
Image(const CompressedImage &);
Image(const ImageLoader &);
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;
}
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())

View file

@ -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;

View file

@ -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;