aboutsummaryrefslogtreecommitdiff
path: root/engines/mohawk/video/qtrle.cpp
blob: c06dbefcb327d66070b54b5413c67ccecead5f8a (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* 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$
 *
 */

// QuickTime RLE Decoder
// Based off ffmpeg's QuickTime RLE decoder (written by Mike Melanson)

#include "mohawk/video/qtrle.h"

#include "common/scummsys.h"
#include "common/stream.h"
#include "common/system.h"
#include "graphics/colormasks.h"
#include "graphics/surface.h"

namespace Mohawk {

QTRLEDecoder::QTRLEDecoder(uint16 width, uint16 height, byte bitsPerPixel) : Graphics::Codec() {
	_bitsPerPixel = bitsPerPixel;
	_pixelFormat = g_system->getScreenFormat();

	// We need to increase the surface size to a multiple of 4
	uint16 wMod = width % 4;
	if(wMod != 0)
		width += 4 - wMod;

	debug(2, "QTRLE corrected width: %d", width);

	_surface = new Graphics::Surface();
	_surface->create(width, height, _bitsPerPixel <= 8 ? 1 : _pixelFormat.bytesPerPixel);
}

#define CHECK_STREAM_PTR(n) \
  if ((stream->pos() + n) > stream->size()) { \
    warning ("Problem: stream out of bounds (%d >= %d)", stream->pos() + n, stream->size()); \
    return; \
  }

#define CHECK_PIXEL_PTR(n) \
  if ((int32)pixelPtr + n > _surface->w * _surface->h) { \
    warning ("Problem: pixel ptr = %d, pixel limit = %d", pixelPtr + n, _surface->w * _surface->h); \
    return; \
  } \

void QTRLEDecoder::decode1(Common::SeekableReadStream *stream, uint32 rowPtr, uint32 linesToChange) {
	uint32 pixelPtr = 0;
	byte *rgb = (byte *)_surface->pixels;

	while (linesToChange) {
		CHECK_STREAM_PTR(2);
		byte skip = stream->readByte();
		int8 rleCode = stream->readSByte();

		if (rleCode == 0)
			break;

		if (skip & 0x80) {
			linesToChange--;
			rowPtr += _surface->w;
			pixelPtr = rowPtr + 2 * (skip & 0x7f);
		} else
			pixelPtr += 2 * skip;

		if (rleCode < 0) {
			// decode the run length code
			rleCode = -rleCode;
			// get the next 2 bytes from the stream, treat them as groups of 8 pixels, and output them rleCode times */
			CHECK_STREAM_PTR(2);
			byte pi0 = stream->readByte();
			byte pi1 = stream->readByte();
			CHECK_PIXEL_PTR(rleCode * 2);

			while (rleCode--) {
				rgb[pixelPtr++] = pi0;
				rgb[pixelPtr++] = pi1;
			}
		} else {
			// copy the same pixel directly to output 2 times
			rleCode *= 2;
			CHECK_STREAM_PTR(rleCode);
			CHECK_PIXEL_PTR(rleCode);

			while (rleCode--)
				rgb[pixelPtr++] = stream->readByte();
		}
	}
}

void QTRLEDecoder::decode2_4(Common::SeekableReadStream *stream, uint32 rowPtr, uint32 linesToChange, byte bpp) {
	uint32 pixelPtr = 0;
	byte *rgb = (byte *)_surface->pixels;
	byte numPixels = (bpp == 4) ? 8 : 16;

	while (linesToChange--) {
		CHECK_STREAM_PTR(2);
		pixelPtr = rowPtr + (numPixels * (stream->readByte() - 1));

		for (int8 rleCode = stream->readSByte(); rleCode != -1; rleCode = stream->readSByte()) {
			if (rleCode == 0) {
				// there's another skip code in the stream
				CHECK_STREAM_PTR(1);
				pixelPtr += (numPixels * (stream->readByte() - 1));
			} else if (rleCode < 0) {
				// decode the run length code
				rleCode = -rleCode;

				// get the next 4 bytes from the stream, treat them as palette indices, and output them rleCode times */
				CHECK_STREAM_PTR(4);

				byte pi[16]; // 16 palette indices

				for (int8 i = numPixels - 1; i >= 0; i--) {
					pi[numPixels - 1 - i] = (stream->readByte() >> ((i * bpp) & 0x07)) & ((1 << bpp) - 1);

					// FIXME: Is this right?
					//stream_ptr += ((i & ((num_pixels>>2)-1)) == 0);
					if ((i & ((numPixels >> 2) - 1)) == 0)
						stream->readByte();
				}

				CHECK_PIXEL_PTR(rleCode * numPixels);

				while (rleCode--)
					for (byte i = 0; i < numPixels; i++)
						rgb[pixelPtr++] = pi[i];
			} else {
				// copy the same pixel directly to output 4 times
				rleCode *= 4;
				CHECK_STREAM_PTR(rleCode);
				CHECK_PIXEL_PTR(rleCode * (numPixels >> 2));

				while (rleCode--) {
					byte temp = stream->readByte();
					if (bpp == 4) {
						rgb[pixelPtr++] = (temp >> 4) & 0x0f;
						rgb[pixelPtr++] = temp & 0x0f;
					} else {
						rgb[pixelPtr++] = (temp >> 6) & 0x03;
						rgb[pixelPtr++] = (temp >> 4) & 0x03;
						rgb[pixelPtr++] = (temp >> 2) & 0x03;
						rgb[pixelPtr++] = temp & 0x03;
					}
				}
			}
		}

		rowPtr += _surface->w;
	}
}

void QTRLEDecoder::decode8(Common::SeekableReadStream *stream, uint32 rowPtr, uint32 linesToChange) {
	uint32 pixelPtr = 0;
	byte *rgb = (byte *)_surface->pixels;

	while (linesToChange--) {
		CHECK_STREAM_PTR(2);
		pixelPtr = rowPtr + 4 * (stream->readByte() - 1);

		for (int8 rleCode = stream->readSByte(); rleCode != -1; rleCode = stream->readSByte()) {
			if (rleCode == 0) {
				// there's another skip code in the stream
				CHECK_STREAM_PTR(1);
				pixelPtr += 4 * (stream->readByte() - 1);
			} else if (rleCode < 0) {
				// decode the run length code
				rleCode = -rleCode;

				// get the next 4 bytes from the stream, treat them as palette indices, and output them rleCode times
				CHECK_STREAM_PTR(4);

				byte pi[4];  // 4 palette indexes

				for (byte i = 0; i < 4; i++)
					pi[i] = stream->readByte();

				CHECK_PIXEL_PTR(rleCode * 4);

				while (rleCode--)
					for (byte i = 0; i < 4; i++)
						rgb[pixelPtr++] = pi[i];
			} else {
				// copy the same pixel directly to output 4 times
				rleCode *= 4;
				CHECK_STREAM_PTR(rleCode);
				CHECK_PIXEL_PTR(rleCode);

				while (rleCode--)
					rgb[pixelPtr++] = stream->readByte();
			}
		}

		rowPtr += _surface->w;
	}
}

void QTRLEDecoder::decode16(Common::SeekableReadStream *stream, uint32 rowPtr, uint32 linesToChange) {
	uint32 pixelPtr = 0;
	OverlayColor *rgb = (OverlayColor *)_surface->pixels;

	while (linesToChange--) {
		CHECK_STREAM_PTR(2);
		pixelPtr = rowPtr + stream->readByte() - 1;

		for (int8 rleCode = stream->readSByte(); rleCode != -1; rleCode = stream->readSByte()) {
			if (rleCode == 0) {
				// there's another skip code in the stream
				CHECK_STREAM_PTR(1);
				pixelPtr += stream->readByte() - 1;
			} else if (rleCode < 0) {
				// decode the run length code
				rleCode = -rleCode;
				CHECK_STREAM_PTR(2);

				uint16 rgb16 = stream->readUint16BE();

				CHECK_PIXEL_PTR(rleCode);

				while (rleCode--) {
					// Convert from RGB555 to the format specified by the Overlay
					byte r = 0, g = 0, b = 0;
					Graphics::colorToRGB<Graphics::ColorMasks<555> >(rgb16, r, g, b);
					rgb[pixelPtr++] = _pixelFormat.RGBToColor(r, g, b);
				}
			} else {
				CHECK_STREAM_PTR(rleCode * 2);
				CHECK_PIXEL_PTR(rleCode);

				// copy pixels directly to output
				while (rleCode--) {
					uint16 rgb16 = stream->readUint16BE();

					// Convert from RGB555 to the format specified by the Overlay
					byte r = 0, g = 0, b = 0;
					Graphics::colorToRGB<Graphics::ColorMasks<555> >(rgb16, r, g, b);
					rgb[pixelPtr++] = _pixelFormat.RGBToColor(r, g, b);
				}
			}
		}

		rowPtr += _surface->w;
	}
}

void QTRLEDecoder::decode24(Common::SeekableReadStream *stream, uint32 rowPtr, uint32 linesToChange) {
	uint32 pixelPtr = 0;
	OverlayColor *rgb = (OverlayColor *)_surface->pixels;

	while (linesToChange--) {
		CHECK_STREAM_PTR(2);
		pixelPtr = rowPtr + stream->readByte() - 1;

		for (int8 rleCode = stream->readSByte(); rleCode != -1; rleCode = stream->readSByte()) {
			if (rleCode == 0) {
				// there's another skip code in the stream
				CHECK_STREAM_PTR(1);
				pixelPtr += stream->readByte() - 1;
			} else if (rleCode < 0) {
				// decode the run length code
				rleCode = -rleCode;

				CHECK_STREAM_PTR(3);

				byte r = stream->readByte();
				byte g = stream->readByte();
				byte b = stream->readByte();

				CHECK_PIXEL_PTR(rleCode);

				while (rleCode--)
					rgb[pixelPtr++] = _pixelFormat.RGBToColor(r, g, b);
			} else {
				CHECK_STREAM_PTR(rleCode * 3);
				CHECK_PIXEL_PTR(rleCode);

				// copy pixels directly to output
				while (rleCode--) {
					byte r = stream->readByte();
					byte g = stream->readByte();
					byte b = stream->readByte();
					rgb[pixelPtr++] = _pixelFormat.RGBToColor(r, g, b);
				}
			}
		}

		rowPtr += _surface->w;
	}
}

void QTRLEDecoder::decode32(Common::SeekableReadStream *stream, uint32 rowPtr, uint32 linesToChange) {
	uint32 pixelPtr = 0;
	OverlayColor *rgb = (OverlayColor *)_surface->pixels;

	while (linesToChange--) {
		CHECK_STREAM_PTR(2);
		pixelPtr = rowPtr + stream->readByte() - 1;

		for (int8 rleCode = stream->readSByte(); rleCode != -1; rleCode = stream->readSByte()) {
			if (rleCode == 0) {
				// there's another skip code in the stream
				CHECK_STREAM_PTR(1);
				pixelPtr += stream->readByte() - 1;
			} else if (rleCode < 0) {
				// decode the run length code
				rleCode = -rleCode;

				CHECK_STREAM_PTR(4);

				byte a = stream->readByte();
				byte r = stream->readByte();
				byte g = stream->readByte();
				byte b = stream->readByte();

				CHECK_PIXEL_PTR(rleCode);

				while (rleCode--)
					rgb[pixelPtr++] = _pixelFormat.ARGBToColor(a, r, g, b);
			} else {
				CHECK_STREAM_PTR(rleCode * 4);
				CHECK_PIXEL_PTR(rleCode);

				// copy pixels directly to output
				while (rleCode--) {
					byte a = stream->readByte();
					byte r = stream->readByte();
					byte g = stream->readByte();
					byte b = stream->readByte();
					rgb[pixelPtr++] = _pixelFormat.ARGBToColor(a, r, g, b);
				}
			}
		}

		rowPtr += _surface->w;
	}
}

Graphics::Surface *QTRLEDecoder::decodeImage(Common::SeekableReadStream *stream) {
	uint16 start_line = 0;
	uint16 height = _surface->h;

	// check if this frame is even supposed to change
	if (stream->size() < 8)
		return _surface;

	// start after the chunk size
	stream->readUint32BE();

	// fetch the header
	uint16 header = stream->readUint16BE();

	// if a header is present, fetch additional decoding parameters
	if (header & 8) {
		if(stream->size() < 14)
			return _surface;
		start_line = stream->readUint16BE();
		stream->readUint16BE(); // Unknown
		height = stream->readUint16BE();
		stream->readUint16BE(); // Unknown
	}

	uint32 row_ptr = _surface->w * start_line;

	switch (_bitsPerPixel) {
		case 1:
		case 33:
			decode1(stream, row_ptr, height);
			break;
		case 2:
		case 34:
			decode2_4(stream, row_ptr, height, 2);
			break;
		case 4:
		case 36:
			decode2_4(stream, row_ptr, height, 4);
			break;
		case 8:
		case 40:
			decode8(stream, row_ptr, height);
			break;
		case 16:
			decode16(stream, row_ptr, height);
			break;
		case 24:
			decode24(stream, row_ptr, height);
			break;
		case 32:
			decode32(stream, row_ptr, height);
			break;
		default:
			error ("Unsupported bits per pixel %d", _bitsPerPixel);
	}

	return _surface;
}

QTRLEDecoder::~QTRLEDecoder() {
	_surface->free();
}

} // End of namespace Mohawk