aboutsummaryrefslogtreecommitdiff
path: root/engines/mads/palette.h
blob: 3af0c2805b6eb5b1d5ca16b329dcb3316bab6570 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef MADS_PALETTE_H
#define MADS_PALETTE_H

#include "common/scummsys.h"
#include "common/stream.h"

namespace MADS {

class MADSEngine;

#define PALETTE_USAGE_COUNT 4

#define PALETTE_RESERVED_LOW_COUNT 18
#define PALETTE_RESERVED_HIGH_COUNT 10

#define PALETTE_COUNT 256
#define PALETTE_SIZE (256 * 3)

/**
 * Palette mapping options
 */
enum {
	PALFLAG_BACKGROUND		= 0x8000,  // Loading initial background
	PALFLAG_RESERVED		= 0x4000,  // Enable mapping reserved colors
	PALFLAG_ANY_TO_CLOSEST	= 0x2000,  // Any color can map to closest
	PALFLAG_ALL_TO_CLOSEST	= 0x1000,  // Any colors that can map must map
	PALFLAG_TOP_COLORS		= 0x0800,  // Allow mapping to high four colors
	PALFLAG_DEFINE_RESERVED	= 0x0400,  // Define initial reserved color
	PALFLAG_MASK			= 0xfc00   // Mask for all the palette flags
};

struct PaletteCycle {
	byte _colorCount;
	byte _firstListColor;
	byte _firstColorIndex;
	byte _ticks;

	PaletteCycle() { _colorCount = _firstListColor = _firstColorIndex = _ticks = 0; }
};

struct RGB6 {
	byte r;
	byte g;
	byte b;
	byte _palIndex;
	byte _u2;
	byte _flags;

	/**
	 * Load an entry from a stream
	 */
	void load(Common::SeekableReadStream *f);
};

class PaletteUsage {
public:
	struct UsageEntry {
		uint16 _palIndex;
		int _sortValue;

		UsageEntry(int palIndex) { _palIndex = palIndex; _sortValue = -1; }
		UsageEntry() { _palIndex = 0; _sortValue = 0; }
	};
	struct UsageRange {
		byte _v1, _v2;

		UsageRange(byte v1, byte v2) { _v1 = v1; _v2 = v2; }
	};
private:
	MADSEngine *_vm;
	Common::Array<UsageEntry> *_data;

	int getGamePalFreeIndex(int *palIndex);

	int rgbFactor(byte *palEntry, RGB6 &pal6);

	Common::Array<UsageEntry> _nullUsage;
public:
	/**
	 * Constructor
	 */
	PaletteUsage(MADSEngine *vm);

	void load(Common::Array<UsageEntry> *data);

	/**
	 * Returns whether the usage list is empty
	 */
	bool empty() const { return _data == nullptr; }

	uint16 &operator[](int index) { return (*_data)[index]._palIndex; }

	/**
	 * Assigns the class to an empty usage array
	 */
	void setEmpty() { _data = &_nullUsage; }

	/**
	 * Gets key entries from the passed palette
	 * @param palette	6-bit per entry read in palette
	 */
	void getKeyEntries(Common::Array<RGB6> &palette);

	/**
	 * Prioritizes the palette index list based on the intensity of the
	 * RGB values of the palette entries that they refer to
	 */
	void prioritize(Common::Array<RGB6> &palette);

	int process(Common::Array<RGB6> &palette, uint flags);

	void transform(Common::Array<RGB6> &palette);

	void updateUsage(Common::Array<int> &usageList, int sceneUsageIndex);

	void resetPalFlags(int idx);
};

class RGBList {
private:
	bool _data[32];
public:
	RGBList() { clear(); }

	void clear();

	void reset();

	/**
	 * Copies the data from another instance
	 */
	void copy(RGBList &src);

	/**
	 * Scans for a free slot
	 */
	int scan();

	bool &operator[](int idx) { return _data[idx]; }
};

class Fader {
public:
	struct GreyEntry {
		byte _intensity;
		byte _mapColor;
		uint16 _accum[3];
	};
private:
	void mapToGreyRamp(byte palette[PALETTE_SIZE], int baseColor, int numColors,
		int baseGrey, int numGreys, GreyEntry *map);

