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