ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

thumbnail.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "graphics/thumbnail.h"
00024 #include "graphics/scaler.h"
00025 #include "graphics/colormasks.h"
00026 #include "common/endian.h"
00027 #include "common/algorithm.h"
00028 #include "common/system.h"
00029 #include "common/stream.h"
00030 #include "common/textconsole.h"
00031 
00032 namespace Graphics {
00033 
00034 namespace {
00035 #define THMB_VERSION 2
00036 
00037 struct ThumbnailHeader {
00038     uint32 type;
00039     uint32 size;
00040     byte version;
00041     uint16 width, height;
00042     PixelFormat format;
00043 };
00044 
00045 #define ThumbnailHeaderSize (4+4+1+2+2+(1+4+4))
00046 
00047 enum HeaderState {
00049     kHeaderNone,
00051     kHeaderUnsupported,
00053     kHeaderPresent
00054 };
00055 
00056 HeaderState loadHeader(Common::SeekableReadStream &in, ThumbnailHeader &header, bool outputWarnings) {
00057     header.type = in.readUint32BE();
00058     // We also accept the bad 'BMHT' header here, for the sake of compatibility
00059     // with some older savegames which were written incorrectly due to a bug in
00060     // ScummVM which wrote the thumb header type incorrectly on LE systems.
00061     if (header.type != MKTAG('T','H','M','B') && header.type != MKTAG('B','M','H','T')) {
00062         if (outputWarnings)
00063             warning("couldn't find thumbnail header type");
00064         return kHeaderNone;
00065     }
00066 
00067     header.size = in.readUint32BE();
00068     header.version = in.readByte();
00069 
00070     // Do a check whether any read errors had occurred. If so we cannot use the
00071     // values obtained for size and version because they might be bad.
00072     if (in.err() || in.eos()) {
00073         // TODO: We fake that there is no header. This is actually not quite
00074         // correct since we found the start of the header and then things
00075         // started to break. Right no we leave detection of this to the client.
00076         // Since this case is caused by broken files, the client code should
00077         // catch it anyway... If there is a nicer solution here, we should
00078         // implement it.
00079         return kHeaderNone;
00080     }
00081 
00082     if (header.version > THMB_VERSION) {
00083         if (outputWarnings)
00084             warning("trying to load a newer thumbnail version: %d instead of %d", header.version, THMB_VERSION);
00085         return kHeaderUnsupported;
00086     }
00087 
00088     header.width = in.readUint16BE();
00089     header.height = in.readUint16BE();
00090     header.format.bytesPerPixel = in.readByte();
00091     // Starting from version 2 on we serialize the whole PixelFormat.
00092     if (header.version >= 2) {
00093         header.format.rLoss = in.readByte();
00094         header.format.gLoss = in.readByte();
00095         header.format.bLoss = in.readByte();
00096         header.format.aLoss = in.readByte();
00097 
00098         header.format.rShift = in.readByte();
00099         header.format.gShift = in.readByte();
00100         header.format.bShift = in.readByte();
00101         header.format.aShift = in.readByte();
00102     } else {
00103         // Version 1 used a hardcoded RGB565.
00104         header.format = createPixelFormat<565>();
00105     }
00106 
00107     if (in.err() || in.eos()) {
00108         // When we reached this point we know that at least the size and
00109         // version field was loaded successfully, thus we tell this header
00110         // is not supported and silently hope that the client code is
00111         // prepared to handle read errors.
00112         return kHeaderUnsupported;
00113     } else {
00114         return kHeaderPresent;
00115     }
00116 }
00117 } // end of anonymous namespace
00118 
00119 bool checkThumbnailHeader(Common::SeekableReadStream &in) {
00120     uint32 position = in.pos();
00121     ThumbnailHeader header;
00122 
00123     // TODO: It is not clear whether this is the best semantics. Now
00124     // checkThumbnailHeader will return true even when the thumbnail header
00125     // found is actually not usable. However, most engines seem to use this
00126     // to detect the presence of any header and if there is none it wont even
00127     // try to skip it. Thus, this looks like the best solution for now...
00128     bool hasHeader = (loadHeader(in, header, false) != kHeaderNone);
00129 
00130     in.seek(position, SEEK_SET);
00131 
00132     return hasHeader;
00133 }
00134 
00135 bool skipThumbnail(Common::SeekableReadStream &in) {
00136     uint32 position = in.pos();
00137     ThumbnailHeader header;
00138 
00139     // We can skip unsupported and supported headers. So we only seek back
00140     // to the old position in case there is no header at all.
00141     if (loadHeader(in, header, false) == kHeaderNone) {
00142         in.seek(position, SEEK_SET);
00143         return false;
00144     }
00145 
00146     in.seek(header.size - (in.pos() - position), SEEK_CUR);
00147     return true;
00148 }
00149 
00150 bool loadThumbnail(Common::SeekableReadStream &in, Graphics::Surface *&thumbnail, bool skipThumbnail) {
00151     if (skipThumbnail) {
00152         thumbnail = nullptr;
00153         return Graphics::skipThumbnail(in);
00154     }
00155 
00156     const uint32 position = in.pos();
00157     ThumbnailHeader header;
00158     HeaderState headerState = loadHeader(in, header, true);
00159 
00160     // Try to handle unsupported/broken headers gracefully. If there is no
00161     // header at all, we seek back and return at this point. If there is an
00162     // unsupported/broken header, we skip the actual data and return. The
00163     // downside is that we might reset the end of stream flag with this and
00164     // the client code would not be able to notice a read past the end of the
00165     // stream at this point then.
00166     if (headerState == kHeaderNone) {
00167         in.seek(position, SEEK_SET);
00168         return false;
00169     } else if (headerState == kHeaderUnsupported) {
00170         in.seek(header.size - (in.pos() - position), SEEK_CUR);
00171         return false;
00172     }
00173 
00174     if (header.format.bytesPerPixel != 2 && header.format.bytesPerPixel != 4) {
00175         warning("trying to load thumbnail with unsupported bit depth %d", header.format.bytesPerPixel);
00176         return false;
00177     }
00178 
00179     thumbnail = new Graphics::Surface();
00180     thumbnail->create(header.width, header.height, header.format);
00181 
00182     for (int y = 0; y < thumbnail->h; ++y) {
00183         switch (header.format.bytesPerPixel) {
00184         case 2: {
00185             uint16 *pixels = (uint16 *)thumbnail->getBasePtr(0, y);
00186             for (uint x = 0; x < thumbnail->w; ++x) {
00187                 *pixels++ = in.readUint16BE();
00188             }
00189             } break;
00190 
00191         case 4: {
00192             uint32 *pixels = (uint32 *)thumbnail->getBasePtr(0, y);
00193             for (uint x = 0; x < thumbnail->w; ++x) {
00194                 *pixels++ = in.readUint32BE();
00195             }
00196             } break;
00197 
00198         default:
00199             assert(0);
00200         }
00201     }
00202     return true;
00203 }
00204 
00205 bool saveThumbnail(Common::WriteStream &out) {
00206     Graphics::Surface thumb;
00207 
00208     if (!createThumbnailFromScreen(&thumb)) {
00209         warning("Couldn't create thumbnail from screen, aborting thumbnail save");
00210         return false;
00211     }
00212 
00213     bool success = saveThumbnail(out, thumb);
00214     thumb.free();
00215 
00216     return success;
00217 }
00218 
00219 bool saveThumbnail(Common::WriteStream &out, const Graphics::Surface &thumb) {
00220     if (thumb.format.bytesPerPixel != 2 && thumb.format.bytesPerPixel != 4) {
00221         warning("trying to save thumbnail with bpp %u", thumb.format.bytesPerPixel);
00222         return false;
00223     }
00224 
00225     ThumbnailHeader header;
00226     header.type = MKTAG('T','H','M','B');
00227     header.size = ThumbnailHeaderSize + thumb.w*thumb.h*thumb.format.bytesPerPixel;
00228     header.version = THMB_VERSION;
00229     header.width = thumb.w;
00230     header.height = thumb.h;
00231 
00232     out.writeUint32BE(header.type);
00233     out.writeUint32BE(header.size);
00234     out.writeByte(header.version);
00235     out.writeUint16BE(header.width);
00236     out.writeUint16BE(header.height);
00237 
00238     // Serialize the PixelFormat
00239     out.writeByte(thumb.format.bytesPerPixel);
00240     out.writeByte(thumb.format.rLoss);
00241     out.writeByte(thumb.format.gLoss);
00242     out.writeByte(thumb.format.bLoss);
00243     out.writeByte(thumb.format.aLoss);
00244     out.writeByte(thumb.format.rShift);
00245     out.writeByte(thumb.format.gShift);
00246     out.writeByte(thumb.format.bShift);
00247     out.writeByte(thumb.format.aShift);
00248 
00249     // Serialize the pixel data
00250     for (uint y = 0; y < thumb.h; ++y) {
00251         switch (thumb.format.bytesPerPixel) {
00252         case 2: {
00253             const uint16 *pixels = (const uint16 *)thumb.getBasePtr(0, y);
00254             for (uint x = 0; x < thumb.w; ++x) {
00255                 out.writeUint16BE(*pixels++);
00256             }
00257             } break;
00258 
00259         case 4: {
00260             const uint32 *pixels = (const uint32 *)thumb.getBasePtr(0, y);
00261             for (uint x = 0; x < thumb.w; ++x) {
00262                 out.writeUint32BE(*pixels++);
00263             }
00264             } break;
00265 
00266         default:
00267             assert(0);
00268         }
00269     }
00270 
00271     return true;
00272 }
00273 
00274 
00279 int *scaleLine(int size, int srcSize) {
00280     int scale = 100 * size / srcSize;
00281     assert(scale > 0);
00282     int *v = new int[size];
00283     Common::fill(v, &v[size], 0);
00284 
00285     int distCtr = 0;
00286     int *destP = v;
00287     for (int distIndex = 0; distIndex < srcSize; ++distIndex) {
00288         distCtr += scale;
00289         while (distCtr >= 100) {
00290             assert(destP < &v[size]);
00291             *destP++ = distIndex;
00292             distCtr -= 100;
00293         }
00294     }
00295 
00296     return v;
00297 }
00298 
00299 Graphics::Surface *scale(const Graphics::Surface &srcImage, int xSize, int ySize) {
00300     Graphics::Surface *s = new Graphics::Surface();
00301     s->create(xSize, ySize, srcImage.format);
00302 
00303     int *horizUsage = scaleLine(xSize, srcImage.w);
00304     int *vertUsage = scaleLine(ySize, srcImage.h);
00305 
00306     // Loop to create scaled version
00307     for (int yp = 0; yp < ySize; ++yp) {
00308         const byte *srcP = (const byte *)srcImage.getBasePtr(0, vertUsage[yp]);
00309         byte *destP = (byte *)s->getBasePtr(0, yp);
00310 
00311         for (int xp = 0; xp < xSize; ++xp) {
00312             const byte *tempSrcP = srcP + (horizUsage[xp] * srcImage.format.bytesPerPixel);
00313             for (int byteCtr = 0; byteCtr < srcImage.format.bytesPerPixel; ++byteCtr) {
00314                 *destP++ = *tempSrcP++;
00315             }
00316         }
00317     }
00318 
00319     // Delete arrays and return surface
00320     delete[] horizUsage;
00321     delete[] vertUsage;
00322     return s;
00323 }
00324 
00325 } // End of namespace Graphics


Generated on Sat Mar 16 2019 05:01:56 for ResidualVM by doxygen 1.7.1
curved edge   curved edge