aboutsummaryrefslogtreecommitdiff
path: root/engines/pegasus/surface.cpp
blob: 8ff2a3a9ce7e1427fffc121cd42b10e15121d551 (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
/* 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.
 *
 * Additional copyright for this file:
 * Copyright (C) 1995-1997 Presto Studios, Inc.
 *
 * 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/file.h"
#include "common/macresman.h"
#include "common/stream.h"
#include "common/system.h"
#include "graphics/surface.h"
#include "image/pict.h"
#include "video/video_decoder.h"

#include "pegasus/pegasus.h"
#include "pegasus/surface.h"

namespace Pegasus {

Surface::Surface() {
	_ownsSurface = false;
	_surface = 0;
}

Surface::~Surface() {
	deallocateSurface();
}

void Surface::deallocateSurface() {
	if (_surface) {
		if (_ownsSurface) {
			_surface->free();
			delete _surface;
		}

		_surface = 0;
		_bounds = Common::Rect();
		_ownsSurface = false;
	}
}

void Surface::shareSurface(Surface *surface) {
	deallocateSurface();

	if (surface) {
		_ownsSurface = false;
		_surface = surface->getSurface();
		surface->getSurfaceBounds(_bounds);
	}
}

void Surface::allocateSurface(const Common::Rect &bounds) {
	deallocateSurface();

	if (bounds.isEmpty())
		return;

	_bounds = bounds;
	_surface = new Graphics::Surface();
	_surface->create(bounds.width(), bounds.height(), g_system->getScreenFormat());
	_ownsSurface = true;
}

void Surface::getImageFromPICTFile(const Common::String &fileName) {
	Common::File pict;
	if (!pict.open(fileName))
		error("Could not open picture '%s'", fileName.c_str());

	if (!getImageFromPICTStream(&pict))
		error("Failed to load PICT '%s'", fileName.c_str());
}

void Surface::getImageFromPICTResource(Common::MacResManager *resFork, uint16 id) {
	Common::SeekableReadStream *res = resFork->getResource(MKTAG('P', 'I', 'C', 'T'), id);
	if (!res)
		error("Could not open PICT resource %d from '%s'", id, resFork->getBaseFileName().c_str());

	if (!getImageFromPICTStream(res))
		error("Failed to load PICT resource %d from '%s'", id, resFork->getBaseFileName().c_str());

	delete res;
}

bool Surface::getImageFromPICTStream(Common::SeekableReadStream *stream) {
	Image::PICTDecoder pict;

	if (!pict.loadStream(*stream))
		return false;

	_surface = pict.getSurface()->convertTo(g_system->getScreenFormat(), pict.getPalette());
	_ownsSurface = true;
	_bounds = Common::Rect(0, 0, _surface->w, _surface->h);
	return true;
}

void Surface::getImageFromMovieFrame(Video::VideoDecoder *video, TimeValue time) {
	video->seek(Audio::Timestamp(0, time, 600));
	const Graphics::Surface *frame = video->decodeNextFrame();

	if (frame) {
		if (!_surface)
			_surface = new Graphics::Surface();

		_surface->copyFrom(*frame);
		_ownsSurface = true;
		_bounds = Common::Rect(0, 0, _surface->w, _surface->h);
	} else {
		deallocateSurface();
	}
}

void Surface::copyToCurrentPort() const {
	copyToCurrentPort(_bounds);
}

void Surface::copyToCurrentPortTransparent() const {
	copyToCurrentPortTransparent(_bounds);
}

void Surface::copyToCurrentPort(const Common::Rect &rect) const {
	copyToCurrentPort(rect, rect);
}

void Surface::copyToCurrentPortTransparent(const Common::Rect &rect) const {
	copyToCurrentPortTransparent(rect, rect);
}

void Surface::copyToCurrentPort(const Common::Rect &srcRect, const Common::Rect &dstRect) const {
	Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getCurSurface();
	byte *src = (byte *)_surface->getBasePtr(srcRect.left, srcRect.top);
	byte *dst = (byte *)screen->getBasePtr(dstRect.left, dstRect.top);

	int lineSize = srcRect.width() * _surface->format.bytesPerPixel;

	for (int y = 0; y < srcRect.height(); y++) {
		memcpy(dst, src, lineSize);
		src += _surface->pitch;
		dst += screen->pitch;
	}
}

void Surface::copyToCurrentPortTransparent(const Common::Rect &srcRect, const Common::Rect &dstRect) const {
	Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getCurSurface();
	byte *src = (byte *)_surface->getBasePtr(srcRect.left, srcRect.top);
	byte *dst = (byte *)screen->getBasePtr(dstRect.left, dstRect.top);

	int lineSize = srcRect.width() * _surface->format.bytesPerPixel;

	for (int y = 0; y < srcRect.height(); y++) {
		for (int x = 0; x < srcRect.width(); x++) {
			if (g_system->getScreenFormat().bytesPerPixel == 2) {
				uint16 color = READ_UINT16(src);
				if (!isTransparent(color))
					memcpy(dst, src, 2);
			} else if (g_system->getScreenFormat().bytesPerPixel == 4) {
				uint32 color = READ_UINT32(src);
				if (!isTransparent(color))
					memcpy(dst, src, 4);
			}

			src += g_system->getScreenFormat().bytesPerPixel;
			dst += g_system->getScreenFormat().bytesPerPixel;
		}

		src += _surface->pitch - lineSize;
		dst += screen->pitch - lineSize;
	}
}

void Surface::copyToCurrentPortMasked(const Common::Rect &srcRect, const Common::Rect &dstRect, const Surface *mask) const {
	Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getCurSurface();
	byte *src = (byte *)_surface->getBasePtr(srcRect.left, srcRect.top);
	byte *dst = (byte *)screen->getBasePtr(dstRect.left, dstRect.top);

	int lineSize = srcRect.width() * _surface->format.bytesPerPixel;

	for (int y = 0; y < srcRect.height(); y++) {
		byte *maskSrc = (byte *)mask->getSurface()->getBasePtr(0, y);

		for (int x = 0; x < srcRect.width(); x++) {
			if (g_system->getScreenFormat().bytesPerPixel == 2) {
				uint16 color = READ_UINT16(maskSrc);
				if (!isTransparent(color))
					memcpy(dst, src, 2);
			} else if (g_system->getScreenFormat().bytesPerPixel == 4) {
				uint32 color = READ_UINT32(maskSrc);
				if (!isTransparent(color))
					memcpy(dst, src, 4);
			}

			src += g_system->getScreenFormat().bytesPerPixel;
			maskSrc += g_system->getScreenFormat().bytesPerPixel;
			dst += g_system->getScreenFormat().bytesPerPixel;
		}

		src += _surface->pitch - lineSize;
		dst += screen->pitch - lineSize;
	}
}

void Surface::copyToCurrentPortTransparentGlow(const Common::Rect &srcRect, const Common::Rect &dstRect) const {
	// This is the same as copyToCurrentPortTransparent(), but turns the red value of each
	// pixel all the way up.

	Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getCurSurface();
	byte *src = (byte *)_surface->getBasePtr(srcRect.left, srcRect.top);
	byte *dst = (byte *)screen->getBasePtr(dstRect.left, dstRect.top);

	int lineSize = srcRect.width() * _surface->format.bytesPerPixel;

	for (int y = 0; y < srcRect.height(); y++) {
		for (int x = 0; x < srcRect.width(); x++) {
			if (g_system->getScreenFormat().bytesPerPixel == 2) {
				uint16 color = READ_UINT16(src);
				if (!isTransparent(color))
					WRITE_UINT16(dst, getGlowColor(color));
			} else if (g_system->getScreenFormat().bytesPerPixel == 4) {
				uint32 color = READ_UINT32(src);
				if (!isTransparent(color))
					WRITE_UINT32(dst, getGlowColor(color));
			}

			src += g_system->getScreenFormat().bytesPerPixel;
			dst += g_system->getScreenFormat().bytesPerPixel;
		}

		src += _surface->pitch - lineSize;
		dst += screen->pitch - lineSize;
	}
}

void Surface::scaleTransparentCopy(const Common::Rect &srcRect, const Common::Rect &dstRect) const {
	// I'm doing simple linear scaling here
	// dstRect(x, y) = srcRect(x * srcW / dstW, y * srcH / dstH);

	Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getCurSurface();

	int srcW = srcRect.width();
	int srcH = srcRect.height();
	int dstW = dstRect.width();
	int dstH = dstRect.height();

	for (int y = 0; y < dstH; y++) {
		for (int x = 0; x < dstW; x++) {
			if (g_system->getScreenFormat().bytesPerPixel == 2) {
				uint16 color = READ_UINT16((byte *)_surface->getBasePtr(
						x * srcW / dstW + srcRect.left,
						y * srcH / dstH + srcRect.top));
				if (!isTransparent(color))
					WRITE_UINT16((byte *)screen->getBasePtr(x + dstRect.left, y + dstRect.top), color);
			} else if (g_system->getScreenFormat().bytesPerPixel == 4) {
				uint32 color = READ_UINT32((byte *)_surface->getBasePtr(
						x * srcW / dstW + srcRect.left,
						y * srcH / dstH + srcRect.top));
				if (!isTransparent(color))
					WRITE_UINT32((byte *)screen->getBasePtr(x + dstRect.left, y + dstRect.top), color);
			}
		}
	}
}

void Surface::scaleTransparentCopyGlow(const Common::Rect &srcRect, const Common::Rect &dstRect) const {
	// This is the same as scaleTransparentCopy(), but turns the red value of each
	// pixel all the way up.

	Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getCurSurface();

	int srcW = srcRect.width();
	int srcH = srcRect.height();
	int dstW = dstRect.width();
	int dstH = dstRect.height();

	for (int y = 0; y < dstH; y++) {
		for (int x = 0; x < dstW; x++) {
			if (g_system->getScreenFormat().bytesPerPixel == 2) {
				uint16 color = READ_UINT16((byte *)_surface->getBasePtr(
						x * srcW / dstW + srcRect.left,
						y * srcH / dstH + srcRect.top));
				if (!isTransparent(color))
					WRITE_UINT16((byte *)screen->getBasePtr(x + dstRect.left, y + dstRect.top), getGlowColor(color));
			} else if (g_system->getScreenFormat().bytesPerPixel == 4) {
				uint32 color = READ_UINT32((byte *)_surface->getBasePtr(
						x * srcW / dstW + srcRect.left,
						y * srcH / dstH + srcRect.top));
				if (!isTransparent(color))
					WRITE_UINT32((byte *)screen->getBasePtr(x + dstRect.left, y + dstRect.top), getGlowColor(color));
			}
		}
	}
}

uint32 Surface::getGlowColor(uint32 color) const {
	// Can't just 'or' it on like the original did :P
	byte r, g, b;
	g_system->getScreenFormat().colorToRGB(color, r, g, b);
	return g_system->getScreenFormat().RGBToColor(0xff, g, b);
}

bool Surface::isTransparent(uint32 color) const {
	// HACK: Seems we're truncating some color data somewhere...
	uint32 transColor1 = g_system->getScreenFormat().RGBToColor(0xff, 0xff, 0xff);
	uint32 transColor2 = g_system->getScreenFormat().RGBToColor(0xf8, 0xf8, 0xf8);

	return color == transColor1 || color == transColor2;
}

PixelImage::PixelImage() {
	_transparent = false;
}

void PixelImage::drawImage(const Common::Rect &sourceBounds, const Common::Rect &destBounds) {
	if (!isSurfaceValid())
		return;

	// Draw from sourceBounds to destBounds based on _transparent
	if (_transparent)
		copyToCurrentPortTransparent(sourceBounds, destBounds);
	else
		copyToCurrentPort(sourceBounds, destBounds);
}

void Frame::initFromPICTFile(const Common::String &fileName, bool transparent) {
	getImageFromPICTFile(fileName);
	_transparent = transparent;
}

void Frame::initFromPICTResource(Common::MacResManager *resFork, uint16 id, bool transparent) {
	getImageFromPICTResource(resFork, id);
	_transparent = transparent;
}

void Frame::initFromMovieFrame(Video::VideoDecoder *video, TimeValue time, bool transparent) {
	getImageFromMovieFrame(video, time);
	_transparent = transparent;
}

void Picture::draw(const Common::Rect &r) {
	Common::Rect surfaceBounds;
	getSurfaceBounds(surfaceBounds);
	Common::Rect r1 = r;

	Common::Rect bounds;
	getBounds(bounds);
	surfaceBounds.moveTo(bounds.left, bounds.top);
	r1 = r1.findIntersectingRect(surfaceBounds);
	getSurfaceBounds(surfaceBounds);

	Common::Rect r2 = r1;
	r2.translate(surfaceBounds.left - bounds.left, surfaceBounds.top - bounds.top);
	drawImage(r2, r1);
}

void Picture::initFromPICTFile(const Common::String &fileName, bool transparent) {
	Frame::initFromPICTFile(fileName, transparent);

	Common::Rect surfaceBounds;
	getSurfaceBounds(surfaceBounds);
	sizeElement(surfaceBounds.width(), surfaceBounds.height());
}

void Picture::initFromPICTResource(Common::MacResManager *resFork, uint16 id, bool transparent) {
	Frame::initFromPICTResource(resFork, id, transparent);

	Common::Rect surfaceBounds;
	getSurfaceBounds(surfaceBounds);
	sizeElement(surfaceBounds.width(), surfaceBounds.height());
}

void Picture::initFromMovieFrame(Video::VideoDecoder *video, TimeValue time, bool transparent) {
	Frame::initFromMovieFrame(video, time, transparent);

	Common::Rect surfaceBounds;
	getSurfaceBounds(surfaceBounds);
	sizeElement(surfaceBounds.width(), surfaceBounds.height());
}

} // End of namespace Pegasus