aboutsummaryrefslogtreecommitdiff
path: root/engines/lastexpress/data/background.cpp
blob: e1626213e0fb41ad3efd09c1840ed6975f50ca3e (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
/* 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.
 *
 */

// Based on Deniz Oezmen's code and Xentax Wiki documentation
// http://oezmen.eu/
// http://wiki.xentax.com/index.php/The_Last_Express_BG

#include "lastexpress/data/background.h"

#include "lastexpress/debug.h"

#include "common/rect.h"
#include "common/stream.h"

namespace LastExpress {

Background::Background() : _data(NULL) {
	memset(&_header, 0, sizeof(BackgroundHeader));
}

Background::~Background() {
	delete[] _data;
}

bool Background::load(Common::SeekableReadStream *stream) {
	if (!stream)
		return false;

	// Reset data
	delete[] _data;

	// Load Background header
	_header.posX = stream->readUint32LE();
	_header.posY = stream->readUint32LE();
	_header.width = stream->readUint32LE();
	_header.height = stream->readUint32LE();
	_header.redSize = stream->readUint32LE();
	_header.blueSize = stream->readUint32LE();
	_header.greenSize = stream->readUint32LE();

	debugC(3, kLastExpressDebugGraphics, "Background Info: (%d, %d) - (%d x %d) - (%d, %d, %d)",
	                                    _header.posX, _header.posY, _header.width, _header.height,
	                                    _header.redSize, _header.blueSize, _header.greenSize);

	// Load and decompress Background channel data
	uint32 numPix = _header.width * _header.height;
	byte *dataR = decodeComponent(stream, _header.redSize, numPix);
	byte *dataB = decodeComponent(stream, _header.blueSize, numPix);
	byte *dataG = decodeComponent(stream, _header.greenSize, numPix);

	// Save to pixel buffer
	// FIXME handle big-endian case
	_data = new uint16[_header.width * _header.height];
	for (uint i = 0; i < _header.width * _header.height; i++)
		_data[i] = (uint16)((dataR[i] << 10) + (dataG[i] << 5) + dataB[i]);

	// Cleanup buffers
	delete[] dataR;
	delete[] dataG;
	delete[] dataB;

	delete stream;

	return true;
}

Common::Rect Background::draw(Graphics::Surface *surface) {
	if (!_data) {
		debugC(2, kLastExpressDebugGraphics, "Trying to show a background before loading data");
		return Common::Rect();
	}

	int i = 0;
	for (uint16 y = 0; y < _header.height; y++) {
		for (uint16 x = 0; x < _header.width; x++) {
			surface->fillRect(Common::Rect((int16)(_header.posX + x), (int16)(_header.posY + y), (int16)(_header.posX + x + 1), (int16)(_header.posY + y + 1)), _data[i]);
			i ++;
		}
	}

	return Common::Rect((int16)_header.posX, (int16)_header.posY, (int16)(_header.posX + _header.width), (int16)(_header.posY + _header.height));
}

byte *Background::decodeComponent(Common::SeekableReadStream *in, uint32 inSize, uint32 outSize) const {
	// Create the destination array
	byte *out = new byte[outSize];
	if (!out)
		return NULL;

	// Initialize the decoding
	memset(out, 0, outSize * sizeof(byte));
	uint32 inPos = 0;
	uint32 outPos = 0;

	// Decode
	while (inPos < inSize) {
		byte inByte = in->readByte();
		inPos++;

		if (inByte < 0x80) {
			// Direct decompression (RLE)
			byte len = (inByte >> 5) + 1;
			byte data = inByte & 0x1f;
			for (int i = 0; i < len && outPos < outSize; i++)
				out[outPos++] = data;
		} else {
			// Buffer back reference, 4096 byte window
			// Take inByte and the following value as a big endian
			// OfsLen while zeroing the first bit
			uint16 ofsLen = ((inByte & 0x7F) << 8) | in->readByte();
			inPos++;

			int32 len = (ofsLen >> 12) + 3;
			int32 hisPos = (int32)(outPos + (ofsLen & 0x0FFF) - 4096);
			for (int i = 0; i < len && outPos < outSize; i++)
				out[outPos++] = out[hisPos++];
		}
	}

	return out;
}

} // End of namespace LastExpress