Simplified and clarified code for monochrome bit/byte computations

This commit is contained in:
Corentin Schreiber 2020-08-21 19:43:45 +01:00 committed by Lukas Dürrenberger
parent 9cb67520c7
commit a2fde65113

View File

@ -104,18 +104,18 @@ bool CursorImpl::loadFromPixelsMonochrome(const Uint8* pixels, Vector2u size, Ve
// The bit data is stored packed into bytes. If the number of pixels on each row of the image // The bit data is stored packed into bytes. If the number of pixels on each row of the image
// does not fit exactly into (width/8) bytes, one extra byte is allocated at the end of each // does not fit exactly into (width/8) bytes, one extra byte is allocated at the end of each
// row to store the extra pixels. // row to store the extra pixels.
std::size_t bytes = (size.x + 7) / 8 * size.y; std::size_t packedWidth = (size.x + 7) / 8;
std::vector<Uint8> mask(bytes, 0); // Defines which pixel is transparent. std::size_t bytes = packedWidth * size.y;
std::vector<Uint8> data(bytes, 1); // Defines which pixel is white/black. std::vector<Uint8> mask(bytes, 0); // Defines which pixel is opaque (1) or transparent (0).
std::vector<Uint8> data(bytes, 1); // Defines which pixel is white (1) or black (0).
for (std::size_t j = 0; j < size.y; ++j) for (std::size_t j = 0; j < size.y; ++j)
{ {
for (std::size_t i = 0; i < size.x; ++i) for (std::size_t i = 0; i < size.x; ++i)
{ {
std::size_t pixelIndex = i + j * size.x; std::size_t pixelIndex = i + j * size.x;
std::size_t pixelIndexMask = i + j * (size.x + 7); std::size_t byteIndex = i / 8 + j * packedWidth;
std::size_t byteIndex = pixelIndexMask / 8; std::size_t bitIndex = i % 8;
std::size_t bitIndex = pixelIndexMask % 8;
// Turn on pixel that are not transparent // Turn on pixel that are not transparent
Uint8 opacity = pixels[pixelIndex * 4 + 3] > 0 ? 1 : 0; Uint8 opacity = pixels[pixelIndex * 4 + 3] > 0 ? 1 : 0;