-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq.cpp
372 lines (320 loc) · 9.11 KB
/
seq.cpp
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
/* 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 "engines/express/seq.h"
#include "common/system.h"
//#include "common/util.h"
// Based on Deniz Oezmen's code: http://oezmen.eu/
namespace Express {
void FrameInfo::read(Common::SeekableReadStream *in, uint16 decompOffset) {
// Save the current position
uint32 basePos = in->pos();
dataOffset = in->readUint32LE();
//warning("frame, dataofs: 0x%04x %d", dataOffset, dataOffset);
/*uint32 unknown =*/ in->readUint32LE();
//warning("frame, unk1: 0x%04x %d", unknown, unknown);
palOffset = in->readUint32LE();
//warning("frame, palofs: 0x%04x %d", palOffset, palOffset);
xPos1 = in->readUint32LE();
//warning("frame, xpos1: 0x%04x %d", xPos1, xPos1);
yPos1 = in->readUint32LE();
//warning("frame, ypos1: 0x%04x %d", yPos1, yPos1);
xPos2 = in->readUint32LE();
//warning("frame, xpos2: 0x%04x %d", xPos2, xPos2);
yPos2 = in->readUint32LE();
//warning("frame, ypos2: 0x%04x %d", yPos2, yPos2);
initialSkip = in->readUint32LE();
//warning("frame, initialskip: 0x%04x %d", initialSkip, initialSkip);
decompSize = in->readUint32LE();
//warning("frame, decompsize: 0x%04x %d", decompSize, decompSize);
// Read the compression type
in->seek(basePos + decompOffset);
compType = in->readByte();
//warning("frame, comptype: 0x%04x %d", compType, compType);
/*
unknown = in->readUint32LE();
//warning("frame, val 0: 0x%04x %d", unknown, unknown);
unknown = in->readUint32LE();
//warning("frame, val 1: 0x%04x %d", unknown, unknown);
compType = in->readByte();
//warning("frame, comptype: 0x%04x %d", compType, compType);
unknown = in->readByte();
//warning("frame, val 2: 0x%04x %d", unknown, unknown);
unknown = in->readUint16LE();
//warning("frame, val 2: 0x%04x %d", unknown, unknown);
// decomp?
for (int i = 3; i < 8; i++) {
unknown = in->readUint32LE();
//warning("frame, val %d: 0x%04x %d", n, i, unknown, unknown);
if (i == 5 || i == 7)
assert (unknown == 0);
}
*/
}
// AnimFrame
AnimFrame::AnimFrame(Common::SeekableReadStream *in, FrameInfo *f) : _palette(NULL) {
_palSize = 1;
_image.create(640, 480, 1);
switch (f->compType) {
case 0:
// Empty frame
break;
case 3:
decomp3(in, f);
break;
case 4:
decomp4(in, f);
break;
case 5:
decomp5(in, f);
break;
case 7:
decomp7(in, f);
break;
case 255:
decompFF(in, f);
break;
default:
error("Unknown compression: %d", f->compType);
}
readPalette(in, f);
}
AnimFrame::~AnimFrame() {
_image.free();
delete[] _palette;
}
void AnimFrame::paint(Graphics::Surface *s) {
byte *inp = (byte *)_image.pixels;
uint16 *outp = (uint16 *)s->pixels;
for (int i = 0; i < 640 * 480; i++, inp++, outp++) {
if (*inp)
*outp = _palette[*inp];
}
}
void AnimFrame::readPalette(Common::SeekableReadStream *in, FrameInfo *f) {
// Read the palette
in->seek(f->palOffset);
_palette = new uint16[_palSize];
for (uint32 i = 0; i < _palSize; i++) {
_palette[i] = in->readUint16LE();
}
}
void AnimFrame::decomp3(Common::SeekableReadStream *in, FrameInfo *f) {
decomp34(in, f, 0x7, 3);
}
void AnimFrame::decomp4(Common::SeekableReadStream *in, FrameInfo *f) {
decomp34(in, f, 0xf, 4);
}
void AnimFrame::decomp34(Common::SeekableReadStream *in, FrameInfo *f, byte mask, byte shift) {
byte *p = (byte *)_image.getBasePtr(0, 0);
uint32 skip = f->initialSkip / 2;
uint32 size = f->decompSize / 2;
//warning("skip: %d, %d", skip % 640, skip / 640);
//warning("size: %d, %d", size % 640, size / 640);
//assert (f->yPos1 == skip / 640);
//assert (f->yPos2 == size / 640);
uint32 numBlanks = 640 - (f->xPos2 - f->xPos1);
in->seek(f->dataOffset);
for (uint32 out = skip; out < size; ) {
uint16 opcode = in->readByte();
if (opcode & 0x80) {
if (opcode & 0x40) {
opcode &= 0x3f;
out += numBlanks + opcode + 1;
} else {
opcode &= 0x3f;
if (opcode & 0x20) {
opcode = ((opcode & 0x1f) << 8) + in->readByte();
if (opcode & 0x1000) {
out += opcode & 0xfff;
continue;
}
}
out += opcode + 2;
}
} else {
byte value = opcode & mask;
opcode >>= shift;
if (_palSize <= value)
_palSize = value + 1;
if (!opcode)
opcode = in->readByte();
for (int i = 0; i < opcode; i++, out++) {
p[out] = value;
}
}
}
}
void AnimFrame::decomp5(Common::SeekableReadStream *in, FrameInfo *f) {
byte *p = (byte *)_image.getBasePtr(0, 0);
uint32 skip = f->initialSkip / 2;
uint32 size = f->decompSize / 2;
//warning("skip: %d, %d", skip % 640, skip / 640);
//warning("size: %d, %d", size % 640, size / 640);
//assert (f->yPos1 == skip / 640);
//assert (f->yPos2 == size / 640);
in->seek(f->dataOffset);
for (uint32 out = skip; out < size; ) {
uint16 opcode = in->readByte();
if (!(opcode & 0x1f)) {
opcode = (opcode << 3) + in->readByte();
if (opcode & 0x400) {
// skip these 10 bits
out += (opcode & 0x3ff);
} else {
out += opcode + 2;
}
} else {
byte value = opcode & 0x1f;
opcode >>= 5;
if (_palSize <= value)
_palSize = value + 1;
if (!opcode)
opcode = in->readByte();
for (int i = 0; i < opcode; i++, out++) {
p[out] = value;
}
}
}
}
void AnimFrame::decomp7(Common::SeekableReadStream *in, FrameInfo *f) {
byte *p = (byte *)_image.getBasePtr(0, 0);
uint32 skip = f->initialSkip / 2;
uint32 size = f->decompSize / 2;
//warning("skip: %d, %d", skip % 640, skip / 640);
//warning("size: %d, %d", size % 640, size / 640);
//assert (f->yPos1 == skip / 640);
//assert (f->yPos2 == size / 640);
uint32 numBlanks = 640 - (f->xPos2 - f->xPos1);
in->seek(f->dataOffset);
for (uint32 out = skip; out < size; ) {
uint16 opcode = in->readByte();
if (opcode & 0x80) {
if (opcode & 0x40) {
if (opcode & 0x20) {
opcode &= 0x1f;
out += numBlanks + opcode + 1;
} else {
opcode &= 0x1f;
if (opcode & 0x10) {
opcode = ((opcode & 0xf) << 8) + in->readByte();
if (opcode & 0x800) {
// skip these 11 bits
out += (opcode & 0x7ff);
continue;
}
}
// skip these 4 bits
out += opcode + 2;
}
} else {
opcode &= 0x3f;
byte value = in->readByte();
if (_palSize <= value)
_palSize = value + 1;
for (int i = 0; i < opcode; i++, out++) {
p[out] = value;
}
}
} else {
if (_palSize <= opcode)
_palSize = opcode + 1;
// set the given value
p[out] = opcode;
out++;
}
}
}
void AnimFrame::decompFF(Common::SeekableReadStream *in, FrameInfo *f) {
byte *p = (byte *)_image.getBasePtr(0, 0);
uint32 skip = f->initialSkip / 2;
uint32 size = f->decompSize / 2;
in->seek(f->dataOffset);
for (uint32 out = skip; out < size; ) {
uint16 opcode = in->readByte();
if (opcode < 0x80) {
if (_palSize <= opcode)
_palSize = opcode + 1;
// set the given value
p[out] = opcode;
out++;
} else {
if (opcode < 0xf0) {
if (opcode < 0xe0) {
// copy old part
uint32 old = out + ((opcode & 0x7) << 8) + in->readByte() - 2048;
opcode = ((opcode >> 3) & 0xf) + 3;
for (int i = 0; i < opcode; i++, out++, old++) {
p[out] = p[old];
}
} else {
opcode = (opcode & 0xf) + 1;
byte value = in->readByte();
if (_palSize <= value)
_palSize = value + 1;
for (int i = 0; i < opcode; i++, out++) {
p[out] = value;
}
}
} else {
out += ((opcode & 0xf) << 8) + in->readByte();
}
}
}
}
// Seq
Seq::Seq(Common::SeekableReadStream *in) {
load(in);
}
Seq::~Seq() {
}
bool Seq::load(Common::SeekableReadStream *in) {
// Frames
uint32 numFrames = in->readUint32LE();
// Unknown
uint32 unknown = in->readUint32LE();
warning("%d frames unk(0x%x)", numFrames, unknown);
assert (unknown == 0);
for (uint n = 0; n < numFrames; n++) {
FrameInfo f;
in->seek(8 + 0x44 * n);
f.read(in, 0x2c);
_frames.push_back(f);
}
for (uint i = 0; i < _frames.size(); i++) {
warning("decoding frame %d", i);
AnimFrame *f = new AnimFrame(in, &_frames[i]);
//TEST: paint it directly to screen
//Graphics::Surface *s = g_system->lockScreen();
Graphics::Surface *s = new Graphics::Surface;
s->create(640, 480, 2);
f->paint(s);
g_system->copyRectToScreen((byte *)s->pixels, s->pitch, 0, 0, s->w, s->h);
s->free();
delete s;
//g_system->unlockScreen();
g_system->updateScreen();
delete f;
}
return true;
}
} // End of Express namespace