From 9236a2a01060760b75ef635850026e1db3a1dd6f Mon Sep 17 00:00:00 2001 From: Adrian Siekierka Date: Thu, 2 Jan 2025 11:30:47 +0100 Subject: [PATCH 1/2] Round down colors evenly in reduce_color() (#55) --- src/Color.h | 5 +++++ src/Mode.h | 24 ++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Color.h b/src/Color.h index e299b54..e7ea850 100644 --- a/src/Color.h +++ b/src/Color.h @@ -73,6 +73,11 @@ constexpr channel_t scale_up(channel_t value, unsigned shift) { } } +// scale down value, rounding evenly +constexpr channel_t scale_down(channel_t value, unsigned shift) { + return (value + (1 << (shift - 1))) >> shift; +} + // // rgba_color / hsva_color // diff --git a/src/Mode.h b/src/Mode.h index 27590d3..ad99671 100644 --- a/src/Mode.h +++ b/src/Mode.h @@ -334,9 +334,9 @@ inline rgba_t reduce_color(const rgba_t color, Mode to_mode) { return transparent_color; } else { rgba_color c(color); - c.r >>= 3; - c.g >>= 3; - c.b >>= 3; + c.r = scale_down(c.r, 3); + c.g = scale_down(c.g, 3); + c.b = scale_down(c.b, 3); rgba_t scaled = c; return (scaled & 0x00ffffff) + 0xff000000; } @@ -372,18 +372,18 @@ inline rgba_t reduce_color(const rgba_t color, Mode to_mode) { return transparent_color; } else { rgba_color c(color); - c.r >>= 5; - c.g >>= 5; - c.b >>= 5; + c.r = scale_down(c.r, 5); + c.g = scale_down(c.g, 5); + c.b = scale_down(c.b, 5); rgba_t scaled = c; return (scaled & 0x00ffffff) + 0xff000000; } break; case Mode::sms: { rgba_color c(color); - c.r >>= 6; - c.g >>= 6; - c.b >>= 6; + c.r = scale_down(c.r, 6); + c.g = scale_down(c.g, 6); + c.b = scale_down(c.b, 6); rgba_t scaled = c; return (scaled & 0x00ffffff) + 0xff000000; } @@ -395,9 +395,9 @@ inline rgba_t reduce_color(const rgba_t color, Mode to_mode) { return transparent_color; } else { rgba_color c(color); - c.r >>= 4; - c.g >>= 4; - c.b >>= 4; + c.r = scale_down(c.r, 4); + c.g = scale_down(c.g, 4); + c.b = scale_down(c.b, 4); rgba_t scaled = c; return (scaled & 0x00ffffff) + 0xff000000; } From 3691d8c87969f4b7eb3d5389b707b5129146285d Mon Sep 17 00:00:00 2001 From: undisbeliever Date: Thu, 2 Jan 2025 20:37:44 +1000 Subject: [PATCH 2/2] Fix remap_tiles_for_output() for -W16 (#57) This commit fixes a bug where the tile data output was squished and missing even columns when converting a SNES tileset with a `-W 16` argument. This commit also fixes a segfault when converting a snes tileset with `-W 8 -H 16` (when _tile_width is 8 `(cells_per_tile_h - 2)` underflows). --- src/Tiles.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tiles.cpp b/src/Tiles.cpp index 156ff2d..3f0ca2c 100644 --- a/src/Tiles.cpp +++ b/src/Tiles.cpp @@ -240,7 +240,7 @@ std::vector Tileset::remap_tiles_for_output(const std::vector& tiles for (unsigned i = 0; i < tiles.size(); ++i) { unsigned base_pos = - (((i / tiles_per_row) * cells_per_tile_v) * cells_per_row) + ((i % tiles_per_row) << (cells_per_tile_h - 2)); + (((i / tiles_per_row) * cells_per_tile_v) * cells_per_row) + ((i % tiles_per_row) * cells_per_tile_h); const auto ct = tiles[i].crops(8, 8); for (unsigned cy = 0; cy < cells_per_tile_v; ++cy) { for (unsigned cx = 0; cx < cells_per_tile_h; ++cx) {