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 MADS_PALETTE_H
23 #define MADS_PALETTE_H
24 
25 #include "common/scummsys.h"
26 #include "common/stream.h"
27 
28 namespace MADS {
29 namespace Nebular {
30 
31 class RexNebularEngine;
32 
33 #define PALETTE_USAGE_COUNT 4
34 
35 #define PALETTE_RESERVED_LOW_COUNT 18
36 #define PALETTE_RESERVED_HIGH_COUNT 10
37 
38 constexpr int PALETTE_COUNT = 256;
39 constexpr int RGB_SIZE = 3;
40 constexpr int PALETTE_SIZE = (256 * 3);
41 
45 enum {
46  PALFLAG_BACKGROUND = 0x8000, // Loading initial background
47  PALFLAG_RESERVED = 0x4000, // Enable mapping reserved colors
48  PALFLAG_ANY_TO_CLOSEST = 0x2000, // Any color can map to closest
49  PALFLAG_ALL_TO_CLOSEST = 0x1000, // Any colors that can map must map
50  PALFLAG_TOP_COLORS = 0x0800, // Allow mapping to high four colors
51  PALFLAG_DEFINE_RESERVED = 0x0400, // Define initial reserved color
52  PALFLAG_MASK = 0xfc00 // Mask for all the palette flags
53 };
54 
55 struct PaletteCycle {
56  byte _colorCount;
57  byte _firstListColor;
58  byte _firstColorIndex;
59  byte _ticks;
60 
61  PaletteCycle() {
62  _colorCount = _firstListColor = _firstColorIndex = _ticks = 0;
63  }
64 };
65 
66 struct RGB6 {
67  byte r;
68  byte g;
69  byte b;
70  byte _palIndex;
71  byte _u2;
72  byte _flags;
73 
77  void load(Common::SeekableReadStream *f);
78 };
79 
80 class PaletteUsage {
81 public:
82  struct UsageEntry {
83  uint16 _palIndex;
84  int _sortValue;
85 
86  UsageEntry(int palIndex) {
87  _palIndex = palIndex; _sortValue = -1;
88  }
89  UsageEntry() {
90  _palIndex = 0; _sortValue = 0;
91  }
92  };
93  struct UsageRange {
94  byte _v1, _v2;
95 
96  UsageRange(byte v1, byte v2) {
97  _v1 = v1; _v2 = v2;
98  }
99  };
100 private:
101  RexNebularEngine *_vm;
103 
104  int getGamePalFreeIndex(int *palIndex);
105 
106  int rgbFactor(byte *palEntry, RGB6 &pal6);
107 
108  Common::Array<UsageEntry> _nullUsage;
109 public:
114 
115  void load(Common::Array<UsageEntry> *data);
116 
120  bool empty() const {
121  return _data == nullptr;
122  }
123 
124  uint16 &operator[](int index) {
125  return (*_data)[index]._palIndex;
126  }
127 
131  void setEmpty() {
132  _data = &_nullUsage;
133  }
134 
139  void getKeyEntries(Common::Array<RGB6> &palette);
140 
145  void prioritize(Common::Array<RGB6> &palette);
146 
147  int process(Common::Array<RGB6> &palette, uint flags);
148 
149  void transform(Common::Array<RGB6> &palette);
150 
151  void updateUsage(Common::Array<int> &usageList, int sceneUsageIndex);
152 
153  void resetPalFlags(int idx);
154 
155  int checkRGB(const byte *rgb, int palStart, bool flag, int *palIndex);
156 };
157 
158 class RGBList {
159 private:
160  bool _data[32];
161 public:
162  RGBList() {
163  clear();
164  }
165 
166  void clear();
167 
168  void reset();
169 
173  void copy(RGBList &src);
174 
178  int scan();
179 
180  bool &operator[](int idx) {
181  return _data[idx];
182  }
183 };
184 
185 class Fader {
186 public:
187  struct GreyEntry {
188  byte _intensity;
189  byte _mapColor;
190  uint16 _accum[3];
191  };
192 private:
193  void mapToGreyRamp(byte palette[PALETTE_SIZE], int baseColor, int numColors,
194  int baseGrey, int numGreys, GreyEntry *map);
195 
196  void getGreyValues(const byte palette[PALETTE_SIZE], byte greyList[PALETTE_COUNT],
197  int baseColor, int numColors);
198 
203  void greyPopularity(const byte greyList[PALETTE_COUNT], byte greyTable[64], int numColors);
204 
208  void insertionSort(int size, byte *id, byte *value);
209 protected:
210  RexNebularEngine *_vm;
211  byte _rgb64Map[PALETTE_COUNT];
212 public:
213  bool _colorFlags[4];
214  int _colorValues[4];
215 public:
219  Fader(RexNebularEngine *vm);
220 
224  void setPalette(const byte *colors, uint start, uint num);
225 
229  void grabPalette(byte *colors, uint start, uint num);
230 
234  void getFullPalette(byte palette[PALETTE_SIZE]);
235 
239  void setFullPalette(byte palette[PALETTE_SIZE]);
240 
244  int rgbMerge(byte r, byte g, byte b);
245 
249  int rgbMerge(RGB6 &palEntry);
250 
254  void fadeOut(byte palette[PALETTE_SIZE], byte *paletteMap,
255  int baseColor, int numColors, int baseGrey, int numGreys,
256  int tickDelay, int steps);
257 
261  void fadeIn(byte palette[PALETTE_SIZE], byte destPalette[PALETTE_SIZE],
262  int baseColor, int numColors, int baseGrey, int numGreys,
263  int tickDelay, int steps);
264 };
265 
266 class Palette : public Fader {
267 private:
272  void initVGAPalette(byte *palette);
273 public:
274  byte _mainPalette[PALETTE_SIZE];
275  byte _cyclingPalette[PALETTE_SIZE];
276  uint32 _palFlags[PALETTE_COUNT];
277  PaletteUsage _paletteUsage;
278  RGBList _rgbList;
279  bool _lockFl;
280  int _lowRange;
281  int _highRange;
282 public:
287 
291  virtual ~Palette() {
292  }
293 
297  void setEntry(byte palIndex, byte r, byte g, byte b);
298 
303  uint8 palIndexFromRgb(byte r, byte g, byte b, byte *paletteData = nullptr);
304 
308  void setSystemPalette();
309 
313  static void setGradient(byte *palette, int start, int count, int rgbValue1, int rgbValue2);
314 
318  void resetGamePalette(int lowRange, int highRange);
319 
323  void initPalette();
324 
328  void setLowRange();
329 
330  void setColorFlags(byte r, byte g, byte b);
331  void setColorValues(byte r, byte g, byte b);
332 
333  void lock();
334  void unlock();
335 
336  void refreshSceneColors();
337 
338  static int closestColor(const byte *matchColor, const byte *refPalette,
339  int paletteInc, int count);
340 };
341 
342 } // namespace Nebular
343 } // namespace MADS
344 
345 #endif
Definition: palette.h:158
Definition: palette.h:185
Definition: nebular.h:51
Definition: array.h:52
Definition: palette.h:82
Definition: stream.h:745
Definition: palette.h:66
Out copy(In first, In last, Out dst)
Definition: algorithm.h:52
virtual ~Palette()
Definition: palette.h:291
Definition: palette.h:187
Definition: palette.h:80
bool empty() const
Definition: palette.h:120
void setEmpty()
Definition: palette.h:131
Definition: palette.h:55
Definition: sound_manager.h:38
Definition: palette.h:266