/
patch.cpp
377 lines (304 loc) · 10.5 KB
/
patch.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
373
374
375
376
377
/** @file patch.cpp Patch Image Format.
*
* @authors Copyright © 1999-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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 St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <QList>
#include <QRect>
#include <de/libcore.h>
#include <de/Log>
#include "resource/patch.h"
#include <de/math.h>
namespace de {
namespace internal {
/**
* Serialized format header.
*/
struct Header : public IReadable
{
/// Logical dimensions of the patch in pixels.
dint16 dimensions[2];
/// Origin offset (top left) in world coordinate space units.
dint16 origin[2];
/// Implements IReadable.
void operator << (Reader &from) {
from >> dimensions[0] >> dimensions[1]
>> origin[0] >> origin[1];
}
};
/**
* A @em Post is a run of one or more non-masked pixels.
*/
struct Post : public IReadable
{
/// Y-Offset to the start of the run in texture space (0-base).
dbyte topOffset;
/// Length of the run in pixels (inclusive).
dbyte length;
/// Offset to the first pixel palette index in the source data.
size_t firstPixel;
/// Implements IReadable.
void operator << (Reader &from)
{
from >> topOffset >> length;
firstPixel = from.offset() + 1 /*unused junk*/;
}
};
/// A @em Column is a list of 0 or more posts.
typedef QList<Post> Posts;
typedef Posts Column;
typedef QList<Column> Columns;
/// Offsets to columns from the start of the source data.
typedef QList<dint32> ColumnOffsets;
/**
* Attempt to read another @a post from the @a reader.
* @return @c true if another post was read; otherwise @c false.
*/
static bool readNextPost(Post &post, Reader &reader)
{
static int const END_OF_POSTS = 0xff;
// Any more?
reader.mark();
dbyte nextByte; reader >> nextByte;
reader.rewind();
if(nextByte == END_OF_POSTS) return false;
// Another post begins.
reader >> post;
return true;
}
/**
* Visit each of @a offsets producing a column => post map.
*/
static Columns readPosts(ColumnOffsets const &offsets, Reader &reader)
{
Columns columns;
#ifdef DENG2_QT_4_7_OR_NEWER
columns.reserve(offsets.size());
#endif
Post post;
DENG2_FOR_EACH_CONST(ColumnOffsets, i, offsets)
{
reader.setOffset(*i);
// A new column begins.
columns.push_back(Column());
Column &column = columns.back();
// Read all posts.
while(readNextPost(post, reader))
{
column.push_back(post);
// Skip to the next post.
reader.seek(1 + post.length + 1); // A byte of unsed junk either side of the pixel palette indices.
}
}
return columns;
}
/**
* Read @a width column offsets from the @a reader.
*/
static ColumnOffsets readColumnOffsets(int width, Reader &reader)
{
ColumnOffsets offsets;
#ifdef DENG2_QT_4_7_OR_NEWER
offsets.reserve(width);
#endif
for(int col = 0; col < width; ++col)
{
dint32 offset; reader >> offset;
offsets.push_back(offset);
}
return offsets;
}
static inline Columns readColumns(int width, Reader &reader)
{
return readPosts(readColumnOffsets(width, reader), reader);
}
/**
* Process @a columns to calculate the "real" pixel height of the image.
*/
static int calcRealHeight(Columns const &columns)
{
QRect geom(QPoint(0, 0), QSize(1, 0));
DENG2_FOR_EACH_CONST(Columns, colIt, columns)
{
int tallTop = -1; // Keep track of pos (clipping).
DENG2_FOR_EACH_CONST(Posts, postIt, *colIt)
{
Post const &post = *postIt;
// Does this post extend the previous one? (a so-called "tall-patch").
if(post.topOffset <= tallTop)
tallTop += post.topOffset;
else
tallTop = post.topOffset;
// Skip invalid posts.
if(post.length <= 0) continue;
// Unite the geometry of the post.
geom |= QRect(QPoint(0, tallTop), QSize(1, post.length));
}
}
return geom.height();
}
static Block compositeImage(Reader &reader, ColorPaletteTranslation const *xlatTable,
Columns const &columns, Patch::Metadata const &meta, Patch::Flags flags)
{
bool const maskZero = flags.testFlag(Patch::MaskZero);
bool const clipToLogicalDimensions = flags.testFlag(Patch::ClipToLogicalDimensions);
#ifdef DENG_DEBUG
// Is the "logical" height of the image equal to the actual height of the
// composited pixel posts?
if(meta.logicalDimensions.y != meta.dimensions.y)
{
int postCount = 0;
DENG2_FOR_EACH_CONST(Columns, i, columns)
{
postCount += i->count();
}
LOGDEV_RES_NOTE("Inequal heights, logical: %i != actual: %i (%i %s)")
<< meta.logicalDimensions.y << meta.dimensions.y
<< postCount << (postCount == 1? "post" : "posts");
}
#endif
// Determine the dimensions of the output buffer.
Vector2i const &dimensions = clipToLogicalDimensions? meta.logicalDimensions : meta.dimensions;
int const w = dimensions.x;
int const h = dimensions.y;
size_t const pels = w * h;
// Create the output buffer and fill with default color (black) and alpha (transparent).
Block output = QByteArray(2 * pels, 0);
// Map the pixel color channels in the output buffer.
dbyte *top = output.data();
dbyte *topAlpha = output.data() + pels;
// Composite the patch into the output buffer.
DENG2_FOR_EACH_CONST(Columns, colIt, columns)
{
int tallTop = -1; // Keep track of pos (clipping).
DENG2_FOR_EACH_CONST(Posts, postIt, *colIt)
{
Post const &post = *postIt;
// Does this post extend the previous one? (a so-called "tall-patch").
if(post.topOffset <= tallTop)
tallTop += post.topOffset;
else
tallTop = post.topOffset;
// Skip invalid posts.
if(post.length <= 0) continue;
// Determine the destination height range.
int y = tallTop;
int length = post.length;
// Clamp height range within bounds.
if(y + length > h)
length = h - y;
int offset = 0;
if(y < 0)
{
offset = de::min(-y, length);
y = 0;
}
length = de::max(0, length - offset);
// Skip empty ranges.
if(!length) continue;
// Find the start of the pixel data for the post.
reader.setOffset(post.firstPixel + offset);
dbyte *out = top + tallTop * w;
dbyte *outAlpha = topAlpha + tallTop * w;
// Composite pixels from the post to the output buffer.
while(length--)
{
// Read the next palette index.
dbyte palIdx;
reader >> palIdx;
// Is palette index translation in effect?
if(xlatTable)
{
palIdx = dbyte(xlatTable->at(palIdx));
}
if(!maskZero || palIdx)
{
*out = palIdx;
}
if(maskZero)
*outAlpha = (palIdx? 0xff : 0);
else
*outAlpha = 0xff;
// Move one row down.
out += w;
outAlpha += w;
}
}
// Move one column right.
top++;
topAlpha++;
}
return output;
}
} // internal
static Patch::Metadata prepareMetadata(internal::Header const &hdr, int realHeight)
{
Patch::Metadata meta;
meta.dimensions = Vector2i(hdr.dimensions[0], realHeight);
meta.logicalDimensions = Vector2i(hdr.dimensions[0], hdr.dimensions[1]);
meta.origin = Vector2i(hdr.origin[0], hdr.origin[1]);
return meta;
}
Patch::Metadata Patch::loadMetadata(IByteArray const &data)
{
LOG_AS("Patch::loadMetadata");
Reader reader = Reader(data);
internal::Header hdr; reader >> hdr;
internal::Columns columns = internal::readColumns(hdr.dimensions[0], reader);
return prepareMetadata(hdr, internal::calcRealHeight(columns));
}
Block Patch::load(IByteArray const &data, ColorPaletteTranslation const &xlatTable, Patch::Flags flags)
{
LOG_AS("Patch::load");
Reader reader = Reader(data);
internal::Header hdr; reader >> hdr;
internal::Columns columns = internal::readColumns(hdr.dimensions[0], reader);
Metadata meta = prepareMetadata(hdr, internal::calcRealHeight(columns));
return internal::compositeImage(reader, &xlatTable, columns, meta, flags);
}
Block Patch::load(IByteArray const &data, Patch::Flags flags)
{
LOG_AS("Patch::load");
Reader reader = Reader(data);
internal::Header hdr; reader >> hdr;
internal::Columns columns = internal::readColumns(hdr.dimensions[0], reader);
Metadata meta = prepareMetadata(hdr, internal::calcRealHeight(columns));
return internal::compositeImage(reader, 0/* no translation */, columns, meta, flags);
}
bool Patch::recognize(IByteArray const &data)
{
try
{
// The format has no identification markings.
// Instead we must rely on heuristic analyses of the column => post map.
Reader from = Reader(data);
internal::Header hdr; from >> hdr;
if(!hdr.dimensions[0] || !hdr.dimensions[1]) return false;
for(int col = 0; col < hdr.dimensions[0]; ++col)
{
dint32 offset;
from >> offset;
if(offset < 0 || (unsigned) offset >= from.source()->size()) return false;
}
// Validated.
return true;
}
catch(IByteArray::OffsetError const &)
{} // Ignore this error.
return false;
}
} // namespace de