From 41a7abcdbb99a0ed48167d6ae1bdd877bfdf9524 Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Tue, 11 Mar 2014 18:02:22 -0400 Subject: [PATCH] Let the image loader return the actual image pointer instead of having the data be constructed --- IO/config/ImageLoader.h.in | 3 ++- IO/src/ImageFile.cpp | 19 ++----------------- IO/src/ImageLoader.cpp | 22 +++++++++++++--------- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/IO/config/ImageLoader.h.in b/IO/config/ImageLoader.h.in index b65d23b..dd06cc1 100644 --- a/IO/config/ImageLoader.h.in +++ b/IO/config/ImageLoader.h.in @@ -46,6 +46,7 @@ #include "ImageFileFormat.h" #include "TexCompTypes.h" +#include "ImageFwd.h" class ImageLoader { @@ -142,7 +143,7 @@ class ImageLoader { uint32 GetHeight() const { return m_Height; } uint32 GetImageDataSz() const { return m_Width * m_Height * 4; } - bool LoadImage(); + virtual FasTC::Image<> *LoadImage(); const uint8 *GetImageData() const { return m_PixelData; } }; diff --git a/IO/src/ImageFile.cpp b/IO/src/ImageFile.cpp index 18d7602..60e4479 100644 --- a/IO/src/ImageFile.cpp +++ b/IO/src/ImageFile.cpp @@ -221,28 +221,13 @@ FasTC::Image<> *ImageFile::LoadImage() const { if(!loader) return NULL; - if(!(loader->LoadImage())) { + FasTC::Image<> *i = loader->LoadImage(); + if(i == NULL) { fprintf(stderr, "Unable to load image!\n"); - delete loader; - return NULL; } - uint8 *pixelData = NULL; - if(loader->GetImageData()) { - pixelData = new uint8[ loader->GetImageDataSz() ]; - if(!pixelData) { fprintf(stderr, "%s\n", "Out of memory!"); exit(1); } - memcpy(pixelData, loader->GetImageData(), loader->GetImageDataSz()); - } - else { - fprintf(stderr, "%s\n", "Failed to get data from image loader!"); - } - - uint32 *pixels = reinterpret_cast(pixelData); - FasTC::Image<> *i = new FasTC::Image<>(loader->GetWidth(), loader->GetHeight(), pixels); - // Cleanup delete loader; - delete [] pixelData; return i; } diff --git a/IO/src/ImageLoader.cpp b/IO/src/ImageLoader.cpp index b65bb94..1bed8e5 100644 --- a/IO/src/ImageLoader.cpp +++ b/IO/src/ImageLoader.cpp @@ -48,6 +48,7 @@ #include #include +#include "Image.h" /////////////////////////////////////////////////////////////////////////////// // // Static helper functions @@ -166,15 +167,17 @@ bool ImageLoader::LoadFromPixelBuffer(const uint32 *data, bool flipY) { return true; } -bool ImageLoader::LoadImage() { +FasTC::Image<> *ImageLoader::LoadImage() { // Do we already have pixel data? - if(m_PixelData) - return true; + if(m_PixelData) { + uint32 *pixels = reinterpret_cast(m_PixelData); + return new FasTC::Image<>(m_Width, m_Height, pixels); + } // Read the image data! if(!ReadData()) - return false; + return NULL; m_Width = GetWidth(); m_Height = GetHeight(); @@ -201,7 +204,7 @@ bool ImageLoader::LoadImage() { unsigned int redVal = GetChannelForPixel(i, j, 0); if(redVal == INT_MAX) - return false; + return NULL; unsigned int greenVal = redVal; unsigned int blueVal = redVal; @@ -209,20 +212,20 @@ bool ImageLoader::LoadImage() { if(GetGreenChannelPrecision() > 0) { greenVal = GetChannelForPixel(i, j, 1); if(greenVal == INT_MAX) - return false; + return NULL; } if(GetBlueChannelPrecision() > 0) { blueVal = GetChannelForPixel(i, j, 2); if(blueVal == INT_MAX) - return false; + return NULL; } unsigned int alphaVal = 0xFF; if(GetAlphaChannelPrecision() > 0) { alphaVal = GetChannelForPixel(i, j, 3); if(alphaVal == INT_MAX) - return false; + return NULL; } // Red channel @@ -239,5 +242,6 @@ bool ImageLoader::LoadImage() { } } - return true; + uint32 *pixels = reinterpret_cast(m_PixelData); + return new FasTC::Image<>(m_Width, m_Height, pixels); }