2008-08-20 14:03:34 +00:00
|
|
|
/* 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.
|
|
|
|
*
|
2021-12-26 17:47:58 +00:00
|
|
|
* 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2008-08-20 14:03:34 +00:00
|
|
|
*
|
|
|
|
* 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
|
2021-12-26 17:47:58 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-02-18 01:34:20 +00:00
|
|
|
*
|
2008-08-20 14:03:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "graphics/thumbnail.h"
|
|
|
|
#include "graphics/scaler.h"
|
2021-05-14 20:59:23 +00:00
|
|
|
#include "graphics/pixelformat.h"
|
2008-08-20 14:03:34 +00:00
|
|
|
#include "common/endian.h"
|
2013-05-16 21:18:09 +00:00
|
|
|
#include "common/algorithm.h"
|
2008-08-20 14:03:34 +00:00
|
|
|
#include "common/system.h"
|
2010-11-19 01:37:04 +00:00
|
|
|
#include "common/stream.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2008-08-20 14:03:34 +00:00
|
|
|
|
|
|
|
namespace Graphics {
|
|
|
|
|
|
|
|
namespace {
|
2012-07-14 01:07:48 +00:00
|
|
|
#define THMB_VERSION 2
|
2008-08-20 14:03:34 +00:00
|
|
|
|
|
|
|
struct ThumbnailHeader {
|
|
|
|
uint32 type;
|
|
|
|
uint32 size;
|
|
|
|
byte version;
|
|
|
|
uint16 width, height;
|
2012-07-14 01:07:48 +00:00
|
|
|
PixelFormat format;
|
2008-08-20 14:03:34 +00:00
|
|
|
};
|
|
|
|
|
2012-07-14 01:07:48 +00:00
|
|
|
#define ThumbnailHeaderSize (4+4+1+2+2+(1+4+4))
|
2008-08-20 14:03:34 +00:00
|
|
|
|
2013-07-12 20:44:21 +00:00
|
|
|
enum HeaderState {
|
|
|
|
/// There is no header present
|
|
|
|
kHeaderNone,
|
|
|
|
/// The header present only has reliable values for version and size
|
|
|
|
kHeaderUnsupported,
|
|
|
|
/// The header is present and the version is supported
|
|
|
|
kHeaderPresent
|
|
|
|
};
|
|
|
|
|
|
|
|
HeaderState loadHeader(Common::SeekableReadStream &in, ThumbnailHeader &header, bool outputWarnings) {
|
2008-08-20 14:03:34 +00:00
|
|
|
header.type = in.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.
|
2011-04-12 14:53:15 +00:00
|
|
|
if (header.type != MKTAG('T','H','M','B') && header.type != MKTAG('B','M','H','T')) {
|
2008-08-20 14:03:34 +00:00
|
|
|
if (outputWarnings)
|
|
|
|
warning("couldn't find thumbnail header type");
|
2013-07-12 20:44:21 +00:00
|
|
|
return kHeaderNone;
|
2008-08-20 14:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
header.size = in.readUint32BE();
|
|
|
|
header.version = in.readByte();
|
|
|
|
|
2016-02-14 09:27:44 +00:00
|
|
|
// Do a check whether any read errors had occurred. If so we cannot use the
|
2013-07-12 20:44:21 +00:00
|
|
|
// values obtained for size and version because they might be bad.
|
|
|
|
if (in.err() || in.eos()) {
|
|
|
|
// TODO: We fake that there is no header. This is actually not quite
|
|
|
|
// correct since we found the start of the header and then things
|
|
|
|
// started to break. Right no we leave detection of this to the client.
|
|
|
|
// Since this case is caused by broken files, the client code should
|
|
|
|
// catch it anyway... If there is a nicer solution here, we should
|
|
|
|
// implement it.
|
|
|
|
return kHeaderNone;
|
|
|
|
}
|
|
|
|
|
2008-08-20 14:03:34 +00:00
|
|
|
if (header.version > THMB_VERSION) {
|
|
|
|
if (outputWarnings)
|
|
|
|
warning("trying to load a newer thumbnail version: %d instead of %d", header.version, THMB_VERSION);
|
2013-07-12 20:44:21 +00:00
|
|
|
return kHeaderUnsupported;
|
2008-08-20 14:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
header.width = in.readUint16BE();
|
|
|
|
header.height = in.readUint16BE();
|
2012-07-14 01:07:48 +00:00
|
|
|
header.format.bytesPerPixel = in.readByte();
|
|
|
|
// Starting from version 2 on we serialize the whole PixelFormat.
|
|
|
|
if (header.version >= 2) {
|
|
|
|
header.format.rLoss = in.readByte();
|
|
|
|
header.format.gLoss = in.readByte();
|
|
|
|
header.format.bLoss = in.readByte();
|
|
|
|
header.format.aLoss = in.readByte();
|
|
|
|
|
|
|
|
header.format.rShift = in.readByte();
|
|
|
|
header.format.gShift = in.readByte();
|
|
|
|
header.format.bShift = in.readByte();
|
|
|
|
header.format.aShift = in.readByte();
|
|
|
|
} else {
|
|
|
|
// Version 1 used a hardcoded RGB565.
|
|
|
|
header.format = createPixelFormat<565>();
|
|
|
|
}
|
2008-08-20 14:03:34 +00:00
|
|
|
|
2013-07-12 20:44:21 +00:00
|
|
|
if (in.err() || in.eos()) {
|
|
|
|
// When we reached this point we know that at least the size and
|
|
|
|
// version field was loaded successfully, thus we tell this header
|
|
|
|
// is not supported and silently hope that the client code is
|
|
|
|
// prepared to handle read errors.
|
|
|
|
return kHeaderUnsupported;
|
|
|
|
} else {
|
|
|
|
return kHeaderPresent;
|
|
|
|
}
|
2008-08-20 14:03:34 +00:00
|
|
|
}
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
bool checkThumbnailHeader(Common::SeekableReadStream &in) {
|
|
|
|
uint32 position = in.pos();
|
|
|
|
ThumbnailHeader header;
|
|
|
|
|
2013-07-12 20:44:21 +00:00
|
|
|
// TODO: It is not clear whether this is the best semantics. Now
|
|
|
|
// checkThumbnailHeader will return true even when the thumbnail header
|
|
|
|
// found is actually not usable. However, most engines seem to use this
|
|
|
|
// to detect the presence of any header and if there is none it wont even
|
|
|
|
// try to skip it. Thus, this looks like the best solution for now...
|
|
|
|
bool hasHeader = (loadHeader(in, header, false) != kHeaderNone);
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2008-08-20 14:03:34 +00:00
|
|
|
in.seek(position, SEEK_SET);
|
|
|
|
|
|
|
|
return hasHeader;
|
|
|
|
}
|
|
|
|
|
2010-08-02 22:28:30 +00:00
|
|
|
bool skipThumbnail(Common::SeekableReadStream &in) {
|
2008-08-20 14:03:34 +00:00
|
|
|
uint32 position = in.pos();
|
|
|
|
ThumbnailHeader header;
|
|
|
|
|
2013-07-12 20:44:21 +00:00
|
|
|
// We can skip unsupported and supported headers. So we only seek back
|
|
|
|
// to the old position in case there is no header at all.
|
|
|
|
if (loadHeader(in, header, false) == kHeaderNone) {
|
2008-08-20 14:03:34 +00:00
|
|
|
in.seek(position, SEEK_SET);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
in.seek(header.size - (in.pos() - position), SEEK_CUR);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-05 22:06:38 +00:00
|
|
|
bool loadThumbnail(Common::SeekableReadStream &in, Graphics::Surface *&thumbnail, bool skipThumbnail) {
|
|
|
|
if (skipThumbnail) {
|
2018-07-26 01:07:20 +00:00
|
|
|
thumbnail = nullptr;
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-05 22:06:38 +00:00
|
|
|
return Graphics::skipThumbnail(in);
|
|
|
|
}
|
|
|
|
|
2013-07-12 20:44:21 +00:00
|
|
|
const uint32 position = in.pos();
|
2008-08-20 14:03:34 +00:00
|
|
|
ThumbnailHeader header;
|
2013-07-12 20:44:21 +00:00
|
|
|
HeaderState headerState = loadHeader(in, header, true);
|
|
|
|
|
|
|
|
// Try to handle unsupported/broken headers gracefully. If there is no
|
|
|
|
// header at all, we seek back and return at this point. If there is an
|
|
|
|
// unsupported/broken header, we skip the actual data and return. The
|
|
|
|
// downside is that we might reset the end of stream flag with this and
|
|
|
|
// the client code would not be able to notice a read past the end of the
|
|
|
|
// stream at this point then.
|
|
|
|
if (headerState == kHeaderNone) {
|
|
|
|
in.seek(position, SEEK_SET);
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-05 22:06:38 +00:00
|
|
|
return false;
|
2013-07-12 20:44:21 +00:00
|
|
|
} else if (headerState == kHeaderUnsupported) {
|
|
|
|
in.seek(header.size - (in.pos() - position), SEEK_CUR);
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-05 22:06:38 +00:00
|
|
|
return false;
|
2013-07-12 20:44:21 +00:00
|
|
|
}
|
2008-08-20 14:03:34 +00:00
|
|
|
|
2012-07-14 01:07:48 +00:00
|
|
|
if (header.format.bytesPerPixel != 2 && header.format.bytesPerPixel != 4) {
|
|
|
|
warning("trying to load thumbnail with unsupported bit depth %d", header.format.bytesPerPixel);
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-05 22:06:38 +00:00
|
|
|
return false;
|
2008-08-20 14:03:34 +00:00
|
|
|
}
|
|
|
|
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-05 22:06:38 +00:00
|
|
|
thumbnail = new Graphics::Surface();
|
|
|
|
thumbnail->create(header.width, header.height, header.format);
|
2008-08-20 14:03:34 +00:00
|
|
|
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-05 22:06:38 +00:00
|
|
|
for (int y = 0; y < thumbnail->h; ++y) {
|
2012-07-14 01:07:48 +00:00
|
|
|
switch (header.format.bytesPerPixel) {
|
|
|
|
case 2: {
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-05 22:06:38 +00:00
|
|
|
uint16 *pixels = (uint16 *)thumbnail->getBasePtr(0, y);
|
2021-07-05 00:24:30 +00:00
|
|
|
for (int x = 0; x < thumbnail->w; ++x) {
|
2012-07-14 01:07:48 +00:00
|
|
|
*pixels++ = in.readUint16BE();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 4: {
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-05 22:06:38 +00:00
|
|
|
uint32 *pixels = (uint32 *)thumbnail->getBasePtr(0, y);
|
2021-07-05 00:24:30 +00:00
|
|
|
for (int x = 0; x < thumbnail->w; ++x) {
|
2012-07-14 01:07:48 +00:00
|
|
|
*pixels++ = in.readUint32BE();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
2008-08-20 14:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-05 22:06:38 +00:00
|
|
|
return true;
|
2008-08-20 14:03:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 01:33:54 +00:00
|
|
|
bool createThumbnail(Graphics::Surface &thumb) {
|
|
|
|
if (thumb.getPixels())
|
|
|
|
thumb.free();
|
|
|
|
|
|
|
|
if (!createThumbnailFromScreen(&thumb)) {
|
|
|
|
warning("Couldn't create thumbnail from screen, aborting thumbnail save");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-20 14:03:34 +00:00
|
|
|
bool saveThumbnail(Common::WriteStream &out) {
|
|
|
|
Graphics::Surface thumb;
|
|
|
|
|
|
|
|
if (!createThumbnailFromScreen(&thumb)) {
|
|
|
|
warning("Couldn't create thumbnail from screen, aborting thumbnail save");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = saveThumbnail(out, thumb);
|
|
|
|
thumb.free();
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool saveThumbnail(Common::WriteStream &out, const Graphics::Surface &thumb) {
|
2012-07-14 01:07:48 +00:00
|
|
|
if (thumb.format.bytesPerPixel != 2 && thumb.format.bytesPerPixel != 4) {
|
|
|
|
warning("trying to save thumbnail with bpp %u", thumb.format.bytesPerPixel);
|
2008-08-20 14:03:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThumbnailHeader header;
|
2011-04-12 14:53:15 +00:00
|
|
|
header.type = MKTAG('T','H','M','B');
|
2011-04-17 15:30:44 +00:00
|
|
|
header.size = ThumbnailHeaderSize + thumb.w*thumb.h*thumb.format.bytesPerPixel;
|
2008-08-20 14:03:34 +00:00
|
|
|
header.version = THMB_VERSION;
|
|
|
|
header.width = thumb.w;
|
|
|
|
header.height = thumb.h;
|
|
|
|
|
|
|
|
out.writeUint32BE(header.type);
|
|
|
|
out.writeUint32BE(header.size);
|
|
|
|
out.writeByte(header.version);
|
|
|
|
out.writeUint16BE(header.width);
|
|
|
|
out.writeUint16BE(header.height);
|
|
|
|
|
2012-07-14 01:07:48 +00:00
|
|
|
// Serialize the PixelFormat
|
|
|
|
out.writeByte(thumb.format.bytesPerPixel);
|
|
|
|
out.writeByte(thumb.format.rLoss);
|
|
|
|
out.writeByte(thumb.format.gLoss);
|
|
|
|
out.writeByte(thumb.format.bLoss);
|
|
|
|
out.writeByte(thumb.format.aLoss);
|
|
|
|
out.writeByte(thumb.format.rShift);
|
|
|
|
out.writeByte(thumb.format.gShift);
|
|
|
|
out.writeByte(thumb.format.bShift);
|
|
|
|
out.writeByte(thumb.format.aShift);
|
|
|
|
|
|
|
|
// Serialize the pixel data
|
2021-07-05 00:24:30 +00:00
|
|
|
for (int y = 0; y < thumb.h; ++y) {
|
2012-07-14 01:07:48 +00:00
|
|
|
switch (thumb.format.bytesPerPixel) {
|
|
|
|
case 2: {
|
|
|
|
const uint16 *pixels = (const uint16 *)thumb.getBasePtr(0, y);
|
2021-07-05 00:24:30 +00:00
|
|
|
for (int x = 0; x < thumb.w; ++x) {
|
2012-07-14 01:07:48 +00:00
|
|
|
out.writeUint16BE(*pixels++);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 4: {
|
|
|
|
const uint32 *pixels = (const uint32 *)thumb.getBasePtr(0, y);
|
2021-07-05 00:24:30 +00:00
|
|
|
for (int x = 0; x < thumb.w; ++x) {
|
2012-07-14 01:07:48 +00:00
|
|
|
out.writeUint32BE(*pixels++);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
2008-08-20 14:03:34 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-16 21:18:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array indicating which pixels of a source image horizontally or vertically get
|
|
|
|
* included in a scaled image
|
|
|
|
*/
|
|
|
|
int *scaleLine(int size, int srcSize) {
|
|
|
|
int scale = 100 * size / srcSize;
|
|
|
|
assert(scale > 0);
|
|
|
|
int *v = new int[size];
|
2022-06-11 10:28:36 +00:00
|
|
|
Common::fill(v, v + size, 0);
|
2013-05-16 21:18:09 +00:00
|
|
|
|
|
|
|
int distCtr = 0;
|
|
|
|
int *destP = v;
|
|
|
|
for (int distIndex = 0; distIndex < srcSize; ++distIndex) {
|
|
|
|
distCtr += scale;
|
|
|
|
while (distCtr >= 100) {
|
|
|
|
assert(destP < &v[size]);
|
|
|
|
*destP++ = distIndex;
|
|
|
|
distCtr -= 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Graphics::Surface *scale(const Graphics::Surface &srcImage, int xSize, int ySize) {
|
|
|
|
Graphics::Surface *s = new Graphics::Surface();
|
|
|
|
s->create(xSize, ySize, srcImage.format);
|
|
|
|
|
|
|
|
int *horizUsage = scaleLine(xSize, srcImage.w);
|
|
|
|
int *vertUsage = scaleLine(ySize, srcImage.h);
|
|
|
|
|
|
|
|
// Loop to create scaled version
|
|
|
|
for (int yp = 0; yp < ySize; ++yp) {
|
|
|
|
const byte *srcP = (const byte *)srcImage.getBasePtr(0, vertUsage[yp]);
|
|
|
|
byte *destP = (byte *)s->getBasePtr(0, yp);
|
|
|
|
|
|
|
|
for (int xp = 0; xp < xSize; ++xp) {
|
|
|
|
const byte *tempSrcP = srcP + (horizUsage[xp] * srcImage.format.bytesPerPixel);
|
|
|
|
for (int byteCtr = 0; byteCtr < srcImage.format.bytesPerPixel; ++byteCtr) {
|
|
|
|
*destP++ = *tempSrcP++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete arrays and return surface
|
|
|
|
delete[] horizUsage;
|
|
|
|
delete[] vertUsage;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-10-04 21:26:33 +00:00
|
|
|
} // End of namespace Graphics
|