[Scummvm-git-logs] scummvm master -> 502d8387ae0a4c9c2ed4abf4791863feee9ade61

sev- noreply at scummvm.org
Sun Jul 3 10:50:39 UTC 2022


This automated email contains information about 3 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
5abc6bda81 GRAPHICS: Added palette lookup class
4649680c9b GRAPHICS: MACGUI: Switch color lookup to Graphics::PaletteLookup
502d8387ae PINK: Switch palette lookup to Graphics::PaletteLookup


Commit: 5abc6bda819e475312bd9377f73efbd15fc80511
    https://github.com/scummvm/scummvm/commit/5abc6bda819e475312bd9377f73efbd15fc80511
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2022-07-03T12:50:06+02:00

Commit Message:
GRAPHICS: Added palette lookup class

Changed paths:
  A graphics/palette.cpp
    graphics/module.mk
    graphics/palette.h


diff --git a/graphics/module.mk b/graphics/module.mk
index 732239c8a2c..d512f4859af 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -33,6 +33,7 @@ MODULE_OBJS := \
 	opengl/context.o \
 	opengl/debug.o \
 	opengl/shader.o \
+	palette.o \
 	pixelformat.o \
 	primitives.o \
 	renderer.o \
diff --git a/graphics/palette.cpp b/graphics/palette.cpp
new file mode 100644
index 00000000000..56c6a45a2f9
--- /dev/null
+++ b/graphics/palette.cpp
@@ -0,0 +1,77 @@
+/* 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "graphics/palette.h"
+
+namespace Graphics {
+
+PaletteLookup::PaletteLookup() {
+	_paletteSize = 0;
+}
+
+PaletteLookup::PaletteLookup(const byte *palette, uint len)  {
+	_paletteSize = len;
+
+	memcpy(_palette, palette, len * 3);
+}
+
+void PaletteLookup::setPalette(const byte *palette, uint len)  {
+	// Check if the passed palette matched the one we have
+	if (len == _paletteSize && !memcmp(_palette, palette, len * 3))
+		return;
+
+	_paletteSize = len;
+	memcpy(_palette, palette, len * 3);
+	_colorHash.clear();
+}
+
+byte PaletteLookup::findBestColor(byte cr, byte cg, byte cb) {
+	if (_paletteSize == 0) {
+		warning("PaletteLookup::findBestColor(): Palette was not set");
+		return 0;
+	}
+
+	uint bestColor = 0;
+	double min = 0xFFFFFFFF;
+
+	uint32 color = cr << 16 | cg << 8 | cb;
+
+	if (_colorHash.contains(color))
+		return _colorHash[color];
+
+	for (uint i = 0; i < _paletteSize; ++i) {
+		int rmean = (*(_palette + 3 * i + 0) + cr) / 2;
+		int r = *(_palette + 3 * i + 0) - cr;
+		int g = *(_palette + 3 * i + 1) - cg;
+		int b = *(_palette + 3 * i + 2) - cb;
+
+		double dist = sqrt((((512 + rmean) * r * r) >> 8) + 4 * g * g + (((767 - rmean) * b * b) >> 8));
+		if (min > dist) {
+			bestColor = i;
+			min = dist;
+		}
+	}
+
+	_colorHash[color] = bestColor;
+
+	return bestColor;
+}
+
+} // end of namespace Graphics
diff --git a/graphics/palette.h b/graphics/palette.h
index f73e1738302..3603d62ecdf 100644
--- a/graphics/palette.h
+++ b/graphics/palette.h
@@ -23,6 +23,7 @@
 #define GRAPHICS_PALETTE_H
 
 #include "common/scummsys.h"
+#include "common/hashmap.h"
 #include "common/noncopyable.h"
 
 /**
@@ -107,4 +108,42 @@ public:
 	virtual void grabPalette(byte *colors, uint start, uint num) const = 0;
 };
  /** @} */
