aboutsummaryrefslogtreecommitdiff
path: root/engines/scumm/he/palette_he.cpp
blob: c7ed9e64a8c7165a42fb2718d056247e37c3aefa (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#ifdef ENABLE_HE

#include "common/system.h"
#include "scumm/scumm.h"
#include "scumm/he/intern_he.h"
#include "scumm/resource.h"
#include "scumm/util.h"

namespace Scumm {

void ScummEngine_v71he::remapHEPalette(const uint8 *src, uint8 *dst) {
	int r, g, b, sum, bestitem, bestsum;
	int ar, ag, ab;
	uint8 *palPtr;
	src += 30;

	if (_game.heversion >= 99) {
		palPtr = _hePalettes + _hePaletteSlot + 30;
	} else {
		palPtr = _currentPalette + 30;
	}

	for (int j = 10; j < 246; j++) {
		bestitem = 0xFFFF;
		bestsum = 0xFFFF;

		r = *src++;
		g = *src++;
		b = *src++;

		uint8 *curPal = palPtr;

		for (int k = 10; k < 246; k++) {
			ar = r - *curPal++;
			ag = g - *curPal++;
			ab = b - *curPal++;

			sum = (ar * ar) + (ag * ag) + (ab * ab);

			if (bestitem == 0xFFFF || sum <= bestsum) {
				bestitem = k;
				bestsum = sum;
			}
		}

		dst[j] = bestitem;
	}
}

uint8 *ScummEngine_v90he::getHEPaletteIndex(int palSlot) {
	if (palSlot) {
		assert(palSlot >= 1 && palSlot <= _numPalettes);
		return _hePalettes + palSlot * _hePaletteSlot;
	} else {
		return _hePalettes + _hePaletteSlot;
	}
}

int ScummEngine_v90he::getHEPaletteSimilarColor(int palSlot, int red, int green, int start, int end) {
	assertRange(1, palSlot, _numPalettes, "palette");
	assertRange(0, start, 255, "start palette slot");
	assertRange(0, end, 255, "pend alette slot");

	uint8 *pal = _hePalettes + palSlot * _hePaletteSlot + start * 3;

	int bestsum = 0x7FFFFFFF;
	int bestitem = start;

	for (int i = start; i <= end; i++) {
		int dr = red - pal[0];
		int dg = green - pal[1];
		int sum = dr * dr + dg * dg * 2;
		if (sum == 0) {
			return i;
		}
		if (sum < bestsum) {
			bestsum = sum;
			bestitem = i;
		}
		pal += 3;
	}
	return bestitem;
}

int ScummEngine_v90he::getHEPalette16BitColorComponent(int component, int type) {
	uint16 col;
	if (type == 2) {
		col = (((component & 0xFFFF) >>  0) & 0x1F) << 3;
	} else if (type == 1) {
		col = (((component & 0xFFFF) >>  5) & 0x1F) << 3;
	} else {
		col = (((component & 0xFFFF) >> 10) & 0x1F) << 3;
	}
	return col;
}

int ScummEngine_v90he::getHEPaletteColorComponent(int palSlot, int color, int component) {
	assertRange(1, palSlot, _numPalettes, "palette");
	assertRange(0, color, 255, "palette slot");

	return _hePalettes[palSlot * _hePaletteSlot + color * 3 + component % 3];
}

int ScummEngine_v90he::getHEPaletteColor(int palSlot, int color) {
	assertRange(1, palSlot, _numPalettes, "palette");
	assertRange(0, color, 255, "palette slot");

	if (_game.features & GF_16BIT_COLOR)
		return READ_LE_UINT16(_hePalettes + palSlot * _hePaletteSlot + 768 + color * 2);
	else
		return _hePalettes[palSlot * _hePaletteSlot + 768 + color];
}

void ScummEngine_v90he::setHEPaletteColor(int palSlot, uint8 color, uint8 r, uint8 g, uint8 b) {
	debug(7, "setHEPaletteColor(%d, %d, %d, %d, %d)", palSlot, color, r, g, b);
	assertRange(1, palSlot, _numPalettes, "palette");

	uint8 *p = _hePalettes + palSlot * _hePaletteSlot + color * 3;
	*(p + 0) = r;
	*(p + 1) = g;
	*(p + 2) = b;
	if (_game.features & GF_16BIT_COLOR) {
		WRITE_LE_UINT16(_hePalettes + palSlot * _hePaletteSlot + 768 + color * 2, get16BitColor(r, g, b));
	} else {
		_hePalettes[palSlot * _hePaletteSlot + 768 + color] = color;
	}
}

void ScummEngine_v90he::setHEPaletteFromPtr(int palSlot, const uint8 *palData) {
	assertRange(1, palSlot, _numPalettes, "palette");

	uint8 *pc = _hePalettes + palSlot * _hePaletteSlot;
	uint8 *pi = pc + 768;
	if (_game.features & GF_16BIT_COLOR) {
		for (int i = 0; i < 256; ++i) {
			uint8 r = *pc++ = *palData++;
			uint8 g = *pc++ = *palData++;
			uint8 b = *pc++ = *palData++;
			WRITE_LE_UINT16(pi, get16BitColor(r, g, b)); pi += 2;
		}
	} else {
		for (int i = 0; i < 256; ++i) {
			*pc++ = *palData++;
			*pc++ = *palData++;
			*pc++ = *palData++;
			*pi++ = i;
		}
	}

	int i;
	uint8 *palPtr = _hePalettes + palSlot * _hePaletteSlot + 768;
	if (_game.features & GF_16BIT_COLOR) {
		for (i = 0; i < 10; ++i)
			WRITE_LE_UINT16(palPtr + i * 2, i);
		for (i = 246; i < 256; ++i)
			WRITE_LE_UINT16(palPtr + i * 2, i);
	} else {
		for (i = 0; i < 10; ++i)
			*(palPtr + i) = i;
		for (i = 246; i < 256; ++i)
			*(palPtr + i) = i;
	}
}

void ScummEngine_v90he::setHEPaletteFromCostume(int palSlot, int resId) {
	debug(7, "setHEPaletteFromCostume(%d, %d)", palSlot, resId);
	assertRange(1, palSlot, _numPalettes, "palette");
	const uint8 *data = getResourceAddress(rtCostume, resId);
	assert(data);
	const uint8 *rgbs = findResourceData(MKID_BE('RGBS'), data);
	assert(rgbs);
	setHEPaletteFromPtr(palSlot, rgbs);
}

void ScummEngine_v90he::setHEPaletteFromImage(int palSlot, int resId, int state) {
	debug(7, "setHEPaletteFromImage(%d, %d, %d)", palSlot, resId, state);
	assertRange(1, palSlot, _numPalettes, "palette");
	uint8 *data = getResourceAddress(rtImage, resId);
	assert(data);
	const uint8 *rgbs = findWrappedBlock(MKID_BE('RGBS'), data, state, 0);
	if (rgbs)
		setHEPaletteFromPtr(palSlot, rgbs);
}

void ScummEngine_v90he::setHEPaletteFromRoom(int palSlot, int resId, int state) {
	debug(7, "setHEPaletteFromRoom(%d, %d, %d)", palSlot, resId, state);
	assertRange(1, palSlot, _numPalettes, "palette");
	const uint8 *data = getResourceAddress(rtRoom, resId);
	assert(data);
	const uint8 *pals = findResourceData(MKID_BE('PALS'), data);
	assert(pals);
	const uint8 *rgbs = findPalInPals(pals, state);
	assert(rgbs);
	setHEPaletteFromPtr(palSlot, rgbs);
}

void ScummEngine_v90he::restoreHEPalette(int palSlot) {
	debug(7, "restoreHEPalette(%d)", palSlot);
	assertRange(1, palSlot, _numPalettes, "palette");

	if (palSlot != 1) {
		memcpy(_hePalettes + palSlot * _hePaletteSlot, _hePalettes + _hePaletteSlot, _hePaletteSlot);
	}
}

void ScummEngine_v90he::copyHEPalette(int dstPalSlot, int srcPalSlot) {
	debug(7, "copyHEPalette(%d, %d)", dstPalSlot, srcPalSlot);
	assert(dstPalSlot >= 1 && dstPalSlot <= _numPalettes);
	assert(srcPalSlot >= 1 && srcPalSlot <= _numPalettes);

	if (dstPalSlot != srcPalSlot) {
		memcpy(_hePalettes + dstPalSlot * _hePaletteSlot, _hePalettes + srcPalSlot * _hePaletteSlot, _hePaletteSlot);
	}
}

void ScummEngine_v90he::copyHEPaletteColor(int palSlot, uint8 dstColor, uint16 srcColor) {
	debug(7, "copyHEPaletteColor(%d, %d, %d)", palSlot, dstColor, srcColor);
	assertRange(1, palSlot, _numPalettes, "palette");

	uint8 *dstPal = _hePalettes + palSlot * _hePaletteSlot + dstColor * 3;
	uint8 *srcPal = _hePalettes + _hePaletteSlot + srcColor * 3;
	if (_game.features & GF_16BIT_COLOR) {
		dstPal[0] = (srcColor >> 10) << 3;
		dstPal[1] = (srcColor >>  5) << 3;
		dstPal[2] = (srcColor >>  0) << 3;
		WRITE_LE_UINT16(_hePalettes + palSlot * _hePaletteSlot + 768 + dstColor * 2, srcColor);
	} else {
		memcpy(dstPal, srcPal, 3);
		_hePalettes[palSlot * _hePaletteSlot + 768 + dstColor] = srcColor;
	}
}

void ScummEngine_v99he::setPaletteFromPtr(const byte *ptr, int numcolor) {
	int i;
	byte *dest, r, g, b;

	if (numcolor < 0) {
		numcolor = getResourceDataSize(ptr) / 3;
	}

	assertRange(0, numcolor, 256, "setPaletteFromPtr: numcolor");

	dest = _hePalettes + _hePaletteSlot;

	for (i = 0; i < numcolor; i++) {
		r = *ptr++;
		g = *ptr++;
		b = *ptr++;

		if (i == 15 || r < 252 || g < 252 || b < 252) {
			*dest++ = r;
			*dest++ = g;
			*dest++ = b;

			if (_game.features & GF_16BIT_COLOR) {
				WRITE_LE_UINT16(_hePalettes + 2048 + i * 2, get16BitColor(r, g, b));
			} else {
				_hePalettes[1792 + i] = i;
			}
		} else {
			dest += 3;
		}
	}

	memcpy(_hePalettes, _hePalettes + _hePaletteSlot, 768);

	if (_game.features & GF_16BIT_COLOR) {
		for (i = 0; i < 10; ++i)
			WRITE_LE_UINT16(_hePalettes + 2048 + i * 2, i);
		for (i = 246; i < 256; ++i)
			WRITE_LE_UINT16(_hePalettes + 2048 + i * 2, i);
	} else {
		for (i = 0; i < 10; ++i)
			_hePalettes[1792 + i] = i;
		for (i = 246; i < 256; ++i)
			_hePalettes[1792 + i] = i;
	}
	setDirtyColors(0, numcolor - 1);
}

void ScummEngine_v99he::darkenPalette(int redScale, int greenScale, int blueScale, int startColor, int endColor) {
	uint8 *src, *dst;
	int j, r, g, b;

	src = _hePalettes + startColor * 3;
	dst = _hePalettes + _hePaletteSlot + startColor * 3;
	for (j = startColor; j <= endColor; j++) {
		r = *src++;
		r = r * redScale / 0xFF;
		if (r > 255)
			r = 255;
		*dst++ = r;

		g = *src++;
		g = g * greenScale / 0xFF;
		if (g > 255)
			g = 255;
		*dst++ = g;

		b = *src++;
		b = b * blueScale / 0xFF;
		if (b > 255)
			b = 255;
		*dst++ = b;

		if (_game.features & GF_16BIT_COLOR) {
			WRITE_LE_UINT16(_hePalettes + 2048 + j * 2, get16BitColor(r, g, b));
		} else {
			_hePalettes[1792 + j] = j;
		}
		setDirtyColors(j, endColor);
	}
}

void ScummEngine_v99he::copyPalColor(int dst, int src) {
	byte *dp, *sp;

	if ((uint) dst >= 256 || (uint) src >= 256)
		error("copyPalColor: invalid values, %d, %d", dst, src);

	dp = &_hePalettes[_hePaletteSlot + dst * 3];
	sp = &_hePalettes[_hePaletteSlot + src * 3];

	dp[0] = sp[0];
	dp[1] = sp[1];
	dp[2] = sp[2];

	if (_game.features & GF_16BIT_COLOR) {
		WRITE_LE_UINT16(_hePalettes + 2048 + dst * 2, get16BitColor(sp[0], sp[1], sp[2]));
	} else {
		_hePalettes[1792 + dst] = dst;
	}

	setDirtyColors(dst, dst);
}

void ScummEngine_v99he::setPalColor(int idx, int r, int g, int b) {
	_hePalettes[_hePaletteSlot + idx * 3 + 0] = r;
	_hePalettes[_hePaletteSlot + idx * 3 + 1] = g;
	_hePalettes[_hePaletteSlot + idx * 3 + 2] = b;

	if (_game.features & GF_16BIT_COLOR) {
		WRITE_LE_UINT16(_hePalettes + 2048 + idx * 2, get16BitColor(r, g, b));
	} else {
		_hePalettes[1792 + idx] = idx;
	}
	setDirtyColors(idx, idx);
}

void ScummEngine_v99he::updatePalette() {
	if (_game.features & GF_16BIT_COLOR)
		return;

	if (_palDirtyMax == -1)
		return;

	int num = _palDirtyMax - _palDirtyMin + 1;
	int i;

	byte palette_colors[1024];
	byte *p = palette_colors;

	for (i = _palDirtyMin; i <= _palDirtyMax; i++) {
		byte *data = _hePalettes + 1024 + i * 3;

		*p++ = data[0];
		*p++ = data[1];
		*p++ = data[2];
		*p++ = 0;
	}

	_system->getPaletteManager()->setPalette(palette_colors, _palDirtyMin, num);

	_palDirtyMax = -1;
	_palDirtyMin = 256;
}

} // End of namespace Scumm

#endif // ENABLE_HE