Fix non-word aligned images

This commit is contained in:
Pk11
2021-08-28 00:40:12 -05:00
parent b53357f753
commit a80633f316
2 changed files with 9 additions and 6 deletions
+1
View File
@@ -34,6 +34,7 @@
class Image {
private:
u32 _width = 0;
u32 _widthAligned = 0;
u32 _height = 0;
u8 _paletteStart = 0;
std::vector<u8> _bitmap = {};
+8 -6
View File
@@ -102,6 +102,8 @@ void Image::load(const u8 *grf, u8 paletteStartLoc) {
case 0x20524448: { // 'HDR '
memcpy(&_width, ptr + 4, 4);
memcpy(&_height, ptr + 5, 4);
_widthAligned = _width % 4 == 0 ? _width : (_width + (4 - (_width % 4)));
break;
} case 0x20584647: { // 'GFX '
_bitmap = std::vector<u8>(ptr[2] >> 8);
@@ -150,7 +152,7 @@ void Image::draw(int x, int y, float scaleX, float scaleY, bool skipAlpha) const
if(scaleX == 1.0f && scaleY == 1.0f) {
if(skipAlpha) {
for(u32 i = 0; i < _height; i++) {
const u8 *src = _bitmap.data() + i * _width;
const u8 *src = _bitmap.data() + i * _widthAligned;
u8 *dst = (u8 *)bgGetGfxPtr(currentScreen ? 3 : 7) + (y + i) * 256 + x;
for(u32 j = 0; j < _width; j++) {
if(_palette[src[j] - _paletteStart] & 0x8000)
@@ -160,14 +162,14 @@ void Image::draw(int x, int y, float scaleX, float scaleY, bool skipAlpha) const
} else {
u8 *dst = (u8 *)bgGetGfxPtr(currentScreen ? 3 : 7) + y * 256 + x;
for(u32 i = 0; i < _height; i++) {
tonccpy(dst + i * 256, _bitmap.data() + i * _width, _width);
tonccpy(dst + i * 256, _bitmap.data() + i * _widthAligned, _width);
}
}
} else {
for(u32 i = 0; i < _height * scaleY; i++) {
u8 *dst = (u8 *)bgGetGfxPtr(currentScreen ? 3 : 7) + (y + i) * 256 + x;
for(u32 j = 0; j < _width * scaleX; j++) {
u8 px = _bitmap[int(i / scaleY) * _width + int(j / scaleX)];
u8 px = _bitmap[int(i / scaleY) * _widthAligned + int(j / scaleX)];
if(_palette[px - _paletteStart] & 0x8000 || !skipAlpha)
toncset(dst + j, px, 1);
}
@@ -183,7 +185,7 @@ void Image::drawSegment(int x, int y, int imageX, int imageY, int w, int h, floa
if(scaleX == 1.0f && scaleY == 1.0f) {
if(skipAlpha) {
for(int i = 0; i < h; i++) {
const u8 *src = _bitmap.data() + i * _width;
const u8 *src = _bitmap.data() + i * _widthAligned;
u8 *dst = (u8 *)bgGetGfxPtr(currentScreen ? 3 : 7) + (y + i) * 256 + x;
for(int j = 0; j < w; j++) {
if(_palette[src[j] - _paletteStart] & 0x8000)
@@ -193,14 +195,14 @@ void Image::drawSegment(int x, int y, int imageX, int imageY, int w, int h, floa
} else {
for(int i = 0; i < h; i++) {
tonccpy((u8 *)bgGetGfxPtr(currentScreen ? 3 : 7) + (y + i) * 256 + x,
_bitmap.data() + (imageY + i) * _width + imageX, w);
_bitmap.data() + (imageY + i) * _widthAligned + imageX, w);
}
}
} else {
for(u32 i = 0; i < h * scaleY; i++) {
u8 *dst = (u8 *)bgGetGfxPtr(currentScreen ? 3 : 7) + (y + i) * 256 + x;
for(u32 j = 0; j < w * scaleX; j++) {
u8 px = _bitmap[(imageY + int(i / scaleY)) * _width + imageX + int(j / scaleX)];
u8 px = _bitmap[(imageY + int(i / scaleY)) * _widthAligned + imageX + int(j / scaleX)];
if(_palette[px - _paletteStart] & 0x8000 || !skipAlpha)
toncset(dst + j, px, 1);
}