1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "cine/cine.h"
24 #include "cine/various.h"
25 #include "cine/pal.h"
26 #include "common/system.h" // For g_system->getPaletteManager()->setPalette
27 #include "common/textconsole.h"
28 
29 #include "graphics/palette.h"
30 
31 namespace Cine {
32 
33 static byte paletteBuffer1[16];
34 static byte paletteBuffer2[16];
35 
loadPal(const char * fileName)36 void loadPal(const char *fileName) {
37 	char buffer[20];
38 
39 	removeExtention(buffer, fileName);
40 
41 	strcat(buffer, ".PAL");
42 	g_cine->_palArray.clear();
43 
44 	Common::File palFileHandle;
45 	if (!palFileHandle.open(buffer))
46 		error("loadPal(): Cannot open file %s", fileName);
47 
48 	uint16 palEntriesCount = palFileHandle.readUint16LE();
49 	palFileHandle.readUint16LE(); // entry size
50 
51 	g_cine->_palArray.resize(palEntriesCount);
52 	for (uint i = 0; i < g_cine->_palArray.size(); ++i) {
53 		palFileHandle.read(g_cine->_palArray[i].name, 10);
54 		palFileHandle.read(g_cine->_palArray[i].pal1, 16);
55 		palFileHandle.read(g_cine->_palArray[i].pal2, 16);
56 	}
57 	palFileHandle.close();
58 }
59 
findPaletteFromName(const char * fileName)60 int16 findPaletteFromName(const char *fileName) {
61 	char buffer[10];
62 	uint16 position = 0;
63 	uint16 i;
64 
65 	Common::strlcpy(buffer, fileName, sizeof(buffer));
66 
67 	while (position < strlen(buffer)) {
68 		if (buffer[position] > 'a' && buffer[position] < 'z') {
69 			buffer[position] += 'A' - 'a';
70 		}
71 
72 		position++;
73 	}
74 
75 	for (i = 0; i < g_cine->_palArray.size(); i++) {
76 		if (!strcmp(buffer, g_cine->_palArray[i].name)) {
77 			return i;
78 		}
79 	}
80 
81 	return -1;
82 
83 }
84 
loadRelatedPalette(const char * fileName)85 void loadRelatedPalette(const char *fileName) {
86 	char localName[16];
87 	byte i;
88 	int16 paletteIndex;
89 
90 	removeExtention(localName, fileName);
91 
92 	paletteIndex = findPaletteFromName(localName);
93 
94 	if (paletteIndex == -1) {
95 		// generate default palette
96 		for (i = 0; i < 16; i++) {
97 			paletteBuffer1[i] = paletteBuffer2[i] = (i << 4) + i;
98 		}
99 	} else {
100 		assert(paletteIndex < (int32)g_cine->_palArray.size());
101 		memcpy(paletteBuffer1, g_cine->_palArray[paletteIndex].pal1, 16);
102 		memcpy(paletteBuffer2, g_cine->_palArray[paletteIndex].pal2, 16);
103 	}
104 }
105 
106 namespace {
107 /** Is given endian type big endian? (Handles native endian type too, otherwise this would be trivial). */
isBigEndian(const EndianType endian)108 bool isBigEndian(const EndianType endian) {
109 	assert(endian == CINE_NATIVE_ENDIAN || endian == CINE_LITTLE_ENDIAN || endian == CINE_BIG_ENDIAN);
110 
111 	// Handle explicit little and big endian types here
112 	if (endian != CINE_NATIVE_ENDIAN) {
113 		return (endian == CINE_BIG_ENDIAN);
114 	}
115 
116 	// Handle native endian type here
117 #if defined(SCUMM_BIG_ENDIAN)
118 	return true;
119 #elif defined(SCUMM_LITTLE_ENDIAN)
120 	return false;
121 #else
122 	#error No endianness defined
123 #endif
124 }
125 
126 /** Calculate byte position of given bit position in a multibyte variable using defined endianness. */
bytePos(const int bitPos,const int numBytes,const bool bigEndian)127 int bytePos(const int bitPos, const int numBytes, const bool bigEndian) {
128 	if (bigEndian)
129 		return (numBytes - 1) - (bitPos / 8);
130 	else // little endian
131 		return bitPos / 8;
132 }
133 
134 /** Calculate the value of "base" to the power of "power". */
power(int base,int power)135 int power(int base, int power) {
136 	int result = 1;
137 	while (power--)
138 		result *= base;
139 	return result;
140 }
141 } // end of anonymous namespace
142 
rotateRight(byte firstIndex,byte lastIndex)143 Palette &Palette::rotateRight(byte firstIndex, byte lastIndex) {
144 	debug(1, "Palette::rotateRight(firstIndex: %d, lastIndex: %d)", firstIndex, lastIndex);
145 
146 	const Color lastColor = _colors[lastIndex];
147 
148 	for (uint i = lastIndex; i > firstIndex; i--)
149 		_colors[i] = _colors[i - 1];
150 
151 	_colors[firstIndex] = lastColor;
152 
153 	return *this;
154 }
155 
rotateLeft(byte firstIndex,byte lastIndex)156 Palette &Palette::rotateLeft(byte firstIndex, byte lastIndex) {
157 	debug(1, "Palette::rotateLeft(firstIndex: %d, lastIndex: %d)", firstIndex, lastIndex);
158 
159 	const Color firstColor = _colors[firstIndex];
160 
161 	for (uint i = firstIndex; i < lastIndex; i++)
162 		_colors[i] = _colors[i + 1];
163 
164 	_colors[lastIndex] = firstColor;
165 
166 	return *this;
167 }
168 
empty() const169 bool Palette::empty() const {
170 	return _colors.empty();
171 }
172 
colorCount() const173 uint Palette::colorCount() const {
174 	return _colors.size();
175 }
176 
brightness(byte colorIndex)177 byte Palette::brightness(byte colorIndex) {
178 	return (byte) ((_colors[colorIndex].r*19 +
179 		_colors[colorIndex].g*38 +
180 		_colors[colorIndex].b*7) / 64);
181 }
182 
isEqual(byte index1,byte index2)183 bool Palette::isEqual(byte index1, byte index2) {
184 	return _colors[index1].r == _colors[index2].r &&
185 		_colors[index1].g == _colors[index2].g &&
186 		_colors[index1].b == _colors[index2].b;
187 }
188 
findMinBrightnessColorIndex(uint minColorIndex)189 int Palette::findMinBrightnessColorIndex(uint minColorIndex) {
190 	int minFoundBrightness = 999;
191 	int foundColorIndex = 0;
192 	for (uint i = minColorIndex; i < colorCount(); i++) {
193 		byte currColorBrightness = brightness(i);
194 		if (currColorBrightness < minFoundBrightness) {
195 			minFoundBrightness = currColorBrightness;
196 			foundColorIndex = i;
197 		}
198 	}
199 
200 	return (_colors.size() >= 3 && isEqual(2, foundColorIndex)) ? 0 : foundColorIndex;
201 }
202 
ensureContrast(byte & minBrightnessColorIndex)203 bool Palette::ensureContrast(byte &minBrightnessColorIndex) {
204 	minBrightnessColorIndex = findMinBrightnessColorIndex();
205 	if (_colors.size() >= 3 && isEqual(2, minBrightnessColorIndex)) {
206 		Color black = {0, 0, 0};
207 		Color white = {static_cast<uint8>(_format.rMax()), static_cast<uint8>(_format.gMax()), static_cast<uint8>(_format.bMax())};
208 
209 		_colors[2] = white;
210 		if (isEqual(2, minBrightnessColorIndex)) {
211 			_colors[minBrightnessColorIndex] = black;
212 		}
213 		return true;
214 	}
215 	return false;
216 }
217 
fillWithBlack()218 Palette &Palette::fillWithBlack() {
219 	for (uint i = 0; i < _colors.size(); i++) {
220 		_colors[i].r = 0;
221 		_colors[i].g = 0;
222 		_colors[i].b = 0;
223 	}
224 
225 	return *this;
226 }
227 
228 // TODO: Add better heuristic for checking whether the color format is valid
isValid() const229 bool Palette::isValid() const {
230 	// Check that the color format has been actually set and not just default constructed.
231 	// Also check that the alpha channel is discarded.
232 	return _format != Graphics::PixelFormat() && _format.aLoss == 8;
233 }
234 
colorFormat() const235 const Graphics::PixelFormat &Palette::colorFormat() const {
236 	return _format;
237 }
238 
setGlobalOSystemPalette() const239 void Palette::setGlobalOSystemPalette() const {
240 	byte buf[256 * 3]; // Allocate space for the largest possible palette
241 
242 	if (g_cine->mayHave256Colors()) {
243 		memset(buf, 0, sizeof(buf)); // Clear whole palette
244 	}
245 
246 	// The color format used by OSystem's setPalette-function:
247 	save(buf, sizeof(buf), Graphics::PixelFormat(3, 8, 8, 8, 0, 0, 8, 16, 0), CINE_LITTLE_ENDIAN);
248 
249 	if (renderer->useTransparentDialogBoxes() && colorCount() == 16) {
250 		// The Amiga version of Future Wars does use the upper 16 colors for a darkened
251 		// game palette to allow transparent dialog boxes. To support that in our code
252 		// we do calculate that palette over here and append it to the screen palette.
253 		for (uint i = 0; i < 16 * 3; ++i)
254 			buf[16 * 3 + i] = buf[i] >> 1;
255 
256 		g_system->getPaletteManager()->setPalette(buf, 0, colorCount() * 2);
257 	} else if (g_cine->mayHave256Colors()) {
258 		// If 256 colors are possible then always set 256 colors
259 		// because resources may be a combination of 16 colors and 256 colors
260 		// and going from a 16 color screen to a 256 color screen (e.g. when leaving
261 		// the rat maze on Dr. Why's island in Operation Stealth) may leave
262 		// the upper 240 colors not faded out.
263 		g_system->getPaletteManager()->setPalette(buf, 0, 256);
264 	} else {
265 		g_system->getPaletteManager()->setPalette(buf, 0, colorCount());
266 	}
267 }
268 
getColor(byte index) const269 Cine::Palette::Color Palette::getColor(byte index) const {
270 	return _colors[index];
271 }
272 
getR(byte index) const273 uint8 Palette::getR(byte index) const {
274 	return _colors[index].r;
275 }
276 
getG(byte index) const277 uint8 Palette::getG(byte index) const {
278 	return _colors[index].g;
279 }
280 
getB(byte index) const281 uint8 Palette::getB(byte index) const {
282 	return _colors[index].b;
283 }
284 
setColorFormat(const Graphics::PixelFormat format)285 void Palette::setColorFormat(const Graphics::PixelFormat format) {
286 	_format = format;
287 }
288 
289 // a.k.a. transformPaletteRange
saturatedAddColor(Palette & output,byte firstIndex,byte lastIndex,signed r,signed g,signed b) const290 Palette &Palette::saturatedAddColor(Palette &output, byte firstIndex, byte lastIndex, signed r, signed g, signed b) const {
291 	assert(firstIndex < colorCount() && lastIndex < colorCount());
292 	assert(firstIndex < output.colorCount() && lastIndex < output.colorCount());
293 	assert(output.colorFormat() == colorFormat());
294 
295 	for (uint i = firstIndex; i <= lastIndex; i++)
296 		saturatedAddColor(output._colors[i], _colors[i], r, g, b);
297 
298 	return output;
299 }
300 
saturatedAddColor(Palette & output,byte firstIndex,byte lastIndex,signed rSource,signed gSource,signed bSource,const Graphics::PixelFormat & sourceFormat) const301 Palette &Palette::saturatedAddColor(Palette &output, byte firstIndex, byte lastIndex, signed rSource, signed gSource, signed bSource, const Graphics::PixelFormat &sourceFormat) const {
302 	// Convert the source color to the internal color format ensuring that no divide by zero will happen
303 	const signed r = ((signed) _format.rMax()) * rSource / MAX<int>(sourceFormat.rMax(), 1);
304 	const signed g = ((signed) _format.gMax()) * gSource / MAX<int>(sourceFormat.gMax(), 1);
305 	const signed b = ((signed) _format.bMax()) * bSource / MAX<int>(sourceFormat.bMax(), 1);
306 
307 	return saturatedAddColor(output, firstIndex, lastIndex, r, g, b);
308 }
309 
saturatedAddNormalizedGray(Palette & output,byte firstIndex,byte lastIndex,int grayDividend,int grayDenominator) const310 Palette &Palette::saturatedAddNormalizedGray(Palette &output, byte firstIndex, byte lastIndex, int grayDividend, int grayDenominator) const {
311 	assert(grayDenominator != 0);
312 	const signed r = ((signed) _format.rMax()) * grayDividend / grayDenominator;
313 	const signed g = ((signed) _format.gMax()) * grayDividend / grayDenominator;
314 	const signed b = ((signed) _format.bMax()) * grayDividend / grayDenominator;
315 
316 	return saturatedAddColor(output, firstIndex, lastIndex, r, g, b);
317 }
318 
319 // a.k.a. transformColor
saturatedAddColor(Color & result,const Color & baseColor,signed r,signed g,signed b) const320 void Palette::saturatedAddColor(Color &result, const Color &baseColor, signed r, signed g, signed b) const {
321 	result.r = CLIP<int>(baseColor.r + r, 0, _format.rMax());
322 	result.g = CLIP<int>(baseColor.g + g, 0, _format.gMax());
323 	result.b = CLIP<int>(baseColor.b + b, 0, _format.bMax());
324 }
325 
Palette(const Graphics::PixelFormat format,const uint numColors)326 Palette::Palette(const Graphics::PixelFormat format, const uint numColors) : _format(format), _colors() {
327 	_colors.resize(numColors);
328 	fillWithBlack();
329 }
330 
Palette(const Palette & other)331 Palette::Palette(const Palette& other) :
332 	_format(other._format),
333 	_colors(other._colors) {
334 }
335 
operator =(const Palette & other)336 Palette& Palette::operator=(const Palette& other) {
337 	if (this != &other) {
338 		_format = other._format;
339 		_colors = other._colors;
340 	}
341 	return *this;
342 }
343 
clear()344 Palette &Palette::clear() {
345 	_format = Graphics::PixelFormat();
346 	_colors.clear();
347 	return *this;
348 }
349 
load(const byte * buf,const uint size,const Graphics::PixelFormat format,const uint numColors,const EndianType endian)350 Palette &Palette::load(const byte *buf, const uint size, const Graphics::PixelFormat format, const uint numColors, const EndianType endian) {
351 	assert(format.bytesPerPixel * numColors <= size); // Make sure there's enough input space
352 	assert(format.aLoss == 8); // No alpha
353 	assert(format.rShift / 8 == (format.rShift + MAX<int>(0, format.rBits() - 1)) / 8); // R must be inside one byte
354 	assert(format.gShift / 8 == (format.gShift + MAX<int>(0, format.gBits() - 1)) / 8); // G must be inside one byte
355 	assert(format.bShift / 8 == (format.bShift + MAX<int>(0, format.bBits() - 1)) / 8); // B must be inside one byte
356 
357 	setColorFormat(format);
358 
359 	_colors.clear();
360 	_colors.resize(numColors);
361 
362 	const int rBytePos = bytePos(format.rShift, format.bytesPerPixel, isBigEndian(endian));
363 	const int gBytePos = bytePos(format.gShift, format.bytesPerPixel, isBigEndian(endian));
364 	const int bBytePos = bytePos(format.bShift, format.bytesPerPixel, isBigEndian(endian));
365 
366 	for (uint i = 0; i < numColors; i++) {
367 		// format.rMax(), format.gMax(), format.bMax() are also used as masks here
368 		_colors[i].r = (buf[i * format.bytesPerPixel + rBytePos] >> (format.rShift % 8)) & format.rMax();
369 		_colors[i].g = (buf[i * format.bytesPerPixel + gBytePos] >> (format.gShift % 8)) & format.gMax();
370 		_colors[i].b = (buf[i * format.bytesPerPixel + bBytePos] >> (format.bShift % 8)) & format.bMax();
371 	}
372 
373 	return *this;
374 }
375 
save(byte * buf,const uint size,const EndianType endian) const376 byte *Palette::save(byte *buf, const uint size, const EndianType endian) const {
377 	return save(buf, size, colorFormat(), colorCount(), endian);
378 }
379 
save(byte * buf,const uint size,const Graphics::PixelFormat format,const EndianType endian) const380 byte *Palette::save(byte *buf, const uint size, const Graphics::PixelFormat format, const EndianType endian) const {
381 	return save(buf, size, format, colorCount(), endian);
382 }
383 
save(byte * buf,const uint size,const Graphics::PixelFormat format,const uint numColors,const EndianType endian,const byte firstIndex) const384 byte *Palette::save(byte *buf, const uint size, const Graphics::PixelFormat format, const uint numColors, const EndianType endian, const byte firstIndex) const {
385 	assert(format.bytesPerPixel * numColors <= size); // Make sure there's enough output space
386 	assert(format.aLoss == 8); // No alpha
387 	assert(format.rShift / 8 == (format.rShift + MAX<int>(0, format.rBits() - 1)) / 8); // R must be inside one byte
388 	assert(format.gShift / 8 == (format.gShift + MAX<int>(0, format.gBits() - 1)) / 8); // G must be inside one byte
389 	assert(format.bShift / 8 == (format.bShift + MAX<int>(0, format.bBits() - 1)) / 8); // B must be inside one byte
390 
391 	// Clear the part of the output palette we're going to be writing to with all black
392 	memset(buf, 0, format.bytesPerPixel * numColors);
393 
394 	// Calculate original R/G/B max values
395 	const int rOrigMax = power(2, 8 - colorFormat().rLoss) - 1;
396 	const int gOrigMax = power(2, 8 - colorFormat().gLoss) - 1;
397 	const int bOrigMax = power(2, 8 - colorFormat().bLoss) - 1;
398 
399 	// Calculate new R/G/B max values
400 	const int rNewMax = power(2, 8 - format.rLoss) - 1;
401 	const int gNewMax = power(2, 8 - format.gLoss) - 1;
402 	const int bNewMax = power(2, 8 - format.bLoss) - 1;
403 
404 	// Calculate the byte position
405 	const int rBytePos = bytePos(format.rShift, format.bytesPerPixel, isBigEndian(endian));
406 	const int gBytePos = bytePos(format.gShift, format.bytesPerPixel, isBigEndian(endian));
407 	const int bBytePos = bytePos(format.bShift, format.bytesPerPixel, isBigEndian(endian));
408 
409 	// Save the palette to the output in the specified format
410 	for (uint i = firstIndex; i < firstIndex + numColors; i++) {
411 		const uint r = (_colors[i].r * rNewMax) / (rOrigMax == 0 ? 1 : rOrigMax);
412 		const uint g = (_colors[i].g * gNewMax) / (gOrigMax == 0 ? 1 : gOrigMax);
413 		const uint b = (_colors[i].b * bNewMax) / (bOrigMax == 0 ? 1 : bOrigMax);
414 
415 		buf[i * format.bytesPerPixel + rBytePos] |= r << (format.rShift % 8);
416 		buf[i * format.bytesPerPixel + gBytePos] |= g << (format.gShift % 8);
417 		buf[i * format.bytesPerPixel + bBytePos] |= b << (format.bShift % 8);
418 	}
419 
420 	// Return the pointer to the output palette
421 	return buf;
422 }
423 
424 } // End of namespace Cine
425