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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
/* 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.
*
*/
#include "common/scummsys.h"
#include "engines/util.h"
#include "graphics/palette.h"
#include "mads/mads.h"
#include "mads/msurface.h"
#include "mads/staticres.h"
namespace MADS {
#define VGA_COLOR_TRANS(x) ((x) * 255 / 63)
void RGB6::load(Common::SeekableReadStream *f) {
r = VGA_COLOR_TRANS(f->readByte());
g = VGA_COLOR_TRANS(f->readByte());
b = VGA_COLOR_TRANS(f->readByte());
_palIndex = f->readByte();
_u2 = f->readByte();
_flags = f->readByte();
}
/*------------------------------------------------------------------------*/
PaletteUsage::PaletteUsage(MADSEngine *vm) {
_vm = vm;
_data = nullptr;
}
void PaletteUsage::load(Common::Array<UsageEntry> *data) {
_data = data;
}
void PaletteUsage::getKeyEntries(Common::Array<RGB6> &palette) {
_data->clear();
for (uint i = 0; i < palette.size(); ++i) {
byte *uPtr = &palette[i]._flags;
if ((*uPtr & 0x10) && _data->size() < 3) {
_data->push_back(UsageEntry(i));
}
}
}
static bool sortHelper(const PaletteUsage::UsageEntry &ue1, const PaletteUsage::UsageEntry &ue2) {
return ue1._sortValue < ue2._sortValue;
}
void PaletteUsage::prioritize(Common::Array<RGB6> &palette) {
for (uint i = 0; i < _data->size(); ++i) {
RGB6 &palEntry = palette[(*_data)[i]._palIndex];
(*_data)[i]._sortValue = rgbMerge(palEntry);
}
Common::sort(_data->begin(), _data->end(), sortHelper);
}
static bool rangeSorter(const PaletteUsage::UsageRange &ur1, const PaletteUsage::UsageRange &ur2) {
return ur1._v2 < ur2._v2;
}
int PaletteUsage::process(Common::Array<RGB6> &palette, uint flags) {
int palLow;
int palHigh = (flags & 0x800) ? 0x100 : 0xFC;
int palIdx;
PaletteUsage tempUsage(_vm);
Common::Array<UsageEntry> tempUsageData;
tempUsage.load(&tempUsageData);
if (flags & 0x4000) {
palLow = 0;
palIdx = palHigh;
} else {
palLow = _vm->_palette->_lowRange;
if ((PALETTE_COUNT - _vm->_palette->_highRange) > palHigh) {
palIdx = palHigh;
} else {
palIdx = PALETTE_COUNT - _vm->_palette->_highRange;
}
}
int rgbIndex = _vm->_palette->_rgbList.scan();
uint32 rgbMask = 1 << rgbIndex;
bool noUsageFlag = flags & 0x8000;
bool hasUsage = _data != nullptr;
bool flag1 = false;
if (hasUsage) {
if (noUsageFlag || _data->size() == 0)
hasUsage = false;
if (noUsageFlag && _data->size() > 0)
flag1 = true;
}
if (hasUsage) {
tempUsage.getKeyEntries(palette);
tempUsage.prioritize(palette);
}
int freeIndex;
int palCount = getGamePalFreeIndex(&freeIndex);
Common::Array<UsageRange> palRange;
for (uint palIndex = 0; palIndex < palette.size(); ++palIndex) {
byte pal2 = palIndex;
byte pal1 = 0;
if (!(palette[palIndex]._flags & 0x80)) {
pal1 = 0x40;
}
if (palette[palIndex]._flags & 0x60) {
pal1 |= 0x20;
}
palRange.push_back(UsageRange(pal1, pal2));
}
Common::sort(palRange.begin(), palRange.end(), rangeSorter);
int var3A = (flags & 0x4000) ? 0xffff : 0xfffe;
for (uint palIndex = 0; palIndex < palette.size(); ++palIndex) {
bool var48 = false;
int var4 = 0xffff;
int v1 = palRange[palIndex]._v2;
if (palette[v1]._flags & 8) {
var48 = true;
var4 = 0xFD;
}
if (hasUsage && palette[v1]._flags & 0x10) {
for (uint usageIndex = 0; usageIndex < tempUsage._data->size() && !var48; ++usageIndex) {
if ((*tempUsage._data)[usageIndex]._palIndex == palIndex) {
var48 = true;
int dataIndex = MIN(usageIndex, _data->size() - 1);
var4 = (*_data)[dataIndex]._palIndex;
}
}
}
if (flag1 && palette[palIndex]._flags & 0x10) {
for (uint usageIndex = 0; usageIndex < _data->size() && !var48; ++usageIndex) {
if ((*_data)[usageIndex]._palIndex == palIndex) {
var48 = true;
var4 = 0xF0 + usageIndex;
// Copy data into the high end of the main palette
RGB6 &pSrc = palette[palIndex];
byte *pDest = &_vm->_palette->_mainPalette[var4 * 3];
pDest[0] = pSrc.r;
pDest[1] = pSrc.g;
pDest[2] = pSrc.b;
}
}
}
if (!var48 && !noUsageFlag) {
int var2 = (palette[palIndex]._flags & 0x20) ||
(((flags & 0x2000) || (palette[palIndex]._flags & 0x4000)) &&
((flags & 0x1000) || (palCount == 0))) ? 0x7fff : 1;
int var36 = (palette[palIndex]._flags & 0x80) ? 0 : 2;
for (int idx = palLow; idx < palIdx; ++idx) {
uint32 v = _vm->_palette->_palFlags[idx];
if ((v & var3A) && !(v & var36)) {
int var10;
if (var2 > 1) {
var10 = rgbFactor(&_vm->_palette->_mainPalette[idx * 3], palette[palIndex]);
}
else if (_vm->_palette->_mainPalette[idx * 3] != palette[palIndex].r ||
_vm->_palette->_mainPalette[idx * 3 + 1] != palette[palIndex].g ||
_vm->_palette->_mainPalette[idx * 3 + 2] != palette[palIndex].b) {
var10 = 1;
} else {
var10 = 0;
}
if (var2 > var10) {
var48 = true;
var4 = idx;
var2 = var10;
}
}
}
}
if (!var48 && (!(flags & 0x1000) || (!(palette[palIndex]._flags & 0x60) && !(flags & 0x2000)))) {
for (int idx = freeIndex; idx < palIdx && !var48; ++idx) {
if (!_vm->_palette->_palFlags[idx]) {
--palCount;
++freeIndex;
var48 = true;
var4 = idx;
RGB6 &pSrc = palette[palIndex];
byte *pDest = &_vm->_palette->_mainPalette[idx * 3];
pDest[0] = pSrc.r;
pDest[1] = pSrc.g;
pDest[2] = pSrc.b;
}
}
}
assert(var48);
int var52 = (noUsageFlag && palette[palIndex]._u2) ? 2 : 0;
_vm->_palette->_palFlags[var4] |= var52 | rgbMask;
palette[palIndex]._palIndex = var4;
}
_vm->_palette->_rgbList[rgbIndex] = true;
return rgbIndex;
}
int PaletteUsage::rgbMerge(RGB6 &palEntry) {
return ((palEntry.r + 1) / 4 - 1) * 38 + ((palEntry.g + 1) / 4 - 1) * 76 +
((palEntry.b + 1) / 4 - 1) * 14;
}
void PaletteUsage::transform(Common::Array<RGB6> &palette) {
if (!empty()) {
for (uint i = 0; i < _data->size(); ++i) {
int palIndex = (*_data)[i]._palIndex;
(*_data)[i]._palIndex = palette[palIndex]._palIndex;
}
}
}
void PaletteUsage::updateUsage(Common::Array<int> &usageList, int sceneUsageIndex) {
uint32 mask1 = 0xFFFFFFFF;
uint32 mask2 = 0;
for (uint idx = 0; idx < usageList.size(); ++idx) {
uint32 bitMask = 1 << usageList[idx];
mask1 ^= bitMask;
mask2 |= bitMask;
_vm->_palette->_rgbList[usageList[idx]] = false;
}
uint32 mask3 = 1 << sceneUsageIndex;
for (uint idx = 0; idx < PALETTE_COUNT; ++idx) {
uint32 mask = mask2 & _vm->_palette->_palFlags[idx];
if (mask) {
_vm->_palette->_palFlags[idx] = (_vm->_palette->_palFlags[idx] &
mask1) | mask3;
}
}
_vm->_palette->_rgbList[sceneUsageIndex] = true;
}
void PaletteUsage::resetPalFlags(int idx) {
if (idx >= 0 && idx < 32) {
uint32 rgbMask = ~(1 << idx);
uint32 *flagP = _vm->_palette->_palFlags;
for (int i = 0; i < 256; ++i, ++flagP) {
*flagP &= rgbMask;
if (*flagP == 2)
*flagP = 0;
}
_vm->_palette->_rgbList[idx] = 0;
}
}
int PaletteUsage::getGamePalFreeIndex(int *palIndex) {
*palIndex = -1;
int count = 0;
for (int i = 0; i < PALETTE_COUNT; ++i) {
if (!_vm->_palette->_palFlags[i]) {
++count;
if (*palIndex < 0)
*palIndex = i;
}
}
return count;
}
int PaletteUsage::rgbFactor(byte *palEntry, RGB6 &pal6) {
int total = 0;
total += (palEntry[0] - pal6.r) * (palEntry[0] - pal6.r);
total += (palEntry[1] - pal6.g) * (palEntry[1] - pal6.g);
total += (palEntry[2] - pal6.b) * (palEntry[2] - pal6.b);
return total;
}
/*------------------------------------------------------------------------*/
void RGBList::clear() {
for (int i = 0; i < 32; i++)
_data[i] = false;
}
void RGBList::reset() {
for (int i = 2; i < 32; i++)
_data[i] = false;
}
int RGBList::scan() {
for (int i = 0; i < 32; ++i) {
if (!_data[i])
return i;
}
error("RGBList was full");
}
/*------------------------------------------------------------------------*/
Palette::Palette(MADSEngine *vm) : _vm(vm), _paletteUsage(vm) {
reset();
_lockFl = false;
_lowRange = 0;
_highRange = 0;
Common::fill(&_mainPalette[0], &_mainPalette[PALETTE_SIZE], 0);
Common::fill(&_palFlags[0], &_palFlags[PALETTE_COUNT], 0);
}
void Palette::setPalette(const byte *colors, uint start, uint num) {
g_system->getPaletteManager()->setPalette(colors, start, num);
reset();
}
void Palette::setEntry(byte palIndex, byte r, byte g, byte b) {
_mainPalette[palIndex * 3] = VGA_COLOR_TRANS(r);
_mainPalette[palIndex * 3 + 1] = VGA_COLOR_TRANS(g);
_mainPalette[palIndex * 3 + 2] = VGA_COLOR_TRANS(b);
setPalette((const byte *)&_mainPalette[palIndex * 3], palIndex, 1);
}
void Palette::grabPalette(byte *colors, uint start, uint num) {
g_system->getPaletteManager()->grabPalette(colors, start, num);
reset();
}
uint8 Palette::palIndexFromRgb(byte r, byte g, byte b, byte *paletteData) {
byte index = 0;
int32 minDist = 0x7fffffff;
byte palData[PALETTE_SIZE];
int Rdiff, Gdiff, Bdiff;
if (paletteData == NULL) {
g_system->getPaletteManager()->grabPalette(palData, 0, PALETTE_COUNT);
paletteData = &palData[0];
}
for (int palIndex = 0; palIndex < PALETTE_COUNT; ++palIndex) {
Rdiff = r - paletteData[palIndex * 3];
Gdiff = g - paletteData[palIndex * 3 + 1];
Bdiff = b - paletteData[palIndex * 3 + 2];
if (Rdiff * Rdiff + Gdiff * Gdiff + Bdiff * Bdiff < minDist) {
minDist = Rdiff * Rdiff + Gdiff * Gdiff + Bdiff * Bdiff;
index = (uint8)palIndex;
}
}
return (uint8)index;
}
void Palette::reset() {
}
void Palette::setGradient(byte *palette, int start, int count, int rgbValue1, int rgbValue2) {
int rgbCtr = 0;
int rgbCurrent = rgbValue2;
int rgbDiff = -(rgbValue2 - rgbValue1);
if (count > 0) {
byte *pDest = palette + start * 3;
int endVal = count - 1;
int numLeft = count;
do {
pDest[0] = pDest[1] = pDest[2] = rgbCurrent;
if (numLeft > 1) {
rgbCtr += rgbDiff;
if (rgbCtr >= endVal) {
do {
++rgbCurrent;
rgbCtr += 1 - numLeft;
} while (rgbCtr >= endVal);
}
}
pDest += 3;
} while (--numLeft > 0);
}
}
void Palette::setSystemPalette() {
byte palData[4 * 3];
palData[0 * 3] = palData[0 * 3 + 1] = palData[0 * 3 + 2] = 0;
palData[1 * 3] = palData[1 * 3 + 1] = palData[1 * 3 + 2] = 0x54;
palData[2 * 3] = palData[2 * 3 + 1] = palData[2 * 3 + 2] = 0xb4;
palData[3 * 3] = palData[3 * 3 + 1] = palData[3 * 3 + 2] = 0xff;
setPalette(palData, 0, 4);
}
void Palette::resetGamePalette(int lowRange, int highRange) {
Common::fill((byte *)&_palFlags[0], (byte *)&_palFlags[PALETTE_COUNT], 0);
initVGAPalette(_mainPalette);
// Reserve the start of the palette for things like on-screen text
if (lowRange) {
Common::fill(&_palFlags[0], &_palFlags[lowRange], 1);
}
// Reserve the high end of the palette for dialog display
if (highRange) {
Common::fill(&_palFlags[256 - highRange], &_palFlags[256], 1);
}
_rgbList.clear();
_rgbList[0] = _rgbList[1] = true;
_lockFl = false;
_lowRange = lowRange;
_highRange = highRange;
}
void Palette::initPalette() {
uint32 palMask = 1;
if (_vm->_game->_player._spritesLoaded && _vm->_game->_player._numSprites) {
for (int idx = 0; idx < _vm->_game->_player._numSprites; ++idx) {
SpriteAsset *asset = _vm->_game->_scene._sprites[
_vm->_game->_player._spritesStart + idx];
uint32 mask = 1;
if (asset->_usageIndex)
mask <<= asset->_usageIndex;
palMask = mask;
}
}
for (int idx = 0; idx < PALETTE_COUNT; ++idx)
_palFlags[idx] = palMask;
_lockFl = false;
_rgbList.reset();
}
void Palette::initVGAPalette(byte *palette) {
byte *destP = palette;
for (int palIndex = 0; palIndex < 16; ++palIndex) {
for (int byteCtr = 2; byteCtr >= 0; --byteCtr)
*destP++ = ((DEFAULT_VGA_PALETTE[palIndex] >> (8 * byteCtr)) & 0xff) >> 2;
}
}
void Palette::setLowRange() {
_mainPalette[0] = _mainPalette[1] = _mainPalette[2] = VGA_COLOR_TRANS(0);
_mainPalette[3] = _mainPalette[4] = _mainPalette[5] = VGA_COLOR_TRANS(0x15);
_mainPalette[6] = _mainPalette[7] = _mainPalette[8] = VGA_COLOR_TRANS(0x2A);
_mainPalette[9] = _mainPalette[10] = _mainPalette[11] = VGA_COLOR_TRANS(0x3F);
_vm->_palette->setPalette(_mainPalette, 0, 4);
}
void Palette::fadeOut(byte palette[PALETTE_SIZE], int start, int count, int v1, int v2, int v3, int v4, int v5, int v6) {
warning("TODO: Palette::fadeOut()");
}
void Palette::lock() {
if (_rgbList[31] && !_lockFl)
error("Palette Lock - Unexpected values");
_lockFl = true;
_rgbList[31] = true;
for (int i = 0; i < 256; i++) {
if (_palFlags[i])
_palFlags[i] |= 0x80000000;
}
}
void Palette::unlock() {
if (!_lockFl)
return;
for (int i = 0; i < 256; i++)
_palFlags[i] &= 0x7FFFFFFF;
_rgbList[31] = false;
_lockFl = false;
}
void Palette::refreshHighColors() {
int val = 18;
if (_vm->_game->_scene._cyclingActive)
val += _vm->_game->_scene._totalCycleColors;
setPalette(_mainPalette, val, 256 - val);
}
} // End of namespace MADS
|