aboutsummaryrefslogtreecommitdiff
path: root/engines/parallaction/iff.cpp
blob: c15e8fcbd2aea38434ba1ff679b3e1497682b4d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* 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.
 *
 * $URL$
 * $Id$
 *
 */


#include "common/iff_container.h"
#include "common/stream.h"
#include "common/util.h"
#include "parallaction/iff.h"

namespace Parallaction {


void IFFParser::setInputStream(Common::SeekableReadStream *stream) {
	destroy();

	assert(stream);
	_stream = stream;
	_startOffset = 0;
	_endOffset = _stream->size();

	_formType = 0;
	_formSize = (uint32)-1;

	if (_stream->size() < 12) {
		// this file is too small to be a valid IFF container
		return;
	}

	if (_stream->readUint32BE() != ID_FORM) {
		// no FORM header was found
		return;
	}

	_formSize = _stream->readUint32BE();
	_formType = _stream->readUint32BE();
}

void IFFParser::destroy() {
	_stream = 0;
	_startOffset = _endOffset = 0;
}

uint32 IFFParser::getFORMSize() const {
	return _formSize;
}

Common::IFF_ID IFFParser::getFORMType() const {
	return _formType;
}

uint32 IFFParser::moveToIFFBlock(Common::IFF_ID chunkName) {
	uint32 size = (uint32)-1;

	_stream->seek(_startOffset + 0x0C);

	while ((uint)_stream->pos() < _endOffset) {
		uint32 chunk = _stream->readUint32BE();
		uint32 size_temp = _stream->readUint32BE();

		if (chunk != chunkName) {
			_stream->seek((size_temp + 1) & (~1), SEEK_CUR);
			assert((uint)_stream->pos() <= _endOffset);
		} else {
			size = size_temp;
			break;
		}
	}

	return size;
}

uint32 IFFParser::getIFFBlockSize(Common::IFF_ID chunkName) {
	uint32 size = moveToIFFBlock(chunkName);
	return size;
}

bool IFFParser::loadIFFBlock(Common::IFF_ID chunkName, void *loadTo, uint32 ptrSize) {
	uint32 chunkSize = moveToIFFBlock(chunkName);

	if (chunkSize == (uint32)-1) {
		return false;
	}

	uint32 loadSize = 0;
	loadSize = MIN(ptrSize, chunkSize);
	_stream->read(loadTo, loadSize);
	return true;
}

Common::SeekableReadStream *IFFParser::getIFFBlockStream(Common::IFF_ID chunkName) {
	uint32 chunkSize = moveToIFFBlock(chunkName);

	if (chunkSize == (uint32)-1) {
		return 0;
	}

	uint32 pos = _stream->pos();
	return new Common::SeekableSubReadStream(_stream, pos, pos + chunkSize, false);
}


// ILBM decoder implementation

ILBMDecoder::ILBMDecoder(Common::SeekableReadStream *in, bool disposeStream) : _in(in), _hasHeader(false), _bodySize((uint32)-1), _paletteSize((uint32)-1) {
	assert(in);
	_parser.setInputStream(in);

	if (_parser.getFORMType() != ID_ILBM) {
		return;
	}

	_hasHeader = _parser.loadIFFBlock(ID_BMHD, &_header, sizeof(_header));
	if (!_hasHeader) {
		return;
	}

	_header.width = TO_BE_16(_header.width);
	_header.height = TO_BE_16(_header.height);

	_paletteSize = _parser.getIFFBlockSize(ID_CMAP);
	_bodySize = _parser.getIFFBlockSize(ID_BODY);
}


ILBMDecoder::~ILBMDecoder() {
	if (_disposeStream) {
		delete _in;
	}
}

uint32 ILBMDecoder::getWidth() {
	assert(_hasHeader);
	return _header.width;
}

uint32 ILBMDecoder::getHeight() {
	assert(_hasHeader);
	return _header.height;
}

uint32 ILBMDecoder::getNumColors() {
	assert(_hasHeader);
	return (1 << _header.depth);
}

byte *ILBMDecoder::getPalette() {
	assert(_paletteSize != (uint32)-1);
	byte *palette = new byte[_paletteSize];
	assert(palette);
	_parser.loadIFFBlock(ID_CMAP, palette, _paletteSize);
	return palette;
}

byte *ILBMDecoder::getBitmap(uint32 numPlanes, bool packPlanes) {
	assert(_bodySize != (uint32)-1);
	assert(numPlanes == 1 || numPlanes == 2 || numPlanes == 3 || numPlanes == 4 || numPlanes == 5 || numPlanes == 8);

	numPlanes = MIN(numPlanes, (uint32)_header.depth);
	if (numPlanes > 4) {
		packPlanes = false;
	}

	uint32 bitmapSize = _header.width * _header.height;
	uint32 bitmapWidth = _header.width;
	if (packPlanes) {
		bitmapSize /= (8 / numPlanes);
		bitmapWidth /= (8 / numPlanes);
	}

	Common::SeekableReadStream *bodyStream = _parser.getIFFBlockStream(ID_BODY);
	assert(bodyStream);

	byte *bitmap = new byte[bitmapSize];
	assert(bitmap);
	memset(bitmap, 0, bitmapSize);

	switch (_header.pack) {
	case 1: {	// PackBits compressed bitmap
		Graphics::PackBitsReadStream stream(*bodyStream);

		byte *out = bitmap;

		// setup a buffer to hold enough data to build a line in the output
		uint32 scanWidth = (_header.width + 7) >> 3;
		byte *scanBuffer = (byte*)malloc(scanWidth * _header.depth);

		for (uint i = 0; i < _header.height; ++i) {
			byte *s = scanBuffer;
			for (uint32 j = 0; j < _header.depth; ++j) {
				stream.read(s, scanWidth);
				s += scanWidth;
			}

			planarToChunky(out, bitmapWidth, scanBuffer, scanWidth, numPlanes, packPlanes);
			out += bitmapWidth;
		}

		free(scanBuffer);
		break;
	}
	default:
		error("only RLE compressed ILBM files are supported");
		break;
	}

	delete bodyStream;

	return bitmap;
}


void ILBMDecoder::planarToChunky(byte *out, uint32 width, byte *in, uint32 planeWidth, uint32 nPlanes, bool packPlanes) {
	byte pix, ofs, bit;
	byte *s;

	uint32 pixels = width;
	if (packPlanes) {
		pixels *= (8 / nPlanes);
	}

	for (uint32 x = 0; x < pixels; ++x) {

		pix = 0;
		ofs = x >> 3;
		bit = 0x80 >> (x & 7);

		// first build a pixel by scanning all the usable planes in the input
		s = in;
		for (uint32 plane = 0; plane < nPlanes; ++plane) {
			if (s[ofs] & bit) {
				pix |= (1 << plane);
			}
			s += planeWidth;
		}


		// then output the pixel according to the requested packing
		if (!packPlanes) {
			out[x] = pix;
		} else
		if (nPlanes == 1) {
			out[x/8] |= (pix << (x & 7));
		} else
		if (nPlanes == 2) {
			out[x/4] |= (pix << ((x & 3) << 1));
		} else
		if (nPlanes == 4) {
			out[x/2] |= (pix << ((x & 1) << 2));
		}
	}

}


} // End of namespace Parallaction