Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color_curve RGBGFX test #1554

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions include/gfx/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ struct Options {

std::string input{}; // positional arg

static constexpr uint8_t VERB_NONE = 0; // Normal, no extra output
static constexpr uint8_t VERB_CFG = 1; // Print configuration after parsing options
static constexpr uint8_t VERB_LOG_ACT = 2; // Log actions before doing them
static constexpr uint8_t VERB_INTERM = 3; // Print some intermediate results
static constexpr uint8_t VERB_DEBUG = 4; // Internals are logged
static constexpr uint8_t VERB_UNMAPPED = 5; // Unused so far
static constexpr uint8_t VERB_VVVVVV = 6; // What, can't I have a little fun?
static constexpr uint8_t VERB_NONE = 0; // Normal, no extra output
static constexpr uint8_t VERB_CFG = 1; // Print configuration after parsing options
static constexpr uint8_t VERB_LOG_ACT = 2; // Log actions before doing them
static constexpr uint8_t VERB_INTERM = 3; // Print some intermediate results
static constexpr uint8_t VERB_DEBUG = 4; // Internals are logged
static constexpr uint8_t VERB_TRACE = 5; // Step-by-step algorithm details
static constexpr uint8_t VERB_VVVVVV = 6; // What, can't I have a little fun?
[[gnu::format(printf, 3, 4)]] void verbosePrint(uint8_t level, char const *fmt, ...) const;

mutable bool hasTransparentPixels = false;
Expand Down
87 changes: 70 additions & 17 deletions src/gfx/pal_packing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ std::tuple<DefaultInitVec<size_t>, size_t>

for (; !queue.empty(); queue.pop()) {
ProtoPalAttrs const &attrs = queue.front(); // Valid until the `queue.pop()`
options.verbosePrint(Options::VERB_DEBUG, "Handling proto-pal %zu\n", attrs.protoPalIndex);
options.verbosePrint(
Options::VERB_TRACE, "Handling proto-palette %zu\n", attrs.protoPalIndex
);

ProtoPalette const &protoPal = protoPalettes[attrs.protoPalIndex];
size_t bestPalIndex = assignments.size();
Expand All @@ -391,9 +393,9 @@ std::tuple<DefaultInitVec<size_t>, size_t>

double relSize = assignments[i].relSizeOf(protoPal);
options.verbosePrint(
Options::VERB_DEBUG,
"%zu/%zu: Rel size: %f (size = %zu)\n",
i + 1,
Options::VERB_TRACE,
" Relative size to palette %zu (of %zu): %.20f (size = %zu)\n",
i,
assignments.size(),
relSize,
protoPal.size()
Expand All @@ -406,45 +408,96 @@ std::tuple<DefaultInitVec<size_t>, size_t>

if (bestPalIndex == assignments.size()) {
// Found nowhere to put it, create a new page containing just that one
options.verbosePrint(
Options::VERB_TRACE,
"Assigning proto-palette %zu to new palette %zu\n",
attrs.protoPalIndex,
bestPalIndex
);
assignments.emplace_back(protoPalettes, std::move(attrs));
} else {
options.verbosePrint(
Options::VERB_TRACE,
"Assigning proto-palette %zu to palette %zu\n",
attrs.protoPalIndex,
bestPalIndex
);
auto &bestPal = assignments[bestPalIndex];
// Add the color to that palette
bestPal.assign(std::move(attrs));

// If this overloads the palette, get it back to normal (if possible)
while (bestPal.volume() > options.maxOpaqueColors()) {
options.verbosePrint(
Options::VERB_DEBUG,
Options::VERB_TRACE,
"Palette %zu is overloaded! (%zu > %" PRIu8 ")\n",
bestPalIndex,
bestPal.volume(),
options.maxOpaqueColors()
);

// Look for a proto-pal minimizing "efficiency" (size / rel_size)
auto efficiency = [&bestPal](ProtoPalette const &pal) {
return pal.size() / bestPal.relSizeOf(pal);
};
auto [minEfficiencyIter, maxEfficiencyIter] = std::minmax_element(
RANGE(bestPal),
[&efficiency,
&protoPalettes](ProtoPalAttrs const &lhs, ProtoPalAttrs const &rhs) {
return efficiency(protoPalettes[lhs.protoPalIndex])
< efficiency(protoPalettes[rhs.protoPalIndex]);
[&bestPal, &protoPalettes](ProtoPalAttrs const &lhs, ProtoPalAttrs const &rhs) {
ProtoPalette const &lhsProtoPal = protoPalettes[lhs.protoPalIndex];
ProtoPalette const &rhsProtoPal = protoPalettes[rhs.protoPalIndex];
size_t lhsSize = lhsProtoPal.size();
size_t rhsSize = rhsProtoPal.size();
double lhsRelSize = bestPal.relSizeOf(lhsProtoPal);
double rhsRelSize = bestPal.relSizeOf(rhsProtoPal);

// This comparison is algebraically equivalent to
// `lhsSize / lhsRelSize < rhsSize / rhsRelSize`,
// but without potential precision loss from floating-point division.
options.verbosePrint(
Options::VERB_TRACE,
" Proto-palettes %zu <=> %zu: Efficiency: %zu / %.20f <=> %zu / "
"%.20f\n",
lhs.protoPalIndex,
rhs.protoPalIndex,
lhsSize,
lhsRelSize,
rhsSize,
rhsRelSize
);
return lhsSize * rhsRelSize < rhsSize * lhsRelSize;
}
);

// All efficiencies are identical iff min equals max
// TODO: maybe not ideal to re-compute these two?
ProtoPalette const &minProtoPal = protoPalettes[minEfficiencyIter->protoPalIndex];
ProtoPalette const &maxProtoPal = protoPalettes[maxEfficiencyIter->protoPalIndex];
size_t minSize = minProtoPal.size();
size_t maxSize = maxProtoPal.size();
double minRelSize = bestPal.relSizeOf(minProtoPal);
double maxRelSize = bestPal.relSizeOf(maxProtoPal);
// This comparison is algebraically equivalent to
// `maxSize / maxRelSize - minSize / minRelSize < .001`,
// but without potential precision loss from floating-point division.
// TODO: yikes for float comparison! I *think* this threshold is OK?
if (efficiency(protoPalettes[maxEfficiencyIter->protoPalIndex])
- efficiency(protoPalettes[minEfficiencyIter->protoPalIndex])
< .001) {
options.verbosePrint(
Options::VERB_TRACE,
" Proto-palettes %zu <= %zu: Efficiency: %zu / %.20f <= %zu / %.20f\n",
minEfficiencyIter->protoPalIndex,
maxEfficiencyIter->protoPalIndex,
minSize,
minRelSize,
maxSize,
maxRelSize
);
if (maxSize * minRelSize - minSize * maxRelSize < minRelSize * maxRelSize * .001) {
options.verbosePrint(Options::VERB_TRACE, " All efficiencies are identical\n");
break;
}

// Remove the proto-pal with minimal efficiency
options.verbosePrint(
Options::VERB_TRACE,
" Removing proto-palette %zu\n",
minEfficiencyIter->protoPalIndex
);
queue.emplace(std::move(*minEfficiencyIter));
queue.back().banFrom(bestPalIndex); // Ban it from this palette
bestPal.remove(minEfficiencyIter);
Expand Down Expand Up @@ -479,15 +532,15 @@ std::tuple<DefaultInitVec<size_t>, size_t>
if (iter == assignments.end()) { // No such page, create a new one
options.verbosePrint(
Options::VERB_DEBUG,
"Adding new palette (%zu) for overflowing proto-pal %zu\n",
"Adding new palette (%zu) for overflowing proto-palette %zu\n",
assignments.size(),
attrs.protoPalIndex
);
assignments.emplace_back(protoPalettes, std::move(attrs));
} else {
options.verbosePrint(
Options::VERB_DEBUG,
"Assigning overflowing proto-pal %zu to palette %zu\n",
"Assigning overflowing proto-palette %zu to palette %zu\n",
attrs.protoPalIndex,
iter - assignments.begin()
);
Expand Down
1 change: 1 addition & 0 deletions test/gfx/color_curve.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-C
Binary file added test/gfx/color_curve.out.2bpp
Binary file not shown.
Binary file added test/gfx/color_curve.out.pal
Binary file not shown.
Binary file added test/gfx/color_curve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.