1 // Aseprite Rener Library
2 // Copyright (c) 2001-2015, 2017 David Capello
3 //
4 // This file is released under the terms of the MIT license.
5 // Read LICENSE.txt for more information.
6 
7 #ifndef RENDER_QUANTIZATION_H_INCLUDED
8 #define RENDER_QUANTIZATION_H_INCLUDED
9 #pragma once
10 
11 #include "doc/frame.h"
12 #include "doc/pixel_format.h"
13 #include "render/color_histogram.h"
14 #include "render/dithering_algorithm.h"
15 
16 #include <vector>
17 
18 namespace doc {
19   class Image;
20   class Palette;
21   class RgbMap;
22   class Sprite;
23 }
24 
25 namespace render {
26   class DitheringMatrix;
27   class TaskDelegate;
28 
29   class PaletteOptimizer {
30   public:
31     void feedWithImage(doc::Image* image, bool withAlpha);
32     void feedWithRgbaColor(doc::color_t color);
33     void calculate(doc::Palette* palette, int maskIndex);
34 
35   private:
36     render::ColorHistogram<5, 6, 5, 5> m_histogram;
37   };
38 
39   // Creates a new palette suitable to quantize the given RGB sprite to Indexed color.
40   doc::Palette* create_palette_from_sprite(
41     const doc::Sprite* sprite,
42     const doc::frame_t fromFrame,
43     const doc::frame_t toFrame,
44     const bool withAlpha,
45     doc::Palette* newPalette, // Can be NULL to create a new palette
46     TaskDelegate* delegate);
47 
48   // Changes the image pixel format. The dithering method is used only
49   // when you want to convert from RGB to Indexed.
50   Image* convert_pixel_format(
51     const doc::Image* src,
52     doc::Image* dst,         // Can be NULL to create a new image
53     doc::PixelFormat pixelFormat,
54     render::DitheringAlgorithm ditheringAlgorithm,
55     const render::DitheringMatrix& ditheringMatrix,
56     const doc::RgbMap* rgbmap,
57     const doc::Palette* palette,
58     bool is_background,
59     doc::color_t new_mask_color,
60     TaskDelegate* delegate = nullptr);
61 
62 } // namespace render
63 
64 #endif
65