From 7f4d93ed935583fb0692f8a9cf92f51be5d4bc32 Mon Sep 17 00:00:00 2001
From: Paul Gilbert
Date: Fri, 18 Nov 2016 20:55:37 -0500
Subject: IMAGE: Respect specified bytesPerPixel in Indeo decoders

---
 image/codecs/codec.cpp       |  2 +-
 image/codecs/indeo/indeo.cpp | 21 +++++++++++++++++----
 image/codecs/indeo/indeo.h   |  2 +-
 image/codecs/indeo4.cpp      |  3 ++-
 image/codecs/indeo4.h        |  2 +-
 image/codecs/indeo5.cpp      |  3 ++-
 image/codecs/indeo5.h        |  2 +-
 7 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/image/codecs/codec.cpp b/image/codecs/codec.cpp
index 913b7afab3..d631a052bb 100644
--- a/image/codecs/codec.cpp
+++ b/image/codecs/codec.cpp
@@ -213,7 +213,7 @@ Codec *createBitmapCodec(uint32 tag, int width, int height, int bitsPerPixel) {
 		return new Indeo3Decoder(width, height);
 	case MKTAG('I', 'V', '4', '1'):
 	case MKTAG('I', 'V', '4', '2'):
-		return new Indeo4Decoder(width, height);
+		return new Indeo4Decoder(width, height, bitsPerPixel);
 	case MKTAG('I', 'V', '5', '0'):
 		return new Indeo5Decoder(width, height);
 #ifdef IMAGE_CODECS_TRUEMOTION1_H
diff --git a/image/codecs/indeo/indeo.cpp b/image/codecs/indeo/indeo.cpp
index 7d8a3ce114..f0be92483c 100644
--- a/image/codecs/indeo/indeo.cpp
+++ b/image/codecs/indeo/indeo.cpp
@@ -466,12 +466,25 @@ IVI45DecContext::IVI45DecContext() : _gb(nullptr), _frameNum(0), _frameType(0),
 
 /*------------------------------------------------------------------------*/
 
-IndeoDecoderBase::IndeoDecoderBase(uint16 width, uint16 height) : Codec() {
-	_pixelFormat = g_system->getScreenFormat();
-	assert(_pixelFormat.bytesPerPixel > 1);
+IndeoDecoderBase::IndeoDecoderBase(uint16 width, uint16 height, uint bytesPerPixel) : Codec() {
+	switch (bytesPerPixel) {
+	case 16:
+		_pixelFormat = Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0);
+		break;
+	case 24:
+		_pixelFormat = Graphics::PixelFormat(4, 8, 8, 8, 0, 16, 8, 0, 0);
+		break;
+	case 32:
+		_pixelFormat = Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
+		break;
+	default:
+		error("Invalid color depth");
+		break;
+	}
+
 	_surface = new Graphics::Surface();
 	_surface->create(width, height, _pixelFormat);
-	_surface->fillRect(Common::Rect(0, 0, width, height), 0);
+	_surface->fillRect(Common::Rect(0, 0, width, height), (bytesPerPixel == 4) ? 0xff : 0);
 	_ctx._bRefBuf = 3; // buffer 2 is used for scalability mode
 }
 
diff --git a/image/codecs/indeo/indeo.h b/image/codecs/indeo/indeo.h
index 336685f625..114f0a10c8 100644
--- a/image/codecs/indeo/indeo.h
+++ b/image/codecs/indeo/indeo.h
@@ -574,7 +574,7 @@ protected:
 	 */
 	int scaleMV(int mv, int mvScale);
 public:
-	IndeoDecoderBase(uint16 width, uint16 height);
+	IndeoDecoderBase(uint16 width, uint16 height, uint bytesPerPixel);
 	virtual ~IndeoDecoderBase();
 };
 
diff --git a/image/codecs/indeo4.cpp b/image/codecs/indeo4.cpp
index ebe2f6f7b4..62b5cae271 100644
--- a/image/codecs/indeo4.cpp
+++ b/image/codecs/indeo4.cpp
@@ -37,7 +37,8 @@ namespace Image {
 
 #define IVI4_PIC_SIZE_ESC   7
 
-Indeo4Decoder::Indeo4Decoder(uint16 width, uint16 height) : IndeoDecoderBase(width, height) {
+Indeo4Decoder::Indeo4Decoder(uint16 width, uint16 height, uint bytesPerPixel) :
+		IndeoDecoderBase(width, height, bytesPerPixel) {
 	_ctx._isIndeo4 = true;
 	_ctx._refBuf = 1;
 	_ctx._bRefBuf = 3;
diff --git a/image/codecs/indeo4.h b/image/codecs/indeo4.h
index 4a6279db37..9fb6435ed5 100644
--- a/image/codecs/indeo4.h
+++ b/image/codecs/indeo4.h
@@ -52,7 +52,7 @@ class Indeo4Decoder : public IndeoDecoderBase {
 		bool _is2dTrans;
 	};
 public:
-	Indeo4Decoder(uint16 width, uint16 height);
+	Indeo4Decoder(uint16 width, uint16 height, uint bytesPerPixel = 2);
 	virtual ~Indeo4Decoder() {}
 
 	virtual const Graphics::Surface *decodeFrame(Common::SeekableReadStream &stream);
diff --git a/image/codecs/indeo5.cpp b/image/codecs/indeo5.cpp
index 7c16a3a6de..c2a4656cea 100644
--- a/image/codecs/indeo5.cpp
+++ b/image/codecs/indeo5.cpp
@@ -48,7 +48,8 @@ enum {
 
 #define IVI5_PIC_SIZE_ESC       15
 
-Indeo5Decoder::Indeo5Decoder(uint16 width, uint16 height) : IndeoDecoderBase(width, height) {
+Indeo5Decoder::Indeo5Decoder(uint16 width, uint16 height, uint bytesPerPixel) :
+		IndeoDecoderBase(width, height, bytesPerPixel) {
 	_ctx._isIndeo4 = false;
 	_ctx._refBuf = 1;
 	_ctx._bRefBuf = 3;
diff --git a/image/codecs/indeo5.h b/image/codecs/indeo5.h
index 704bda5e4b..4ce9d30b92 100644
--- a/image/codecs/indeo5.h
+++ b/image/codecs/indeo5.h
@@ -52,7 +52,7 @@ class Indeo5Decoder : public IndeoDecoderBase {
 		int             is_2d_trans;
 	};
 public:
-	Indeo5Decoder(uint16 width, uint16 height);
+	Indeo5Decoder(uint16 width, uint16 height, uint bytesPerPixel = 2);
 	virtual ~Indeo5Decoder() {}
 
 	virtual const Graphics::Surface *decodeFrame(Common::SeekableReadStream &stream);
-- 
cgit v1.2.3