	void getGreyValues(const byte palette[PALETTE_SIZE], byte greyList[PALETTE_COUNT],
		int baseColor, int numColors);

	/**
	 * Given a grey value list containing grey shades (0-63), creates a 64 byte
	 * grey table containing the number of grey values for each intensity
	 */
	void greyPopularity(const byte greyList[PALETTE_COUNT], byte greyTable[64], int numColors);

	/**
	 * Does an insertion sort of a given id/value array pair
	 */
	void insertionSort(int size, byte *id, byte *value);
protected:
	MADSEngine *_vm;
public:
	bool _colorFlags[4];
	int _colorValues[4];
public:
	/**
	 * Constructor
	 */
	Fader(MADSEngine *vm);

	/**
	* Sets a new palette
	*/
	void setPalette(const byte *colors, uint start, uint num);

	/**
	* Returns a subset of the currently loaded palette
	*/
	void grabPalette(byte *colors, uint start, uint num);

	/**
	* Gets the entire palette at once
	*/
	void getFullPalette(byte palette[PALETTE_SIZE]) {
		grabPalette(&palette[0], 0, PALETTE_COUNT);
	}

	/**
	* Sets the entire palette at once
	*/
	void setFullPalette(byte palette[PALETTE_SIZE]) {
		setPalette(&palette[0], 0, PALETTE_COUNT);
	}

	/**
	* Calculates a merge/hash for a given palette entry
	*/
	static int rgbMerge(byte r, byte g, byte b);

	/**
	* Calculates a merge/hash for a given palette entry
	*/
	static int rgbMerge(RGB6 &palEntry);

	/**
	* Fades the given palette to greyscale
	*/
	void fadeToGrey(byte palette[PALETTE_SIZE], byte *paletteMap,
		int baseColor, int numColors, int baseGrey, int numGreys,
		int tickDelay, int steps);
};

class Palette: public Fader {
private:
	/**
	 * Initialises the first 16 palette indexes with the equivalent
	 * standard VGA palette
	 */
	void initVGAPalette(byte *palette);
public:
	byte _mainPalette[PALETTE_SIZE];
	byte _cyclingPalette[PALETTE_SIZE];
	uint32 _palFlags[PALETTE_COUNT];
	PaletteUsage _paletteUsage;
	RGBList _rgbList;
	bool _lockFl;
	int _lowRange;
	int _highRange;
public:
	/**
	 * Constructor
	 */
	Palette(MADSEngine *vm);

	/**
	 * Destructor
	 */
	virtual ~Palette() {}

	/**
	* Set a palette entry
	*/
	void setEntry(byte palIndex, byte r, byte g, byte b);

	/**
	 * Returns the palette index in the palette that most closely matches the
	 * specified RGB pair
	 */
	uint8 palIndexFromRgb(byte r, byte g, byte b, byte *paletteData = nullptr);

	/**
	 * Sets a small set of system/core colors needed by the game
	 */
	void setSystemPalette();

	/**
	 * Update a range of an arbitrary palette
	 */
	static void setGradient(byte *palette, int start, int count, int rgbValue1, int rgbValue2);

	/**
	 * Resets the game palette
	 */
	void resetGamePalette(int v1, int v2);

	/**
	 * Initialises the main palette
	 */
	void initPalette();

	/**
	 * Set the first four palette entries with preset values
	 */
	void setLowRange();

	/**
	 * Set up the palette as the game ends
	 */
	void close() {
		warning("TODO: Palette::close");
	}

	void setColorFlags(byte r, byte g, byte b);
	void setColorValues(byte r, byte g, byte b);
	void fadeOut(byte palette[PALETTE_SIZE], int start, int count, int v1, int v2, int v3, int v4, int v5, int v6);

	void lock();
	void unlock();

	void refreshSceneColors();
};

} // End of namespace MADS

#endif /* MADS_PALETTE_H */