aboutsummaryrefslogtreecommitdiff
path: root/engines/macventure/image.cpp
blob: a472c6880ae6c49d1ccba6657db9d889f01bbe09 (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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/* 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 "macventure/image.h"

namespace MacVenture {

PPICHuff PPIC1Huff = {
	// Masks
	{ 0x0000,0x2000,0x4000,0x5000,0x6000,0x7000,0x8000,0x9000,0xa000,
	0xb000,0xc000,0xd000,0xd800,0xe000,0xe800,0xf000,0xf800 },
	// Lens
	{ 3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5 },
	// Symbols
	{ 0x00,0x0f,0x03,0x05,0x06,0x07,0x08,0x09,0x0a,0x0c,0xff,0x01,
	0x02,0x04,0x0b,0x0d,0xe }
};

PPICHuff PPIC2Huff = {
	// Masks
	{ 0x0000,0x4000,0x8000,0xc000,0xc800,0xd000,0xd800,0xe000,0xe800,
	0xf000,0xf400,0xf600,0xf800,0xfa00,0xfc00,0xfe00,0xff00 },
	// Lens
	{ 2,2,2,5,5,5,5,5,5,6,7,7,7,7,7,8,8 },
	// Symbols
	{ 0xff,0x00,0x0f,0x01,0x03,0x07,0x0e,0x0c,0x08,0x06,0x02,0x04,
	0x09,0x0d,0x0b,0x0a,0x05 }
};

// Used to load the huffman table in PPIC3 decoding
byte loadBits[] = {
		0x08, 0x0f, 0x02, 0xff, 0x00,
		0x04, 0xff, 0x01,
		0x07, 0x09, 0x08, 0xff, 0x03,
		0x04, 0xff, 0x04,
		0x0a, 0x07, 0x0a, 0x0b, 0x06, 0xff, 0x05,
		0x06, 0x06, 0x0b, 0xff, 0x07,
		0x03, 0xff, 0x09,
		0x04, 0x03, 0x0e, 0xff, 0x0c,
		0x02, 0xff, 0x0d,
		0x01, 0xff, 0x0f,
		0xff };

ImageAsset::ImageAsset(ObjID original, Container * container) {
	_id = (original * 2);
	_mask = (original * 2) + 1;

	uint imgRowBytes = 0;
	uint imgBitWidth = 0;
	uint imgBitHeight = 0;
	uint maskRowBytes = 0;
	uint maskBitWidth = 0;
	uint maskBitHeight = 0;

	_container = container;
	decodePPIC(_id, _imgData, imgBitHeight, imgBitWidth, imgRowBytes);
	_imgRowBytes = imgRowBytes;
	_imgBitWidth = imgBitWidth;
	_imgBitHeight = imgBitHeight;

	if (_container->getItemByteSize(_mask)) {
		decodePPIC(_mask, _maskData, maskBitHeight, maskBitWidth, maskRowBytes);
	}
	_maskRowBytes = maskRowBytes;
	_maskBitWidth = maskBitWidth;
	_maskBitHeight = maskBitHeight;
}

ImageAsset::~ImageAsset() {
	debugC(3, kMVDebugImage, "~ImageAsset(%d)", _id / 2);
}

void ImageAsset::decodePPIC(ObjID id, Common::Array<byte> &data, uint &bitHeight, uint &bitWidth, uint &rowBytes) {
	ObjID realID = id;
	uint32 size = _container->getItemByteSize(id);
	if (size < 2) {
		rowBytes = 0;
		bitHeight = 0;
		bitWidth = 0;
		return;
	}
	if (size == 2) {
		Common::SeekableReadStream *newItemStream = _container->getItem(id);
		realID = newItemStream->readUint16BE();
		delete newItemStream;
	}
	Common::SeekableReadStream *baseStream = _container->getItem(realID);
	Common::BitStream32BEMSB stream(baseStream);

	uint8 mode = stream.getBits(3);
	int w, h;
	if (stream.getBit()) h = stream.getBits(10);
	else h = stream.getBits(6);

	if (stream.getBit()) w = stream.getBits(10);
	else w = stream.getBits(6);

	rowBytes = ((w + 0xF) >> 3) & 0xFFFE;
	bitWidth = w;
	bitHeight = h;

	for (uint i = 0; i < rowBytes * h; i++) {
		data.push_back(0);
	}

	switch (mode) {
	case MacVenture::kPPIC0:
		decodePPIC0(stream, data, bitHeight, bitWidth, rowBytes);
		break;
	case MacVenture::kPPIC1:
		decodePPIC1(stream, data, bitHeight, bitWidth, rowBytes);
		break;
	case MacVenture::kPPIC2:
		decodePPIC2(stream, data, bitHeight, bitWidth, rowBytes);
		break;
	case MacVenture::kPPIC3:
		decodePPIC3(stream, data, bitHeight, bitWidth, rowBytes);
		break;
	}

	delete baseStream;
}

void ImageAsset::decodePPIC0(Common::BitStream & stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
	uint words = bitWidth >> 4;
	uint bytes = bitWidth & 0xF;
	uint v = 0;
	uint p = 0;
	for (uint y = 0; y < bitHeight; y++) {
		for (uint x = 0; x < words; x++) {
			v = stream.peekBits(32);
			stream.skip(16);
			v >>= 16 - (stream.pos() % 8);
			data[p] = (v >> 8) & 0xff; p++;
			data[p] = v & 0xff; p++;
		}
		if (bytes) {
			v = stream.getBits(bytes);
			v <<= 16 - bytes;
			data[p] = (v >> 8) & 0xff; p++;
			data[p] = v & 0xff; p++;
		}
	}

}

void ImageAsset::decodePPIC1(Common::BitStream & stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
	decodeHuffGraphic(PPIC1Huff, stream, data, bitHeight, bitWidth, rowBytes);
}

void ImageAsset::decodePPIC2(Common::BitStream & stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
	decodeHuffGraphic(PPIC2Huff, stream, data, bitHeight, bitWidth, rowBytes);
}

void ImageAsset::decodePPIC3(Common::BitStream & stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
	// We need to load the huffman from the PPIC itself
	PPICHuff huff;
	uint16 v, bits;
	uint16 load = 0;
	while ((bits = loadBits[load++]) != 0xFF) {
		v = stream.getBits(bits);
		while ((bits = loadBits[load++]) != 0xFF) {
			huff.symbols[loadBits[load++]] = v % bits;
			v = (v / bits) | 0;
		}
		huff.symbols[loadBits[load++]] = v;
	}
	huff.symbols[0x10] = 0;
	for (uint i = 0x10; i > 0; i--)
		for (uint j = i; j <= 0x10; j++)
			if (huff.symbols[j] >= huff.symbols[i - 1])
				huff.symbols[j]++;

	for (int i = 0x10; i >= 0; i--) {
		if (huff.symbols[i] == 0x10) {
			huff.symbols[i] = 0xff;
			break;
		}
	}

	bits = stream.getBits(2) + 1;
	uint16 mask = 0;
	for (uint i = 0; i < 0xf; i++) {
		if (i)
			while (!stream.getBit()) bits++;
		huff.lens[i] = bits;
		huff.masks[i] = mask;
		mask += 1 << (16 - bits);
	}
	huff.masks[0xf] = mask;
	while (mask&(1 << (16 - bits))) bits++;
	huff.masks[0x10] = mask | (1 << (16 - bits));
	huff.lens[0xf] = bits;
	huff.lens[0x10] = bits;

	decodeHuffGraphic(huff, stream, data, bitHeight, bitWidth, rowBytes);
}

void ImageAsset::decodeHuffGraphic(const PPICHuff & huff, Common::BitStream & stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
	byte flags = 0;
	_walkRepeat = 0;
	_walkLast = 0;
	if (bitWidth & 3)
		flags = stream.getBits(5);
	else
		flags = stream.getBits(4) << 1;

	byte odd = 0;
	byte blank = bitWidth & 0xf;
	if (blank) {
		blank >>= 2;
		odd = blank & 1;
		blank = 2 - (blank >> 1);
	}

	uint16 pos = 0;
	for (uint y = 0; y < bitHeight; y++) {
		uint16 x = 0;
		for (; x < bitWidth >> 3; x++) {
			byte hi = walkHuff(huff, stream) << 4;
			data[pos++] = walkHuff(huff, stream) | hi;
		}
		if (odd) {
			data[pos] = walkHuff(huff, stream) << 4;
		}
		pos += blank;
	}

	uint16 edge = bitWidth & 3;
	if (edge) {
		pos = rowBytes - blank;
		uint16 bits = 0;
		uint16 val = 0;
		uint16 v;
		for (uint y = 0; y < bitHeight; y++) {
			if (flags & 1) {
				if (bits < edge) {
					v = walkHuff(huff, stream) << 4;
					val |= v >> bits;
					bits += 4;
				}
				bits -= edge;
				v = val;
				val <<= edge;
				val &= 0xFF;
			} else {
				v = stream.getBits(edge);
				v <<= 8 - edge;
			}
			if (odd)
				v >>= 4;

			data[pos] |= v & 0xff;
			pos += rowBytes;
		}
	}
	if (flags & 8) {
		pos = 0;
		for (uint y = 0; y < bitHeight; y++) {
			uint16 v = 0;
			if (flags & 2) {
				for (uint x = 0; x < rowBytes; x++) {
					data[pos] ^= v;
					v = data[pos];
					pos++;
				}
			}
			else {
				for (uint x = 0; x < rowBytes; x++) {
					uint16 val = data[pos] ^ v;
					val ^= (val >> 4) & 0xf;
					data[pos] = val;
					pos++;
					v = (val << 4) & 0xff;
				}
			}
		}
	}
	if (flags & 4) {
		uint16 delta = rowBytes * 4;
		if (flags & 2) delta *= 2;
		pos = 0;
		uint q = delta;
		for (uint i = 0; i < bitHeight * rowBytes - delta; i++) {
			data[q] ^= data[pos];
			q++;
			pos++;
		}
	}
}

byte ImageAsset::walkHuff(const PPICHuff & huff, Common::BitStream & stream) {
	if (_walkRepeat) {
		_walkRepeat--;
		_walkLast = ((_walkLast << 8) & 0xFF00) | (_walkLast >> 8);
		return _walkLast & 0xFF;
	}
	uint16 dw = stream.peekBits(16);
	uint16 i = 0;
	for (;i < 16; i++) {
		if (huff.masks[i + 1] > dw)
			break;
	}
	stream.skip(huff.lens[i]);
	uint8 val = huff.symbols[i];
	if (val == 0xFF) {
		if (!stream.getBit()) {
			_walkLast &= 0xFF;
			_walkLast |= _walkLast << 8;
		}
		_walkRepeat = stream.getBits(3);
		if (_walkRepeat < 3) {
			_walkRepeat <<= 4;
			_walkRepeat |= stream.getBits(4);
			if (_walkRepeat < 8) {
				_walkRepeat <<= 8;
				_walkRepeat |= stream.getBits(8);
			}
		}
		_walkRepeat -= 2;
		_walkLast = ((_walkLast << 8) & 0xFF00) | (_walkLast >> 8);
		return _walkLast & 0xFF;
	} else {
		_walkLast <<= 8;
		_walkLast |= val;
		_walkLast &= 0xFFFF;
	}
	return val;
}

void ImageAsset::blitInto(Graphics::ManagedSurface *target, int x, int y, BlitMode mode) {
	if (mode == kBlitDirect) {
		blitDirect(target, x, y, _imgData, _imgBitHeight, _imgBitWidth, _imgRowBytes);
	} else if (mode < kBlitXOR){
		if (_container->getItemByteSize(_mask)) { // Has mask
			switch (mode) {
			case MacVenture::kBlitBIC:
				blitBIC(target, x, y, _maskData, _maskBitHeight, _maskBitWidth, _maskRowBytes);
				break;
			case MacVenture::kBlitOR:
				blitOR(target, x, y, _maskData, _maskBitHeight, _maskBitWidth, _maskRowBytes);
				break;
			default:
				break;
			}
		} else if (_container->getItemByteSize(_id)) {
			switch (mode) {
			case MacVenture::kBlitBIC:
				target->fillRect(Common::Rect(x, y, x + _imgBitWidth, y + _imgBitHeight), kColorWhite);
				break;
			case MacVenture::kBlitOR:
				target->fillRect(Common::Rect(x, y, x + _imgBitWidth, y + _imgBitHeight), kColorBlack);
				break;
			default:
				break;
			}
		}

		if (_container->getItemByteSize(_id) && mode > 0) {
			blitXOR(target, x, y, _imgData, _imgBitHeight, _imgBitWidth, _imgRowBytes);
		}
	}
}

bool ImageAsset::isPointInside(Common::Point point) {
	if (point.x >= _maskBitWidth || point.y >= _maskBitHeight) return false;
	if (_maskData.empty()) return false;
	// We see if the point lands on the mask.
	uint pix = _maskData[(point.y * _maskRowBytes) + (point.x >> 3)] & (1 << (7 - (point.x & 7)));
	return pix != 0;
}

bool ImageAsset::isRectInside(Common::Rect rect) {
	// HACK is it &&, or ||?
	if (_maskData.empty()) return (rect.width() > 0 && rect.height() > 0);

	for (int y = rect.top; y < rect.top + rect.height(); y++) {
		uint bmpofs = y * _maskRowBytes;
		byte pix;
		for (int x = rect.left; x < rect.left + rect.width(); x++) {
			pix = _maskData[bmpofs + (x >> 3)] & (1 << (7 - (x & 7)));
			if (pix) return true;
		}
	}
	return false;
}

int ImageAsset::getWidth() {
	if (_imgData.size() == 0) return 0;
	return MAX(0, (int)_imgBitWidth);
}

int ImageAsset::getHeight() {
	if (_imgData.size() == 0) return 0;
	return MAX(0, (int)_imgBitHeight);
}

void ImageAsset::blitDirect(Graphics::ManagedSurface * target, int ox, int oy, const Common::Array<byte>& data, uint bitHeight, uint bitWidth, uint rowBytes) {
	uint sx, sy, w, h;
	calculateSectionToDraw(target, ox, oy, bitWidth, bitHeight, sx, sy, w, h);

	for (uint y = 0; y < h; y++) {
		uint bmpofs = (y + sy) * rowBytes;
		byte pix = 0;
		for (uint x = 0; x < w; x++) {
			assert(ox + x <= target->w);
			assert(oy + y <= target->h);
			pix = data[bmpofs + ((x + sx) >> 3)] & (1 << (7 - ((x + sx) & 7)));
			pix = pix ? kColorBlack : kColorWhite;
			*((byte *)target->getBasePtr(ox + x, oy + y)) = pix;
		}
	}
}

void ImageAsset::blitBIC(Graphics::ManagedSurface * target, int ox, int oy, const Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
	uint sx, sy, w, h;
	calculateSectionToDraw(target, ox, oy, bitWidth, bitHeight, sx, sy, w, h);

	for (uint y = 0; y < h; y++) {
		uint bmpofs = (y + sy) * rowBytes;
		byte pix = 0;
		for (uint x = 0; x < w; x++) {
			assert(ox + x <= target->w);
			assert(oy + y <= target->h);
			pix = data[bmpofs + ((x + sx) >> 3)] & (1 << (7 - ((x + sx) & 7)));
			if (pix) *((byte *)target->getBasePtr(ox + x, oy + y)) = kColorWhite;
		}
	}
}

void ImageAsset::blitOR(Graphics::ManagedSurface * target, int ox, int oy, const Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
	uint sx, sy, w, h;
	calculateSectionToDraw(target, ox, oy, bitWidth, bitHeight, sx, sy, w, h);

	for (uint y = 0; y < h; y++) {
		uint bmpofs = (y + sy) * rowBytes;
		byte pix = 0;
		for (uint x = 0; x < w; x++) {
			assert(ox + x <= target->w);
			assert(oy + y <= target->h);
			pix = data[bmpofs + ((x + sx) >> 3)] & (1 << (7 - ((x + sx) & 7)));
			if (pix) *((byte *)target->getBasePtr(ox + x, oy + y)) = kColorBlack;
		}
	}
}

void ImageAsset::blitXOR(Graphics::ManagedSurface * target, int ox, int oy, const Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
	uint sx, sy, w, h;
	calculateSectionToDraw(target, ox, oy, bitWidth, bitHeight, sx, sy, w, h);

	for (uint y = 0; y < h; y++) {
		uint bmpofs = (y + sy) * rowBytes;
		byte pix = 0;
		for (uint x = 0; x < w; x++) {
			pix = data[bmpofs + ((x + sx) >> 3)] & (1 << (7 - ((x + sx) & 7)));
			if (pix) { // We need to xor
				assert(ox + x <= target->w);
				assert(oy + y <= target->h);
				byte p = *((byte *)target->getBasePtr(ox + x, oy + y));
				*((byte *)target->getBasePtr(ox + x, oy + y)) =
					(p == kColorWhite) ? kColorBlack : kColorWhite;
			}
		}
	}
}

void ImageAsset::calculateSectionToDraw(Graphics::ManagedSurface *target, int &ox, int &oy, uint bitWidth, uint bitHeight, uint &sx, uint &sy, uint &w, uint &h) {

	calculateSectionInDirection(target->w, bitWidth, ox, sx, w);
	calculateSectionInDirection(target->h, bitHeight, oy, sy, h);

	assert(w <= target->w);
	assert((int)w >= 0);
	assert(w <= bitWidth);
	assert(h <= target->h);
	assert((int)h >= 0);
	assert(h <= bitHeight);
}

void ImageAsset::calculateSectionInDirection(uint targetWhole, uint originWhole, int &originPosition, uint &startPosition, uint &blittedWhole) {
	blittedWhole = originWhole;
	if (originPosition + blittedWhole > targetWhole) {
		if (originPosition > (int)targetWhole) {
			blittedWhole = 0;
		} else {
			blittedWhole = (blittedWhole) - ((blittedWhole + originPosition) - targetWhole);
		}

	}
	if (originPosition < 0) {
		if (ABS(originPosition) > (int)blittedWhole) {
			blittedWhole = 0;
		} else {
			blittedWhole -= -originPosition;
		}
	}

	startPosition = 0;
	if (originPosition < 0) {
		startPosition = -originPosition;
		originPosition = 0;
	}
}

} // End of namespace MacVenture