Skip to content

Commit

Permalink
refactor(tiledit): put more important functions together
Browse files Browse the repository at this point in the history
  • Loading branch information
NoxHarmonium committed Aug 5, 2024
1 parent b418101 commit b3999c1
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions sirc-tiledit/libs/shared/src/mediancutquantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,6 @@ findChannelWithMostRange(const std::vector<SircColor> &originalPalette) {
return ImageChannel::B;
}

std::vector<std::pair<SircColor, SircColor>>
// NOLINTNEXTLINE(misc-no-recursion)
quantizePalette(const std::vector<SircColor> &palette,
const size_t maxBucketSize) {
if (palette.size() <= maxBucketSize) {
const auto average = componentWiseAverageOfAllColors(palette);
return pairWithValue(palette, average);
}

const auto channelWithMostRange = findChannelWithMostRange(palette);

const auto sortedPalette =
paletteSortedByChannel(palette, channelWithMostRange);

const long halfSize = static_cast<long>(sortedPalette.size() / 2);
const std::vector lowerPalette(sortedPalette.begin(),
sortedPalette.begin() + halfSize);
const std::vector upperPalette(sortedPalette.begin() + halfSize,
sortedPalette.end());

return concatVecs(quantizePalette(lowerPalette, maxBucketSize),
quantizePalette(upperPalette, maxBucketSize));
}

std::unordered_map<PaletteReference, PaletteReference> buildPaletteMapping(
const std::vector<std::pair<SircColor, SircColor>> &quantizedColorPairs,
std::vector<SircColor> originalPalette,
Expand Down Expand Up @@ -138,6 +114,31 @@ std::vector<SircColor> deduplicatePalette(
return {quantizedPaletteSet.begin(), quantizedPaletteSet.end()};
}

std::vector<std::pair<SircColor, SircColor>>
// NOLINTNEXTLINE(misc-no-recursion)
splitPaletteIntoBucketsAndAverage(const std::vector<SircColor> &palette,
const size_t maxBucketSize) {
if (palette.size() <= maxBucketSize) {
const auto average = componentWiseAverageOfAllColors(palette);
return pairWithValue(palette, average);
}

const auto channelWithMostRange = findChannelWithMostRange(palette);

const auto sortedPalette =
paletteSortedByChannel(palette, channelWithMostRange);

const long halfSize = static_cast<long>(sortedPalette.size() / 2);
const std::vector lowerPalette(sortedPalette.begin(),
sortedPalette.begin() + halfSize);
const std::vector upperPalette(sortedPalette.begin() + halfSize,
sortedPalette.end());

return concatVecs(
splitPaletteIntoBucketsAndAverage(lowerPalette, maxBucketSize),
splitPaletteIntoBucketsAndAverage(upperPalette, maxBucketSize));
}

SircImage MedianCutQuantizer::quantize(const SircImage &sircImage,
const PaletteReductionBpp bpp) const {
const auto maxPaletteSize = to_underlying(bpp);
Expand All @@ -151,7 +152,7 @@ SircImage MedianCutQuantizer::quantize(const SircImage &sircImage,
(existingPalette.size() + maxPaletteSize - 1) / maxPaletteSize;

const auto quantizedPalettePairs =
quantizePalette(existingPalette, maxBucketSize);
splitPaletteIntoBucketsAndAverage(existingPalette, maxBucketSize);
const auto quantizedPaletteWithoutDupes =
deduplicatePalette(quantizedPalettePairs);
const auto paletteMapping = buildPaletteMapping(
Expand Down

0 comments on commit b3999c1

Please sign in to comment.