mirror of
https://github.com/gbdev/rgbds.git
synced 2026-11-21 21:48:39 +00:00
Use named constants for meaningful numbers in RGBGFX (#2233)
This commit is contained in:
1 parent
65d730336f
commit
27a0a6bc2a
6 files changed
+84
-57
No files matched your search
+19
-8
@@ -13,6 +13,17 @@
|
||||
|
||||
#include "gfx/rgba.hpp"
|
||||
|
||||
static constexpr size_t NB_BANKS = 2;
|
||||
|
||||
static constexpr uint32_t TILE_WIDTH = 8; // in pixels
|
||||
static constexpr uint32_t TILE_HEIGHT = 8; // in pixels
|
||||
|
||||
static constexpr size_t MAX_PAL_COLORS = 4;
|
||||
|
||||
// Forward declaration so `inputSlice`'s `bottom` method inside `Options` can refer to `options`.
|
||||
struct Options;
|
||||
extern Options options;
|
||||
|
||||
struct Options {
|
||||
bool useColorCurve = false; // -C
|
||||
bool allowDedup = false; // -u
|
||||
@@ -20,9 +31,9 @@ struct Options {
|
||||
bool allowMirroringY = false; // -Y, -m
|
||||
bool columnMajor = false; // -Z
|
||||
|
||||
std::string attrmap{}; // -a, -A
|
||||
std::optional<Rgba> bgColor{}; // -B
|
||||
std::array<uint8_t, 2> baseTileIDs{0, 0}; // -b
|
||||
std::string attrmap{}; // -a, -A
|
||||
std::optional<Rgba> bgColor{}; // -B
|
||||
std::array<uint8_t, NB_BANKS> baseTileIDs{0, 0}; // -b
|
||||
enum {
|
||||
NO_SPEC,
|
||||
INLINE,
|
||||
@@ -30,7 +41,7 @@ struct Options {
|
||||
EMBEDDED,
|
||||
DMG,
|
||||
} palSpecType = NO_SPEC; // -c
|
||||
std::vector<std::array<std::optional<Rgba>, 4>> palSpec{};
|
||||
std::vector<std::array<std::optional<Rgba>, MAX_PAL_COLORS>> palSpec{};
|
||||
uint8_t palSpecDmg = 0;
|
||||
uint8_t bitDepth = 2; // -d
|
||||
std::string inputTileset{}; // -i
|
||||
@@ -39,8 +50,8 @@ struct Options {
|
||||
uint16_t top;
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
uint32_t right() const { return left + width * 8; }
|
||||
uint32_t bottom() const { return top + height * 8; }
|
||||
uint32_t right() const { return left + width * TILE_WIDTH; }
|
||||
uint32_t bottom() const { return top + height * TILE_HEIGHT; }
|
||||
} inputSlice{0, 0, 0, 0}; // -L (margins in clockwise order, like CSS)
|
||||
uint8_t basePalID = 0; // -l
|
||||
std::array<uint16_t, 2> maxNbTiles{UINT16_MAX, 0}; // -N
|
||||
@@ -62,9 +73,9 @@ struct Options {
|
||||
|
||||
bool hasExplicitPalSpec() const { return palSpecType == INLINE || palSpecType == EXTERNAL; }
|
||||
|
||||
uint8_t dmgColors[4] = {};
|
||||
uint8_t dmgColors[MAX_PAL_COLORS] = {};
|
||||
uint8_t dmgValue(uint8_t i) const {
|
||||
assume(i < 4);
|
||||
assume(i < MAX_PAL_COLORS);
|
||||
return (palSpecDmg >> (2 * i)) & 0b11;
|
||||
}
|
||||
};
|
||||
|
||||
+4
-4
@@ -387,12 +387,12 @@ static void parseArg(int ch, char *arg) {
|
||||
break;
|
||||
|
||||
case 's':
|
||||
options.nbColorsPerPal = readNumber(argPtr, "Number of colors per palette", 4);
|
||||
options.nbColorsPerPal = readNumber(argPtr, "Number of colors per palette", MAX_PAL_COLORS);
|
||||
if (*argPtr != '\0') {
|
||||
error("Palette size ('-s') must be a valid number, not \"%s\"", arg);
|
||||
}
|
||||
if (options.nbColorsPerPal > 4) {
|
||||
error("Palette size ('-s') must not exceed 4");
|
||||
if (options.nbColorsPerPal > MAX_PAL_COLORS) {
|
||||
error("Palette size ('-s') must not exceed %zu", MAX_PAL_COLORS);
|
||||
} else if (options.nbColorsPerPal == 0) {
|
||||
error("Palette size ('-s') may not be 0");
|
||||
}
|
||||
@@ -684,7 +684,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
// Execute deferred pal spec parsing, now that all other params are known.
|
||||
// Do not parse pal specs if `options.nbColorsPerPal` is invalid.
|
||||
if (options.nbColorsPerPal > 0 && options.nbColorsPerPal <= 4) {
|
||||
if (options.nbColorsPerPal > 0 && options.nbColorsPerPal <= MAX_PAL_COLORS) {
|
||||
switch (options.palSpecType) {
|
||||
case Options::NO_SPEC:
|
||||
assume(!localOptions.palSpec);
|
||||
|
||||
@@ -550,9 +550,9 @@ static void parseGBCFile(char const *filename, std::filebuf &file) {
|
||||
options.palSpec.clear();
|
||||
|
||||
size_t const palSize = sizeof(uint16_t) * options.nbColorsPerPal;
|
||||
assume(options.nbColorsPerPal <= 4);
|
||||
assume(options.nbColorsPerPal <= MAX_PAL_COLORS);
|
||||
for (;;) {
|
||||
char buf[sizeof(uint16_t) * 4];
|
||||
char buf[sizeof(uint16_t) * MAX_PAL_COLORS];
|
||||
if (size_t len = file.sgetn(buf, palSize); len == 0) {
|
||||
break;
|
||||
} else if (len != palSize) {
|
||||
@@ -699,13 +699,13 @@ void parseDmgPalSpec(char const * const rawArg) {
|
||||
options.palSpecDmg = toHex(arg[0], arg[1]);
|
||||
|
||||
// Map gray shades to their DMG color indexes for fast lookup by `Rgba::grayIndex`
|
||||
for (uint8_t i = 0; i < 4; ++i) {
|
||||
for (uint8_t i = 0; i < MAX_PAL_COLORS; ++i) {
|
||||
options.dmgColors[options.dmgValue(i)] = i;
|
||||
}
|
||||
|
||||
// Validate that DMG palette spec does not have conflicting colors
|
||||
for (uint8_t i = 0; i < 3; ++i) {
|
||||
for (uint8_t j = i + 1; j < 4; ++j) {
|
||||
for (uint8_t i = 0; i < MAX_PAL_COLORS - 1; ++i) {
|
||||
for (uint8_t j = i + 1; j < MAX_PAL_COLORS; ++j) {
|
||||
if (options.dmgValue(i) == options.dmgValue(j)) {
|
||||
error("DMG palette specification maps two gray shades to the same color index");
|
||||
return;
|
||||
|
||||
+34
-22
@@ -128,11 +128,19 @@ struct Image {
|
||||
|
||||
explicit Image(Png &&png_) : png(std::move(png_)), colors() {
|
||||
// Validate input slice
|
||||
if (options.inputSlice.width == 0 && png.width % 8 != 0) {
|
||||
fatal("Image width (%" PRIu32 " pixels) is not a multiple of 8", png.width);
|
||||
if (options.inputSlice.width == 0 && png.width % TILE_WIDTH != 0) {
|
||||
fatal(
|
||||
"Image width (%" PRIu32 " pixels) is not a multiple of %" PRIu32,
|
||||
png.width,
|
||||
TILE_WIDTH
|
||||
);
|
||||
}
|
||||
if (options.inputSlice.height == 0 && png.height % 8 != 0) {
|
||||
fatal("Image height (%" PRIu32 " pixels) is not a multiple of 8", png.height);
|
||||
if (options.inputSlice.height == 0 && png.height % TILE_HEIGHT != 0) {
|
||||
fatal(
|
||||
"Image height (%" PRIu32 " pixels) is not a multiple of %" PRIu32,
|
||||
png.height,
|
||||
TILE_HEIGHT
|
||||
);
|
||||
}
|
||||
if (options.inputSlice.right() > png.width || options.inputSlice.bottom() > png.height) {
|
||||
error(
|
||||
@@ -145,15 +153,16 @@ struct Image {
|
||||
png.width,
|
||||
png.height
|
||||
);
|
||||
if (options.inputSlice.width % 8 == 0 && options.inputSlice.height % 8 == 0) {
|
||||
if (options.inputSlice.width % TILE_WIDTH == 0
|
||||
&& options.inputSlice.height % TILE_HEIGHT == 0) {
|
||||
fprintf(
|
||||
stderr,
|
||||
" (Did you mean the slice \"%" PRIu32 ",%" PRIu32 ":%" PRId32 ",%" PRId32
|
||||
"\"? The width and height are in tiles, not pixels!)\n",
|
||||
options.inputSlice.left,
|
||||
options.inputSlice.top,
|
||||
options.inputSlice.width / 8,
|
||||
options.inputSlice.height / 8
|
||||
options.inputSlice.width / TILE_WIDTH,
|
||||
options.inputSlice.height / TILE_HEIGHT
|
||||
);
|
||||
}
|
||||
giveUp();
|
||||
@@ -256,10 +265,11 @@ struct Image {
|
||||
}
|
||||
|
||||
Iterator &operator++() {
|
||||
assume(TILE_WIDTH == TILE_HEIGHT);
|
||||
auto [major, minor] = parent._columnMajor ? std::tie(y, x) : std::tie(x, y);
|
||||
major += 8;
|
||||
major += TILE_WIDTH;
|
||||
if (major == limit) {
|
||||
minor += 8;
|
||||
minor += TILE_HEIGHT;
|
||||
major = 0;
|
||||
}
|
||||
return *this;
|
||||
@@ -271,8 +281,10 @@ struct Image {
|
||||
public:
|
||||
Iterator begin() const { return {*this, _limit, 0, 0}; }
|
||||
Iterator end() const {
|
||||
Iterator it{*this, _limit, _width - 8, _height - 8}; // Last valid one...
|
||||
return ++it; // ...now one-past-last!
|
||||
// Construct an iterator to the last valid tile...
|
||||
Iterator it{*this, _limit, _width - TILE_WIDTH, _height - TILE_HEIGHT};
|
||||
// ...and return one past the end
|
||||
return ++it;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -281,8 +293,8 @@ public:
|
||||
return {
|
||||
*this,
|
||||
options.columnMajor,
|
||||
options.inputSlice.width ? options.inputSlice.width * 8 : png.width,
|
||||
options.inputSlice.height ? options.inputSlice.height * 8 : png.height,
|
||||
options.inputSlice.width ? options.inputSlice.width * TILE_WIDTH : png.width,
|
||||
options.inputSlice.height ? options.inputSlice.height * TILE_HEIGHT : png.height,
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -523,7 +535,7 @@ public:
|
||||
static uint16_t
|
||||
rowBitplanes(Image::TilesVisitor::Tile const &tile, Palette const &palette, uint32_t y) {
|
||||
uint16_t row = 0;
|
||||
for (uint32_t x = 0; x < 8; ++x) {
|
||||
for (uint32_t x = 0; x < TILE_WIDTH; ++x) {
|
||||
row <<= 1;
|
||||
uint8_t index = palette.indexOf(tile.pixel(x, y).cgbColor());
|
||||
assume(index < palette.size()); // The color should be in the palette
|
||||
@@ -538,7 +550,7 @@ public:
|
||||
}
|
||||
|
||||
TileData(std::array<uint8_t, 16> &&raw) : _data(raw), _hash(0) {
|
||||
for (uint8_t y = 0; y < 8; ++y) {
|
||||
for (uint8_t y = 0; y < TILE_HEIGHT; ++y) {
|
||||
uint16_t bitplanes = _data[y * 2] | _data[y * 2 + 1] << 8;
|
||||
hashBitplanes(bitplanes, _hash);
|
||||
}
|
||||
@@ -546,7 +558,7 @@ public:
|
||||
|
||||
TileData(Image::TilesVisitor::Tile const &tile, Palette const &palette) : _hash(0) {
|
||||
size_t writeIndex = 0;
|
||||
for (uint32_t y = 0; y < 8; ++y) {
|
||||
for (uint32_t y = 0; y < TILE_HEIGHT; ++y) {
|
||||
uint16_t bitplanes = rowBitplanes(tile, palette, y);
|
||||
hashBitplanes(bitplanes, _hash);
|
||||
|
||||
@@ -652,7 +664,7 @@ static void outputUnoptimizedTileData(
|
||||
Palette const &palette = palettes[attr.getPalID(mappings)];
|
||||
|
||||
bool empty = true;
|
||||
for (uint32_t y = 0; y < 8; ++y) {
|
||||
for (uint32_t y = 0; y < TILE_HEIGHT; ++y) {
|
||||
uint16_t bitplanes = TileData::rowBitplanes(tile, palette, y);
|
||||
if (bitplanes != 0) {
|
||||
empty = false;
|
||||
@@ -710,7 +722,7 @@ static void outputUnoptimizedMaps(
|
||||
if (attr.isBackgroundTile()) {
|
||||
// The tile bank may be 2 here, which is fine since background tiles are emitted as
|
||||
// if they used the base tile ID and bank 0.
|
||||
assume(bank <= 2);
|
||||
assume(bank <= NB_BANKS);
|
||||
|
||||
emit(tilemapOutput, options.baseTileIDs[0]);
|
||||
emit(attrmapOutput, palID & 0b111); // The other flags are all zeros.
|
||||
@@ -718,7 +730,7 @@ static void outputUnoptimizedMaps(
|
||||
// Since background tiles are not in tile data, they do not increment the tile index.
|
||||
} else {
|
||||
// The only valid tile banks are 0 and 1.
|
||||
assume(bank < 2);
|
||||
assume(bank < NB_BANKS);
|
||||
|
||||
// A non-zero base ID may make this addition overflow, wrapping around the available
|
||||
// tile IDs. Since the operands are unsigned, this won't cause undefined behavior.
|
||||
@@ -870,7 +882,7 @@ static void outputTileData(UniqueTiles const &tiles) {
|
||||
for (TileData const *tile : tiles) {
|
||||
assume(tile->tileID == tileIdx);
|
||||
bool empty = true;
|
||||
for (uint32_t y = 0; y < 8; ++y) {
|
||||
for (uint32_t y = 0; y < TILE_HEIGHT; ++y) {
|
||||
uint8_t bitplane0 = tile->data()[y * 2];
|
||||
uint8_t bitplane1 = tile->data()[y * 2 + 1];
|
||||
if (bitplane0 || bitplane1) {
|
||||
@@ -1006,8 +1018,8 @@ void process(Png &&png) {
|
||||
|
||||
// Count the unique opaque colors for packing
|
||||
std::unordered_set<uint16_t> tileColors;
|
||||
for (uint32_t y = 0; y < 8; ++y) {
|
||||
for (uint32_t x = 0; x < 8; ++x) {
|
||||
for (uint32_t y = 0; y < TILE_HEIGHT; ++y) {
|
||||
for (uint32_t x = 0; x < TILE_WIDTH; ++x) {
|
||||
Rgba color = tile.pixel(x, y);
|
||||
// Ambiguous colors should not be in `tileColors`
|
||||
assume(color.isOpaque() != color.isTransparent());
|
||||
|
||||
+20
-17
@@ -94,7 +94,7 @@ static void printColor(std::optional<Rgba> const &color) {
|
||||
}
|
||||
}
|
||||
|
||||
static void printPalette(std::array<std::optional<Rgba>, 4> const &palette) {
|
||||
static void printPalette(std::array<std::optional<Rgba>, MAX_PAL_COLORS> const &palette) {
|
||||
putc('[', stderr);
|
||||
printColor(palette[0]);
|
||||
fputs(", ", stderr);
|
||||
@@ -196,10 +196,10 @@ void reverse() {
|
||||
|
||||
verbosePrint(VERB_INFO, "Reversed image dimensions: %zux%zu tiles\n", width, height);
|
||||
|
||||
Rgba const grayColors[4] = {
|
||||
Rgba const grayColors[MAX_PAL_COLORS] = {
|
||||
Rgba(0xFFFFFFFF), Rgba(0xAAAAAAFF), Rgba(0x555555FF), Rgba(0x000000FF)
|
||||
};
|
||||
std::vector<std::array<std::optional<Rgba>, 4>> palettes{
|
||||
std::vector<std::array<std::optional<Rgba>, MAX_PAL_COLORS>> palettes{
|
||||
{grayColors[0], grayColors[1], grayColors[2], grayColors[3]}
|
||||
};
|
||||
// If a palette file or palette spec is used as input, it overrides the default colors.
|
||||
@@ -213,7 +213,7 @@ void reverse() {
|
||||
}
|
||||
|
||||
palettes.clear();
|
||||
std::array<uint8_t, sizeof(uint16_t) * 4> buf; // max 4 colors
|
||||
std::array<uint8_t, sizeof(uint16_t) * MAX_PAL_COLORS> buf;
|
||||
size_t const palSize = sizeof(uint16_t) * options.nbColorsPerPal;
|
||||
assume(buf.size() >= palSize);
|
||||
for (;;) {
|
||||
@@ -250,7 +250,9 @@ void reverse() {
|
||||
if (options.hasExplicitPalSpec() && palettes != options.palSpec) {
|
||||
// The explicit `-c` pal spec does not match the input `-p` palette file.
|
||||
// Check whether their 8-to-5-bit-reduced GB colors nevertheless match.
|
||||
std::vector<std::array<std::optional<Rgba>, 4>> palSpecQuantized(options.palSpec);
|
||||
std::vector<std::array<std::optional<Rgba>, MAX_PAL_COLORS>> palSpecQuantized(
|
||||
options.palSpec
|
||||
);
|
||||
for (auto &pal : palSpecQuantized) {
|
||||
for (auto &color : pal) {
|
||||
if (color.has_value()) {
|
||||
@@ -305,7 +307,7 @@ void reverse() {
|
||||
}
|
||||
|
||||
std::optional<std::vector<uint8_t>> attrmap;
|
||||
uint16_t nbTilesMappedInBank[2] = {0, 0}; // Only used if there is an attrmap.
|
||||
uint16_t nbTilesMappedInBank[NB_BANKS] = {0, 0}; // Only used if there is an attrmap.
|
||||
if (!options.attrmap.empty()) {
|
||||
attrmap = readInto(options.attrmap);
|
||||
if (attrmap->size() != mapSize) {
|
||||
@@ -368,10 +370,10 @@ void reverse() {
|
||||
nbTilesMappedInBank[1]
|
||||
);
|
||||
|
||||
for (int bank = 0; bank < 2; ++bank) {
|
||||
for (size_t bank = 0; bank < NB_BANKS; ++bank) {
|
||||
if (nbTilesMappedInBank[bank] > options.maxNbTiles[bank]) {
|
||||
error(
|
||||
"Bank %d contains %" PRIu16 " tiles, but the specified limit is %" PRIu16,
|
||||
"Bank %zu contains %" PRIu16 " tiles, but the specified limit is %" PRIu16,
|
||||
bank,
|
||||
nbTilesMappedInBank[bank],
|
||||
options.maxNbTiles[bank]
|
||||
@@ -522,8 +524,8 @@ void reverse() {
|
||||
png_set_IHDR(
|
||||
png,
|
||||
pngInfo,
|
||||
width * 8,
|
||||
height * 8,
|
||||
width * TILE_WIDTH,
|
||||
height * TILE_HEIGHT,
|
||||
pngDepth,
|
||||
pngColorType,
|
||||
PNG_INTERLACE_NONE,
|
||||
@@ -544,8 +546,8 @@ void reverse() {
|
||||
|
||||
if (pngColorType == PNG_COLOR_TYPE_PALETTE) {
|
||||
assume(palettes.size() == 1);
|
||||
png_color pngPalette[4] = {};
|
||||
png_byte pngTrans[4] = {};
|
||||
png_color pngPalette[MAX_PAL_COLORS] = {};
|
||||
png_byte pngTrans[MAX_PAL_COLORS] = {};
|
||||
int nbPngColors = 0, nbPngTrans = 0;
|
||||
for (auto const &slot : palettes[0]) {
|
||||
Rgba color = slot.has_value() ? *slot : Rgba(255, 255, 255, 255);
|
||||
@@ -567,10 +569,11 @@ void reverse() {
|
||||
png_write_info(png, pngInfo);
|
||||
|
||||
// N bits/pixel * 8 pixels/tile row / 8 bits/byte = N bytes/tile row
|
||||
assume(TILE_WIDTH == 8);
|
||||
uint8_t const bytesPerTileRow = pngColorType == PNG_COLOR_TYPE_RGB_ALPHA ? 32 : pngDepth;
|
||||
size_t const bytesPerRow = width * bytesPerTileRow;
|
||||
std::vector<uint8_t> tileRow(8 * bytesPerRow, 0xFF); // Data for 8 rows of pixels
|
||||
uint8_t * const rowPtrs[8] = {
|
||||
std::vector<uint8_t> tileRow(TILE_HEIGHT * bytesPerRow, 0xFF); // Data for 8 rows of pixels
|
||||
uint8_t * const rowPtrs[TILE_HEIGHT] = {
|
||||
&tileRow.data()[0 * bytesPerRow],
|
||||
&tileRow.data()[1 * bytesPerRow],
|
||||
&tileRow.data()[2 * bytesPerRow],
|
||||
@@ -605,7 +608,7 @@ void reverse() {
|
||||
uint8_t const *tileData =
|
||||
tileOfs >= nbTiles ? trimmedTile.data() : &tiles[tileOfs * tileSize];
|
||||
auto const &palette = palettes[palOfs];
|
||||
for (uint8_t y = 0; y < 8; ++y) {
|
||||
for (uint8_t y = 0; y < TILE_HEIGHT; ++y) {
|
||||
// If vertically mirrored, fetch the bytes from the other end
|
||||
uint8_t realY = (attribute & 0x40 ? 7 - y : y) * options.bitDepth;
|
||||
uint8_t bitplane0 = tileData[realY];
|
||||
@@ -617,7 +620,7 @@ void reverse() {
|
||||
|
||||
uint8_t *ptr = &rowPtrs[y][tx * bytesPerTileRow];
|
||||
uint16_t gray = 0;
|
||||
for (uint8_t x = 0; x < 8; ++x) {
|
||||
for (uint8_t x = 0; x < TILE_WIDTH; ++x) {
|
||||
uint8_t bit0 = bitplane0 & 0x80, bit1 = bitplane1 & 0x80;
|
||||
uint8_t colorID = bit0 >> 7 | bit1 >> 6;
|
||||
|
||||
@@ -666,7 +669,7 @@ void reverse() {
|
||||
// signature.
|
||||
// (AIUI, casting away const-ness is okay as long as you don't actually modify the
|
||||
// pointed-to data)
|
||||
png_write_rows(png, const_cast<uint8_t **>(rowPtrs), 8);
|
||||
png_write_rows(png, const_cast<uint8_t **>(rowPtrs), TILE_HEIGHT);
|
||||
}
|
||||
|
||||
// Finalize the write
|
||||
|
||||
+2
-1
@@ -108,7 +108,8 @@ uint8_t Rgba::grayIndex() const {
|
||||
assume(!options.hasTransparentPixels);
|
||||
// Reduce gray shade from 0..<256 to 0..<4, then map to color index,
|
||||
// then reduce to 0..<nbColorsPerPal
|
||||
return options.dmgColors[gray * 4 / 256] * options.nbColorsPerPal / 4;
|
||||
return options.dmgColors[gray * MAX_PAL_COLORS / 256] * options.nbColorsPerPal
|
||||
/ MAX_PAL_COLORS;
|
||||
}
|
||||
// Reduce gray shade from 0..<256 to hasTransparentPixels..<nbColorsPerPal
|
||||
// Note that `maxOpaqueColors()` already takes `hasTransparentPixels` into account
|
||||
|
||||
Reference in new issue
Block a user