diff options
author | Willem Jan Palenstijn | 2013-04-18 23:55:01 +0200 |
---|---|---|
committer | Willem Jan Palenstijn | 2013-05-08 20:47:44 +0200 |
commit | 102299630901d08a44ef3aec367fcbcae065b9fe (patch) | |
tree | 98db4bbe0c54176c0a43e2f5076f8b3d63b8065c /graphics/decoders | |
parent | 583f9abaf98f64895546b75573e9442ca47426e3 (diff) | |
parent | 78ba3210a57094086d44b25d5a8507c33ce9bef3 (diff) | |
download | scummvm-rg350-102299630901d08a44ef3aec367fcbcae065b9fe.tar.gz scummvm-rg350-102299630901d08a44ef3aec367fcbcae065b9fe.tar.bz2 scummvm-rg350-102299630901d08a44ef3aec367fcbcae065b9fe.zip |
Merge branch 'master'
Diffstat (limited to 'graphics/decoders')
-rw-r--r-- | graphics/decoders/bmp.h | 1 | ||||
-rw-r--r-- | graphics/decoders/iff.cpp | 241 | ||||
-rw-r--r-- | graphics/decoders/iff.h | 119 | ||||
-rw-r--r-- | graphics/decoders/jpeg.cpp | 16 | ||||
-rw-r--r-- | graphics/decoders/jpeg.h | 1 | ||||
-rw-r--r-- | graphics/decoders/pict.cpp | 11 | ||||
-rw-r--r-- | graphics/decoders/png.cpp | 2 | ||||
-rw-r--r-- | graphics/decoders/png.h | 1 | ||||
-rw-r--r-- | graphics/decoders/tga.h | 2 |
9 files changed, 383 insertions, 11 deletions
diff --git a/graphics/decoders/bmp.h b/graphics/decoders/bmp.h index 59da682e4d..779da352be 100644 --- a/graphics/decoders/bmp.h +++ b/graphics/decoders/bmp.h @@ -24,6 +24,7 @@ * Image decoder used in engines: * - hugo * - mohawk + * - wintermute */ #ifndef GRAPHICS_DECODERS_BMP_H diff --git a/graphics/decoders/iff.cpp b/graphics/decoders/iff.cpp new file mode 100644 index 0000000000..50c7b4f7de --- /dev/null +++ b/graphics/decoders/iff.cpp @@ -0,0 +1,241 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "common/iff_container.h" +#include "common/stream.h" +#include "common/util.h" + +#include "graphics/decoders/iff.h" + +namespace Graphics { + +IFFDecoder::IFFDecoder() { + _surface = 0; + _palette = 0; + + destroy(); +} + +IFFDecoder::~IFFDecoder() { + destroy(); +} + +void IFFDecoder::destroy() { + if (_surface) { + _surface->free(); + delete _surface; + _surface = 0; + } + + if (_palette) { + delete[] _palette; + _palette = 0; + } + + memset(&_header, 0, sizeof(Header)); + _paletteRanges.clear(); + _type = TYPE_UNKNOWN; + _paletteColorCount = 0; + _numRelevantPlanes = 8; + _pixelPacking = false; +} + +bool IFFDecoder::loadStream(Common::SeekableReadStream &stream) { + destroy(); + + const uint32 form = stream.readUint32BE(); + + if (form != ID_FORM) { + warning("Failed reading IFF-file"); + return false; + } + + stream.skip(4); + + const uint32 type = stream.readUint32BE(); + + switch (type) { + case ID_ILBM: + _type = TYPE_ILBM; + break; + case ID_PBM: + _type = TYPE_PBM; + break; + } + + if (type == TYPE_UNKNOWN) { + warning("Failed reading IFF-file"); + return false; + } + + while (1) { + const uint32 chunkType = stream.readUint32BE(); + const uint32 chunkSize = stream.readUint32BE(); + + if (stream.eos()) + break; + + switch (chunkType) { + case ID_BMHD: + loadHeader(stream); + break; + case ID_CMAP: + loadPalette(stream, chunkSize); + break; + case ID_CRNG: + loadPaletteRange(stream, chunkSize); + break; + case ID_BODY: + loadBitmap(stream); + break; + default: + stream.skip(chunkSize); + } + } + + return true; +} + +void IFFDecoder::loadHeader(Common::SeekableReadStream &stream) { + _header.width = stream.readUint16BE(); + _header.height = stream.readUint16BE(); + _header.x = stream.readUint16BE(); + _header.y = stream.readUint16BE(); + _header.numPlanes = stream.readByte(); + _header.masking = stream.readByte(); + _header.compression = stream.readByte(); + _header.flags = stream.readByte(); + _header.transparentColor = stream.readUint16BE(); + _header.xAspect = stream.readByte(); + _header.yAspect = stream.readByte(); + _header.pageWidth = stream.readUint16BE(); + _header.pageHeight = stream.readUint16BE(); + + assert(_header.width >= 1); + assert(_header.height >= 1); + assert(_header.numPlanes >= 1 && _header.numPlanes <= 8 && _header.numPlanes != 7); +} + +void IFFDecoder::loadPalette(Common::SeekableReadStream &stream, const uint32 size) { + _palette = new byte[size]; + stream.read(_palette, size); + _paletteColorCount = size / 3; +} + +void IFFDecoder::loadPaletteRange(Common::SeekableReadStream &stream, const uint32 size) { + PaletteRange range; + + range.timer = stream.readSint16BE(); + range.step = stream.readSint16BE(); + range.flags = stream.readSint16BE(); + range.first = stream.readByte(); + range.last = stream.readByte(); + + _paletteRanges.push_back(range); +} + +void IFFDecoder::loadBitmap(Common::SeekableReadStream &stream) { + _numRelevantPlanes = MIN(_numRelevantPlanes, _header.numPlanes); + + if (_numRelevantPlanes != 1 && _numRelevantPlanes != 2 && _numRelevantPlanes != 4) + _pixelPacking = false; + + uint16 outPitch = _header.width; + + if (_pixelPacking) + outPitch /= (8 / _numRelevantPlanes); + + // FIXME: CLUT8 is not a proper format for packed bitmaps but there is no way to tell it to use 1, 2 or 4 bits per pixel + _surface = new Graphics::Surface(); + _surface->create(outPitch, _header.height, Graphics::PixelFormat::createFormatCLUT8()); + + if (_type == TYPE_ILBM) { + uint32 scanlinePitch = ((_header.width + 15) >> 4) << 1; + byte *scanlines = new byte[scanlinePitch * _header.numPlanes]; + byte *data = (byte *)_surface->pixels; + + for (uint16 i = 0; i < _header.height; ++i) { + byte *scanline = scanlines; + + for (uint16 j = 0; j < _header.numPlanes; ++j) { + uint16 outSize = scanlinePitch; + + if (_header.compression) { + Common::PackBitsReadStream packStream(stream); + packStream.read(scanline, outSize); + } else { + stream.read(scanline, outSize); + } + + scanline += outSize; + } + + packPixels(scanlines, data, scanlinePitch, outPitch); + data += outPitch; + } + + delete[] scanlines; + } else if (_type == TYPE_PBM) { + byte *data = (byte *)_surface->pixels; + uint32 outSize = _header.width * _header.height; + + if (_header.compression) { + Common::PackBitsReadStream packStream(stream); + packStream.read(data, outSize); + } else { + stream.read(data, outSize); + } + } +} + +void IFFDecoder::packPixels(byte *scanlines, byte *data, const uint16 scanlinePitch, const uint16 outPitch) { + uint32 numPixels = _header.width; + + if (_pixelPacking) + numPixels = outPitch * (8 / _numRelevantPlanes); + + for (uint32 x = 0; x < numPixels; ++x) { + byte *scanline = scanlines; + byte pixel = 0; + byte offset = x >> 3; + byte bit = 0x80 >> (x & 7); + + // first build a pixel by scanning all the usable planes in the input + for (uint32 plane = 0; plane < _numRelevantPlanes; ++plane) { + if (scanline[offset] & bit) + pixel |= (1 << plane); + + scanline += scanlinePitch; + } + + // then output the pixel according to the requested packing + if (!_pixelPacking) + data[x] = pixel; + else if (_numRelevantPlanes == 1) + data[x / 8] |= (pixel << (x & 7)); + else if (_numRelevantPlanes == 2) + data[x / 4] |= (pixel << ((x & 3) << 1)); + else if (_numRelevantPlanes == 4) + data[x / 2] |= (pixel << ((x & 1) << 2)); + } +} + +} // End of namespace Graphics diff --git a/graphics/decoders/iff.h b/graphics/decoders/iff.h new file mode 100644 index 0000000000..beac62e519 --- /dev/null +++ b/graphics/decoders/iff.h @@ -0,0 +1,119 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +/** + * @file + * Image decoder used in engines: + * - gob + * - parallaction + * - queen + * - saga + */ + +#ifndef GRAPHICS_DECODERS_IFF_H +#define GRAPHICS_DECODERS_IFF_H + +#include "common/array.h" +#include "common/endian.h" + +#include "graphics/surface.h" +#include "graphics/decoders/image_decoder.h" + +namespace Common { +class SeekableReadStream; +} + +namespace Graphics { + +struct Surface; + +class IFFDecoder : public ImageDecoder { +public: + struct Header { + uint16 width, height; + uint16 x, y; + byte numPlanes; + byte masking; + byte compression; + byte flags; + uint16 transparentColor; + byte xAspect, yAspect; + uint16 pageWidth, pageHeight; + }; + + struct PaletteRange { + int16 timer, step, flags; + byte first, last; + }; + + enum Type { + TYPE_UNKNOWN = 0, + TYPE_ILBM, + TYPE_PBM + }; + + IFFDecoder(); + virtual ~IFFDecoder(); + + // ImageDecoder API + void destroy(); + bool loadStream(Common::SeekableReadStream &stream); + const Header *getHeader() const { return &_header; } + const Surface *getSurface() const { return _surface; } + const byte *getPalette() const { return _palette; } + const Common::Array<PaletteRange> &getPaletteRanges() const { return _paletteRanges; } + uint16 getPaletteColorCount() const { return _paletteColorCount; } + + /** + * The number of planes to decode, also determines the pixel packing if _packPixels is true. + * 8 == decode all planes, map 1 pixel in 1 byte. (default, no packing even if _packPixels is true) + */ + void setNumRelevantPlanes(const uint8 numRelevantPlanes) { _numRelevantPlanes = numRelevantPlanes; } + + /** + * Enables pixel packing, the amount of packing is determined by _numRelevantPlanes + * 1 == decode first plane, pack 8 pixels in 1 byte. This makes _surface->w 1/8th of _header.width + * 2 == decode first 2 planes, pack 4 pixels in 1 byte. This makes _surface->w 1/4th of _header.width + * 4 == decode first 4 planes, pack 2 pixels in 1 byte. This makes _surface->w half of _header.width + * Packed bitmaps won't have a proper surface format since there is no way to tell it to use 1, 2 or 4 bits per pixel + */ + void setPixelPacking(const bool pixelPacking) { _pixelPacking = pixelPacking; } +private: + + Header _header; + Surface *_surface; + byte *_palette; + Common::Array<PaletteRange> _paletteRanges; + Type _type; + uint16 _paletteColorCount; + uint8 _numRelevantPlanes; + bool _pixelPacking; + + void loadHeader(Common::SeekableReadStream &stream); + void loadPalette(Common::SeekableReadStream &stream, const uint32 size); + void loadPaletteRange(Common::SeekableReadStream &stream, const uint32 size); + void loadBitmap(Common::SeekableReadStream &stream); + void packPixels(byte *scanlines, byte *data, const uint16 scanlinePitch, const uint16 outPitch); +}; + +} // End of namespace Graphics + +#endif // GRAPHICS_DECODERS_IFF_H diff --git a/graphics/decoders/jpeg.cpp b/graphics/decoders/jpeg.cpp index 08bc1f7a3d..75fdcd6e5a 100644 --- a/graphics/decoders/jpeg.cpp +++ b/graphics/decoders/jpeg.cpp @@ -74,7 +74,7 @@ const Surface *JPEGDecoder::getSurface() const { // Create an RGBA8888 surface _rgbSurface = new Graphics::Surface(); - _rgbSurface->create(_w, _h, Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0)); + _rgbSurface->create(_w, _h, Graphics::PixelFormat(4, 8, 8, 8, 0, 24, 16, 8, 0)); // Get our component surfaces const Graphics::Surface *yComponent = getComponent(1); @@ -215,28 +215,34 @@ bool JPEGDecoder::loadStream(Common::SeekableReadStream &stream) { bool JPEGDecoder::readJFIF() { uint16 length = _stream->readUint16BE(); uint32 tag = _stream->readUint32BE(); + if (tag != MKTAG('J', 'F', 'I', 'F')) { warning("JPEGDecoder::readJFIF() tag mismatch"); return false; } + if (_stream->readByte() != 0) { // NULL warning("JPEGDecoder::readJFIF() NULL mismatch"); return false; } + byte majorVersion = _stream->readByte(); byte minorVersion = _stream->readByte(); - if (majorVersion != 1 || minorVersion != 1) - warning("JPEGDecoder::readJFIF() Non-v1.1 JPEGs may not be handled correctly"); + if (majorVersion != 1 || minorVersion > 2) + warning("JPEGDecoder::readJFIF(): v%d.%02d JPEGs may not be handled correctly", majorVersion, minorVersion); + /* byte densityUnits = */_stream->readByte(); /* uint16 xDensity = */_stream->readUint16BE(); /* uint16 yDensity = */_stream->readUint16BE(); byte thumbW = _stream->readByte(); byte thumbH = _stream->readByte(); + _stream->seek(thumbW * thumbH * 3, SEEK_CUR); // Ignore thumbnail if (length != (thumbW * thumbH * 3) + 16) { warning("JPEGDecoder::readJFIF() length mismatch"); return false; } + return true; } @@ -481,8 +487,8 @@ bool JPEGDecoder::readDQT() { // Validate the table id tableId &= 0xF; - if (tableId > JPEG_MAX_QUANT_TABLES) { - warning("JPEG: Invalid number of components"); + if (tableId >= JPEG_MAX_QUANT_TABLES) { + warning("JPEG: Invalid quantization table"); return false; } diff --git a/graphics/decoders/jpeg.h b/graphics/decoders/jpeg.h index c74aa57ca1..d59b72adf4 100644 --- a/graphics/decoders/jpeg.h +++ b/graphics/decoders/jpeg.h @@ -25,6 +25,7 @@ * Image decoder used in engines: * - groovie * - mohawk + * - wintermute */ #ifndef GRAPHICS_JPEG_H diff --git a/graphics/decoders/pict.cpp b/graphics/decoders/pict.cpp index d35e5c3064..b1d408ebc3 100644 --- a/graphics/decoders/pict.cpp +++ b/graphics/decoders/pict.cpp @@ -235,10 +235,13 @@ bool PICTDecoder::loadStream(Common::SeekableReadStream &stream) { // PICT v2 opcodes are two bytes uint16 opcode = stream.readUint16BE(); - if (opNum == 0 && opcode != 0x0011) - error("Cannot find PICT version opcode"); - else if (opNum == 1 && opcode != 0x0C00) - error("Cannot find PICT header opcode"); + if (opNum == 0 && opcode != 0x0011) { + warning("Cannot find PICT version opcode"); + return false; + } else if (opNum == 1 && opcode != 0x0C00) { + warning("Cannot find PICT header opcode"); + return false; + } // Since opcodes are word-aligned, we need to mark our starting // position here. diff --git a/graphics/decoders/png.cpp b/graphics/decoders/png.cpp index 4f917b44b1..11e26162eb 100644 --- a/graphics/decoders/png.cpp +++ b/graphics/decoders/png.cpp @@ -227,7 +227,7 @@ bool PNGDecoder::loadStream(Common::SeekableReadStream &stream) { png_read_end(pngPtr, NULL); // Destroy libpng structures - png_destroy_read_struct(&pngPtr, &infoPtr, NULL); + png_destroy_read_struct(&pngPtr, &infoPtr, &endInfo); // We no longer need the file stream, thus close it here _stream = 0; diff --git a/graphics/decoders/png.h b/graphics/decoders/png.h index e52ddabd7d..5e608eb7b1 100644 --- a/graphics/decoders/png.h +++ b/graphics/decoders/png.h @@ -23,6 +23,7 @@ /* * PNG decoder used in engines: * - sword25 + * - wintermute * Dependencies: * - libpng */ diff --git a/graphics/decoders/tga.h b/graphics/decoders/tga.h index dfdc5a4da9..d8ccf8f766 100644 --- a/graphics/decoders/tga.h +++ b/graphics/decoders/tga.h @@ -25,7 +25,7 @@ /* * TGA decoder used in engines: - * - none + * - wintermute */ #ifndef GRAPHICS_DECODERS_TGA_H |