Make sure that we assume pixels are in block stream order when accessing.

This commit is contained in:
Pavel Krajcevski 2012-10-20 21:33:23 -04:00
parent 1424765866
commit 3bc9510996

View file

@ -1,7 +1,24 @@
#include "ImageWriter.h"
uint32 ImageWriter::GetChannelForPixel(uint32 x, uint32 y, uint32 ch) {
uint32 bytesPerRow = GetWidth() * 4;
uint32 byteLocation = y * bytesPerRow + x*4 + ch;
return m_PixelData[byteLocation];
// Assume pixels are in block stream order, hence we would need to first find
// the block that contains pixel (x, y) and then find the byte location for it.
const uint32 blocksPerRow = GetWidth() / 4;
const uint32 blockIdxX = x / 4;
const uint32 blockIdxY = y / 4;
const uint32 blockIdx = blockIdxY * blocksPerRow + blockIdxX;
// Now we find the offset in the block
const uint32 blockOffsetX = x % 4;
const uint32 blockOffsetY = y % 4;
const uint32 pixelOffset = blockOffsetY * 4 + blockOffsetX;
// There are 16 pixels per block and bytes per pixel...
uint32 dataOffset = blockIdx * 4 * 16;
dataOffset += 4 * pixelOffset;
dataOffset += ch;
return m_PixelData[dataOffset];
}