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
|
/* 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.
*
*/
// Based off ffmpeg's RPZA decoder
#include "image/codecs/rpza.h"
#include "common/debug.h"
#include "common/system.h"
#include "common/stream.h"
#include "common/textconsole.h"
namespace Image {
RPZADecoder::RPZADecoder(uint16 width, uint16 height) : Codec() {
_format = Graphics::PixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0);
_ditherPalette = 0;
_dirtyPalette = false;
_colorMap = 0;
_width = width;
_height = height;
_blockWidth = (width + 3) / 4;
_blockHeight = (height + 3) / 4;
_surface = 0;
}
RPZADecoder::~RPZADecoder() {
if (_surface) {
_surface->free();
delete _surface;
}
delete[] _ditherPalette;
delete[] _colorMap;
}
#define ADVANCE_BLOCK() \
blockPtr += 4; \
if (blockPtr >= endPtr) { \
blockPtr += pitch * 3; \
endPtr = blockPtr + pitch; \
} \
totalBlocks--; \
if (totalBlocks < 0) \
error("rpza block counter just went negative (this should not happen)") \
struct BlockDecoderRaw {
static inline void drawFillBlock(uint16 *blockPtr, uint16 pitch, uint16 color, const byte *colorMap) {
blockPtr[0] = color;
blockPtr[1] = color;
blockPtr[2] = color;
blockPtr[3] = color;
blockPtr += pitch;
blockPtr[0] = color;
blockPtr[1] = color;
blockPtr[2] = color;
blockPtr[3] = color;
blockPtr += pitch;
blockPtr[0] = color;
blockPtr[1] = color;
blockPtr[2] = color;
blockPtr[3] = color;
blockPtr += pitch;
blockPtr[0] = color;
blockPtr[1] = color;
blockPtr[2] = color;
blockPtr[3] = color;
}
static inline void drawRawBlock(uint16 *blockPtr, uint16 pitch, const uint16 (&colors)[16], const byte *colorMap) {
blockPtr[0] = colors[0];
blockPtr[1] = colors[1];
blockPtr[2] = colors[2];
blockPtr[3] = colors[3];
blockPtr += pitch;
blockPtr[0] = colors[4];
blockPtr[1] = colors[5];
blockPtr[2] = colors[6];
blockPtr[3] = colors[7];
blockPtr += pitch;
blockPtr[0] = colors[8];
blockPtr[1] = colors[9];
blockPtr[2] = colors[10];
blockPtr[3] = colors[11];
blockPtr += pitch;
blockPtr[0] = colors[12];
blockPtr[1] = colors[13];
blockPtr[2] = colors[14];
blockPtr[3] = colors[15];
}
static inline void drawBlendBlock(uint16 *blockPtr, uint16 pitch, const uint16 (&colors)[4], const byte (&indexes)[4], const byte *colorMap) {
blockPtr[0] = colors[(indexes[0] >> 6) & 0x03];
blockPtr[1] = colors[(indexes[0] >> 4) & 0x03];
blockPtr[2] = colors[(indexes[0] >> 2) & 0x03];
blockPtr[3] = colors[(indexes[0] >> 0) & 0x03];
blockPtr += pitch;
blockPtr[0] = colors[(indexes[1] >> 6) & 0x03];
blockPtr[1] = colors[(indexes[1] >> 4) & 0x03];
blockPtr[2] = colors[(indexes[1] >> 2) & 0x03];
blockPtr[3] = colors[(indexes[1] >> 0) & 0x03];
blockPtr += pitch;
blockPtr[0] = colors[(indexes[2] >> 6) & 0x03];
blockPtr[1] = colors[(indexes[2] >> 4) & 0x03];
blockPtr[2] = colors[(indexes[2] >> 2) & 0x03];
blockPtr[3] = colors[(indexes[2] >> 0) & 0x03];
blockPtr += pitch;
blockPtr[0] = colors[(indexes[3] >> 6) & 0x03];
blockPtr[1] = colors[(indexes[3] >> 4) & 0x03];
blockPtr[2] = colors[(indexes[3] >> 2) & 0x03];
blockPtr[3] = colors[(indexes[3] >> 0) & 0x03];
}
};
struct BlockDecoderDither {
static inline void drawFillBlock(byte *blockPtr, uint16 pitch, uint16 color, const byte *colorMap) {
const byte *mapOffset = colorMap + (color >> 1);
byte pixel1 = mapOffset[0x0000];
byte pixel2 = mapOffset[0x4000];
byte pixel3 = mapOffset[0x8000];
byte pixel4 = mapOffset[0xC000];
blockPtr[0] = pixel1;
blockPtr[1] = pixel2;
blockPtr[2] = pixel3;
blockPtr[3] = pixel4;
blockPtr += pitch;
blockPtr[0] = pixel4;
blockPtr[1] = pixel1;
blockPtr[2] = pixel2;
blockPtr[3] = pixel3;
blockPtr += pitch;
blockPtr[0] = pixel2;
blockPtr[1] = pixel3;
blockPtr[2] = pixel4;
blockPtr[3] = pixel1;
blockPtr += pitch;
blockPtr[0] = pixel3;
blockPtr[1] = pixel4;
blockPtr[2] = pixel1;
blockPtr[3] = pixel2;
}
static inline void drawRawBlock(byte *blockPtr, uint16 pitch, const uint16 (&colors)[16], const byte *colorMap) {
blockPtr[0] = colorMap[(colors[0] >> 1) + 0x0000];
blockPtr[1] = colorMap[(colors[1] >> 1) + 0x4000];
blockPtr[2] = colorMap[(colors[2] >> 1) + 0x8000];
blockPtr[3] = colorMap[(colors[3] >> 1) + 0xC000];
blockPtr += pitch;
blockPtr[0] = colorMap[(colors[4] >> 1) + 0xC000];
blockPtr[1] = colorMap[(colors[5] >> 1) + 0x0000];
blockPtr[2] = colorMap[(colors[6] >> 1) + 0x4000];
blockPtr[3] = colorMap[(colors[7] >> 1) + 0x8000];
blockPtr += pitch;
blockPtr[0] = colorMap[(colors[8] >> 1) + 0x4000];
blockPtr[1] = colorMap[(colors[9] >> 1) + 0x8000];
blockPtr[2] = colorMap[(colors[10] >> 1) + 0xC000];
blockPtr[3] = colorMap[(colors[11] >> 1) + 0x0000];
blockPtr += pitch;
blockPtr[0] = colorMap[(colors[12] >> 1) + 0x8000];
blockPtr[1] = colorMap[(colors[13] >> 1) + 0xC000];
blockPtr[2] = colorMap[(colors[14] >> 1) + 0x0000];
blockPtr[3] = colorMap[(colors[15] >> 1) + 0x4000];
}
static inline void drawBlendBlock(byte *blockPtr, uint16 pitch, const uint16 (&colors)[4], const byte (&indexes)[4], const byte *colorMap) {
blockPtr[0] = colorMap[(colors[(indexes[0] >> 6) & 0x03] >> 1) + 0x0000];
blockPtr[1] = colorMap[(colors[(indexes[0] >> 4) & 0x03] >> 1) + 0x4000];
blockPtr[2] = colorMap[(colors[(indexes[0] >> 2) & 0x03] >> 1) + 0x8000];
blockPtr[3] = colorMap[(colors[(indexes[0] >> 0) & 0x03] >> 1) + 0xC000];
blockPtr += pitch;
blockPtr[0] = colorMap[(colors[(indexes[1] >> 6) & 0x03] >> 1) + 0xC000];
blockPtr[1] = colorMap[(colors[(indexes[1] >> 4) & 0x03] >> 1) + 0x0000];
blockPtr[2] = colorMap[(colors[(indexes[1] >> 2) & 0x03] >> 1) + 0x4000];
blockPtr[3] = colorMap[(colors[(indexes[1] >> 0) & 0x03] >> 1) + 0x8000];
blockPtr += pitch;
blockPtr[0] = colorMap[(colors[(indexes[2] >> 6) & 0x03] >> 1) + 0x4000];
blockPtr[1] = colorMap[(colors[(indexes[2] >> 4) & 0x03] >> 1) + 0x8000];
blockPtr[2] = colorMap[(colors[(indexes[2] >> 2) & 0x03] >> 1) + 0xC000];
blockPtr[3] = colorMap[(colors[(indexes[2] >> 0) & 0x03] >> 1) + 0x0000];
blockPtr += pitch;
blockPtr[0] = colorMap[(colors[(indexes[3] >> 6) & 0x03] >> 1) + 0x8000];
blockPtr[1] = colorMap[(colors[(indexes[3] >> 4) & 0x03] >> 1) + 0xC000];
blockPtr[2] = colorMap[(colors[(indexes[3] >> 2) & 0x03] >> 1) + 0x0000];
blockPtr[3] = colorMap[(colors[(indexes[3] >> 0) & 0x03] >> 1) + 0x4000];
}
};
template<typename PixelInt, typename BlockDecoder>
static inline void decodeFrameTmpl(Common::SeekableReadStream &stream, PixelInt *ptr, uint16 pitch, uint16 blockWidth, uint16 blockHeight, const byte *colorMap) {
uint16 colorA = 0, colorB = 0;
uint16 color4[4];
PixelInt *blockPtr = ptr;
PixelInt *endPtr = ptr + pitch;
uint16 ta;
uint16 tb;
// First byte is always 0xe1. Warn if it's different
byte firstByte = stream.readByte();
if (firstByte != 0xe1)
warning("First RPZA chunk byte is 0x%02x instead of 0xe1", firstByte);
// Get chunk size, ingnoring first byte
uint32 chunkSize = stream.readUint16BE() << 8;
chunkSize += stream.readByte();
// If length mismatch use size from MOV file and try to decode anyway
if (chunkSize != (uint32)stream.size()) {
warning("MOV chunk size != encoded chunk size; using MOV chunk size");
chunkSize = stream.size();
}
// Number of 4x4 blocks in frame
int32 totalBlocks = blockWidth * blockHeight;
// Process chunk data
while ((uint32)stream.pos() < chunkSize) {
byte opcode = stream.readByte(); // Get opcode
byte numBlocks = (opcode & 0x1f) + 1; // Extract block counter from opcode
// If opcode MSbit is 0, we need more data to decide what to do
if ((opcode & 0x80) == 0) {
colorA = (opcode << 8) | stream.readByte();
opcode = 0;
if (stream.readByte() & 0x80) {
// Must behave as opcode 110xxxxx, using colorA computed
// above. Use fake opcode 0x20 to enter switch block at
// the right place
opcode = 0x20;
numBlocks = 1;
}
stream.seek(-1, SEEK_CUR);
}
switch (opcode & 0xe0) {
case 0x80: // Skip blocks
while (numBlocks--) {
ADVANCE_BLOCK();
}
break;
case 0xa0: // Fill blocks with one color
colorA = stream.readUint16BE();
while (numBlocks--) {
BlockDecoder::drawFillBlock(blockPtr, pitch, colorA, colorMap);
ADVANCE_BLOCK();
}
break;
// Fill blocks with 4 colors
case 0xc0:
colorA = stream.readUint16BE();
case 0x20:
colorB = stream.readUint16BE();
// Sort out the colors
color4[0] = colorB & 0x7FFF;
color4[1] = 0;
color4[2] = 0;
color4[3] = colorA & 0x7FFF;
// Red components
ta = (colorA >> 10) & 0x1F;
tb = (colorB >> 10) & 0x1F;
color4[1] |= ((11 * ta + 21 * tb) >> 5) << 10;
color4[2] |= ((21 * ta + 11 * tb) >> 5) << 10;
// Green components
ta = (colorA >> 5) & 0x1F;
tb = (colorB >> 5) & 0x1F;
color4[1] |= ((11 * ta + 21 * tb) >> 5) << 5;
color4[2] |= ((21 * ta + 11 * tb) >> 5) << 5;
// Blue components
ta = colorA & 0x1F;
tb = colorB & 0x1F;
color4[1] |= ((11 * ta + 21 * tb) >> 5);
color4[2] |= ((21 * ta + 11 * tb) >> 5);
while (numBlocks--) {
byte indexes[4];
stream.read(indexes, 4);
BlockDecoder::drawBlendBlock(blockPtr, pitch, color4, indexes, colorMap);
ADVANCE_BLOCK();
}
break;
// Fill block with 16 colors
case 0x00: {
uint16 colors[16];
colors[0] = colorA;
for (int i = 0; i < 15; i++)
colors[i + 1] = stream.readUint16BE();
BlockDecoder::drawRawBlock(blockPtr, pitch, colors, colorMap);
ADVANCE_BLOCK();
break;
}
// Unknown opcode
default:
error("Unknown opcode %02x in rpza chunk", opcode);
}
}
}
const Graphics::Surface *RPZADecoder::decodeFrame(Common::SeekableReadStream &stream) {
if (!_surface) {
_surface = new Graphics::Surface();
// Allocate enough space in the surface for the blocks
_surface->create(_blockWidth * 4, _blockHeight * 4, getPixelFormat());
// Adjust width/height to be the right ones
_surface->w = _width;
_surface->h = _height;
}
if (_colorMap)
decodeFrameTmpl<byte, BlockDecoderDither>(stream, (byte *)_surface->getPixels(), _surface->pitch, _blockWidth, _blockHeight, _colorMap);
else
decodeFrameTmpl<uint16, BlockDecoderRaw>(stream, (uint16 *)_surface->getPixels(), _surface->pitch / 2, _blockWidth, _blockHeight, _colorMap);
return _surface;
}
bool RPZADecoder::canDither(DitherType type) const {
return type == kDitherTypeQT;
}
void RPZADecoder::setDither(DitherType type, const byte *palette) {
assert(canDither(type));
_ditherPalette = new byte[256 * 3];
memcpy(_ditherPalette, palette, 256 * 3);
_dirtyPalette = true;
_format = Graphics::PixelFormat::createFormatCLUT8();
delete[] _colorMap;
_colorMap = createQuickTimeDitherTable(palette, 256);
}
} // End of namespace Image
|