+
+namespace Graphics {
+
+class PaletteLookup {
+public:
+	PaletteLookup();
+	/**
+	 * @brief Construct a new Palette Lookup object
+	 *
+	 * @param palette   the palette data, in interleaved RGB format
+	 * @param len       the number of palette entries to be read
+	 */
+	PaletteLookup(const byte *palette, uint len);
+
+	/**
+	 * @brief Pass palette to the look up. It also compares given palette
+	 * with the current one and resets cache only when their contents is different.
+	 *
+	 * @param palette   the palette data, in interleaved RGB format
+	 * @param len       the number of palette entries to be read
+	 */
+	void setPalette(const byte *palette, uint len);
+
+	/**
+	 * @brief This method returns closest color from the palette
+	 *        and it uses cache for faster lookups
+	 *
+	 * @return the palette index
+	 */
+	byte findBestColor(byte r, byte g, byte b);
+
+private:
+	byte _palette[256 * 3];
+	uint _paletteSize;
+	Common::HashMap<int, byte> _colorHash;
+};
+
+} //  // end of namespace Graphics
 #endif


Commit: 4649680c9bf02af578dbf66c2c1d8c5f70afaab1
    https://github.com/scummvm/scummvm/commit/4649680c9bf02af578dbf66c2c1d8c5f70afaab1
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2022-07-03T12:50:06+02:00

Commit Message:
GRAPHICS: MACGUI: Switch color lookup to Graphics::PaletteLookup

Changed paths:
    graphics/macgui/macwindowmanager.cpp
    graphics/macgui/macwindowmanager.h


diff --git a/graphics/macgui/macwindowmanager.cpp b/graphics/macgui/macwindowmanager.cpp
index b29883ed30f..0dce8cfffc4 100644
--- a/graphics/macgui/macwindowmanager.cpp
+++ b/graphics/macgui/macwindowmanager.cpp
@@ -215,6 +215,7 @@ MacWindowManager::MacWindowManager(uint32 mode, MacPatterns *patterns, Common::L
 		_palette = (byte *)malloc(_paletteSize * 3);
 		memcpy(_palette, palette, _paletteSize * 3);
 	}
+	_paletteLookup.setPalette(_palette, _paletteSize);
 
 	_fontMan = new MacFontManager(mode, language);
 
