aboutsummaryrefslogtreecommitdiff
path: root/engines/saga/image.cpp
blob: 9f72036634f9db8943eb28f88a568aa17f2013df (plain)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* 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$
 *
 */

// SAGA Image resource management routines

#include "saga/saga.h"

#include "saga/stream.h"

namespace Saga {

static int granulate(int value, int granularity) {
	int remainder;

	if (value == 0)
		return 0;

	if (granularity == 0)
		return 0;

	remainder = value % granularity;

	if (remainder == 0) {
		return value;
	} else {
		return (granularity - remainder + value);
	}
}

int SagaEngine::decodeBGImage(const byte *image_data, size_t image_size,
				  byte **output_buf, size_t *output_buf_len, int *w, int *h, bool flip) {
	ImageHeader hdr;
	int modex_height;
	const byte *RLE_data_ptr;
	size_t RLE_data_len;
	byte *decode_buf;
	size_t decode_buf_len;
	byte *out_buf;
	size_t out_buf_len;

	if (image_size <= SAGA_IMAGE_DATA_OFFSET) {
		error("decodeBGImage() Image size is way too small (%d)", (int)image_size);
	}

	MemoryReadStreamEndian readS(image_data, image_size, isBigEndian());

	hdr.width = readS.readUint16();
	hdr.height = readS.readUint16();
	// The next four bytes of the image header aren't used.
	readS.readUint16();
	readS.readUint16();

	RLE_data_ptr = image_data + SAGA_IMAGE_DATA_OFFSET;
	RLE_data_len = image_size - SAGA_IMAGE_DATA_OFFSET;

	modex_height = granulate(hdr.height, 4);

	decode_buf_len = hdr.width * modex_height;
	decode_buf = (byte *)malloc(decode_buf_len);

	out_buf_len = hdr.width * hdr.height;
	out_buf = (byte *)malloc(out_buf_len);

	if (decodeBGImageRLE(RLE_data_ptr,
		RLE_data_len, decode_buf, decode_buf_len) != SUCCESS) {
		free(decode_buf);
		free(out_buf);
		return FAILURE;
	}

	unbankBGImage(out_buf, decode_buf, hdr.width, hdr.height);

	// For some reason bg images in IHNM are upside down
	if (getGameType() == GType_IHNM && !flip) {
		flipImage(out_buf, hdr.width, hdr.height);
	}

	free(decode_buf);

	*output_buf_len = out_buf_len;
	*output_buf = out_buf;

	*w = hdr.width;
	*h = hdr.height;

	return SUCCESS;
}

int SagaEngine::decodeBGImageRLE(const byte *inbuf, size_t inbuf_len, byte *outbuf, size_t outbuf_len) {
	const byte *inbuf_ptr;
	byte *outbuf_ptr;
	uint32 inbuf_remain;

	const byte *inbuf_end;
	byte *outbuf_end;
	uint32 outbuf_remain;

	byte mark_byte;
	int test_byte;

	uint32 runcount;

	byte bitfield;
	byte bitfield_byte1;
	byte bitfield_byte2;

	byte *backtrack_ptr;
	int backtrack_amount;

	uint16 c, b;

	int decode_err = 0;

	inbuf_ptr = inbuf;
	inbuf_remain = inbuf_len;

	outbuf_ptr = outbuf;
	outbuf_remain = outbuf_len;

	inbuf_end = (inbuf + inbuf_len) - 1;
	outbuf_end = (outbuf + outbuf_len) - 1;

	memset(outbuf, 0, outbuf_len);

	while ((inbuf_remain > 1) && (outbuf_remain > 0) && !decode_err) {

		if ((inbuf_ptr > inbuf_end) || (outbuf_ptr > outbuf_end)) {
			return FAILURE;
		}

		mark_byte = *inbuf_ptr++;
		inbuf_remain--;

		test_byte = mark_byte & 0xC0; // Mask all but two high order bits

		switch (test_byte) {
		case 0xC0: // 1100 0000
			// Uncompressed run follows: Max runlength 63
			runcount = mark_byte & 0x3f;
			if ((inbuf_remain < runcount) || (outbuf_remain < runcount)) {
				return FAILURE;
			}

			for (c = 0; c < runcount; c++) {
				*outbuf_ptr++ = *inbuf_ptr++;
			}

			inbuf_remain -= runcount;
			outbuf_remain -= runcount;
			continue;
			break;
		case 0x80: // 1000 0000
			// Compressed run follows: Max runlength 63
			runcount = (mark_byte & 0x3f) + 3;
			if (!inbuf_remain || (outbuf_remain < runcount)) {
				return FAILURE;
			}

			for (c = 0; c < runcount; c++) {
				*outbuf_ptr++ = *inbuf_ptr;
			}

			inbuf_ptr++;
			inbuf_remain--;
			outbuf_remain -= runcount;
			continue;

			break;

		case 0x40: // 0100 0000
			// Repeat decoded sequence from output stream:
			// Max runlength 10

			runcount = ((mark_byte >> 3) & 0x07U) + 3;
			backtrack_amount = *inbuf_ptr;

			if (!inbuf_remain || (backtrack_amount > (outbuf_ptr - outbuf)) || (runcount > outbuf_remain)) {
				return FAILURE;
			}

			inbuf_ptr++;
			inbuf_remain--;

			backtrack_ptr = outbuf_ptr - backtrack_amount;

			for (c = 0; c < runcount; c++) {
				*outbuf_ptr++ = *backtrack_ptr++;
			}

			outbuf_remain -= runcount;
			continue;
			break;
		default: // 0000 0000
			break;
		}

		// Mask all but the third and fourth highest order bits
		test_byte = mark_byte & 0x30;

		switch (test_byte) {

		case 0x30: // 0011 0000
			// Bitfield compression
			runcount = (mark_byte & 0x0F) + 1;

			if ((inbuf_remain < (runcount + 2)) || (outbuf_remain < (runcount * 8))) {
				return FAILURE;
			}

			bitfield_byte1 = *inbuf_ptr++;
			bitfield_byte2 = *inbuf_ptr++;

			for (c = 0; c < runcount; c++) {
				bitfield = *inbuf_ptr;
				for (b = 0; b < 8; b++) {
					if (bitfield & 0x80) {
						*outbuf_ptr = bitfield_byte2;
					} else {
						*outbuf_ptr = bitfield_byte1;
					}
					bitfield <<= 1;
					outbuf_ptr++;
				}
				inbuf_ptr++;
			}

			inbuf_remain -= (runcount + 2);
			outbuf_remain -= (runcount * 8);
			continue;
			break;
		case 0x20: // 0010 0000
			// Uncompressed run follows
			runcount = ((mark_byte & 0x0F) << 8) + *inbuf_ptr;
			if ((inbuf_remain < (runcount + 1)) || (outbuf_remain < runcount)) {
				return FAILURE;
			}

			inbuf_ptr++;

			for (c = 0; c < runcount; c++) {
				*outbuf_ptr++ = *inbuf_ptr++;
			}

			inbuf_remain -= (runcount + 1);
			outbuf_remain -= runcount;
			continue;

			break;

		case 0x10: // 0001 0000
			// Repeat decoded sequence from output stream
			backtrack_amount = ((mark_byte & 0x0F) << 8) + *inbuf_ptr;
			if (inbuf_remain < 2) {
				return FAILURE;
			}

			inbuf_ptr++;
			runcount = *inbuf_ptr++;

			if ((backtrack_amount > (outbuf_ptr - outbuf)) || (outbuf_remain < runcount)) {
				return FAILURE;
			}

			backtrack_ptr = outbuf_ptr - backtrack_amount;

			for (c = 0; c < runcount; c++) {
				*outbuf_ptr++ = *backtrack_ptr++;
			}

			inbuf_remain -= 2;
			outbuf_remain -= runcount;
			continue;
			break;
		default:
			return FAILURE;
		}
	}

	return SUCCESS;
}

int SagaEngine::flipImage(byte *img_buf, int columns, int scanlines) {
	int line;
	byte *tmp_scan;

	byte *flip_p1;
	byte *flip_p2;

	int flipcount = scanlines / 2;

	tmp_scan = (byte *)malloc(columns);
	if (tmp_scan == NULL) {
		return FAILURE;
	}

	flip_p1 = img_buf;
	flip_p2 = img_buf + (columns * (scanlines - 1));

	for (line = 0; line < flipcount; line++) {
		memcpy(tmp_scan, flip_p1, columns);
		memcpy(flip_p1, flip_p2, columns);
		memcpy(flip_p2, tmp_scan, columns);
		flip_p1 += columns;
		flip_p2 -= columns;
	}

	free(tmp_scan);

	return SUCCESS;
}

int SagaEngine::unbankBGImage(byte *dst_buf, const byte *src_buf, int columns, int scanlines) {
	int x, y;
	int temp;
	int quadruple_rows;
	int remain_rows;
	int rowjump_src;
	int rowjump_dest;
	const byte *src_p;
	byte *dst_p;
	const byte *srcptr1, *srcptr2, *srcptr3, *srcptr4;
	byte *dstptr1, *dstptr2, *dstptr3, *dstptr4;

	quadruple_rows = scanlines - (scanlines % 4);
	remain_rows = scanlines - quadruple_rows;

	assert(scanlines > 0);

	src_p = src_buf;
	dst_p = dst_buf + columns;

	srcptr1 = src_p;
	srcptr2 = src_p + 1;
	srcptr3 = src_p + 2;
	srcptr4 = src_p + 3;

	dstptr1 = dst_buf;
	dstptr2 = dst_buf + columns;
	dstptr3 = dst_buf + columns * 2;
	dstptr4 = dst_buf + columns * 3;

	rowjump_src = columns * 4;
	rowjump_dest = columns * 4;

	// Unbank groups of 4 first
	for (y = 0; y < quadruple_rows; y += 4) {
		for (x = 0; x < columns; x++) {
			temp = x * 4;
			dstptr1[x] = srcptr1[temp];
			dstptr2[x] = srcptr2[temp];
			dstptr3[x] = srcptr3[temp];
			dstptr4[x] = srcptr4[temp];
		}

		// This is to avoid generating invalid pointers -
		// usually innocuous, but undefined
		if (y < quadruple_rows - 4) {
			dstptr1 += rowjump_dest;
			dstptr2 += rowjump_dest;
			dstptr3 += rowjump_dest;
			dstptr4 += rowjump_dest;
			srcptr1 += rowjump_src;
			srcptr2 += rowjump_src;
			srcptr3 += rowjump_src;
			srcptr4 += rowjump_src;
		}
	}

	// Unbank rows remaining
	switch (remain_rows) {
	case 1:
		dstptr1 += rowjump_dest;
		srcptr1 += rowjump_src;
		for (x = 0; x < columns; x++) {
			temp = x * 4;
			dstptr1[x] = srcptr1[temp];
		}
		break;
	case 2:
		dstptr1 += rowjump_dest;
		dstptr2 += rowjump_dest;
		srcptr1 += rowjump_src;
		srcptr2 += rowjump_src;
		for (x = 0; x < columns; x++) {
			temp = x * 4;
			dstptr1[x] = srcptr1[temp];
			dstptr2[x] = srcptr2[temp];
		}
		break;
	case 3:
		dstptr1 += rowjump_dest;
		dstptr2 += rowjump_dest;
		dstptr3 += rowjump_dest;
		srcptr1 += rowjump_src;
		srcptr2 += rowjump_src;
		srcptr3 += rowjump_src;
		for (x = 0; x < columns; x++) {
			temp = x * 4;
			dstptr1[x] = srcptr1[temp];
			dstptr2[x] = srcptr2[temp];
			dstptr3[x] = srcptr3[temp];
		}
		break;
	default:
		break;
	}
	return SUCCESS;
}

const byte *SagaEngine::getImagePal(const byte *image_data, size_t image_size) {
	if (image_size <= SAGA_IMAGE_HEADER_LEN) {
		return NULL;
	}

	return image_data + SAGA_IMAGE_HEADER_LEN;
}

} // End of namespace Saga