[Scummvm-git-logs] scummvm master -> e024c5c81d7c67dad2e30fd39432eb5b4a50891e

sev- noreply at scummvm.org
Sun Jul 17 11:17:43 UTC 2022


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

Summary:
e024c5c81d GRAPHICS: Add naive algorithm option for PaletteLookup


Commit: e024c5c81d7c67dad2e30fd39432eb5b4a50891e
    https://github.com/scummvm/scummvm/commit/e024c5c81d7c67dad2e30fd39432eb5b4a50891e
Author: AndywinXp (andywinxp at gmail.com)
Date: 2022-07-17T13:17:41+02:00

Commit Message:
GRAPHICS: Add naive algorithm option for PaletteLookup

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


diff --git a/graphics/palette.cpp b/graphics/palette.cpp
index aae0fdf8bb2..8c0c388723e 100644
--- a/graphics/palette.cpp
+++ b/graphics/palette.cpp
@@ -44,7 +44,7 @@ bool PaletteLookup::setPalette(const byte *palette, uint len)  {
 	return true;
 }
 
-byte PaletteLookup::findBestColor(byte cr, byte cg, byte cb) {
+byte PaletteLookup::findBestColor(byte cr, byte cg, byte cb, bool useNaiveAlg) {
 	if (_paletteSize == 0) {
 		warning("PaletteLookup::findBestColor(): Palette was not set");
 		return 0;
@@ -58,16 +58,34 @@ byte PaletteLookup::findBestColor(byte cr, byte cg, byte 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;
+	if (useNaiveAlg) {
+		byte *palettePtr = _palette;
 
-		double dist = sqrt((((512 + rmean) * r * r) >> 8) + 4 * g * g + (((767 - rmean) * b * b) >> 8));
-		if (min > dist) {
-			bestColor = i;
-			min = dist;
+		for (uint i = 0; i < _paletteSize; i++) {
+			int redSquareDiff = (cr - palettePtr[0]) * (cr - palettePtr[0]);
+			int greenSquareDiff = (cg - palettePtr[1]) * (cg - palettePtr[1]);
+			int blueSquareDiff = (cb - palettePtr[2]) * (cb - palettePtr[2]);
+
+			int weightedColorError = 3 * redSquareDiff + 5 * greenSquareDiff + 2 * blueSquareDiff;
+			if (weightedColorError < min) {
+				bestColor = i;
+				min = weightedColorError;
+			}
+
+			palettePtr += 3;
+		}
+	} else {
+		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;
+			}
 		}
 	}
 
diff --git a/graphics/palette.h b/graphics/palette.h
index 011b86e652f..93f9d41a366 100644
--- a/graphics/palette.h
+++ b/graphics/palette.h
@@ -137,9 +137,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 (non-floating point calculations)
+	 *
 	 * @return the palette index
 	 */
-	byte findBestColor(byte r, byte g, byte b);
+	byte findBestColor(byte r, byte g, byte b, bool useNaiveAlg = false);
 
 private:
 	byte _palette[256 * 3];




More information about the Scummvm-git-logs mailing list