aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/gfx/image/renderedimage.cpp
blob: 346b46f3b445f4d04828f1f27710ae476386c228 (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
/* 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.
 *
 */

/*
 * This code is based on Broken Sword 2.5 engine
 *
 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
 *
 * Licensed under GNU GPL v2
 *
 */

// -----------------------------------------------------------------------------
// INCLUDES
// -----------------------------------------------------------------------------

#include "common/savefile.h"
#include "sword25/package/packagemanager.h"
#include "sword25/gfx/image/imgloader.h"
#include "sword25/gfx/image/renderedimage.h"

#include "sword25/gfx/renderobjectmanager.h"

#include "common/system.h"
#include "graphics/thumbnail.h"

namespace Sword25 {

// -----------------------------------------------------------------------------
// CONSTRUCTION / DESTRUCTION
// -----------------------------------------------------------------------------

/**
 * Load a NULL-terminated string from the given stream.
 */
static Common::String loadString(Common::SeekableReadStream &in, uint maxSize = 999) {
	Common::String result;

	while (!in.eos() && (result.size() < maxSize)) {
		char ch = (char)in.readByte();
		if (ch == '\0')
			break;

		result += ch;
	}

	return result;
}

static byte *readSavegameThumbnail(const Common::String &filename, uint &fileSize, bool &isPNG) {
	byte *pFileData;
	Common::SaveFileManager *sfm = g_system->getSavefileManager();
	Common::InSaveFile *file = sfm->openForLoading(lastPathComponent(filename, '/'));
	if (!file)
		error("Save file \"%s\" could not be loaded.", filename.c_str());

	// Seek to the actual PNG image
	loadString(*file);		// Marker (BS25SAVEGAME)
	Common::String storedVersionID = loadString(*file);		// Version
	if (storedVersionID != "SCUMMVM1")
		loadString(*file);

	loadString(*file);		// Description
	uint32 compressedGamedataSize = atoi(loadString(*file).c_str());
	loadString(*file);		// Uncompressed game data size
	file->skip(compressedGamedataSize);	// Skip the game data and move to the thumbnail itself
	uint32 thumbnailStart = file->pos();

	fileSize = file->size() - thumbnailStart;

	// Check if the thumbnail is in our own format, or a PNG file.
	uint32 header = file->readUint32BE();
	isPNG = (header != MKTAG('S','C','R','N'));
	file->seek(-4, SEEK_CUR);

	pFileData = new byte[fileSize];
	file->read(pFileData, fileSize);
	delete file;

	return pFileData;
}

RenderedImage::RenderedImage(const Common::String &filename, bool &result) :
	_data(0),
	_width(0),
	_height(0),
	_isTransparent(true) {
	result = false;

	PackageManager *pPackage = Kernel::getInstance()->getPackage();
	assert(pPackage);

	_backSurface = Kernel::getInstance()->getGfx()->getSurface();

	// Load file
	byte *pFileData;
	uint fileSize;

	bool isPNG = true;

	if (filename.hasPrefix("/saves")) {
		pFileData = readSavegameThumbnail(filename, fileSize, isPNG);
	} else {
		pFileData = pPackage->getFile(filename, &fileSize);
	}

	if (!pFileData) {
		error("File \"%s\" could not be loaded.", filename.c_str());
		return;
	}

	// Uncompress the image
	int pitch;
	if (isPNG)
		result = ImgLoader::decodePNGImage(pFileData, fileSize, _data, _width, _height, pitch);
	else
		result = ImgLoader::decodeThumbnailImage(pFileData, fileSize, _data, _width, _height, pitch);

	if (!result) {
		error("Could not decode image.");
		delete[] pFileData;
		return;
	}

	// Cleanup FileData
	delete[] pFileData;

	_doCleanup = true;

#if defined(SCUMM_LITTLE_ENDIAN)
	// Makes sense for LE only at the moment
	checkForTransparency();
#endif

	return;
}

// -----------------------------------------------------------------------------

RenderedImage::RenderedImage(uint width, uint height, bool &result) :
	_width(width),
	_height(height),
	_isTransparent(true) {

	_data = new byte[width * height * 4];
	Common::fill(_data, &_data[width * height * 4], 0);

	_backSurface = Kernel::getInstance()->getGfx()->getSurface();

	_doCleanup = true;

	result = true;
	return;
}

RenderedImage::RenderedImage() : _width(0), _height(0), _data(0), _isTransparent(true) {
	_backSurface = Kernel::getInstance()->getGfx()->getSurface();

	_doCleanup = false;

	return;
}

// -----------------------------------------------------------------------------

RenderedImage::~RenderedImage() {
	if (_doCleanup)
		delete[] _data;
}

// -----------------------------------------------------------------------------

bool RenderedImage::fill(const Common::Rect *pFillRect, uint color) {
	error("Fill() is not supported.");
	return false;
}

// -----------------------------------------------------------------------------

bool RenderedImage::setContent(const byte *pixeldata, uint size, uint offset, uint stride) {
	// Check if PixelData contains enough pixel to create an image with image size equals width * height
	if (size < static_cast<uint>(_width * _height * 4)) {
		error("PixelData vector is too small to define a 32 bit %dx%d image.", _width, _height);
		return false;
	}

	const byte *in = &pixeldata[offset];
	byte *out = _data;

	for (int i = 0; i < _height; i++) {
		memcpy(out, in, _width * 4);
		out += _width * 4;
		in += stride;
	}

	return true;
}

void RenderedImage::replaceContent(byte *pixeldata, int width, int height) {
	_width = width;
	_height = height;
	_data = pixeldata;
}
// -----------------------------------------------------------------------------

uint RenderedImage::getPixel(int x, int y) {
	error("GetPixel() is not supported. Returning black.");
	return 0;
}

// -----------------------------------------------------------------------------

bool RenderedImage::blit(int posX, int posY, int flipping, Common::Rect *pPartRect, uint color, int width, int height, RectangleList *updateRects) {
	int ca = (color >> 24) & 0xff;

	// Check if we need to draw anything at all
	if (ca == 0)
		return true;

	int cr = (color >> 16) & 0xff;
	int cg = (color >> 8) & 0xff;
	int cb = (color >> 0) & 0xff;

	// Compensate for transparency. Since we're coming
	// down to 255 alpha, we just compensate for the colors here
	if (ca != 255) {
		cr = cr * ca >> 8;
		cg = cg * ca >> 8;
		cb = cb * ca >> 8;
	}

	// Create an encapsulating surface for the data
	Graphics::Surface srcImage;
	// TODO: Is the data really in the screen format?
	srcImage.init(_width, _height, _width * 4, _data, g_system->getScreenFormat());

	if (pPartRect) {
		srcImage.setPixels(&_data[pPartRect->top * srcImage.pitch + pPartRect->left * 4]);
		srcImage.w = pPartRect->right - pPartRect->left;
		srcImage.h = pPartRect->bottom - pPartRect->top;

		debug(6, "Blit(%d, %d, %d, [%d, %d, %d, %d], %08x, %d, %d)", posX, posY, flipping,
			pPartRect->left,  pPartRect->top, pPartRect->width(), pPartRect->height(), color, width, height);
	} else {

		debug(6, "Blit(%d, %d, %d, [%d, %d, %d, %d], %08x, %d, %d)", posX, posY, flipping, 0, 0,
			srcImage.w, srcImage.h, color, width, height);
	}

	if (width == -1)
		width = srcImage.w;
	if (height == -1)
		height = srcImage.h;

#ifdef SCALING_TESTING
	// Hardcode scaling to 66% to test scaling
	width = width * 2 / 3;
	height = height * 2 / 3;
#endif

	Graphics::Surface *img;
	Graphics::Surface *imgScaled = NULL;
	byte *savedPixels = NULL;
	if ((width != srcImage.w) || (height != srcImage.h)) {
		// Scale the image
		img = imgScaled = scale(srcImage, width, height);
		savedPixels = (byte *)img->getPixels();
	} else {
		img = &srcImage;
	}

	for (RectangleList::iterator it = updateRects->begin(); it != updateRects->end(); ++it) {
		const Common::Rect &clipRect = *it;

		int skipLeft = 0, skipTop = 0;
		int drawX = posX, drawY = posY;
		int drawWidth = img->w;
		int drawHeight = img->h;

		// Handle clipping
		if (drawX < clipRect.left) {
			skipLeft = clipRect.left - drawX;
			drawWidth -= skipLeft;
			drawX = clipRect.left;
		}

		if (drawY < clipRect.top) {
			skipTop = clipRect.top - drawY;
			drawHeight -= skipTop;
			drawY = clipRect.top;
		}

		if (drawX + drawWidth >= clipRect.right)
			drawWidth = clipRect.right - drawX;

		if (drawY + drawHeight >= clipRect.bottom)
			drawHeight = clipRect.bottom - drawY;

		if ((drawWidth > 0) && (drawHeight > 0)) {
			int xp = 0, yp = 0;

			int inStep = 4;
			int inoStep = img->pitch;
			if (flipping & Image::FLIP_V) {
				inStep = -inStep;
				xp = img->w - 1 - skipLeft;
			} else {
				xp = skipLeft;
			}

			if (flipping & Image::FLIP_H) {
				inoStep = -inoStep;
				yp = img->h - 1 - skipTop;
			} else {
				yp = skipTop;
			}

			byte *ino = (byte *)img->getBasePtr(xp, yp);
			byte *outo = (byte *)_backSurface->getBasePtr(drawX, drawY);

#if defined(SCUMM_LITTLE_ENDIAN)
			// Simple memcpy if the source bitmap doesn't have transparent pixels and the drawing transparency is 255
			// NOTE Only possible with LE-machines at the moment, maybe it would be feasible to convert the bitmap pixels at loading time?
			if (!_isTransparent && ca == 255) {
				for (int i = 0; i < drawHeight; i++) {
					memcpy(outo, ino, drawWidth * 4);
					outo += _backSurface->pitch;
					ino += inoStep;
				}
			} else
#endif
			{
				byte *in, *out;
				for (int i = 0; i < drawHeight; i++) {
					out = outo;
					in = ino;
					for (int j = 0; j < drawWidth; j++) {
						uint32 pix = *(uint32 *)in;
						int a = (pix >> 24) & 0xff;
						in += inStep;

						if (ca != 255) {
							a = a * ca >> 8;
						}

						if (a == 0) {
							// Full transparency
							out += 4;
							continue;
						}

						int b = (pix >> 0) & 0xff;
						int g = (pix >> 8) & 0xff;
						int r = (pix >> 16) & 0xff;

						if (a == 255) {
#if defined(SCUMM_LITTLE_ENDIAN)
							if (cb != 255)
								b = (b * cb) >> 8;
							if (cg != 255)
								g = (g * cg) >> 8;
							if (cr != 255)
								r = (r * cr) >> 8;
							*(uint32 *)out = (255 << 24) | (r << 16) | (g << 8) | b;
							out += 4;
#else
							*out++ = a;
							if (cr != 255)
								*out++ = (r * cr) >> 8;
							else
								*out++ = r;
							if (cg != 255)
								*out++ = (g * cg) >> 8;
							else
								*out++ = g;
							if (cb != 255)
								*out++ = (b * cb) >> 8;
							else
								*out++ = b;
#endif
						} else {
#if defined(SCUMM_LITTLE_ENDIAN)
							pix = *(uint32 *)out;
							int outb = (pix >> 0) & 0xff;
							int outg = (pix >> 8) & 0xff;
							int outr = (pix >> 16) & 0xff;
							if (cb == 0)
								outb = 0;
							else if (cb != 255)
								outb += ((b - outb) * a * cb) >> 16;
							else
								outb += ((b - outb) * a) >> 8;
							if (cg == 0)
								outg = 0;
							else if (cg != 255)
								outg += ((g - outg) * a * cg) >> 16;
							else
								outg += ((g - outg) * a) >> 8;
							if (cr == 0)
								outr = 0;
							else if (cr != 255)
								outr += ((r - outr) * a * cr) >> 16;
							else
								outr += ((r - outr) * a) >> 8;
							*(uint32 *)out = (255 << 24) | (outr << 16) | (outg << 8) | outb;
							out += 4;
#else
							*out = 255;
							out++;
							if (cr == 0)
								*out = 0;
							else if (cr != 255)
								*out += ((r - *out) * a * cr) >> 16;
							else
								*out += ((r - *out) * a) >> 8;
							out++;
							if (cg == 0)
								*out = 0;
							else if (cg != 255)
								*out += ((g - *out) * a * cg) >> 16;
							else
								*out += ((g - *out) * a) >> 8;
							out++;
							if (cb == 0)
								*out = 0;
							else if (cb != 255)
								*out += ((b - *out) * a * cb) >> 16;
							else
								*out += ((b - *out) * a) >> 8;
							out++;
#endif
						}
					}
					outo += _backSurface->pitch;
					ino += inoStep;
				}
			}

		}

	}

	if (imgScaled) {
		imgScaled->setPixels(savedPixels);
		imgScaled->free();
		delete imgScaled;
	}

	return true;
}

void RenderedImage::copyDirectly(int posX, int posY) {
	byte *data = _data;
	int w = _width;
	int h = _height;

	// Handle off-screen clipping
	if (posY < 0) {
		h = MAX(0, (int)_height - -posY);
		data = (byte *)_data + _width * -posY;
		posY = 0;
	}

	if (posX < 0) {
		w = MAX(0, (int)_width - -posX);
		data = (byte *)_data + (-posX * 4);
		posX = 0;
	}

	w = CLIP((int)w, 0, (int)MAX((int)_backSurface->w - posX, 0));
	h = CLIP((int)h, 0, (int)MAX((int)_backSurface->h - posY, 0));

	g_system->copyRectToScreen(data, _backSurface->pitch, posX, posY, w, h);
}

void RenderedImage::checkForTransparency() {
	// Check if the source bitmap has any transparent pixels at all
	_isTransparent = false;
	byte *data = _data;
	for (int i = 0; i < _height; i++) {
		for (int j = 0; j < _width; j++) {
			_isTransparent = data[3] != 0xff;
			if (_isTransparent)
				return;
			data += 4;
		}
	}
}

} // End of namespace Sword25