[Scummvm-cvs-logs] CVS: scummvm/scumm wiz_he.h,2.31,2.32 wiz_he.cpp,2.97,2.98

Gregory Montoir cyx at users.sourceforge.net
Tue Oct 18 12:17:07 CEST 2005


Update of /cvsroot/scummvm/scummvm/scumm
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3267/scumm

Modified Files:
	wiz_he.h wiz_he.cpp 
Log Message:
Fixed wiz TRLE encoding

Index: wiz_he.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/scumm/wiz_he.h,v
retrieving revision 2.31
retrieving revision 2.32
diff -u -d -r2.31 -r2.32
--- wiz_he.h	18 Oct 2005 01:30:21 -0000	2.31
+++ wiz_he.h	18 Oct 2005 19:16:17 -0000	2.32
@@ -166,7 +166,7 @@
 	void fillWizLine(const WizParameters *params);
 	void fillWizPixel(const WizParameters *params);
 
-	void getWizImageDim(int resNum, int state,  int32 &w, int32 &h);
+	void getWizImageDim(int resNum, int state, int32 &w, int32 &h);
 	int getWizImageStates(int resnum);
 	int isWizPixelNonTransparent(int resnum, int state, int x, int y, int flags);
 	uint8 getWizPixelColor(int resnum, int state, int x, int y, int flags);
@@ -193,8 +193,8 @@
 	int isWizPixelNonTransparent(const uint8 *data, int x, int y, int w, int h);
 	uint8 getWizPixelColor(const uint8 *data, int x, int y, int w, int h, uint8 color);
 	uint8 getRawWizPixelColor(const uint8 *data, int x, int y, int w, int h, uint8 color);
-	void computeWizHistogram(uint32 *histogram, const uint8 *data, const Common::Rect *srcRect);
-	void computeRawWizHistogram(uint32 *histogram, const uint8 *data, int srcPitch, const Common::Rect *srcRect);
+	void computeWizHistogram(uint32 *histogram, const uint8 *data, const Common::Rect& rCapt);
+	void computeRawWizHistogram(uint32 *histogram, const uint8 *data, int srcPitch, const Common::Rect& rCapt);
 
 private:
 	ScummEngine_v70he *_vm;

Index: wiz_he.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/scumm/wiz_he.cpp,v
retrieving revision 2.97
retrieving revision 2.98
diff -u -d -r2.97 -r2.98
--- wiz_he.cpp	18 Oct 2005 01:30:21 -0000	2.97
+++ wiz_he.cpp	18 Oct 2005 19:16:17 -0000	2.98
@@ -601,19 +601,19 @@
 	return data[y * w + x];
 }
 
