1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "common/savefile.h"
24 #include "common/system.h"
25 
26 #include "image/png.h"
27 
28 #include "sludge/errors.h"
29 #include "sludge/graphics.h"
30 #include "sludge/imgloader.h"
31 #include "sludge/newfatal.h"
32 #include "sludge/version.h"
33 
34 namespace Sludge {
35 
setThumbnailSize(int thumbWidth,int thumbHeight)36 bool GraphicsManager::setThumbnailSize(int thumbWidth, int thumbHeight) {
37 	if (checkSizeValide(thumbWidth, thumbHeight))
38 	{
39 		_thumbWidth = thumbWidth;
40 		_thumbHeight = thumbHeight;
41 		return true;
42 	}
43 	return false;
44 }
45 
saveThumbnail(Common::WriteStream * stream)46 bool GraphicsManager::saveThumbnail(Common::WriteStream *stream) {
47 	stream->writeUint32LE(_thumbWidth);
48 	stream->writeUint32LE(_thumbHeight);
49 
50 	if (_thumbWidth && _thumbHeight) {
51 		if (!freeze())
52 			return false;
53 
54 		if(!Image::writePNG(*stream, _renderSurface))
55 			return false;
56 
57 		unfreeze(true);
58 	}
59 	stream->writeByte('!');
60 	return true;
61 }
62 
showThumbnail(const Common::String & filename,int atX,int atY)63 void GraphicsManager::showThumbnail(const Common::String &filename, int atX, int atY) {
64 	Common::InSaveFile *fp = g_system->getSavefileManager()->openForLoading(filename);
65 
66 	if (fp == nullptr)
67 		return;
68 
69 	bool headerBad = false;
70 	if (fp->readByte() != 'S')
71 		headerBad = true;
72 	if (fp->readByte() != 'L')
73 		headerBad = true;
74 	if (fp->readByte() != 'U')
75 		headerBad = true;
76 	if (fp->readByte() != 'D')
77 		headerBad = true;
78 	if (fp->readByte() != 'S')
79 		headerBad = true;
80 	if (fp->readByte() != 'A')
81 		headerBad = true;
82 	if (headerBad) {
83 		fatal(ERROR_GAME_LOAD_NO, filename);
84 		return;
85 	}
86 	char c = fp->readByte();
87 	while ((c = fp->readByte()))
88 		;
89 
90 	int majVersion = fp->readByte();
91 	int minVersion = fp->readByte();
92 	int ssgVersion = VERSION(majVersion, minVersion);
93 
94 	if (ssgVersion >= VERSION(1, 4)) {
95 		int fileWidth = fp->readUint32LE();
96 		int fileHeight = fp->readUint32LE();
97 
98 		Graphics::TransparentSurface thumbnail;
99 		if (!ImgLoader::loadPNGImage(fp, &thumbnail))
100 			return;
101 
102 		delete fp;
103 		fp = nullptr;
104 
105 
106 		if (atX < 0) {
107 			fileWidth += atX;
108 			atX = 0;
109 		}
110 		if (atY < 0) {
111 			fileHeight += atY;
112 			atY = 0;
113 		}
114 		if (fileWidth + atX > (int)_sceneWidth)
115 			fileWidth = _sceneWidth - atX;
116 		if (fileHeight + atY > (int)_sceneHeight)
117 			fileHeight = _sceneHeight - atY;
118 
119 		thumbnail.blit(_backdropSurface, atX, atY, Graphics::FLIP_NONE, nullptr, TS_ARGB((uint)255, (uint)255, (uint)255, (uint)255), fileWidth, fileHeight);
120 		thumbnail.free();
121 	}
122 }
123 
skipThumbnail(Common::SeekableReadStream * stream)124 bool GraphicsManager::skipThumbnail(Common::SeekableReadStream *stream) {
125 	_thumbWidth = stream->readUint32LE();
126 	_thumbHeight = stream->readUint32LE();
127 
128 	// Load image
129 	Graphics::Surface tmp;
130 	if (_thumbWidth & _thumbHeight) {
131 		if (!ImgLoader::loadPNGImage(stream, &tmp))
132 			return false;
133 		else
134 			tmp.free();
135 	}
136 
137 	// Check flag
138 	return (stream->readByte() == '!');
139 	return true;
140 }
141 
142 } // End of namespace Sludge
143