aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/frotz/pics.cpp
blob: 86cd5feb25d47ab359911346780f2405631b56ad (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
/* 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 "glk/frotz/pics.h"
#include "glk/frotz/pics_decoder.h"
#include "glk/glk.h"
#include "common/algorithm.h"
#include "common/memstream.h"

namespace Glk {
namespace Frotz {

enum {
	PIC_FILE_HEADER_FLAGS      = 1,
	PIC_FILE_HEADER_NUM_IMAGES = 4,
	PIC_FILE_HEADER_ENTRY_SIZE = 8,
	PIC_FILE_HEADER_VERSION    = 14
};

Pics::Pics() : Common::Archive(), _filename(getFilename()) {
	Common::File f;
	if (!f.open(_filename))
		error("Error reading Pics file");

	Common::Array<uint> offsets;
	byte buffer[16];
	f.read(buffer, 16);
	_index.resize(READ_LE_UINT16(&buffer[PIC_FILE_HEADER_NUM_IMAGES]));
	_entrySize = buffer[PIC_FILE_HEADER_ENTRY_SIZE];
	_version = buffer[PIC_FILE_HEADER_FLAGS];
	assert(_entrySize >= 8 && _entrySize <= 14);

	// Iterate through loading the index
	for (uint idx = 0; idx < _index.size(); ++idx) {
		Entry &e = _index[idx];
		f.read(buffer, _entrySize);

		e._number = READ_LE_UINT16(buffer);
		e._width = READ_LE_UINT16(buffer + 2);
		e._height = READ_LE_UINT16(buffer + 4);
		e._flags = READ_LE_UINT16(buffer + 6);

		if (_entrySize >= 11) {
			e._dataOffset = READ_BE_UINT32(buffer + 7) & 0xffffff;
			if (e._dataOffset)
				offsets.push_back(e._dataOffset);

			if (_entrySize == 14) {
				e._paletteOffset = READ_BE_UINT32(buffer + 10) & 0xffffff;
			}
		}

		if (e._dataOffset)
			e._filename = Common::String::format("pic%u.raw", e._number);
		else
			e._filename = Common::String::format("pic%u.rect", e._number);
	}

	// Further processing of index to calculate data sizes
	Common::sort(offsets.begin(), offsets.end());

	for (uint idx = 0; idx < _index.size(); ++idx) {
		Entry &e = _index[idx];
		if (!e._dataOffset)
			continue;

		// Find the entry in the offsets array
		uint oidx = 0;
		while (oidx < offsets.size() && offsets[oidx] != e._dataOffset)
			++oidx;

		// Set the size
		e._dataSize = (oidx == (offsets.size() - 1) ? f.size() : offsets[oidx + 1]) - e._dataOffset;
	}

	f.close();
}

Common::String Pics::getFilename() {
	Common::String filename = g_vm->getFilename();
	while (filename.contains('.'))
		filename.deleteLastChar();

	return filename + ".mg1";
}

bool Pics::exists() {
	return Common::File::exists(getFilename());
}

bool Pics::hasFile(const Common::String &name) const {
	for (uint idx = 0; idx < _index.size(); ++idx) {
		if (_index[idx]._filename.equalsIgnoreCase(name))
			return true;
	}

	return false;
}

int Pics::listMembers(Common::ArchiveMemberList &list) const {
	for (uint idx = 0; idx < _index.size(); ++idx) {
		list.push_back(Common::ArchiveMemberList::value_type(new Common::GenericArchiveMember(_index[idx]._filename, this)));
	}

	return (int)_index.size();
}

const Common::ArchiveMemberPtr Pics::getMember(const Common::String &name) const {
	if (!hasFile(name))
		return Common::ArchiveMemberPtr();

	return Common::ArchiveMemberPtr(new Common::GenericArchiveMember(name, this));
}

Common::SeekableReadStream *Pics::createReadStreamForMember(const Common::String &name) const {
	PictureDecoder decoder;

	for (uint idx = 0; idx < _index.size(); ++idx) {
		const Entry &e = _index[idx];
		if (e._filename.equalsIgnoreCase(name)) {
			Common::Array<byte> palette;
			Common::File f;
			Common::SeekableReadStream *dest;
			if (!f.open(_filename))
				error("Reading failed");

			if (e._dataSize) {
				// Read in the image's palette
				assert(e._paletteOffset);
				f.seek(e._paletteOffset);
				palette.resize(f.readByte() * 3);
				f.read(&palette[0], palette.size());

				f.seek(e._dataOffset);
				Common::SeekableReadStream *src = f.readStream(e._dataSize);
				dest = decoder.decode(*src, e._flags, palette, kMCGA, e._width, e._height);
				delete src;
			} else {
				byte *rect = (byte *)malloc(2 * sizeof(uint16));
				WRITE_LE_UINT16(rect, e._width);
				WRITE_LE_UINT16(rect + 2, e._height);
				dest = new Common::MemoryReadStream(rect, 2 * sizeof(uint16), DisposeAfterUse::YES);
			}

			f.close();
			return dest;
		}
	}

	return nullptr;
}

} // End of namespace Frotz
} // End of namespace Glk