aboutsummaryrefslogtreecommitdiff
path: root/graphics/maccursor.cpp
blob: a66a9104f3fb4a5350dc8698a62568a3fed58dd7 (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
/* 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 "common/textconsole.h"

#include "graphics/maccursor.h"

namespace Graphics {

MacCursor::MacCursor() {
	_surface = 0;
	memset(_palette, 0, 256 * 3);

	_hotspotX = 0;
	_hotspotY = 0;
}

MacCursor::~MacCursor() {
	clear();
}

void MacCursor::clear() {
	delete[] _surface; _surface = 0;
	memset(_palette, 0, 256 * 3);
}

bool MacCursor::readFromStream(Common::SeekableReadStream &stream, bool forceMonochrome) {
	clear();

	// Older Mac CURS monochrome cursors had a set size
	// All crsr cursors are larger than this
	if (stream.size() == 32 * 2 + 4)
		return readFromCURS(stream);

	return readFromCRSR(stream, forceMonochrome);
}

bool MacCursor::readFromCURS(Common::SeekableReadStream &stream) {
	// Grab B/W icon data
	_surface = new byte[16 * 16];
	for (int i = 0; i < 32; i++) {
		byte imageByte = stream.readByte();
		for (int b = 0; b < 8; b++)
			_surface[i * 8 + b] = (byte)((imageByte & (0x80 >> b)) > 0 ? 0 : 1);
	}

	// Apply mask data
	for (int i = 0; i < 32; i++) {
		byte imageByte = stream.readByte();
		for (int b = 0; b < 8; b++)
			if ((imageByte & (0x80 >> b)) == 0)
				_surface[i * 8 + b] = 0xff;
	}

	_hotspotY = stream.readUint16BE();
	_hotspotX = stream.readUint16BE();

	// Setup a basic palette
	_palette[1 * 3 + 0] = 0xff;
	_palette[1 * 3 + 1] = 0xff;
	_palette[1 * 3 + 2] = 0xff;

	return !stream.eos();
}

bool MacCursor::readFromCRSR(Common::SeekableReadStream &stream, bool forceMonochrome) {
	stream.readUint16BE(); // type
	stream.readUint32BE(); // offset to pixel map
	stream.readUint32BE(); // offset to pixel data
	stream.readUint32BE(); // expanded cursor data
	stream.readUint16BE(); // expanded data depth
	stream.readUint32BE(); // reserved

	// Read the B/W data first
	if (!readFromCURS(stream))
		return false;

	// Use b/w cursor on backends which don't support cursor palettes
	if (forceMonochrome)
		return true;

	stream.readUint32BE(); // reserved
	stream.readUint32BE(); // cursorID

	// Color version of cursor
	stream.readUint32BE(); // baseAddr

	// Keep only lowbyte for now
	stream.readByte();
	int iconRowBytes = stream.readByte();

	if (!iconRowBytes)
		return false;

	int iconBounds[4];
	iconBounds[0] = stream.readUint16BE();
	iconBounds[1] = stream.readUint16BE();
	iconBounds[2] = stream.readUint16BE();
	iconBounds[3] = stream.readUint16BE();

	stream.readUint16BE(); // pmVersion
	stream.readUint16BE(); // packType
	stream.readUint32BE(); // packSize

	stream.readUint32BE(); // hRes
	stream.readUint32BE(); // vRes

	stream.readUint16BE(); // pixelType
	stream.readUint16BE(); // pixelSize
	stream.readUint16BE(); // cmpCount
	stream.readUint16BE(); // cmpSize

	stream.readUint32BE(); // planeByte
	stream.readUint32BE(); // pmTable
	stream.readUint32BE(); // reserved

	// Pixel data for cursor
	int iconDataSize =  iconRowBytes * (iconBounds[3] - iconBounds[1]);
	byte *iconData = new byte[iconDataSize];

	if (!iconData)
		error("Cannot allocate Mac color cursor iconData");

	stream.read(iconData, iconDataSize);

	// Color table
	stream.readUint32BE(); // ctSeed
	stream.readUint16BE(); // ctFlag
	uint16 ctSize = stream.readUint16BE() + 1;

	// Read just high byte of 16-bit color
	for (int c = 0; c < ctSize; c++) {
		stream.readUint16BE();
		_palette[c * 3 + 0] = stream.readUint16BE() >> 8;
		_palette[c * 3 + 1] = stream.readUint16BE() >> 8;
		_palette[c * 3 + 2] = stream.readUint16BE() >> 8;
	}

	int pixelsPerByte = (iconBounds[2] - iconBounds[0]) / iconRowBytes;
	int bpp           = 8 / pixelsPerByte;

	// build a mask to make sure the pixels are properly shifted out
	int bitmask = 0;
	for (int m = 0; m < bpp; m++) {
		bitmask <<= 1;
		bitmask  |= 1;
	}

	// Extract pixels from bytes
	for (int j = 0; j < iconDataSize; j++) {
		for (int b = 0; b < pixelsPerByte; b++) {
			int idx = j * pixelsPerByte + (pixelsPerByte - 1 - b);

			if (_surface[idx] != 0xff) // if mask is not there
				_surface[idx] = (byte)((iconData[j] >> (b * bpp)) & bitmask);
		}
	}

	delete[] iconData;
	return stream.pos() == stream.size();
}

} // End of namespace Common