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
|
/* 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 "sci/gfx/palette.h"
#include "sci/gfx/gfx_system.h"
// #define DEBUG_MERGE
namespace Sci {
Palette::Palette(uint s) {
_size = s;
_colors = new PaletteEntry[s];
_parent = 0;
_dirty = true;
_refcount = 1;
_revision = 0;
}
Palette::Palette(const PaletteEntry *colors, uint s) {
_size = s;
_colors = new PaletteEntry[s];
_parent = 0;
_dirty = true;
_refcount = 1;
_revision = 0;
for (uint i = 0; i < _size; ++i) {
_colors[i].r = colors[i].r;
_colors[i].g = colors[i].g;
_colors[i].b = colors[i].b;
_colors[i].refcount = 0;
_colors[i].parent_index = -1;
}
}
Palette::~Palette() {
if (_parent)
unmerge();
delete[] _colors;
_colors = 0;
}
Palette *Palette::getref() {
_refcount++;
return this;
}
void Palette::free() {
assert(_refcount > 0);
_refcount--;
if (_refcount == 0)
delete this;
}
void Palette::resize(uint s) {
if (s == _size)
return;
assert(!_parent);
assert(_refcount == 1);
assert(s >= _size);
PaletteEntry *n = new PaletteEntry[s];
for (uint i = 0; i < _size; ++i)
n[i] = _colors[i];
delete[] _colors;
_colors = n;
_size = s;
}
void Palette::unmerge() {
assert(_parent);
#ifdef DEBUG_MERGE
fprintf(stderr, "Unmerge %s from %s (%d colors)\n", name.c_str(), _parent->name.c_str(), _size);
#endif
if (_parent->_revision != _revision) {
#ifdef DEBUG_MERGE
fprintf(stderr, "NOP (revision mismatch)\n");
#endif
return;
}
int count = 0;
for (uint i = 0; i < _size; ++i) {
if (_colors[i].refcount == PALENTRY_FREE)
continue;
int pi = _colors[i].parent_index;
assert(pi >= 0);
assert(pi < (int)_parent->_size);
assert(_parent->_colors[pi].refcount != 0);
assert(_parent->_colors[pi].refcount != PALENTRY_FREE);
if (_parent->_colors[pi].refcount != PALENTRY_LOCKED)
_parent->_colors[pi].refcount--;
if (_parent->_colors[pi].refcount == 0) {
_parent->_colors[pi].refcount = PALENTRY_FREE;
count++;
}
_colors[i].parent_index = -1;
}
#ifdef DEBUG_MERGE
fprintf(stderr, "Unmerge free %d colors\n", count);
#endif
_parent = 0;
}
void Palette::setColor(uint index, byte r, byte g, byte b, int parentIndex) {
assert(index < _size);
assert(!_parent);
// FIXME: We may want to have this assert. This will require changing the
// way loops sharing a single view's palette works.
// assert(_refcount == 1);
PaletteEntry& entry = _colors[index];
assert(entry.refcount == PALENTRY_FREE || entry.refcount == 0);
entry.refcount = 0;
entry.r = r;
entry.g = g;
entry.b = b;
entry.parent_index = parentIndex;
_dirty = true;
}
void Palette::makeSystemColor(uint index, const PaletteEntry &color) {
assert(index < _size);
PaletteEntry& entry = _colors[index];
entry.r = color.r;
entry.g = color.g;
entry.b = color.b;
entry.refcount = PALENTRY_LOCKED;
}
uint Palette::findNearbyColor(byte r, byte g, byte b, bool lock) {
int bestdelta = 1 + ((0x100 * 0x100) * 3);
int bestcolor = -1;
int firstfree = -1;
assert(_size != 0);
for (uint i = 0; i < _size; ++i) {
PaletteEntry& entry = _colors[i];
if (entry.refcount != PALENTRY_FREE) {
int dr = abs(entry.r - r);
int dg = abs(entry.g - g);
int db = abs(entry.b - b);
if (dr == 0 && dg == 0 && db == 0) {
// Exact match
//exact = true;
if (lock && entry.refcount != PALENTRY_LOCKED)
entry.refcount++;
return i;
}
int delta = (dr * dr) + (dg * dg) + (db * db);
if (delta < bestdelta) {
bestdelta = delta;
bestcolor = i;
}
} else {
if (firstfree == -1)
firstfree = i;
}
}
if (firstfree != -1) {
// TODO: mark palette as dirty
setColor(firstfree, r, g, b);
//exact = true;
if (lock)
_colors[firstfree].refcount++;
return firstfree;
}
//exact = false;
if (lock && _colors[bestcolor].refcount != PALENTRY_LOCKED) {
#if 0
_colors[bestcolor].r = r;
_colors[bestcolor].g = g;
_colors[bestcolor].b = b;
_dirty = true;
#endif
_colors[bestcolor].refcount++;
}
return bestcolor;
}
bool Palette::mergeInto(Palette *parent) {
assert(!_parent || _parent == parent);
assert(parent != this);
#ifdef DEBUG_MERGE
fprintf(stderr, "Merge: %s into %s (%d colors)\n", name.c_str(), parent->name.c_str(), _size);
#endif
if (_parent == parent && parent->_revision == _revision) {
#ifdef DEBUG_MERGE
fprintf(stderr, "NOP\n");
#endif
return false;
}
_parent = parent;
_revision = parent->_revision;
#ifdef DEBUG_MERGE
bool *used = new bool[_parent->size()];
for (uint i = 0; i < _parent->size(); ++i)
used[i] = false;
int count = 0;
uint used_min = 1000;
uint used_max = 0;
#endif
for (uint i = 0; i < _size; ++i) {
PaletteEntry& entry = _colors[i];
if (entry.refcount == PALENTRY_FREE)
continue;
uint pi = _parent->findNearbyColor(entry.r, entry.g, entry.b);
#ifdef DEBUG_MERGE
if (!used[pi]) count++;
used[pi] = true;
if (pi > used_max) used_max = pi;
if (pi < used_min) used_min = pi;
#endif
entry.parent_index = pi;
if (_parent->_colors[pi].refcount != PALENTRY_LOCKED)
_parent->_colors[pi].refcount++;
}
#ifdef DEBUG_MERGE
fprintf(stderr, "Merge used %d colours in [%d..%d]\n", count, used_min, used_max);
delete[] used;
#endif
return true;
}
void Palette::forceInto(Palette *parent) {
assert(!_parent || _parent == parent);
assert(parent != this);
#ifdef DEBUG_MERGE
fprintf(stderr, "Merge: force %s into %s (%d colors)\n", name.c_str(), parent->name.c_str(), _size);
#endif
_parent = parent;
parent->_revision++;
_revision = parent->_revision;
for (unsigned int i = 0; i < _size; ++i) {
// FIXME: PALENTRY_LOCKED doesn't work well with forceInto...
if (_colors[i].refcount != PALENTRY_FREE) {
_parent->_colors[i] = _colors[i];
_parent->_colors[i].parent_index = -1;
_colors[i].parent_index = i;
if (_parent->_colors[i].refcount != PALENTRY_LOCKED)
_parent->_colors[i].refcount = 1;
} else {
_parent->_colors[i].refcount = 0;
}
}
_parent->_dirty = true;
}
Palette *Palette::copy() {
assert(!_parent);
Palette* p = new Palette(_size);
p->name = "copy of " + name;
for (uint i = 0; i < _size; ++i) {
p->_colors[i] = _colors[i];
if (p->_colors[i].refcount >= 0)
p->_colors[i].refcount = 0;
}
return p;
}
} // End of namespace Sci
|