Fix rgbgfx -Z palette overgeneration on merged color sets (#1912)

- Fix logic for color set comparison (which affects sorting them)
- Prune color sets which are proper subsets of newly-encountered ones
  (a comment implied we were already doing this, but we weren't)
- Add more verbose logging to debug this behavior
This commit is contained in:
vulcandth
2026-06-07 08:55:05 -05:00
committed by GitHub
parent 075f132d77
commit 998f636495
6 changed files with 100 additions and 29 deletions
+1 -5
View File
@@ -21,11 +21,7 @@ public:
// Adds the specified color to the set, or **silently drops it** if the set is full. // Adds the specified color to the set, or **silently drops it** if the set is full.
void add(uint16_t color); void add(uint16_t color);
enum ComparisonResult { enum ComparisonResult { INCOMPARABLE, SUBSET_OR_EQUAL, STRICT_SUPERSET };
NEITHER,
WE_BIGGER,
THEY_BIGGER = -1,
};
ComparisonResult compare(ColorSet const &other) const; ComparisonResult compare(ColorSet const &other) const;
size_t size() const; size_t size() const;
+31 -16
View File
@@ -42,29 +42,44 @@ void ColorSet::add(uint16_t color) {
} }
ColorSet::ComparisonResult ColorSet::compare(ColorSet const &other) const { ColorSet::ComparisonResult ColorSet::compare(ColorSet const &other) const {
// This works because the sets are sorted numerically // This algorithm works because the sets are sorted numerically
assume(std::is_sorted(RANGE(_colorIndices))); assume(std::is_sorted(RANGE(_colorIndices)));
assume(std::is_sorted(RANGE(other._colorIndices))); assume(std::is_sorted(RANGE(other._colorIndices)));
auto ours = _colorIndices.begin(), theirs = other._colorIndices.begin(); auto self_item = begin(), other_item = other.begin();
bool weBigger = true, theyBigger = true; auto const self_end = end(), other_end = other.end();
bool self_has_unique = false, other_has_unique = false;
while (ours != end() && theirs != other.end()) { while (self_item != self_end && other_item != other_end) {
if (*ours == *theirs) { if (*self_item < *other_item) {
++ours; // *self_item is not in other, so self cannot be a strict subset of other
++theirs; self_has_unique = true;
} else if (*ours < *theirs) { ++self_item;
++ours; } else if (*self_item > *other_item) {
theyBigger = false; // *other_item is not in self, so self cannot be a strict superset of other
} else { // *ours > *theirs other_has_unique = true;
++theirs; ++other_item;
weBigger = false; } else {
// *self_item == *other_item, so continue comparing
++self_item;
++other_item;
}
// Early return optimization: we already know self and other are incomparable
if (self_has_unique && other_has_unique) {
return INCOMPARABLE;
} }
} }
weBigger &= theirs == other.end();
theyBigger &= ours == end();
return theyBigger ? THEY_BIGGER : (weBigger ? WE_BIGGER : NEITHER); // Check if either color set has unique items remaining after one set has been fully iterated
if (self_item != self_end) {
self_has_unique = true;
}
if (other_item != other_end) {
other_has_unique = true;
}
return self_has_unique ? other_has_unique ? INCOMPARABLE : STRICT_SUPERSET : SUBSET_OR_EQUAL;
} }
size_t ColorSet::size() const { size_t ColorSet::size() const {
+66 -8
View File
@@ -1027,22 +1027,65 @@ void process() {
// Insert the color set, making sure to avoid overlaps // Insert the color set, making sure to avoid overlaps
for (size_t n = 0; n < colorSets.size(); ++n) { for (size_t n = 0; n < colorSets.size(); ++n) {
switch (colorSet.compare(colorSets[n])) { switch (colorSet.compare(colorSets[n])) {
case ColorSet::WE_BIGGER: case ColorSet::STRICT_SUPERSET:
colorSets[n] = colorSet; // Override them // Override the previous color set that this one is a strict superset of
// Remove any other color sets that we encompass
// (Example [(0, 1), (0, 2)], inserting (0, 1, 2)) if (checkVerbosity(VERB_DEBUG)) {
fprintf(
stderr,
"- Tile (%" PRIu32 ", %" PRIu32 ") overrides color set #%zu: [",
tile.x,
tile.y,
n
);
for (uint16_t color : colorSets[n]) {
fprintf(stderr, "$%04x, ", color);
}
fputs("] becomes [", stderr);
for (uint16_t color : colorSet) {
fprintf(stderr, "$%04x, ", color);
}
fputs("]\n", stderr);
}
colorSets[n] = colorSet;
// Remove any other color sets that we are also a strict superset of
// (example: we have [(0, 1), (0, 2)] and are inserting (0, 1, 2))
for (size_t m = n + 1; m < colorSets.size();) {
if (colorSet.compare(colorSets[m]) != ColorSet::STRICT_SUPERSET) {
++m;
} else {
// We are about to remove a set, which will shift sets that may be
// already referenced in the attrmap: re-number to keep it consistent
for (size_t i = 0; i + 1 < attrmap.size(); ++i) {
AttrmapEntry &entry = attrmap[i];
if (entry.colorSetID == AttrmapEntry::transparent
|| entry.colorSetID == AttrmapEntry::background) {
continue;
}
if (entry.colorSetID == m) {
entry.colorSetID = n;
} else if (entry.colorSetID > m) {
--entry.colorSetID;
}
}
colorSets.erase(colorSets.begin() + m);
}
}
[[fallthrough]]; [[fallthrough]];
case ColorSet::THEY_BIGGER: case ColorSet::SUBSET_OR_EQUAL:
// Do nothing, they already contain us // Use the previous color set that this one is a subset or duplicate of
attrs.colorSetID = n; attrs.colorSetID = n;
goto continue_visiting_tiles; // Can't `continue` from within a nested loop goto continue_visiting_tiles; // Can't `continue` from within a nested loop
case ColorSet::NEITHER: case ColorSet::INCOMPARABLE:
break; // Keep going // This color set is incomparable so far, so keep going
break;
} }
} }
// This color set is incomparable with all previous ones, so add it as a new one
attrs.colorSetID = colorSets.size(); attrs.colorSetID = colorSets.size();
if (colorSets.size() == AttrmapEntry::background) { // Check for overflow if (colorSets.size() == AttrmapEntry::background) { // Check for overflow
fatal( fatal(
@@ -1050,6 +1093,21 @@ void process() {
AttrmapEntry::transparent AttrmapEntry::transparent
); );
} }
if (checkVerbosity(VERB_DEBUG)) {
fprintf(
stderr,
"- Tile (%" PRIu32 ", %" PRIu32 ") adds color set #%zu: [",
tile.x,
tile.y,
colorSets.size()
);
for (uint16_t color : colorSet) {
fprintf(stderr, "$%04x, ", color);
}
fputs("]\n", stderr);
}
colorSets.push_back(colorSet); colorSets.push_back(colorSet);
continue_visiting_tiles:; continue_visiting_tiles:;
} }
+1
View File
@@ -0,0 +1 @@
-Z
+1
View File
@@ -0,0 +1 @@
"XH9g礎pIH9gS"g
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB