This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
xmg.cpp
258 lines (208 loc) · 6.32 KB
/
xmg.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
/* Residual - A 3D game interpreter
*
* Residual is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* $URL$
* $Id$
*
*/
#include "engines/stark/xmg.h"
#include "engines/stark/debug.h"
#include "graphics/pixelformat.h"
#include "common/stream.h"
namespace Stark {
// XMG DECODER
class XMGDecoder {
public:
static Graphics::Surface *decode(Common::ReadStream *stream);
private:
XMGDecoder() {}
Graphics::Surface *decodeImage(Common::ReadStream *stream);
void processYCrCb();
void processTrans();
void processRGB();
uint32 *_pixels;
Common::ReadStream *_stream;
uint32 _transColor;
uint32 _scanLen;
};
Graphics::Surface *XMGDecoder::decode(Common::ReadStream *stream) {
XMGDecoder dec;
return dec.decodeImage(stream);
}
// TODO: fix color handling in BE machines?
Graphics::Surface *XMGDecoder::decodeImage(Common::ReadStream *stream) {
_stream = stream;
// Read the file version
uint32 version = stream->readUint32LE();
if (version != 3) {
warning("Stark::XMG: File version unknown: %d", version);
}
// Read the transparency color (RGBA)
_transColor = stream->readUint32LE();
// Read the image size
uint32 width = stream->readUint32LE();
uint32 height = stream->readUint32LE();
debugC(10, kDebugXMG, "Stark::XMG: Version=%d, TransparencyColor=0x%08x, size=%dx%d", version, _transColor, width, height);
// Read the scan length
_scanLen = stream->readUint32LE();
if (_scanLen != 3 * width) {
warning("Stark::XMG: The scan length (%d) doesn't match the width bytes (%d)", _scanLen, 3 * width);
}
// We're using RGBA instead of RGB, so we use our own scanlength
_scanLen = width;
// Unknown
uint32 unknown2 = stream->readUint32LE();
debugC(kDebugUnknown, "Stark::XMG: unknown2 = %08x = %d", unknown2, unknown2);
uint32 unknown3 = stream->readUint32LE();
debugC(kDebugUnknown, "Stark::XMG: unknown3 = %08x = %d", unknown3, unknown3);
// Create the destination surface
Graphics::Surface *surface = new Graphics::Surface();
if (!surface)
return NULL;
// Placeholder pixelformat
Graphics::PixelFormat pixFormat(4, 8, 8, 8, 8, 24, 16, 8 ,0);
surface->create(width, height, pixFormat);
_pixels = (uint32 *)surface->pixels;
uint32 currX = 0, currY = 0;
while (!stream->eos()) {
// TODO: Handle odd-sized images
if (currX >= width) {
//assert (currX == width);
currX -= width;
currY += 2;
_pixels += _scanLen;
if (currY + 1 >= height)
break;
}
// Read the number and mode of the tiles
byte op = stream->readByte();
uint16 count;
if ((op & 0xC0) != 0xC0) {
count = op & 0x3F;
} else {
count = ((op & 0xF) << 8) + stream->readByte();
op <<= 4;
}
op &= 0xC0;
// Process the current serie
for (int i = 0; i < count; i++) {
switch (op) {
case 0x00:
// YCrCb
processYCrCb();
break;
case 0x40:
// Trans
processTrans();
break;
case 0x80:
// RGB
processRGB();
break;
case 0xC0:
error("Unsupported colour mode");
break;
}
_pixels += 2;
}
currX += 2 * count;
}
return surface;
}
inline static void YUV2RGB(byte y, byte u, byte v, byte &r, byte &g, byte &b) {
r = CLIP<int>(y + ((1357 * (v - 128)) >> 10), 0, 255);
g = CLIP<int>(y - (( 691 * (v - 128)) >> 10) - ((333 * (u - 128)) >> 10), 0, 255);
b = CLIP<int>(y + ((1715 * (u - 128)) >> 10), 0, 255);
}
void XMGDecoder::processYCrCb() {
byte y0, y1, y2, y3;
byte cr, cb;
y0 = _stream->readByte();
y1 = _stream->readByte();
y2 = _stream->readByte();
y3 = _stream->readByte();
cr = _stream->readByte();
cb = _stream->readByte();
byte r, g, b;
YUV2RGB(y0, cb, cr, r, g, b);
_pixels[0] = (255 << 24) + (b << 16) + (g << 8) + r;
YUV2RGB(y1, cb, cr, r, g, b);
_pixels[1] = (255 << 24) + (b << 16) + (g << 8) + r;
YUV2RGB(y2, cb, cr, r, g, b);
_pixels[_scanLen + 0] = (255 << 24) + (b << 16) + (g << 8) + r;
YUV2RGB(y3, cb, cr, r, g, b);
_pixels[_scanLen + 1] = (255 << 24) + (b << 16) + (g << 8) + r;
}
void XMGDecoder::processTrans() {
_pixels[0] = _transColor;
_pixels[1] = _transColor;
_pixels[_scanLen + 0] = _transColor;
_pixels[_scanLen + 1] = _transColor;
}
void XMGDecoder::processRGB() {
uint32 color;
color = _stream->readUint16LE();
color += _stream->readByte() << 16;
if (color != _transColor)
color += 255 << 24;
_pixels[0] = color;
color = _stream->readUint16LE();
color += _stream->readByte() << 16;
if (color != _transColor)
color += 255 << 24;
_pixels[1] = color;
color = _stream->readUint16LE();
color += _stream->readByte() << 16;
if (color != _transColor)
color += 255 << 24;
_pixels[_scanLen + 0] = color;
color = _stream->readUint16LE();
color += _stream->readByte() << 16;
if (color != _transColor)
color += 255 << 24;
_pixels[_scanLen + 1] = color;
}
// SCENE ELEMENT XMG
SceneElementXMG::SceneElementXMG() : _surface(NULL) {
}
SceneElementXMG::~SceneElementXMG() {
// Free the surface
if (_surface)
_surface->free();
delete _surface;
}
SceneElementXMG *SceneElementXMG::load(const Common::Archive *archive, const Common::String &name, uint16 x, uint16 y) {
// Open the resource
Common::SeekableReadStream *res = archive->createReadStreamForMember(name);
if (!res)
return NULL;
// Create the element to return
SceneElementXMG *element = new SceneElementXMG();
// Decode the XMG
element->_surface = XMGDecoder::decode(res);
delete res;
// Save the rendering coordinates
element->_x = x;
element->_y = y;
return element;
}
void SceneElementXMG::render(GfxDriver *gfx) {
// Draw the current element
gfx->drawSurface(_surface, Common::Point(_x, _y));
}
} // End of namespace Stark