From f01d4c5aa84d80bac1355f8b709f4e1244fec3dc Mon Sep 17 00:00:00 2001 From: Tomas Jakobsson Date: Sun, 30 Dec 2012 19:33:59 +0100 Subject: COMMON: Add PackBitsReadStream to iff_container --- common/iff_container.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ common/iff_container.h | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 80 insertions(+), 8 deletions(-) diff --git a/common/iff_container.cpp b/common/iff_container.cpp index 7bcbf86e0f..ffaa5c6d06 100644 --- a/common/iff_container.cpp +++ b/common/iff_container.cpp @@ -22,6 +22,7 @@ #include "common/iff_container.h" #include "common/substream.h" +#include "common/util.h" namespace Common { @@ -75,4 +76,50 @@ void IFFParser::parse(IFFCallback &callback) { } while (!stop); } + +PackBitsReadStream::PackBitsReadStream(Common::ReadStream &input) : _input(&input) { +} + +PackBitsReadStream::~PackBitsReadStream() { +} + +bool PackBitsReadStream::eos() const { + return _input->eos(); +} + +uint32 PackBitsReadStream::read(void *dataPtr, uint32 dataSize) { + byte *out = (byte *)dataPtr; + uint32 left = dataSize; + + uint32 lenR = 0, lenW = 0; + while (left > 0 && !_input->eos()) { + lenR = _input->readByte(); + + if (lenR == 128) { + // no-op + lenW = 0; + } else if (lenR <= 127) { + // literal run + lenR++; + lenW = MIN(lenR, left); + for (uint32 j = 0; j < lenW; j++) { + *out++ = _input->readByte(); + } + for (; lenR > lenW; lenR--) { + _input->readByte(); + } + } else { // len > 128 + // expand run + lenW = MIN((256 - lenR) + 1, left); + byte val = _input->readByte(); + memset(out, val, lenW); + out += lenW; + } + + left -= lenW; + } + + return dataSize - left; +} + } // End of namespace Common diff --git a/common/iff_container.h b/common/iff_container.h index 104ecf0f36..a730930b2c 100644 --- a/common/iff_container.h +++ b/common/iff_container.h @@ -77,22 +77,22 @@ page 376) */ #define ID_copy MKTAG('(','c',')',' ') /* EA IFF 85 Generic Copyright text chunk */ -/* ILBM chunks */ +/* IFF chunks */ #define ID_BMHD MKTAG('B','M','H','D') -/* ILBM BitmapHeader */ +/* IFF BitmapHeader */ #define ID_CMAP MKTAG('C','M','A','P') -/* ILBM 8bit RGB colormap */ +/* IFF 8bit RGB colormap */ #define ID_GRAB MKTAG('G','R','A','B') -/* ILBM "hotspot" coordiantes */ +/* IFF "hotspot" coordiantes */ #define ID_DEST MKTAG('D','E','S','T') -/* ILBM destination image info */ +/* IFF destination image info */ #define ID_SPRT MKTAG('S','P','R','T') -/* ILBM sprite identifier */ +/* IFF sprite identifier */ #define ID_CAMG MKTAG('C','A','M','G') /* Amiga viewportmodes */ #define ID_BODY MKTAG('B','O','D','Y') -/* ILBM image data */ +/* IFF image data */ #define ID_CRNG MKTAG('C','R','N','G') /* color cycling */ #define ID_CCRT MKTAG('C','C','R','T') @@ -114,7 +114,7 @@ page 376) */ #define ID_PCHG MKTAG('P','C','H','G') /* Line by line palette control information (Sebastiano Vigna) */ #define ID_PRVW MKTAG('P','R','V','W') -/* A mini duplicate ILBM used for preview (Gary Bonham) */ +/* A mini duplicate IFF used for preview (Gary Bonham) */ #define ID_XBMI MKTAG('X','B','M','I') /* eXtended BitMap Information (Soft-Logik) */ #define ID_CTBL MKTAG('C','T','B','L') @@ -239,6 +239,31 @@ public: }; +/** + * Decode a given PackBits encoded stream. + * + * PackBits is an RLE compression algorithm introduced by Apple. It is also + * used to encode ILBM and PBM subtypes of IFF files, and some flavors of + * TIFF. + * + * As there is no compression across row boundaries in the above formats, + * read() will extract a *new* line on each call, discarding any alignment + * or padding. + */ +class PackBitsReadStream : public Common::ReadStream { + +protected: + Common::ReadStream *_input; + +public: + PackBitsReadStream(Common::ReadStream &input); + ~PackBitsReadStream(); + + virtual bool eos() const; + + uint32 read(void *dataPtr, uint32 dataSize); +}; + } // namespace Common #endif -- cgit v1.2.3