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