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
|
/* 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$
*
*/
#include "lure/lure.h"
#include "lure/palette.h"
#include "common/util.h"
namespace Lure {
// Constructor
// Defaults the palette to a full 256 entry palette
Palette::Palette() {
_numEntries = GAME_COLOURS;
_palette = Memory::allocate(_numEntries * 4);
_palette->empty();
}
// Consructor
// Sets up a palette with the given number of entries and a copy of the passed data
Palette::Palette(uint16 srcNumEntries, const byte *srcData, PaletteSource paletteSource) {
_numEntries = srcNumEntries;
_palette = Memory::allocate(_numEntries * 4);
if (srcData) {
if (paletteSource == RGB64)
convertRgb64Palette(srcData, _numEntries);
else if (paletteSource == EGA) {
assert((srcNumEntries == 16) || (srcNumEntries == 17));
convertEGAPalette(srcData);
} else
_palette->copyFrom(srcData, 0, 0, _numEntries * 4);
} else {
// No data provided, set a null palette
_palette->empty();
}
}
// Constructor
// Makes a copy of a passed palette object
Palette::Palette(Palette &src) {
_numEntries = src.numEntries();
_palette = Memory::duplicate(src._palette);
}
// Constructor
// Loads a palette from a resource
Palette::Palette(uint16 resourceId, PaletteSource paletteSource) {
Disk &disk = Disk::getReference();
bool isEGA = LureEngine::getReference().isEGA();
MemoryBlock *srcData = disk.getEntry(resourceId);
if (paletteSource == DEFAULT)
paletteSource = isEGA ? EGA : RGB64;
switch (paletteSource) {
case EGA:
// Handle EGA palette
if ((srcData->size() != 16) && (srcData->size() != 17))
error("Specified resource %d is not a palette", resourceId);
_numEntries = 16;
_palette = Memory::allocate(_numEntries * 4);
convertEGAPalette(srcData->data());
break;
case RGB64:
if (((srcData->size() % 3) != 0) || ((srcData->size() / 3) > GAME_COLOURS))
error("Specified resource %d is not a palette", resourceId);
_numEntries = srcData->size() / 3;
_palette = Memory::allocate(_numEntries * 4);
convertRgb64Palette(srcData->data(), _numEntries);
break;
default:
error("Invalid palette type specified for palette resource");
}
delete srcData;
}
void Palette::convertRgb64Palette(const byte *srcPalette, uint16 srcNumEntries) {
byte *pDest = _palette->data();
const byte *pSrc = srcPalette;
while (srcNumEntries-- > 0) {
*pDest++ = (pSrc[0] << 2) + (pSrc[0] >> 4);
*pDest++ = (pSrc[1] << 2) + (pSrc[1] >> 4);
*pDest++ = (pSrc[2] << 2) + (pSrc[2] >> 4);
*pDest++ = 0;
pSrc += 3;
}
}
// EGA palette definition copied from DOSBox 0.72
static byte ega_palette[64][3] =
{
{0x00,0x00,0x00}, {0x00,0x00,0x2a}, {0x00,0x2a,0x00}, {0x00,0x2a,0x2a}, {0x2a,0x00,0x00}, {0x2a,0x00,0x2a}, {0x2a,0x15,0x00}, {0x2a,0x2a,0x2a},
{0x00,0x00,0x00}, {0x00,0x00,0x2a}, {0x00,0x2a,0x00}, {0x00,0x2a,0x2a}, {0x2a,0x00,0x00}, {0x2a,0x00,0x2a}, {0x2a,0x15,0x00}, {0x2a,0x2a,0x2a},
{0x15,0x15,0x15}, {0x15,0x15,0x3f}, {0x15,0x3f,0x15}, {0x15,0x3f,0x3f}, {0x3f,0x15,0x15}, {0x3f,0x15,0x3f}, {0x3f,0x3f,0x15}, {0x3f,0x3f,0x3f},
{0x15,0x15,0x15}, {0x15,0x15,0x3f}, {0x15,0x3f,0x15}, {0x15,0x3f,0x3f}, {0x3f,0x15,0x15}, {0x3f,0x15,0x3f}, {0x3f,0x3f,0x15}, {0x3f,0x3f,0x3f},
{0x00,0x00,0x00}, {0x00,0x00,0x2a}, {0x00,0x2a,0x00}, {0x00,0x2a,0x2a}, {0x2a,0x00,0x00}, {0x2a,0x00,0x2a}, {0x2a,0x15,0x00}, {0x2a,0x2a,0x2a},
{0x00,0x00,0x00}, {0x00,0x00,0x2a}, {0x00,0x2a,0x00}, {0x00,0x2a,0x2a}, {0x2a,0x00,0x00}, {0x2a,0x00,0x2a}, {0x2a,0x15,0x00}, {0x2a,0x2a,0x2a},
{0x15,0x15,0x15}, {0x15,0x15,0x3f}, {0x15,0x3f,0x15}, {0x15,0x3f,0x3f}, {0x3f,0x15,0x15}, {0x3f,0x15,0x3f}, {0x3f,0x3f,0x15}, {0x3f,0x3f,0x3f},
{0x15,0x15,0x15}, {0x15,0x15,0x3f}, {0x15,0x3f,0x15}, {0x15,0x3f,0x3f}, {0x3f,0x15,0x15}, {0x3f,0x15,0x3f}, {0x3f,0x3f,0x15}, {0x3f,0x3f,0x3f}
};
void Palette::convertEGAPalette(const byte *srcPalette) {
byte *pDest = _palette->data();
const byte *pSrc = srcPalette;
for (int index = 0; index < 16; ++index, ++pSrc) {
// Handle RGB components of entry
assert(*pSrc < 64);
byte *v = &ega_palette[*pSrc][0];
*pDest++ = *v++ * 4;
*pDest++ = *v++ * 4;
*pDest++ = *v++ * 4;
*pDest++ = 0;
}
}
void Palette::setEntry(uint8 index, uint32 value) {
if (index >= numEntries()) error("Invalid palette index: %d", index);
uint32 *entry = (uint32 *) (data() + index * 4);
*entry = value;
}
uint32 Palette::getEntry(uint8 index) {
if (index >= numEntries()) error("Invalid palette index: %d", index);
uint32 *entry = (uint32 *) (data() + index * 4);
return *entry;
}
void Palette::copyFrom(Palette *src) {
_palette->copyFrom(src->palette());
}
/*--------------------------------------------------------------------------*/
PaletteCollection::PaletteCollection(uint16 resourceId) {
Disk &d = Disk::getReference();
MemoryBlock *resource = d.getEntry(resourceId);
bool isEGA = LureEngine::getReference().isEGA();
uint32 palSize;
uint8 *data = resource->data();
if (isEGA) {
// EGA Palette collection - only has 1 sub-palette
if ((resource->size() != 16) && (resource->size() != 17))
error("Resource #%d is not a valid palette set", resourceId);
_numPalettes = 1;
_palettes = (Palette **) Memory::alloc(1 * sizeof(Palette *));
_palettes[0] = new Palette(16, data, EGA);
} else {
// VGA Palette collection
if (resource->size() % (SUB_PALETTE_SIZE * 3) != 0)
error("Resource #%d is not a valid palette set", resourceId);
palSize = SUB_PALETTE_SIZE * 3;
_numPalettes = resource->size() / palSize;
_palettes = (Palette **) Memory::alloc(_numPalettes * sizeof(Palette *));
for (uint8 paletteCtr = 0; paletteCtr < _numPalettes; ++paletteCtr, data += palSize)
_palettes[paletteCtr] = new Palette(SUB_PALETTE_SIZE, data, RGB64);
}
delete resource;
}
PaletteCollection::~PaletteCollection() {
for (int paletteCtr = 0; paletteCtr < _numPalettes; ++paletteCtr)
delete _palettes[paletteCtr];
free(_palettes);
}
Palette &PaletteCollection::getPalette(uint8 paletteNum) {
if (paletteNum >= _numPalettes)
error("Invalid palette index specified");
return *_palettes[paletteNum];
}
} // end of namespace Lure
|