Small patch to fix loading of non-multiple-of-four images

This commit is contained in:
Pavel Krajcevski 2014-04-02 13:59:08 -04:00
parent 640b098af7
commit ea1c61913a

View File

@ -75,6 +75,18 @@ void ReportError(const char *str) {
}
unsigned int ImageLoader::GetChannelForPixel(uint32 x, uint32 y, uint32 ch) {
// First make sure that we're in bounds...
if(x >= GetWidth()) {
assert(!"Fix requirement that images have multiple-of-four dimensions");
return 0;
}
if(y >= GetHeight()) {
assert(!"Fix requirement that images have multiple-of-four dimensions");
return 0;
}
uint32 prec;
const uint8 *data;
@ -107,9 +119,6 @@ unsigned int ImageLoader::GetChannelForPixel(uint32 x, uint32 y, uint32 ch) {
if(0 == prec)
return 0;
assert(x < GetWidth());
assert(y < GetHeight());
uint32 pixelIdx = y * GetWidth() + x;
const uint32 val = data[pixelIdx];
@ -182,16 +191,15 @@ FasTC::Image<> *ImageLoader::LoadImage() {
m_Width = GetWidth();
m_Height = GetHeight();
// Create RGBA buffer
const unsigned int dataSz = 4 * m_Width * m_Height;
m_PixelData = new unsigned char[dataSz];
// Populate buffer in block stream order... make
// sure that width and height are aligned to multiples of four.
const unsigned int aw = ((m_Width + 3) >> 2) << 2;
const unsigned int ah = ((m_Height + 3) >> 2) << 2;
// Create RGBA buffer
const unsigned int dataSz = 4 * aw * ah;
m_PixelData = new unsigned char[dataSz];
#ifndef NDEBUG
if(aw != m_Width || ah != m_Height)
fprintf(stderr, "Warning: Image dimension not multiple of four. "
@ -242,6 +250,9 @@ FasTC::Image<> *ImageLoader::LoadImage() {
}
}
m_Width = aw;
m_Height = ah;
uint32 *pixels = reinterpret_cast<uint32 *>(m_PixelData);
return new FasTC::Image<>(m_Width, m_Height, pixels);
}