aboutsummaryrefslogtreecommitdiff
path: root/engines/scumm/thumbnail.cpp
blob: 40f1ee48e561af0cd907d1d0f69098668e8428cf (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
/* 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 file 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.
 *
 * $URL$
 * $Id$
 *
 */


#include "common/system.h"
#include "common/savefile.h"
#include "graphics/scaler.h"
#include "scumm/scumm.h"

namespace Scumm {

#define THMB_VERSION 1

struct ThumbnailHeader {
	uint32 type;
	uint32 size;
	byte version;
	uint16 width, height;
	byte bpp;
};

#define ThumbnailHeaderSize (4+4+1+2+2+1)

inline void colorToRGB(uint16 color, uint8 &r, uint8 &g, uint8 &b) {
	r = (((color >> 11) & 0x1F) << 3);
	g = (((color >> 5) & 0x3F) << 2);
	b = ((color&0x1F) << 3);
}

Graphics::Surface *ScummEngine::loadThumbnail(Common::SeekableReadStream *file) {
	ThumbnailHeader header;

	header.type = file->readUint32BE();
	// We also accept the bad 'BMHT' header here, for the sake of compatibility
	// with some older savegames which were written incorrectly due to a bug in
	// ScummVM which wrote the thumb header type incorrectly on LE systems.
	if (header.type != MKID_BE('THMB') && header.type != MKID_BE('BMHT'))
		return 0;

	header.size = file->readUint32BE();
	header.version = file->readByte();

	if (header.version > THMB_VERSION) {
		file->skip(header.size - 9);
		warning("Loading a newer thumbnail version");
		return 0;
	}

	header.width = file->readUint16BE();
	header.height = file->readUint16BE();
	header.bpp = file->readByte();

	// TODO: support other bpp values than 2
	if (header.bpp != 2) {
		file->skip(header.size - 14);
		return 0;
	}

	Graphics::Surface *thumb = new Graphics::Surface();
	thumb->create(header.width, header.height, sizeof(OverlayColor));

	OverlayColor* pixels = (OverlayColor *)thumb->pixels;

	for (int y = 0; y < thumb->h; ++y) {
		for (int x = 0; x < thumb->w; ++x) {
			uint8 r, g, b;
			colorToRGB(file->readUint16BE(), r, g, b);

			// converting to current OSystem Color
			*pixels++ = _system->RGBToColor(r, g, b);
		}
	}

	return thumb;
}

void ScummEngine::saveThumbnail(Common::OutSaveFile *file) {
	Graphics::Surface thumb;

#if !defined(__DS__)
	if (!createThumbnailFromScreen(&thumb))
#endif
		thumb.create(kThumbnailWidth, kThumbnailHeight2, sizeof(uint16));

	ThumbnailHeader header;
	header.type = MKID_BE('THMB');
	header.size = ThumbnailHeaderSize + thumb.w*thumb.h*thumb.bytesPerPixel;
	header.version = THMB_VERSION;
	header.width = thumb.w;
	header.height = thumb.h;
	header.bpp = thumb.bytesPerPixel;

	file->writeUint32BE(header.type);
	file->writeUint32BE(header.size);
	file->writeByte(header.version);
	file->writeUint16BE(header.width);
	file->writeUint16BE(header.height);
	file->writeByte(header.bpp);

	// TODO: for later this shouldn't be casted to uint16...
	uint16* pixels = (uint16 *)thumb.pixels;
	for (uint16 p = 0; p < thumb.w*thumb.h; ++p, ++pixels)
		file->writeUint16BE(*pixels);

	thumb.free();
}

} // end of namespace Scumm