Let the image loader return the actual image pointer instead of having the data be constructed

This commit is contained in:
Pavel Krajcevski 2014-03-11 18:02:22 -04:00
parent 625b202b46
commit 41a7abcdbb
3 changed files with 17 additions and 27 deletions

View File

@ -46,6 +46,7 @@
#include "ImageFileFormat.h" #include "ImageFileFormat.h"
#include "TexCompTypes.h" #include "TexCompTypes.h"
#include "ImageFwd.h"
class ImageLoader { class ImageLoader {
@ -142,7 +143,7 @@ class ImageLoader {
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; }
bool LoadImage(); virtual FasTC::Image<> *LoadImage();
const uint8 *GetImageData() const { return m_PixelData; } const uint8 *GetImageData() const { return m_PixelData; }
}; };

View File

@ -221,28 +221,13 @@ FasTC::Image<> *ImageFile::LoadImage() const {
if(!loader) if(!loader)
return NULL; return NULL;
if(!(loader->LoadImage())) { FasTC::Image<> *i = loader->LoadImage();
if(i == NULL) {
fprintf(stderr, "Unable to load image!\n"); 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<uint32 *>(pixelData);
FasTC::Image<> *i = new FasTC::Image<>(loader->GetWidth(), loader->GetHeight(), pixels);
// Cleanup // Cleanup
delete loader; delete loader;
delete [] pixelData;
return i; return i;
} }

View File

@ -48,6 +48,7 @@
#include <limits.h> #include <limits.h>
#include <assert.h> #include <assert.h>
#include "Image.h"
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// //
// Static helper functions // Static helper functions
@ -166,15 +167,17 @@ bool ImageLoader::LoadFromPixelBuffer(const uint32 *data, bool flipY) {
return true; return true;
} }
bool ImageLoader::LoadImage() { FasTC::Image<> *ImageLoader::LoadImage() {
// Do we already have pixel data? // Do we already have pixel data?
if(m_PixelData) if(m_PixelData) {
return true; uint32 *pixels = reinterpret_cast<uint32 *>(m_PixelData);
return new FasTC::Image<>(m_Width, m_Height, pixels);
}
// Read the image data! // Read the image data!
if(!ReadData()) if(!ReadData())
return false; return NULL;
m_Width = GetWidth(); m_Width = GetWidth();
m_Height = GetHeight(); m_Height = GetHeight();
@ -201,7 +204,7 @@ bool ImageLoader::LoadImage() {
unsigned int redVal = GetChannelForPixel(i, j, 0); unsigned int redVal = GetChannelForPixel(i, j, 0);
if(redVal == INT_MAX) if(redVal == INT_MAX)
return false; return NULL;
unsigned int greenVal = redVal; unsigned int greenVal = redVal;
unsigned int blueVal = redVal; unsigned int blueVal = redVal;
@ -209,20 +212,20 @@ bool ImageLoader::LoadImage() {
if(GetGreenChannelPrecision() > 0) { if(GetGreenChannelPrecision() > 0) {
greenVal = GetChannelForPixel(i, j, 1); greenVal = GetChannelForPixel(i, j, 1);
if(greenVal == INT_MAX) if(greenVal == INT_MAX)
return false; return NULL;
} }
if(GetBlueChannelPrecision() > 0) { if(GetBlueChannelPrecision() > 0) {
blueVal = GetChannelForPixel(i, j, 2); blueVal = GetChannelForPixel(i, j, 2);
if(blueVal == INT_MAX) if(blueVal == INT_MAX)
return false; return NULL;
} }
unsigned int alphaVal = 0xFF; unsigned int alphaVal = 0xFF;
if(GetAlphaChannelPrecision() > 0) { if(GetAlphaChannelPrecision() > 0) {
alphaVal = GetChannelForPixel(i, j, 3); alphaVal = GetChannelForPixel(i, j, 3);
if(alphaVal == INT_MAX) if(alphaVal == INT_MAX)
return false; return NULL;
} }
// Red channel // Red channel
@ -239,5 +242,6 @@ bool ImageLoader::LoadImage() {
} }
} }
return true; uint32 *pixels = reinterpret_cast<uint32 *>(m_PixelData);
return new FasTC::Image<>(m_Width, m_Height, pixels);
} }