-void Wiz::computeWizHistogram(uint32 *histogram, const uint8 *data, const Common::Rect *srcRect) {
-	int y = srcRect->top;
+void Wiz::computeWizHistogram(uint32 *histogram, const uint8 *data, const Common::Rect &rCapt) {
+	int y = rCapt.top;
 	while (y != 0) {
 		data += READ_LE_UINT16(data) + 2;
 		--y;
 	}
-	int ih = srcRect->height();
+	int ih = rCapt.height();
 	while (ih--) {
 		uint16 off = READ_LE_UINT16(data); data += 2;
 		if (off != 0) {
 			const uint8 *p = data;
-			int x1 = srcRect->left;
-			int x2 = srcRect->right;
+			int x1 = rCapt.left;
+			int x2 = rCapt.right;
 			uint8 code;
 			while (x1 > 0) {
 				code = *p++;
@@ -673,10 +673,10 @@
 	}
 }
 
-void Wiz::computeRawWizHistogram(uint32 *histogram, const uint8 *data, int srcPitch, const Common::Rect *srcRect) {
-	data += srcRect->top * srcPitch + srcRect->left;
-	int iw = srcRect->width();
-	int ih = srcRect->height();
+void Wiz::computeRawWizHistogram(uint32 *histogram, const uint8 *data, int srcPitch, const Common::Rect &rCapt) {
+	data += rCapt.top * srcPitch + rCapt.left;
+	int iw = rCapt.width();
+	int ih = rCapt.height();
 	while (ih--) {
 		for (int i = 0; i < iw; ++i) {
 			++histogram[data[i]];
@@ -685,161 +685,127 @@
 	}
 }
 
-struct wizPackCtx {
-	uint32 len;
-	uint8 saveCode;
-	uint8 saveBuf[256];
-};
-
-static void wizPackType1Helper1(uint8 *&dst, int len, byte newColor, byte prevColor, wizPackCtx *ctx) {
-	assert(len > 0);
-	if (newColor == prevColor) {
-		do {
-			int blockLen = MIN(len, 0x7F);
-			len -= blockLen;
-			if (dst) {
-				*dst++ = (blockLen * 2) | 1;
-			}
-			++ctx->len;
-		} while (len > 0);
-	} else {
-		do {
-			int blockLen = MIN(len, 0x40);
-			len -= blockLen;
-			if (dst) {
-				*dst++ = ((blockLen - 1) * 4) | 2;
-			}
-			++ctx->len;
-			if (dst) {
-				*dst++ = newColor;
-			}
-			++ctx->len;
-		} while (len > 0);
-	}
-}
-
-static void wizPackType1Helper2(uint8 *&dst, int len, wizPackCtx *ctx) {
-	assert(len > 0);
-	const uint8 *src = ctx->saveBuf;
-	do {
-		int blockLen = MIN(len, 0x40);
-		len -= blockLen;
-		if (dst) {
-			*dst++ = (blockLen - 1) * 4;
-		}
-		++ctx->len;
-		while (blockLen--) {
-			if (dst) {
-				*dst++ = *src++;
-			}
-			++ctx->len;
-		}
-	} while (len > 0);
-}
-
 static int wizPackType1(uint8 *dst, const uint8 *src, int srcPitch, const Common::Rect& rCapt, uint8 tColor) {
-	debug(1, "wizPackType1(%d, [%d,%d,%d,%d])", tColor, rCapt.left, rCapt.top, rCapt.right, rCapt.bottom);
-	wizPackCtx ctx;
-	memset(&ctx, 0, sizeof(ctx));
-
+	debug(9, "wizPackType1(%d, [%d,%d,%d,%d])", tColor, rCapt.left, rCapt.top, rCapt.right, rCapt.bottom);
 	src += rCapt.top * srcPitch + rCapt.left;
 	int w = rCapt.width();
 	int h = rCapt.height();
-
-	uint8 *nextDstPtr, *curDstPtr;
-	uint8 curColor, prevColor;
-	int saveBufPos;
-
-	nextDstPtr = curDstPtr = 0;
-
 	int dataSize = 0;
 	while (h--) {
+		uint8 *dstLine = dst;
 		if (dst) {
-			curDstPtr = dst;
-			nextDstPtr = dst;
 			dst += 2;
 		}
-		dataSize += 2;
-		int numBytes = 0;
-
-		int i, code;
-		for (i = 0; i < w; ++i) {
-			if (src[i] != tColor)
-				break;
-		}
-		if (i != w) {
-			curDstPtr = dst;
-			ctx.len = 0;
-			prevColor = ctx.saveBuf[0] = *src;
-			const uint8 *curSrcPtr = src + 1;
-			saveBufPos = 1;
-			code = (tColor - ctx.saveBuf[0] == 0) ? 1 : 0;
-			int curw = w;
-			while (curw--) {
-				ctx.saveBuf[saveBufPos] = curColor = *curSrcPtr++;
-				++saveBufPos;
-				if (code == 0) {
-					if (curColor == tColor) {
-						--saveBufPos;
-						wizPackType1Helper2(curDstPtr, saveBufPos, &ctx);
-						code = saveBufPos = 1;
-						ctx.saveBuf[0] = curColor;
-						numBytes = 0;
-						prevColor = curColor;
-						continue;
+		uint8 diffBuffer[0x40];
+		int runCountSame = 0;
+		int runCountDiff = 0;
+		uint8 prevColor = src[0];
+		for (int i = 1; i < w; ) {
+			uint8 color = src[i++];
+			if (i == 2) {
+				if (prevColor == color) {
+					runCountSame = 1;
+				} else {
+					diffBuffer[0] = prevColor;
+					runCountDiff = 1;
+				}
+			}
+			if (prevColor == color) {
+				if (runCountDiff != 0) {
+					runCountSame = 1;
+					if (runCountDiff > 1) {
+						--runCountDiff;
+						if (dst) {
+							*dst++ = ((runCountDiff - 1) << 2) | 0;
+							memcpy(dst, diffBuffer, runCountDiff);
+							dst += runCountDiff;
+						}
+						dataSize += runCountDiff + 1;
 					}
-					if (saveBufPos > 0x80) {
-						--saveBufPos;
-						wizPackType1Helper2(curDstPtr, saveBufPos, &ctx);
-						saveBufPos = 1;
-						ctx.saveBuf[0] = curColor;
-						numBytes = 0;
-						prevColor = curColor;
-						continue;
+					runCountDiff = 0;
+				}
+				++runCountSame;
+				if (prevColor == tColor) {
+					if (runCountSame == 0x7F) {
+						if (dst) {
+							*dst++ = (runCountSame << 1) | 1;
+						}
+						++dataSize;
+						runCountSame = 0;
 					}
-					if (prevColor != curColor) {
-						numBytes = saveBufPos - 1;
-						prevColor = curColor;
-						continue;
+				} else {
+					if (runCountSame == 0x40) {
+						if (dst) {
+							*dst++ = ((runCountSame - 1) << 2) | 2;
+							*dst++ = prevColor;
+						}
+						dataSize += 2;
+						runCountSame = 0;
 					}
-					code = 1;
-					if (numBytes != 0) {
-						if (saveBufPos - numBytes < 3) {
-							code = 0;
-						} else {
-							wizPackType1Helper2(curDstPtr, numBytes, &ctx);
+				}
+			} else {
+				if (runCountSame != 0) {
+					if (prevColor == tColor) {
+						if (dst) {
+							*dst++ = (runCountSame << 1) | 1;
+						}
+						++dataSize;
+					} else {
+						if (dst) {
+							*dst++ = ((runCountSame - 1) << 2) | 2;
+							*dst++ = prevColor;
 						}
+						dataSize += 2;
 					}
+					runCountSame = 0;
 				}
-				if (prevColor != curColor || saveBufPos - numBytes > 0x80) {
-					saveBufPos -= numBytes;
-					--saveBufPos;
-					wizPackType1Helper1(curDstPtr, saveBufPos, prevColor, tColor, &ctx);
-					saveBufPos = 1;
-					numBytes = 0;
-					ctx.saveBuf[0] = curColor;
-					code = (tColor - ctx.saveBuf[0] == 0) ? 1 : 0;
+				assert(runCountDiff < ARRAYSIZE(diffBuffer));
+				diffBuffer[runCountDiff++] = color;				
+				if (runCountDiff == 0x40) {
+					if (dst) {
+						*dst++ = ((runCountDiff - 1) << 2) | 0;
+						memcpy(dst, diffBuffer, runCountDiff);
+						dst += runCountDiff + 1;
+					}
+					dataSize += runCountDiff + 1;
+					runCountDiff = 0;
 				}
-				prevColor = curColor;
 			}
-			if (code == 0) {
-				wizPackType1Helper2(curDstPtr, saveBufPos, &ctx);
+			prevColor = color;
+		}
+		if (runCountSame != 0) {
+			if (prevColor == tColor) {
+				if (dst) {
+					*dst++ = (runCountSame << 1) | 1;
+				}
+				++dataSize;
 			} else {
-				saveBufPos -= numBytes;
-				wizPackType1Helper1(curDstPtr, saveBufPos, prevColor, tColor, &ctx);
+				if (dst) {
+					*dst++ = ((runCountSame - 1) << 2) | 2;
+					*dst++ = prevColor;
+				}
+				dataSize += 2;
 			}
-			dataSize += ctx.len;
-			src += srcPitch;
+		}
+		if (runCountDiff != 0) {
 			if (dst) {
-				dst += ctx.len;
-				*(uint16 *)nextDstPtr = TO_LE_16(ctx.len);
+				*dst++ = ((runCountDiff - 1) << 2) | 0;
+				memcpy(dst, diffBuffer, runCountDiff);
+				dst += runCountDiff;
 			}
+			dataSize += runCountDiff + 1;
+		}
+		if (dst) {
+			WRITE_LE_UINT16(dstLine, dst - dstLine - 2);
 		}
+		dataSize += 2;
+		src += srcPitch;
 	}
 	return dataSize;
 }
 
 static int wizPackType0(uint8 *dst, const uint8 *src, int srcPitch, const Common::Rect& rCapt, uint8 tColor) {
+	debug(9, "wizPackType0(%d, [%d,%d,%d,%d])", tColor, rCapt.left, rCapt.top, rCapt.right, rCapt.bottom);
 	int w = rCapt.width();
 	int h = rCapt.height();
 	int size = w * h;
@@ -855,7 +821,7 @@
 }
 
 void Wiz::captureWizImage(int resNum, const Common::Rect& r, bool backBuffer, int compType) {
-	debug(1, "ScummEngine_v72he::captureWizImage(%d, %d, [%d,%d,%d,%d])", resNum, compType, r.left, r.top, r.right, r.bottom);
+	debug(5, "ScummEngine_v72he::captureWizImage(%d, %d, [%d,%d,%d,%d])", resNum, compType, r.left, r.top, r.right, r.bottom);
 	uint8 *src = NULL;
 	VirtScreen *pvs = &_vm->virtscr[kMainVirtScreen];
 	if (backBuffer) {
@@ -987,6 +953,7 @@
 
 	uint8 *dataPtr = _vm->getResourceAddress(rtImage, resNum);
 	assert(dataPtr);
+	
 	uint8 *wizh = _vm->findWrappedBlock(MKID('WIZH'), dataPtr, state, 0);
 	assert(wizh);
 	uint32 comp   = READ_LE_UINT32(wizh + 0x0);
@@ -1032,7 +999,6 @@
 			_vm->res.lock(rtImage, dstResNum);
 			dst = _vm->findWrappedBlock(MKID('WIZD'), dstPtr, 0, 0);
 			assert(dst);
-
 			getWizImageDim(dstResNum, 0, cw, ch);
 		} else {
 			VirtScreen *pvs = &_vm->virtscr[kMainVirtScreen];
@@ -1994,7 +1960,7 @@
 	writeVar(0, 0);
 	defineArray(0, kDwordArray, 0, 0, 0, 255);
 	if (readVar(0) != 0) {
-		Common::Rect rCap(x, y, w + 1, h + 1);
+		Common::Rect rCapt(x, y, w + 1, h + 1);
 		uint8 *data = getResourceAddress(rtImage, resNum);
 		assert(data);
 		uint8 *wizh = findWrappedBlock(MKID('WIZH'), data, state, 0);
@@ -2005,16 +1971,16 @@
 		Common::Rect rWiz(w, h);
 		uint8 *wizd = findWrappedBlock(MKID('WIZD'), data, state, 0);
 		assert(wizd);
-		if (rCap.intersects(rWiz)) {
-			rCap.clip(rWiz);
+		if (rCapt.intersects(rWiz)) {
+			rCapt.clip(rWiz);
 			uint32 histogram[256];
 			memset(histogram, 0, sizeof(histogram));
 			switch (c) {
 			case 0:
-				_wiz->computeRawWizHistogram(histogram, wizd, w, &rCap);
+				_wiz->computeRawWizHistogram(histogram, wizd, w, rCapt);
 				break;
 			case 1:
-				_wiz->computeWizHistogram(histogram, wizd, &rCap);
+				_wiz->computeWizHistogram(histogram, wizd, rCapt);
 				break;
 			default:
 				error("computeWizHistogram: Unhandled wiz compression type %d", c);





More information about the Scummvm-git-logs mailing list