[Scummvm-cvs-logs] SF.net SVN: scummvm:[39310] scummvm/trunk/engines/cine

buddha_ at users.sourceforge.net buddha_ at users.sourceforge.net
Tue Mar 10 21:37:54 CET 2009


Revision: 39310
          http://scummvm.svn.sourceforge.net/scummvm/?rev=39310&view=rev
Author:   buddha_
Date:     2009-03-10 20:37:54 +0000 (Tue, 10 Mar 2009)

Log Message:
-----------
Palette-class for Cine-engine, WIP: Take 2. Testing, using and documentation still TODO.

Modified Paths:
--------------
    scummvm/trunk/engines/cine/pal.cpp
    scummvm/trunk/engines/cine/pal.h

Modified: scummvm/trunk/engines/cine/pal.cpp
===================================================================
--- scummvm/trunk/engines/cine/pal.cpp	2009-03-10 19:32:38 UTC (rev 39309)
+++ scummvm/trunk/engines/cine/pal.cpp	2009-03-10 20:37:54 UTC (rev 39310)
@@ -25,6 +25,7 @@
 
 #include "cine/cine.h"
 #include "cine/various.h"
+#include "graphics/pixelformat.h"
 
 namespace Cine {
 
@@ -161,47 +162,26 @@
 	}
 }
 
-byte& Palette::getComponent(byte colorIndex, byte componentIndex) {
-	assert(colorIndex < getColorCount() && componentIndex < COMPONENTS_PER_COLOR);
-	return _colors[colorIndex * COMPONENTS_PER_COLOR + componentIndex];
-}
-
-void Palette::setComponent(byte colorIndex, byte componentIndex, byte value) {
-	getComponent(colorIndex, componentIndex) = value;
-}
-
-Palette::PackedColor Palette::getColor(byte colorIndex) {
-	return (getComponent(colorIndex, R_INDEX) << (R_INDEX * BITS_PER_COMPONENT)) |
-		   (getComponent(colorIndex, G_INDEX) << (G_INDEX * BITS_PER_COMPONENT)) |
-		   (getComponent(colorIndex, B_INDEX) << (B_INDEX * BITS_PER_COMPONENT)) |
-		   (getComponent(colorIndex, A_INDEX) << (A_INDEX * BITS_PER_COMPONENT));
-}
-
-void Palette::setColor(byte colorIndex, PackedColor color) {
-	setComponent(colorIndex, R_INDEX, (color >> (R_INDEX * BITS_PER_COMPONENT)) & COMPONENT_MASK);
-	setComponent(colorIndex, G_INDEX, (color >> (G_INDEX * BITS_PER_COMPONENT)) & COMPONENT_MASK);
-	setComponent(colorIndex, B_INDEX, (color >> (B_INDEX * BITS_PER_COMPONENT)) & COMPONENT_MASK);
-	setComponent(colorIndex, A_INDEX, (color >> (A_INDEX * BITS_PER_COMPONENT)) & COMPONENT_MASK);
-}
-
 // a.k.a. palRotate
 Palette& Palette::rotateRight(byte firstIndex, byte lastIndex) {
-	PackedColor lastColor = getColor(lastIndex);
+	const Color lastColor = _colors[lastIndex];
 
 	for (int i = lastIndex; i > firstIndex; i--) {
-		setColor(i, getColor(i - 1));
+		_colors[i] = _colors[i - 1];
 	}
 
-	setColor(firstIndex, lastColor);
+	_colors[firstIndex] = lastColor;
 	return *this;
 }
 
