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 
33 enum ColorDistanceMethod {
34  kColorDistanceEuclidean,
35  kColorDistanceNaive,
36  kColorDistanceRedmean,
37 };
38 
42 constexpr int PALETTE_COUNT = 256;
43 constexpr int PALETTE_SIZE = (256 * 3);
44 
55 class Palette {
56  byte *_data;
57  uint16 _size;
58  DisposeAfterUse::Flag _disposeAfterUse;
59 
60 public:
61  static const uint16 npos = 0xFFFF;
62 
68  Palette(uint size);
69 
76  Palette(const byte *data, uint size);
77 
85  Palette(byte *data, uint size, DisposeAfterUse::Flag disposeAfterUse);
86 
87  Palette(const Palette &p);
88 
89  ~Palette();
90 
94  static const Palette createEGAPalette();
95 
96  Palette &operator=(const Palette &rhs);
97  bool operator==(const Palette &rhs) const { return equals(rhs); }
98  bool operator!=(const Palette &rhs) const { return !equals(rhs); }
99 
100  bool equals(const Palette &p) const;
101 
102  bool contains(const Palette &p) const;
103 
104  const byte *data() const { return _data; }
105  uint size() const { return _size; }
106 
110  void clear();
111 
115  bool empty() const { return _size == 0; }
116 
123  void resize(uint newSize, bool preserve);
124 
125  void set(uint entry, byte r, byte g, byte b) {
126  assert(entry < _size);
127  _data[entry * 3 + 0] = r;
128  _data[entry * 3 + 1] = g;
129  _data[entry * 3 + 2] = b;
130  }
131 
132  void get(uint entry, byte &r, byte &g, byte &b) const {
133  assert(entry < _size);
134  r = _data[entry * 3 + 0];
135  g = _data[entry * 3 + 1];
136  b = _data[entry * 3 + 2];
137  }
138 
144  uint find(byte r, byte g, byte b) const {
145  for (uint i = 0; i < _size; i++) {
146  if (_data[i * 3 + 0] == r && _data[i * 3 + 1] == g && _data[i * 3 + 2] == b)
147  return i;
148  }
149  return npos;
150  }
151 
159  byte findBestColor(byte r, byte g, byte b, ColorDistanceMethod method = kColorDistanceRedmean) const;
160 
172  void set(const byte *colors, uint start, uint num);
173  void set(const Palette &p, uint start, uint num);
174 
185  void grab(byte *colors, uint start, uint num) const;
186  void grab(Palette &p, uint start, uint num) const;
187 };
188 
190 public:
191  PaletteLookup();
198  PaletteLookup(const byte *palette, uint len);
199 
209  bool setPalette(const byte *palette, uint len);
210 
219  byte findBestColor(byte r, byte g, byte b, ColorDistanceMethod method = kColorDistanceRedmean);
220 
231  uint32 *createMap(const byte *srcPalette, uint len, ColorDistanceMethod method = kColorDistanceRedmean);
232 
233 private:
234  Palette _palette;
235  uint _paletteSize;
236  Common::HashMap<int, byte> _colorHash;
237 };
238 
239 } // // end of namespace Graphics
240 #endif
bool empty() const
Definition: palette.h:115
Palette(uint size)
Construct a new Palette object.
byte findBestColor(byte r, byte g, byte b, ColorDistanceMethod method=kColorDistanceRedmean) const
Definition: palette.h:189
Definition: formatinfo.h:28
uint find(byte r, byte g, byte b) const
Definition: palette.h:144
Simple class for handling a palette data.
Definition: palette.h:55
void grab(byte *colors, uint start, uint num) const
static const Palette createEGAPalette()
void resize(uint newSize, bool preserve)