ScummVM API documentation
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 #include "common/types.h"
27 
28 #define PALETTE_6BIT_TO_8BIT(x) ((x) * 255 / 63)
29 #define PALETTE_8BIT_TO_6BIT(x) ((x) * 63 / 255)
30 
31 namespace Graphics {
32 
37 };
38 
42 constexpr int PALETTE_COUNT = 256;
43 constexpr int PALETTE_SIZE = (256 * 3);
44 
45 extern const byte HGC_A_PALETTE[6];
46 extern const byte HGC_G_PALETTE[6];
47 
48 
61 class Palette {
62  byte *_data;
63  uint16 _size;
64  DisposeAfterUse::Flag _disposeAfterUse;
65 
66 public:
67  static const uint16 npos = 0xFFFF;
68 
74  Palette(uint size = 0);
75 
82  Palette(const byte *data, uint size);
83 
91  Palette(byte *data, uint size, DisposeAfterUse::Flag disposeAfterUse);
92 
93  Palette(const Palette &p);
94 
95  ~Palette();
96 
100  static const Palette createEGAPalette();
101 
102  Palette &operator=(const Palette &rhs);
103  bool operator==(const Palette &rhs) const { return equals(rhs); }
104  bool operator!=(const Palette &rhs) const { return !equals(rhs); }
105 
106  bool equals(const Palette &p) const;
107 
108  bool contains(const Palette &p) const;
109 
110  const byte *data() const { return _data; }
111  uint size() const { return _size; }
112 
116  void clear();
117 
121  bool empty() const { return _size == 0; }
122 
129  void resize(uint newSize, bool preserve);
130 
131  void set(uint entry, byte r, byte g, byte b) {
132  assert(entry < _size);
133  _data[entry * 3 + 0] = r;
134  _data[entry * 3 + 1] = g;
135  _data[entry * 3 + 2] = b;
136  }
137 
138  void get(uint entry, byte &r, byte &g, byte &b) const {
139  assert(entry < _size);
140  r = _data[entry * 3 + 0];
141  g = _data[entry * 3 + 1];
142  b = _data[entry * 3 + 2];
143  }
144 
150  uint find(byte r, byte g, byte b) const {
151  for (uint i = 0; i < _size; i++) {
152  if (_data[i * 3 + 0] == r && _data[i * 3 + 1] == g && _data[i * 3 + 2] == b)
153  return i;
154  }
155  return npos;
156  }
157 
165  byte findBestColor(byte r, byte g, byte b, ColorDistanceMethod method = kColorDistanceRedmean) const;
166 
178  void set(const byte *colors, uint start, uint num);
179  void set(const Palette &p, uint start, uint num);
180 
191  void grab(byte *colors, uint start, uint num) const;
192  void grab(Palette &p, uint start, uint num) const;
193 };
194 
196 public:
197  PaletteLookup();
204  PaletteLookup(const byte *palette, uint len);
205 
215  bool setPalette(const byte *palette, uint len);
216 
225  byte findBestColor(byte r, byte g, byte b, ColorDistanceMethod method = kColorDistanceRedmean);
226 
237  uint32 *createMap(const byte *srcPalette, uint len, ColorDistanceMethod method = kColorDistanceRedmean);
238 
239 private:
240  Palette _palette;
241  uint _paletteSize;
242  Common::HashMap<int, byte> _colorHash;
243 };
244 
245 } // // end of namespace Graphics
246 #endif
bool empty() const
Definition: palette.h:121
byte findBestColor(byte r, byte g, byte b, ColorDistanceMethod method=kColorDistanceRedmean) const
ColorDistanceMethod
Definition: palette.h:33
Definition: palette.h:195
constexpr int PALETTE_COUNT
Definition: palette.h:42
Definition: formatinfo.h:28
uint find(byte r, byte g, byte b) const
Definition: palette.h:150
Non-Weighted distance.
Definition: palette.h:34
Weighted red 30%, green 50%, blue 20%.
Definition: palette.h:35
Palette(uint size=0)
Construct a new Palette object.
Simple class for handling a palette data.
Definition: palette.h:61
Common low-cost approximation.
Definition: palette.h:36
void grab(byte *colors, uint start, uint num) const
static const Palette createEGAPalette()
void resize(uint newSize, bool preserve)