-uint Palette::getColorCount() const {
-	return _colors.size() / COMPONENTS_PER_COLOR;
+uint Palette::colorCount() const {
+	return _colors.size();
 }
 
 // a.k.a. transformPaletteRange
 Palette& Palette::saturatedAddColor(byte firstIndex, byte lastIndex, signed r, signed g, signed b) {
+	assert(firstIndex < colorCount() && lastIndex < colorCount());
+
 	for (uint i = firstIndex; i <= lastIndex; i++) {
 		saturatedAddColor(i, r, g, b);
 	}
@@ -213,39 +193,47 @@
 // e.g. r = 7 sets the resulting color's red component to maximum
 // e.g. r = -7 sets the resulting color's red component to minimum (i.e. zero)
 void Palette::saturatedAddColor(byte index, signed r, signed g, signed b) {
-	byte newR = CLIP<int>(getComponent(index, R_INDEX) + r * COMPONENT_MUL, 0, COMPONENT_MAX);
-	byte newG = CLIP<int>(getComponent(index, G_INDEX) + g * COMPONENT_MUL, 0, COMPONENT_MAX);
-	byte newB = CLIP<int>(getComponent(index, B_INDEX) + b * COMPONENT_MUL, 0, COMPONENT_MAX);
+	_colors[index].r = CLIP<int>(_colors[index].r + r, 0, _rMax);
+	_colors[index].g = CLIP<int>(_colors[index].g + g, 0, _gMax);
+	_colors[index].b = CLIP<int>(_colors[index].b + b, 0, _bMax);
+}
 
-	setComponent(index, R_INDEX, newR);
-	setComponent(index, G_INDEX, newG);
-	setComponent(index, B_INDEX, newB);
+Palette& Palette::loadCineLowPal(const byte *colors, const uint colorCount) {
+	static const Graphics::PixelFormat format = {2, 5, 5, 5, 8, 8, 4, 0, 0};
+	return load(colors, format, colorCount);
 }
 
-Palette& Palette::load9BitColors(uint16 *colors, uint colorCount) {
-	setColorCount(colorCount);
-	for (uint i = 0; i < colorCount; i++) {
-		setComponent(i, R_INDEX, ((colors[i] >> 8) & 7) * COMPONENT_MUL);
-		setComponent(i, G_INDEX, ((colors[i] >> 4) & 7) * COMPONENT_MUL);
-		setComponent(i, B_INDEX, ((colors[i] >> 0) & 7) * COMPONENT_MUL);
-		setComponent(i, A_INDEX, 0);
-	}
-	return *this;
+Palette& Palette::loadCineHighPal(const byte *colors, const uint colorCount) {
+	static const Graphics::PixelFormat format = {3, 0, 0, 0, 8, 0, 8, 16, 0};
+	return load(colors, format, colorCount);
 }
 
-Palette& Palette::load24BitColors(byte *colors, uint colorCount) {
-	setColorCount(colorCount);
-	for (uint i = 0; i < colorCount; i++) {
-		setComponent(i, R_INDEX, colors[i * 3 + 0]);
-		setComponent(i, G_INDEX, colors[i * 3 + 1]);
-		setComponent(i, B_INDEX, colors[i * 3 + 2]);
-		setComponent(i, A_INDEX, 0);
+Palette& Palette::load(const byte *colors, const Graphics::PixelFormat format, const uint colorCount) {
+	assert(format.aLoss == 8); // No alpha	
+	assert(format.rShift % 8 == ((format.rShift + (8 - format.rLoss - 1)) % 8)); // R must be inside one byte
+	assert(format.gShift % 8 == ((format.gShift + (8 - format.gLoss - 1)) % 8)); // G must be inside one byte
+	assert(format.bShift % 8 == ((format.bShift + (8 - format.bLoss - 1)) % 8)); // B must be inside one byte
+
+	_rBits = (8 - format.rLoss);
+	_gBits = (8 - format.gLoss);
+	_bBits = (8 - format.bLoss);
+
+	_rMax = (1 << _rBits) - 1;
+	_gMax = (1 << _gBits) - 1;
+	_bMax = (1 << _bBits) - 1;
+
+	_colors.clear();
+	_colors.resize(colorCount);
+	
+	for (uint i = 0; i < colorCount; i++)
+	{
+		// _rMax, _gMax, _bMax are also used as masks here
+		_colors[i].r = (colors[i * format.bytesPerPixel + (format.rShift / 8)] >> (format.rShift % 8)) & _rMax;
+		_colors[i].g = (colors[i * format.bytesPerPixel + (format.gShift / 8)] >> (format.gShift % 8)) & _gMax;
+		_colors[i].b = (colors[i * format.bytesPerPixel + (format.bShift / 8)] >> (format.bShift % 8)) & _bMax;
 	}
+
 	return *this;
 }
 
-void Palette::setColorCount(uint colorCount) {
-	_colors.resize(colorCount * COMPONENTS_PER_COLOR);
-}
-
 } // End of namespace Cine

Modified: scummvm/trunk/engines/cine/pal.h
===================================================================
--- scummvm/trunk/engines/cine/pal.h	2009-03-10 19:32:38 UTC (rev 39309)
+++ scummvm/trunk/engines/cine/pal.h	2009-03-10 20:37:54 UTC (rev 39310)
@@ -26,6 +26,11 @@
 #ifndef CINE_PAL_H
 #define CINE_PAL_H
 
+// Forward declare Graphics::PixelFormat so we don't have to include its header here
+namespace Graphics {
+	struct PixelFormat;
+};
+
 namespace Cine {
 
 struct PalEntry {
@@ -47,42 +52,30 @@
 void transformPaletteRange(byte *srcPal, byte *dstPal, int startColor, int stopColor, int r, int g, int b);
 
 // This class might be used for handling Cine-engine's palettes in the future. WIP!
-// All colors are represented internally as 32-bit RGBA, but
-// 9-bit color palettes with 16 colors can be loaded and converted on-the-fly.
-// TODO: Add palette saving in the peculiar but used 9-bit color format.
-// TODO: Make use of this class.
+// TODO: Document
+// TODO: Make use of
+// TODO: Test
 class Palette {
 public:
-	Palette& load24BitColors(byte *colors, uint colorCount = 256);
-	Palette& load9BitColors(uint16 *colors, uint colorCount = 16);
+	Palette& loadCineLowPal(const byte *colors, const uint colorCount = 16);
+	Palette& loadCineHighPal(const byte *colors, const uint colorCount = 256);
+	Palette& load(const byte *colors, const Graphics::PixelFormat format, const uint colorCount);
 	Palette& rotateRight(byte firstIndex, byte lastIndex);
-	Palette& saturatedAddColor(byte firstIndex, byte lastIndex, signed r, signed g, signed b);
-	uint getColorCount() const;
+	Palette& saturatedAddColor(byte firstIndex, byte lastIndex, signed r, signed g, signed b);	
+	uint colorCount() const;
 
 private:
-	static const byte
-		R_INDEX = 0,
-		G_INDEX = 1,
-		B_INDEX = 2,
-		A_INDEX = 3;
+	void saturatedAddColor(byte index, signed r, signed g, signed b);
 
-	static const uint
-		COMPONENTS_PER_COLOR = 4,
-		BITS_PER_COMPONENT = 8,
-		COMPONENT_MASK = ((1 << BITS_PER_COMPONENT) - 1),
-		COMPONENT_MAX = COMPONENT_MASK,
-		COMPONENT_MUL = COMPONENT_MAX / 7;
+private:
+	struct Color {
+		uint8 r, g, b;
+	};
 
-	typedef uint32 PackedColor;
+	uint _rBits, _gBits, _bBits;
+	uint _rMax, _gMax, _bMax;
 
-	byte& getComponent(byte colorIndex, byte componentIndex);
-	void setComponent(byte colorIndex, byte componentIndex, byte value);
-	PackedColor getColor(byte colorIndex);
-	void setColor(byte colorIndex, PackedColor color);
-	void saturatedAddColor(byte index, signed r, signed g, signed b);
-	void setColorCount(uint colorCount);
-
-	Common::Array<byte> _colors;
+	Common::Array<Color> _colors;
 };
 
 } // End of namespace Cine


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list