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
|
/* 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 "mutationofjb/animationdecoder.h"
#include "mutationofjb/encryptedfile.h"
#include "mutationofjb/util.h"
#include "common/debug.h"
#include "common/translation.h"
namespace MutationOfJB {
AnimationDecoder::AnimationDecoder(const Common::String &fileName) : _fileName(fileName) {
_surface.create(IMAGE_WIDTH, IMAGE_HEIGHT, Graphics::PixelFormat::createFormatCLUT8());
}
bool AnimationDecoder::decode(AnimationDecoderCallback *callback) {
EncryptedFile file;
file.open(_fileName);
if (!file.isOpen()) {
reportFileMissingError(_fileName.c_str());
return false;
}
file.seek(0, SEEK_END);
const int32 endPos = file.pos();
// Skip header - we don't need it anyway.
file.seek(0x80);
int frameNo = 0;
while (file.pos() != endPos) {
// Record.
const uint32 length = file.readUint32LE();
const uint16 recordId = file.readUint16LE();
const uint16 subrecords = file.readUint16LE();
// Skip 8 empty bytes.
file.seek(8, SEEK_CUR);
// Subrecords.
if (recordId == 0xF1FA) {
for (int i = 0; i < subrecords; ++i) {
int32 filePos = file.pos();
const uint32 subLength = file.readUint32LE();
const uint16 type = file.readUint16LE();
if (type == 0x0B) {
loadPalette(file);
if (callback) {
callback->onPaletteUpdated(_palette);
}
} else if (type == 0x0F) {
loadFullFrame(file, subLength - 6);
if (callback) {
callback->onFrame(frameNo, _surface);
}
} else if (type == 0x0C) {
loadDiffFrame(file, subLength - 6);
if (callback) {
callback->onFrame(frameNo, _surface);
}
} else {
debug(_("Unsupported record type %02X."), type);
file.seek(subLength - 6, SEEK_CUR);
}
// Makes decoding more robust, because for some reason records might have extra data at the end.
file.seek(filePos + subLength, SEEK_SET);
}
frameNo++;
} else {
file.seek(length - 16, SEEK_CUR);
}
}
file.close();
return true;
}
void AnimationDecoder::loadPalette(Common::SeekableReadStream &file) {
uint16 packets = file.readUint16LE();
const uint8 skipCount = file.readByte();
int copyCount = file.readByte();
if (copyCount == 0) {
copyCount = PALETTE_COLORS;
}
while(packets--) {
file.read(_palette + skipCount * 3, copyCount * 3);
for (int j = skipCount * 3; j < (skipCount + copyCount) * 3; ++j) {
_palette[j] <<= 2; // Uses 6-bit colors.
}
}
}
void AnimationDecoder::loadFullFrame(EncryptedFile &file, uint32 size) {
uint8 *const pixels = reinterpret_cast<uint8 *>(_surface.getPixels());
uint8 *ptr = pixels;
uint32 readBytes = 0;
uint32 lines = 0;
while (readBytes != size) {
if (lines == 200) {
// Some full frames have an unknown byte at the end,
// so break when we encounter all 200 lines.
break;
}
uint8 no = file.readByte();
readBytes++;
while (no--) {
uint8 n = file.readByte();
readBytes++;
if (n < 0x80) {
// RLE - Copy color n times.
uint8 color = file.readByte();
readBytes++;
while(n--) {
*ptr++ = color;
}
} else {
// Take next 0x100 - n bytes as they are.
const uint32 rawlen = 0x100 - n;
file.read(ptr, rawlen);
readBytes += rawlen;
ptr += rawlen;
}
}
lines++;
}
}
void AnimationDecoder::loadDiffFrame(EncryptedFile &file, uint32) {
const uint16 firstLine = file.readUint16LE();
const uint16 numLines = file.readUint16LE();
for (uint16 line = firstLine; line < firstLine + numLines; ++line) {
uint8 *imageData = reinterpret_cast<uint8 *>(_surface.getBasePtr(0, line));
uint8 runs = file.readByte();
while (runs--) {
uint8 localOffset = file.readByte();
uint8 num = file.readByte();
imageData += localOffset;
if (num == 0) {
// Ignore?
debug("Zero RLE number found.");
} else if (num < 0x80) {
file.read(imageData, num);
imageData += num;
} else {
const uint8 color = file.readByte();
const int no = 0x100 - num;
memset(imageData, color, no);
imageData += no;
}
}
}
}
AnimationDecoder::~AnimationDecoder() {
_surface.free();
}
}
|