[Scummvm-git-logs] scummvm master -> 3fd3f189cf1429cbd85554ea9e539a6a986ce284

bluegr noreply at scummvm.org
Thu Dec 26 08:59:51 UTC 2024


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

Summary:
6c661eb473 GRAPHICS: Define color distance methods for palette
3fd3f189cf GRAPHICS: Return early on exact color match for palette lookup


Commit: 6c661eb473705ee9c4653c64c5f2c21e5c7a54b9
    https://github.com/scummvm/scummvm/commit/6c661eb473705ee9c4653c64c5f2c21e5c7a54b9
Author: Matthew Jimenez (matthew.jimenez at outlook.com)
Date: 2024-12-26T10:59:48+02:00

Commit Message:
GRAPHICS: Define color distance methods for palette

Changed paths:
    engines/scumm/palette.cpp
    graphics/palette.cpp
    graphics/palette.h


diff --git a/engines/scumm/palette.cpp b/engines/scumm/palette.cpp
index a1cb32557dd..349ae14fb7c 100644
--- a/engines/scumm/palette.cpp
+++ b/engines/scumm/palette.cpp
@@ -1586,7 +1586,7 @@ void ScummEngine::fetchBlackAndWhite(uint32 &black, uint32 &white, byte *palette
 
 uint32 ScummEngine::findClosestPaletteColor(byte *palette, int paletteLength, byte r, byte g, byte b) {
 	_pl.setPalette(palette, paletteLength);
-	return (uint32)_pl.findBestColor(r, g, b, true);
+	return (uint32)_pl.findBestColor(r, g, b, Graphics::kColorDistanceNaive);
 }
 
 void ScummEngine::applyGrayscaleToPaletteRange(int min, int max) {
diff --git a/graphics/palette.cpp b/graphics/palette.cpp
index 09d718e0b7b..1aa3e72d7ac 100644
--- a/graphics/palette.cpp
+++ b/graphics/palette.cpp
@@ -83,11 +83,13 @@ bool Palette::contains(const Palette& p) const {
 	return p._size <= _size && !memcmp(_data, p._data, p._size * 3);
 }
 
-byte Palette::findBestColor(byte cr, byte cg, byte cb, bool useNaiveAlg) const {
+byte Palette::findBestColor(byte cr, byte cg, byte cb, ColorDistanceMethod method) const {
 	uint bestColor = 0;
 	uint32 min = 0xFFFFFFFF;
 
-	if (useNaiveAlg) {
+	switch (method)
+	{
+	case kColorDistanceNaive:
 		for (uint i = 0; i < _size; i++) {
 			int r = _data[3 * i + 0] - cr;
 			int g = _data[3 * i + 1] - cg;
@@ -99,7 +101,8 @@ byte Palette::findBestColor(byte cr, byte cg, byte cb, bool useNaiveAlg) const {
 				min = distWeighted;
 			}
 		}
-	} else {
+		break;
+	case kColorDistanceRedmean:
 		for (uint i = 0; i < _size; ++i) {
 			int rmean = (_data[3 * i + 0] + cr) / 2;
 			int r = _data[3 * i + 0] - cr;
@@ -112,6 +115,9 @@ byte Palette::findBestColor(byte cr, byte cg, byte cb, bool useNaiveAlg) const {
 				min = distSquared;
 			}
 		}
+		break;
+	default:
+		break;
 	}
 
 	return bestColor;
@@ -164,7 +170,7 @@ bool PaletteLookup::setPalette(const byte *palette, uint len)  {
 	return true;
 }
 
-byte PaletteLookup::findBestColor(byte cr, byte cg, byte cb, bool useNaiveAlg) {
+byte PaletteLookup::findBestColor(byte cr, byte cg, byte cb, ColorDistanceMethod method) {
 	if (_paletteSize == 0) {
 		warning("PaletteLookup::findBestColor(): Palette was not set");
 		return 0;
@@ -175,13 +181,13 @@ byte PaletteLookup::findBestColor(byte cr, byte cg, byte cb, bool useNaiveAlg) {
 	if (_colorHash.contains(color))
 		return _colorHash[color];
 
-	uint bestColor = _palette.findBestColor(cr, cg, cb, useNaiveAlg);
+	uint bestColor = _palette.findBestColor(cr, cg, cb, method);
 	_colorHash[color] = bestColor;
 
 	return bestColor;
 }
 
-uint32 *PaletteLookup::createMap(const byte *srcPalette, uint len, bool useNaiveAlg) {
+uint32 *PaletteLookup::createMap(const byte *srcPalette, uint len, ColorDistanceMethod method) {
 	if (len <= _paletteSize && memcmp(_palette.data(), srcPalette, len * 3) == 0)
 		return nullptr;
 
@@ -191,7 +197,7 @@ uint32 *PaletteLookup::createMap(const byte *srcPalette, uint len, bool useNaive
 		byte g = *srcPalette++;
 		byte b = *srcPalette++;
 
-		map[i] = findBestColor(r, g, b, useNaiveAlg);
+		map[i] = findBestColor(r, g, b, method);
 	}
 	return map;
 }
diff --git a/graphics/palette.h b/graphics/palette.h
index 779f9f6633e..f574f1ca672 100644
--- a/graphics/palette.h
+++ b/graphics/palette.h
@@ -26,6 +26,11 @@
 
 namespace Graphics {
 
+enum ColorDistanceMethod {
+	kColorDistanceNaive,	///< Weighted red 30%, green 50%, blue 20%
+	kColorDistanceRedmean,	///< Common low-cost approximation
+};
+
 /**
  * Constants available for use in paletted code
  */
@@ -114,11 +119,11 @@ public:
 	/**
 	 * Finds the index of the closest color from the palette.
 	 *
-	 * @param useNaiveAlg            if true, use a simpler algorithm
+	 * @param method           the method used to determine the closest color
 	 *
 	 * @return the palette index
 	 */
-	byte findBestColor(byte r, byte g, byte b, bool useNaiveAlg = false) const;
+	byte findBestColor(byte r, byte g, byte b, ColorDistanceMethod method = kColorDistanceRedmean) const;
 
 	void clear();
 
@@ -176,11 +181,11 @@ public:
 	 * @brief This method returns closest color from the palette
 	 *        and it uses cache for faster lookups
 	 *
-	 * @param useNaiveAlg            if true, use a simpler algorithm
+	 * @param method           the method used to determine the closest color
 	 *
 	 * @return the palette index
 	 */
-	byte findBestColor(byte r, byte g, byte b, bool useNaiveAlg = false);
+	byte findBestColor(byte r, byte g, byte b, ColorDistanceMethod method = kColorDistanceRedmean);
 
 	/**
 	 * @brief This method creates a map from the given palette
@@ -188,11 +193,11 @@ public:
 	 *
 	 * @param palette   the palette data, in interleaved RGB format
 	 * @param len       the number of palette entries to be read
-	 * @param useNaiveAlg            if true, use a simpler algorithm
+	 * @param method    the method used to determine the closest color
 	 *
 	 * @return the created map, or nullptr if one isn't needed.
 	 */
-	uint32 *createMap(const byte *srcPalette, uint len, bool useNaiveAlg = false);
+	uint32 *createMap(const byte *srcPalette, uint len, ColorDistanceMethod method = kColorDistanceRedmean);
 
 private:
 	Palette _palette;


Commit: 3fd3f189cf1429cbd85554ea9e539a6a986ce284
    https://github.com/scummvm/scummvm/commit/3fd3f189cf1429cbd85554ea9e539a6a986ce284
Author: Matthew Jimenez (matthew.jimenez at outlook.com)
Date: 2024-12-26T10:59:48+02:00

Commit Message:
GRAPHICS: Return early on exact color match for palette lookup

Changed paths:
    graphics/palette.cpp


diff --git a/graphics/palette.cpp b/graphics/palette.cpp
index 1aa3e72d7ac..c874323bfff 100644
--- a/graphics/palette.cpp
+++ b/graphics/palette.cpp
@@ -94,6 +94,8 @@ byte Palette::findBestColor(byte cr, byte cg, byte cb, ColorDistanceMethod metho
 			int r = _data[3 * i + 0] - cr;
 			int g = _data[3 * i + 1] - cg;
 			int b = _data[3 * i + 2] - cb;
+			if (r == 0 && g == 0 && b == 0)
+				return i;
 
 			uint32 distWeighted = 3 * r * r + 5 * g * g + 2 * b * b;
 			if (distWeighted < min) {
@@ -104,11 +106,13 @@ byte Palette::findBestColor(byte cr, byte cg, byte cb, ColorDistanceMethod metho
 		break;
 	case kColorDistanceRedmean:
 		for (uint i = 0; i < _size; ++i) {
-			int rmean = (_data[3 * i + 0] + cr) / 2;
 			int r = _data[3 * i + 0] - cr;
 			int g = _data[3 * i + 1] - cg;
 			int b = _data[3 * i + 2] - cb;
+			if (r == 0 && g == 0 && b == 0)
+				return i;
 
+			int rmean = (_data[3 * i + 0] + cr) / 2;
 			uint32 distSquared = (((512 + rmean) * r * r) >> 8) + 4 * g * g + (((767 - rmean) * b * b) >> 8);
 			if (distSquared < min) {
 				bestColor = i;




More information about the Scummvm-git-logs mailing list