1// Aseprite Document Library
2// Copyright (c) 2020-2022 Igara Studio S.A.
3// Copyright (c) 2001-2016 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 DOC_RGBMAP_RGB5A3_H_INCLUDED
9#define DOC_RGBMAP_RGB5A3_H_INCLUDED
10#pragma once
11
12#include "base/debug.h"
13#include "base/disable_copying.h"
14#include "doc/object.h"
15#include "doc/rgbmap.h"
16
17#include <vector>
18
19namespace doc {
20
21 class Palette;
22
23 // It acts like a cache for Palette:findBestfit() calls.
24 class RgbMapRGB5A3 : public RgbMap {
25 // Bit activated on m_map entries that aren't yet calculated.
26 const int INVALID = 256;
27
28 public:
29 RgbMapRGB5A3();
30
31 // RgbMap impl
32 void regenerateMap(const Palette* palette, int maskIndex) override;
33 int mapColor(const color_t rgba) const override {
34 const int r = rgba_getr(rgba);
35 const int g = rgba_getg(rgba);
36 const int b = rgba_getb(rgba);
37 const int a = rgba_geta(rgba);
38 // bits -> bbbbbgggggrrrrraaa
39 const int i = (a>>5) | ((b>>3) << 3) | ((g>>3) << 8) | ((r>>3) << 13);
40 const int v = m_map[i];
41 return (v & INVALID) ? generateEntry(i, r, g, b, a): v;
42 }
43
44 int maskIndex() const override { return m_maskIndex; }
45
46 private:
47 int generateEntry(int i, int r, int g, int b, int a) const;
48
49 mutable std::vector<uint16_t> m_map;
50 const Palette* m_palette;
51 int m_modifications;
52 int m_maskIndex;
53
54 DISABLE_COPYING(RgbMapRGB5A3);
55 };
56
57} // namespace doc
58
59#endif
60