File indexing completed on 2025-03-09 04:00:22

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 
0010 #include <QImage>
0011 #include <vector>
0012 
0013 namespace glaxnimate::utils::quantize {
0014 
0015 
0016 using ColorFrequency = std::pair<QRgb, int>;
0017 
0018 /**
0019  * \brief Returns the \p k colors that appear most frequently in \p image.
0020  */
0021 std::vector<QRgb> k_modes(const QImage& image, int k);
0022 
0023 
0024 enum KMeansMatch
0025 {
0026     None,
0027     MostFrequent,
0028     Closest,
0029 };
0030 
0031 /**
0032  * \brief k-means Algorithm
0033  */
0034 std::vector<QRgb> k_means(const QImage& image, int k, int iterations, KMeansMatch match);
0035 
0036 
0037 /**
0038  * \brief Octree Algorithm
0039  */
0040 std::vector<QRgb> octree(const QImage& image, int k);
0041 
0042 
0043 /**
0044  * \brief Edge cutoff
0045  *
0046  * Selects the most frequent color, then excludes areas within 1 pixel of that color
0047  * Then it repeats until all the colors have been found
0048  *
0049  * \param image Image to get the colors for
0050  * \param max_colors Maximum number of colors
0051  * \param min_frequency A color must have at least min_frequency * image.width * image.height pixels to be selected
0052  */
0053 std::vector<QRgb> edge_exclusion_modes(const QImage& image, int max_colors, qreal min_frequency = 0.0005);
0054 
0055 /**
0056  * \brief Counts pixel values and returns a list of [rgba, count] pairs
0057  * \param image             The image to analyze
0058  * \param alpha_threshold   Minimum alpha value [0-255] for a color to be included
0059  */
0060 std::vector<ColorFrequency> color_frequencies(const QImage& image, int alpha_threshold = 128);
0061 
0062 /**
0063  * \brief Returns a quantized image with the given colors
0064  */
0065 QImage quantize(const QImage& source, const std::vector<QRgb>& colors);
0066 
0067 } // namespace glaxnimate::utils::quantize