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

sev- noreply at scummvm.org
Sun Mar 5 20:16:47 UTC 2023


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:
fe27708438 IMAGE: Add getTransparentColor() to the base ImageDecoder class


Commit: fe27708438278d082e46080488ac3634d7624e57
    https://github.com/scummvm/scummvm/commit/fe27708438278d082e46080488ac3634d7624e57
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2023-03-05T21:16:43+01:00

Commit Message:
IMAGE: Add getTransparentColor() to the base ImageDecoder class

Changed paths:
    engines/glk/debugger.cpp
    engines/glk/picture.cpp
    engines/glk/raw_decoder.h
    image/gif.cpp
    image/gif.h
    image/image_decoder.h
    image/png.cpp
    image/png.h


diff --git a/engines/glk/debugger.cpp b/engines/glk/debugger.cpp
index 70dae0d76cd..f7eae41c248 100644
--- a/engines/glk/debugger.cpp
+++ b/engines/glk/debugger.cpp
@@ -105,7 +105,8 @@ void Debugger::saveRawPicture(const RawDecoder &rd, Common::WriteStream &ws) {
 	const byte *palette = rd.getPalette();
 	int paletteCount = rd.getPaletteColorCount();
 	int palStart = rd.getPaletteStartIndex();
-	int transColor = rd.getTransparentColor();
+	bool hasTransColor = rd.hasTransparentColor();
+	uint32 transColor = rd.getTransparentColor();
 
 	// If the image doesn't have a palette, we can directly write out the image
 	if (!palette) {
@@ -122,7 +123,7 @@ void Debugger::saveRawPicture(const RawDecoder &rd, Common::WriteStream &ws) {
 		uint32 *destP = (uint32 *)destSurface.getBasePtr(0, y);
 
 		for (int x = 0; x < surface->w; ++x, ++srcP, ++destP) {
-			if ((int)*srcP == transColor || (int)*srcP < palStart) {
+			if ((hasTransColor && (uint32)*srcP == transColor) || (int)*srcP < palStart) {
 				*destP = format.ARGBToColor(0, 0, 0, 0);
 			} else {
 				assert(*srcP < paletteCount);
diff --git a/engines/glk/picture.cpp b/engines/glk/picture.cpp
index 9e16726db3c..13d3e2cbca9 100644
--- a/engines/glk/picture.cpp
+++ b/engines/glk/picture.cpp
@@ -118,7 +118,8 @@ Picture *Pictures::load(const Common::String &name) {
 	const Graphics::Surface *img;
 	const byte *palette = nullptr;
 	uint palCount = 0;
-	int transColor = -1;
+	bool hasTransColor = false;
+	uint32 transColor = 0;
 	Picture *pic;
 
 	// Check if the picture is already in the store
@@ -136,6 +137,7 @@ Picture *Pictures::load(const Common::String &name) {
 		img = png.getSurface();
 		palette = png.getPalette();
 		palCount = png.getPaletteColorCount();
+		hasTransColor = png.hasTransparentColor();
 		transColor = png.getTransparentColor();
 	} else if (
 		((name.hasSuffixIgnoreCase(".jpg") || name.hasSuffixIgnoreCase(".jpeg")) && f.open(name))
@@ -152,6 +154,7 @@ Picture *Pictures::load(const Common::String &name) {
 		img = raw.getSurface();
 		palette = raw.getPalette();
 		palCount = raw.getPaletteColorCount();
+		hasTransColor = raw.hasTransparentColor();
 		transColor = raw.getTransparentColor();
 	} else if (f.open(Common::String::format("pic%s.rect", name.c_str()))) {
 		rectImg.w = f.readUint32BE();
@@ -183,7 +186,7 @@ Picture *Pictures::load(const Common::String &name) {
 	pic->_refCount = 1;
 	pic->_name = name;
 	pic->_scaled = false;
-	if (transColor != -1 || (!palette && img->format.aBits() > 0))
+	if (hasTransColor || (!palette && img->format.aBits() > 0))
 		pic->clear(pic->getTransparentColor());
 
 	if (!img->getPixels()) {
@@ -199,7 +202,7 @@ Picture *Pictures::load(const Common::String &name) {
 		const byte *srcP = (const byte *)img->getPixels();
 		byte *destP = (byte *)pic->getPixels();
 		for (int idx = 0; idx < img->w * img->h; ++idx, srcP++, destP += pic->format.bytesPerPixel) {
-			if ((int)*srcP != transColor) {
+			if (!hasTransColor || (uint32)*srcP != transColor) {
 				uint val = (*srcP >= palCount) ? 0 : pal[*srcP];
 				if (pic->format.bytesPerPixel == 2)
 					WRITE_LE_UINT16(destP, val);
diff --git a/engines/glk/raw_decoder.h b/engines/glk/raw_decoder.h
index a5fd0537ad9..292efe4e052 100644
--- a/engines/glk/raw_decoder.h
+++ b/engines/glk/raw_decoder.h
@@ -44,7 +44,7 @@ private:
 	Graphics::Surface _surface;
 	byte *_palette;
 	uint16 _paletteColorCount;
-	int _transColor;
+	uint32 _transColor;
 public:
 	RawDecoder();
 	~RawDecoder() override;
@@ -54,7 +54,8 @@ public:
 	const Graphics::Surface *getSurface() const override { return &_surface; }
 	const byte *getPalette() const override { return _palette; }
 	uint16 getPaletteColorCount() const override { return _paletteColorCount; }
-	int getTransparentColor() const { return _transColor; }
+	bool hasTransparentColor() const override { return true; }
+	uint32 getTransparentColor() const override { return _transColor; }
 };
 
 } // End of namespace Glk
diff --git a/image/gif.cpp b/image/gif.cpp
index 4365d841aec..cf5cf58fdfd 100644
--- a/image/gif.cpp
+++ b/image/gif.cpp
@@ -82,12 +82,13 @@ bool GIFDecoder::loadStream(Common::SeekableReadStream &stream) {
 	const int height = gif->SHeight;
 
 	const ColorMapObject *colorMap = gif->SColorMap;
-	_transparentColor = NO_TRANSPARENT_COLOR;
+	_hasTransparentColor = false;
 	for (int i = 0; i < gif->ExtensionBlockCount; ++i) {
 		const ExtensionBlock &eb = gif->ExtensionBlocks[i];
 		GraphicsControlBlock gcb;
 		DGifExtensionToGCB(eb.ByteCount, eb.Bytes, &gcb);
 		if (gcb.TransparentColor != NO_TRANSPARENT_COLOR) {
+			_hasTransparentColor = true;
 			_transparentColor = gcb.TransparentColor;
 			break;
 		}
diff --git a/image/gif.h b/image/gif.h
index 55da75705e1..4b5d22e61c8 100644
--- a/image/gif.h
+++ b/image/gif.h
@@ -56,12 +56,14 @@ public:
 	const byte *getPalette() const override { return _palette; }
 	uint16 getPaletteColorCount() const override { return _colorCount; }
 	const Graphics::Surface *getSurface() const override { return _outputSurface; }
-	int getTransparentColor() const { return _transparentColor; }
+	bool hasTransparentColor() const override { return _hasTransparentColor; }
+	uint32 getTransparentColor() const override { return _transparentColor; }
 private:
 	Graphics::Surface *_outputSurface;
 	uint8 *_palette;
 	uint16 _colorCount;
-	int _transparentColor;
+	bool _hasTransparentColor;
+	uint32 _transparentColor;
 };
 
 /** @} */
diff --git a/image/image_decoder.h b/image/image_decoder.h
index 06597be3aeb..c9554aaa8af 100644
--- a/image/image_decoder.h
+++ b/image/image_decoder.h
@@ -110,6 +110,11 @@ public:
 	virtual byte getPaletteStartIndex() const { return 0; }
 	/** Return the number of colors in the palette. */
 	virtual uint16 getPaletteColorCount() const { return 0; }
+
+	/** Query whether the decoded image has a transparent color. */
+	virtual bool hasTransparentColor() const { return false; }
+	/** Return the transparent color. */
+	virtual uint32 getTransparentColor() const { return 0; }
 };
 /** @} */
 } // End of namespace Image
diff --git a/image/png.cpp b/image/png.cpp
index 625a8f4fccb..2e294476bab 100644
--- a/image/png.cpp
+++ b/image/png.cpp
@@ -45,7 +45,8 @@ PNGDecoder::PNGDecoder() :
 		_paletteColorCount(0),
 		_skipSignature(false),
 		_keepTransparencyPaletted(false),
-		_transparentColor(-1) {
+		_hasTransparentColor(false),
+		_transparentColor(0) {
 }
 
 PNGDecoder::~PNGDecoder() {
@@ -60,6 +61,7 @@ void PNGDecoder::destroy() {
 	}
 	delete[] _palette;
 	_palette = NULL;
+	_hasTransparentColor = false;
 }
 
 Graphics::PixelFormat PNGDecoder::getByteOrderRgbaPixelFormat(bool isAlpha) const {
@@ -190,6 +192,7 @@ bool PNGDecoder::loadStream(Common::SeekableReadStream &stream) {
 			if (numTrans == 1) {
 				// For a single transparency color, the alpha should be fully transparent
 				assert(*trans == 0);
+				_hasTransparentColor = true;
 				_transparentColor = 0;
 			} else {
 				// Multiple alphas are being specified for the palette, so we can't use
diff --git a/image/png.h b/image/png.h
index b35ea166436..09a3fddae06 100644
--- a/image/png.h
+++ b/image/png.h
@@ -63,7 +63,8 @@ public:
 	const Graphics::Surface *getSurface() const override { return _outputSurface; }
 	const byte *getPalette() const override { return _palette; }
 	uint16 getPaletteColorCount() const override { return _paletteColorCount; }
-	int getTransparentColor() const { return _transparentColor; }
+	bool hasTransparentColor() const override { return _hasTransparentColor; }
+	uint32 getTransparentColor() const override { return _transparentColor; }
 	void setSkipSignature(bool skip) { _skipSignature = skip; }
 	void setKeepTransparencyPaletted(bool keep) { _keepTransparencyPaletted = keep; }
 private:
@@ -77,7 +78,8 @@ private:
 
 	// Flag to keep paletted images paletted, even when the image has transparency
 	bool _keepTransparencyPaletted;
-	int _transparentColor;
+	bool _hasTransparentColor;
+	uint32 _transparentColor;
 
 	Graphics::Surface *_outputSurface;
 };




More information about the Scummvm-git-logs mailing list