diff --git a/sirc-tiledit/libs/shared/src/mediancutquantizer.cpp b/sirc-tiledit/libs/shared/src/mediancutquantizer.cpp index 5008ea3..af3e1b0 100644 --- a/sirc-tiledit/libs/shared/src/mediancutquantizer.cpp +++ b/sirc-tiledit/libs/shared/src/mediancutquantizer.cpp @@ -85,30 +85,6 @@ findChannelWithMostRange(const std::vector &originalPalette) { return ImageChannel::B; } -std::vector> -// NOLINTNEXTLINE(misc-no-recursion) -quantizePalette(const std::vector &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(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 buildPaletteMapping( const std::vector> &quantizedColorPairs, std::vector originalPalette, @@ -138,6 +114,31 @@ std::vector deduplicatePalette( return {quantizedPaletteSet.begin(), quantizedPaletteSet.end()}; } +std::vector> +// NOLINTNEXTLINE(misc-no-recursion) +splitPaletteIntoBucketsAndAverage(const std::vector &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(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); @@ -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(