Implement -c #none (#1301)

Also adds a test case for round-tripping `-r` with `-c #none`.
This commit is contained in:
Evie
2024-03-03 18:45:33 -05:00
committed by GitHub
parent 930a5c3e44
commit 6b67c82b94
12 changed files with 95 additions and 38 deletions

View File

@@ -768,9 +768,16 @@ int main(int argc, char *argv[]) {
}());
if (options.palSpecType == Options::EXPLICIT) {
fputs("\t[\n", stderr);
for (std::array<Rgba, 4> const &pal : options.palSpec) {
fprintf(stderr, "\t\t#%06x, #%06x, #%06x, #%06x,\n", pal[0].toCSS() >> 8,
pal[1].toCSS() >> 8, pal[2].toCSS() >> 8, pal[3].toCSS() >> 8);
for (const auto &pal : options.palSpec) {
fputs("\t\t", stderr);
for (auto &color : pal) {
if (color) {
fprintf(stderr, "#%06x, ", color->toCSS() >> 8);
} else {
fputs("#none, ", stderr);
}
}
fputc('\n', stderr);
}
fputs("\t]\n", stderr);
}
@@ -847,18 +854,27 @@ auto Palette::begin() -> decltype(colors)::iterator {
// Skip the first slot if reserved for transparency
return colors.begin() + options.hasTransparentPixels;
}
auto Palette::end() -> decltype(colors)::iterator {
return std::find(begin(), colors.end(), UINT16_MAX);
// Return an iterator pointing past the last non-empty element.
// Since the palette may contain gaps, we must scan from the end.
return std::find_if(colors.rbegin(), colors.rend(),
[](uint16_t c) { return c != UINT16_MAX; })
.base();
}
auto Palette::begin() const -> decltype(colors)::const_iterator {
// Skip the first slot if reserved for transparency
return colors.begin() + options.hasTransparentPixels;
}
auto Palette::end() const -> decltype(colors)::const_iterator {
return std::find(begin(), colors.end(), UINT16_MAX);
// Same as the non-const end().
return std::find_if(colors.rbegin(), colors.rend(),
[](uint16_t c) { return c != UINT16_MAX; })
.base();
}
uint8_t Palette::size() const {
return indexOf(UINT16_MAX);
return end() - colors.begin();
}