ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
palette.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef GRAPHICS_PALETTE_H
23 #define GRAPHICS_PALETTE_H
24 
25 #include "common/hashmap.h"
26 
27 namespace Graphics {
28 
29 enum ColorDistanceMethod {
30  kColorDistanceEuclidean,
31  kColorDistanceNaive,
32  kColorDistanceRedmean,
33 };
34 
38 constexpr int PALETTE_COUNT = 256;
39 constexpr int PALETTE_SIZE = (256 * 3);
40 
51 class Palette {
52  byte *_data;
53  uint16 _size;
54 
55 public:
56  static const uint16 npos = 0xFFFF;
57 
63  Palette(uint size);
64 
71  Palette(const byte *data, uint size);
72 
73  Palette(const Palette &p);
74 
75  ~Palette();
76 
80  static Palette createEGAPalette();
81 
82  Palette &operator=(const Palette &rhs);
83  bool operator==(const Palette &rhs) const { return equals(rhs); }
84  bool operator!=(const Palette &rhs) const { return !equals(rhs); }
85 
86  bool equals(const Palette &p) const;
87 
88  bool contains(const Palette &p) const;
89 
90  const byte *data() const { return _data; }
91  uint size() const { return _size; }
92 
96  void clear();
97 
104  void resize(uint newSize, bool preserve);
105 
106  void set(uint entry, byte r, byte g, byte b) {
107  assert(entry < _size);
108  _data[entry * 3 + 0] = r;
109  _data[entry * 3 + 1] = g;
110  _data[entry * 3 + 2] = b;
111  }
112 
113  void get(uint entry, byte &r, byte &g, byte &b) const {
114  assert(entry < _size);
115  r = _data[entry * 3 + 0];
116  g = _data[entry * 3 + 1];
117  b = _data[entry * 3 + 2];
118  }
119 
125  uint find(byte r, byte g, byte b) const {
126  for (uint i = 0; i < _size; i++) {
127  if (_data[i * 3 + 0] == r && _data[i * 3 + 1] == g && _data[i * 3 + 2] == b)
128  return i;
129  }
130  return npos;
131  }
132 
140  byte findBestColor(byte r, byte g, byte b, ColorDistanceMethod method = kColorDistanceRedmean) const;
141 
153  void set(const byte *colors, uint start, uint num);
154  void set(const Palette &p, uint start, uint num);
155 
166  void grab(byte *colors, uint start, uint num) const;
167  void grab(Palette &p, uint start, uint num) const;
168 };
169 
171 public:
172  PaletteLookup();
179  PaletteLookup(const byte *palette, uint len);
180 
190  bool setPalette(const byte *palette, uint len);
191 
200  byte findBestColor(byte r, byte g, byte b, ColorDistanceMethod method = kColorDistanceRedmean);
201 
212  uint32 *createMap(const byte *srcPalette, uint len, ColorDistanceMethod method = kColorDistanceRedmean);
213 
214 private:
215  Palette _palette;
216  uint _paletteSize;
217  Common::HashMap<int, byte> _colorHash;
218 };
219 
220 } // // end of namespace Graphics
221 #endif
Palette(uint size)
Construct a new Palette object.
byte findBestColor(byte r, byte g, byte b, ColorDistanceMethod method=kColorDistanceRedmean) const
Definition: palette.h:170
Definition: formatinfo.h:28
uint find(byte r, byte g, byte b) const
Definition: palette.h:125
Simple class for handling a palette data.
Definition: palette.h:51
static Palette createEGAPalette()
void grab(byte *colors, uint start, uint num) const
void resize(uint newSize, bool preserve)