@@ -794,7 +795,7 @@ void MacWindowManager::drawDesktop() {
 					byte r, g, b;
 					_desktopBmp->format.colorToRGB(color, r, g, b);
 					if (color > 0) {
-						*((byte *)_desktop->getBasePtr(i, j)) = findBestColor(r, g, b);
+						*((byte *)_desktop->getBasePtr(i, j)) = _paletteLookup.findBestColor(r, g, b);
 					}
 				} else {
 					*((uint32 *)_desktop->getBasePtr(i, j)) = color;
@@ -1269,7 +1270,7 @@ void MacWindowManager::popCursor() {
 ///////////////////
 // Palette stuff
 ///////////////////
-#define LOOKUPCOLOR(x) _color ## x = findBestColor(palette[kColor ## x * 3], palette[kColor ## x  * 3 + 1], palette[kColor ## x * 3 + 2]);
+#define LOOKUPCOLOR(x) _color ## x = _paletteLookup.findBestColor(palette[kColor ## x * 3], palette[kColor ## x  * 3 + 1], palette[kColor ## x * 3 + 2]);
 
 void MacWindowManager::passPalette(const byte *pal, uint size) {
 	if (_palette)
@@ -1281,8 +1282,7 @@ void MacWindowManager::passPalette(const byte *pal, uint size) {
 	}
 	_paletteSize = size;
 
-	_colorHash.clear();
-	_invertColorHash.clear();
+	_paletteLookup.setPalette(pal, size);
 
 	LOOKUPCOLOR(White);
 	LOOKUPCOLOR(Gray80);
@@ -1299,37 +1299,14 @@ void MacWindowManager::passPalette(const byte *pal, uint size) {
 uint MacWindowManager::findBestColor(uint32 color) {
 	byte r, g, b;
 	decomposeColor(color, r, g, b);
-	return findBestColor(r, g, b);
+	return _paletteLookup.findBestColor(r, g, b);
 }
 
 uint MacWindowManager::findBestColor(byte cr, byte cg, byte cb) {
 	if (_pixelformat.bytesPerPixel == 4)
 		return _pixelformat.RGBToColor(cr, cg, cb);
 
-	uint bestColor = 0;
-	double min = 0xFFFFFFFF;
-
-	uint32 color = cr << 16 | cg << 8 | cb;
-
-	if (_colorHash.contains(color))
-		return _colorHash[color];
-
-	for (uint i = 0; i < _paletteSize; ++i) {
-		int rmean = (*(_palette + 3 * i + 0) + cr) / 2;
-		int r = *(_palette + 3 * i + 0) - cr;
-		int g = *(_palette + 3 * i + 1) - cg;
-		int b = *(_palette + 3 * i + 2) - cb;
-
-		double dist = sqrt((((512 + rmean) * r * r) >> 8) + 4 * g * g + (((767 - rmean) * b * b) >> 8));
-		if (min > dist) {
-			bestColor = i;
-			min = dist;
-		}
-	}
-
-	_colorHash[color] = bestColor;
-
-	return bestColor;
+	return _paletteLookup.findBestColor(cr, cg, cb);
 }
 
 void MacWindowManager::decomposeColor(uint32 color, byte &r, byte &g, byte &b) {
@@ -1352,7 +1329,7 @@ uint MacWindowManager::inverter(uint src) {
 		r = ~r;
 		g = ~g;
 		b = ~b;
-		_invertColorHash[src] = findBestColor(r, g, b);
+		_invertColorHash[src] = _paletteLookup.findBestColor(r, g, b);
 	} else {
 		uint32 alpha = _pixelformat.ARGBToColor(255, 0, 0, 0);
 		_invertColorHash[src] = ~(src & ~alpha) | alpha;
diff --git a/graphics/macgui/macwindowmanager.h b/graphics/macgui/macwindowmanager.h
index fca47420817..37c989f2543 100644
--- a/graphics/macgui/macwindowmanager.h
+++ b/graphics/macgui/macwindowmanager.h
@@ -231,12 +231,12 @@ public:
 	 * Return Top Window containing a point
 	 * @param x x coordinate of point
 	 * @param y y coordiante of point
-	 */ 
+	 */
 	MacWindow *findWindowAtPoint(int16 x, int16 y);
 	/**
 	 * Return Top Window containing a point
 	 * @param point Point
-	 */ 
+	 */
 	MacWindow *findWindowAtPoint(Common::Point point);
 
 	/**
@@ -442,7 +442,7 @@ private:
 	PauseToken *_screenCopyPauseToken;
 
 	Common::Array<ZoomBox *> _zoomBoxes;
-	Common::HashMap<uint, uint> _colorHash;
+	Graphics::PaletteLookup _paletteLookup;
 	Common::HashMap<uint, uint> _invertColorHash;
 
 	Common::Archive *_dataBundle;


Commit: 502d8387ae0a4c9c2ed4abf4791863feee9ade61
    https://github.com/scummvm/scummvm/commit/502d8387ae0a4c9c2ed4abf4791863feee9ade61
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2022-07-03T12:50:06+02:00

Commit Message:
PINK: Switch palette lookup to Graphics::PaletteLookup

Changed paths:
    engines/pink/objects/actions/action_text.cpp
    engines/pink/pink.cpp
    engines/pink/pink.h


diff --git a/engines/pink/objects/actions/action_text.cpp b/engines/pink/objects/actions/action_text.cpp
index 0519b2d5dfb..dd2b0c2d9ca 100644
--- a/engines/pink/objects/actions/action_text.cpp
+++ b/engines/pink/objects/actions/action_text.cpp
@@ -188,35 +188,15 @@ void ActionText::draw(Graphics::ManagedSurface *surface) {
 #define GREEN(rgb) (((rgb) >> 8) & 0xFF)
 #define RED(rgb) (((rgb) >> 16) & 0xFF)
 
-static uint findBestColor(byte *palette, uint32 rgb) {
-	uint bestColor = 0;
-	double min = 0xFFFFFFFF;
-	for (uint i = 0; i < 256; ++i) {
-		int rmean = (*(palette + 3 * i + 0) + RED(rgb)) / 2;
-		int r = *(palette + 3 * i + 0) - RED(rgb);
-		int g = *(palette + 3 * i + 1) - GREEN(rgb);
-		int b = *(palette + 3 * i + 2) - BLUE(rgb);
-
-		double dist = sqrt((((512 + rmean) * r * r) >> 8) + 4 * g * g + (((767 - rmean) * b * b) >> 8));
-		if (min > dist) {
-			bestColor = i;
-			min = dist;
-		}
-	}
-
-	debug(2, "for color %06x the best color is %02x%02x%02x", rgb, palette[bestColor * 3], palette[bestColor * 3 + 1], palette[bestColor * 3 + 2]);
-
-	return bestColor;
-}
-
 void ActionText::findColorsInPalette() {
 	byte palette[256 * 3];
 	g_system->getPaletteManager()->grabPalette(palette, 0, 256);
+	g_paletteLookup->setPalette(palette, 256);
 
 	debug(2, "textcolorindex: %06x", _textRGB);
-	_textColorIndex = findBestColor(palette, _textRGB);
+	_textColorIndex = g_paletteLookup->findBestColor(RED(_textRGB), GREEN(_textRGB), BLUE(_textRGB));
 	debug(2, "backgroundColorIndex: %06x", _backgroundRGB);
-	_backgroundColorIndex = findBestColor(palette, _backgroundRGB);
+	_backgroundColorIndex = g_paletteLookup->findBestColor(RED(_backgroundRGB), GREEN(_backgroundRGB), BLUE(_backgroundRGB));
 }
 
 } // End of namespace Pink
diff --git a/engines/pink/pink.cpp b/engines/pink/pink.cpp
index 587ed6cd4da..b115e35082a 100644
--- a/engines/pink/pink.cpp
+++ b/engines/pink/pink.cpp
@@ -39,6 +39,8 @@
 
 namespace Pink {
 
+Graphics::PaletteLookup *g_paletteLookup;
+
 PinkEngine::PinkEngine(OSystem *system, const ADGameDescription *desc)
 	: Engine(system), _rnd("pink"), _exeResources(nullptr),
 	_desc(desc), _bro(nullptr), _menu(nullptr), _actor(nullptr),
@@ -46,6 +48,8 @@ PinkEngine::PinkEngine(OSystem *system, const ADGameDescription *desc)
 
 	const Common::FSNode gameDataDir(ConfMan.get("path"));
 	SearchMan.addSubDirectoryMatching(gameDataDir, "install");
+
+	g_paletteLookup = new Graphics::PaletteLookup;
 }
 
 PinkEngine::~PinkEngine() {
@@ -59,6 +63,8 @@ PinkEngine::~PinkEngine() {
 		delete _cursors[j];
 	}
 	delete _screen;
+
+	delete g_paletteLookup;
 }
 
 Common::Error PinkEngine::init() {
diff --git a/engines/pink/pink.h b/engines/pink/pink.h
index 6cdd5b4628b..c197e629378 100644
--- a/engines/pink/pink.h
+++ b/engines/pink/pink.h
@@ -65,6 +65,7 @@ namespace Common {
 namespace Graphics {
 class MacMenu;
 struct WinCursorGroup;
+class PaletteLookup;
 }
 
 namespace Pink {
@@ -85,6 +86,8 @@ enum {
 	kPinkDebugActions = 1 << 4
 };
 
+extern Graphics::PaletteLookup *g_paletteLookup;
+
 class PinkEngine : public Engine {
 public:
 	PinkEngine(OSystem *system, const ADGameDescription *desc);




More information about the Scummvm-git-logs mailing list