diff options
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/dxa_player.cpp | 1 | ||||
-rw-r--r-- | graphics/dxa_player.h | 6 | ||||
-rw-r--r-- | graphics/font.cpp | 1 | ||||
-rw-r--r-- | graphics/iff.cpp | 9 | ||||
-rw-r--r-- | graphics/module.mk | 2 | ||||
-rw-r--r-- | graphics/scaler.h | 16 | ||||
-rw-r--r-- | graphics/scaler/thumbnail_intern.cpp (renamed from graphics/scaler/thumbnail.cpp) | 91 | ||||
-rw-r--r-- | graphics/surface.cpp | 5 | ||||
-rw-r--r-- | graphics/surface.h | 19 | ||||
-rw-r--r-- | graphics/thumbnail.cpp | 174 | ||||
-rw-r--r-- | graphics/thumbnail.h | 69 |
11 files changed, 348 insertions, 45 deletions
diff --git a/graphics/dxa_player.cpp b/graphics/dxa_player.cpp index 28a1bc4dbd..f4c93a51f1 100644 --- a/graphics/dxa_player.cpp +++ b/graphics/dxa_player.cpp @@ -24,6 +24,7 @@ */ #include "common/endian.h" +#include "common/file.h" #include "graphics/dxa_player.h" #include "common/util.h" diff --git a/graphics/dxa_player.h b/graphics/dxa_player.h index 5415e440d2..dbe39bbcee 100644 --- a/graphics/dxa_player.h +++ b/graphics/dxa_player.h @@ -27,11 +27,7 @@ #define GRAPHICS_DXA_PLAYER_H #include "common/scummsys.h" -#include "common/file.h" - -namespace Common { - class File; -} +#include "common/stream.h" namespace Graphics { diff --git a/graphics/font.cpp b/graphics/font.cpp index 3e817e3e6c..404e04d18e 100644 --- a/graphics/font.cpp +++ b/graphics/font.cpp @@ -524,6 +524,7 @@ int bdf_read_bitmaps(Common::SeekableReadStream &fp, NewFontData* pf) { } /* read the next non-comment line, returns buf or NULL if EOF*/ +// TODO: Can we use SeekableReadStream::readLine resp. readLine_NEW instead? char *bdf_getline(Common::SeekableReadStream &fp, char *buf, int len) { int c; char *b; diff --git a/graphics/iff.cpp b/graphics/iff.cpp index 514fba9cc0..b3846c5d26 100644 --- a/graphics/iff.cpp +++ b/graphics/iff.cpp @@ -219,7 +219,7 @@ void PBMDecoder::readBODY(Common::IFFChunk& chunk) { switch (_bitmapHeader.pack) { case 0: - while (!chunk.eos()) { + while (!chunk.hasReadAll()) { ((byte*)_surface->pixels)[si++] = chunk.readByte(); } break; @@ -245,7 +245,9 @@ PackBitsReadStream::~PackBitsReadStream() { } bool PackBitsReadStream::eos() const { - return _input->eos() & (_rStoragePos == _wStoragePos); + //FIXME: eos definition needs to be changed in parallaction engine + // which is the only place where this class is used + return _input->eos() && (_rStoragePos == _wStoragePos); } uint32 PackBitsReadStream::read(void *dataPtr, uint32 dataSize) { @@ -291,6 +293,9 @@ void PackBitsReadStream::unpack() { while (_out < _outEnd && !_input->eos()) { byteRun = _input->readByte(); + //FIXME: eos definition needs to be changed in parallaction engine + // which is the only place where this class is used + //if (_input->eos()) break; if (byteRun <= 127) { i = byteRun + 1; for (j = 0; j < i; j++) { diff --git a/graphics/module.mk b/graphics/module.mk index b3618ddcfc..d21915803e 100644 --- a/graphics/module.mk +++ b/graphics/module.mk @@ -15,7 +15,7 @@ MODULE_OBJS := \ mpeg_player.o \ primitives.o \ scaler.o \ - scaler/thumbnail.o \ + scaler/thumbnail_intern.o \ surface.o \ VectorRenderer.o \ VectorRendererSpec.o diff --git a/graphics/scaler.h b/graphics/scaler.h index 2cf3f66239..95900de412 100644 --- a/graphics/scaler.h +++ b/graphics/scaler.h @@ -78,10 +78,22 @@ enum { extern void createThumbnail(const uint8* src, uint32 srcPitch, uint8* dstPtr, uint32 dstPitch, int width, int height); /** - * creates a thumbnail from the current screen (without overlay) + * Creates a thumbnail from the current screen (without overlay). + * * @param surf a surface (will always have 16 bpp after this for now) * @return false if a error occured */ -extern bool createThumbnailFromScreen(Graphics::Surface* surf); +extern bool createThumbnailFromScreen(Graphics::Surface *surf); + +/** + * Creates a thumbnail from a buffer. + * + * @param surf destination surface (will always have 16 bpp after this for now) + * @param pixels raw pixel data + * @param w width + * @param h height + * @param palette palette in RGB format + */ +extern bool createThumbnail(Graphics::Surface *surf, const uint8 *pixels, int w, int h, const uint8 *palette); #endif diff --git a/graphics/scaler/thumbnail.cpp b/graphics/scaler/thumbnail_intern.cpp index f1caa5d2e5..bdfa0ff5f6 100644 --- a/graphics/scaler/thumbnail.cpp +++ b/graphics/scaler/thumbnail_intern.cpp @@ -126,70 +126,93 @@ static bool grabScreen565(Graphics::Surface *surf) { return true; } -bool createThumbnailFromScreen(Graphics::Surface* surf) { - assert(surf); - - int screenWidth = g_system->getWidth(); - int screenHeight = g_system->getHeight(); - - Graphics::Surface screen; - - if (!grabScreen565(&screen)) - return false; +static bool createThumbnail(Graphics::Surface &out, Graphics::Surface &in) { + uint16 width = in.w; + uint16 inHeight = in.h; - uint16 width = screenWidth; - - if (screenWidth < 320) { + if (width < 320) { // Special case to handle MM NES (uses a screen width of 256) width = 320; // center MM NES screen Graphics::Surface newscreen; - newscreen.create(width, screen.h, screen.bytesPerPixel); + newscreen.create(width, in.h, in.bytesPerPixel); - uint8 *dst = (uint8*)newscreen.getBasePtr((320 - screenWidth) / 2, 0); - uint8 *src = (uint8*)screen.getBasePtr(0, 0); - uint16 height = screen.h; + uint8 *dst = (uint8*)newscreen.getBasePtr((320 - in.w) / 2, 0); + const uint8 *src = (uint8*)in.getBasePtr(0, 0); + uint16 height = in.h; while (height--) { - memcpy(dst, src, screen.pitch); + memcpy(dst, src, in.pitch); dst += newscreen.pitch; - src += screen.pitch; + src += in.pitch; } - screen.free(); - screen = newscreen; - } else if (screenWidth == 720) { + in.free(); + in = newscreen; + } else if (width == 720) { // Special case to handle Hercules mode width = 640; - screenHeight = 400; + inHeight = 400; // cut off menu and so on.. Graphics::Surface newscreen; - newscreen.create(width, 400, screen.bytesPerPixel); + newscreen.create(width, 400, in.bytesPerPixel); - uint8 *dst = (uint8*)newscreen.getBasePtr(0, (400 - 240) / 2); - uint8 *src = (uint8*)screen.getBasePtr(41, 28); + uint8 *dst = (uint8*)in.getBasePtr(0, (400 - 240) / 2); + const uint8 *src = (uint8*)in.getBasePtr(41, 28); for (int y = 0; y < 240; ++y) { - memcpy(dst, src, 640 * screen.bytesPerPixel); + memcpy(dst, src, 640 * in.bytesPerPixel); dst += newscreen.pitch; - src += screen.pitch; + src += in.pitch; } - screen.free(); - screen = newscreen; + in.free(); + in = newscreen; } - uint16 newHeight = !(screenHeight % 240) ? kThumbnailHeight2 : kThumbnailHeight1; + uint16 newHeight = !(inHeight % 240) ? kThumbnailHeight2 : kThumbnailHeight1; int gBitFormatBackUp = gBitFormat; gBitFormat = 565; - surf->create(kThumbnailWidth, newHeight, sizeof(uint16)); - createThumbnail((const uint8*)screen.pixels, width * sizeof(uint16), (uint8*)surf->pixels, surf->pitch, width, screenHeight); + out.create(kThumbnailWidth, newHeight, sizeof(uint16)); + createThumbnail((const uint8 *)in.pixels, width * sizeof(uint16), (uint8 *)out.pixels, out.pitch, width, inHeight); gBitFormat = gBitFormatBackUp; - screen.free(); + in.free(); return true; } + +bool createThumbnailFromScreen(Graphics::Surface* surf) { + assert(surf); + + Graphics::Surface screen; + + if (!grabScreen565(&screen)) + return false; + + return createThumbnail(*surf, screen); +} + +bool createThumbnail(Graphics::Surface *surf, const uint8 *pixels, int w, int h, const uint8 *palette) { + assert(surf); + + Graphics::Surface screen; + screen.create(w, h, 2); + + for (uint y = 0; y < screen.h; ++y) { + for (uint x = 0; x < screen.w; ++x) { + byte r, g, b; + r = palette[pixels[y * w + x] * 3]; + g = palette[pixels[y * w + x] * 3 + 1]; + b = palette[pixels[y * w + x] * 3 + 2]; + + ((uint16 *)screen.pixels)[y * screen.w + x] = RGBToColor<ColorMasks<565> >(r, g, b); + } + } + + return createThumbnail(*surf, screen); +} + diff --git a/graphics/surface.cpp b/graphics/surface.cpp index a9f3e75886..263a4fd23b 100644 --- a/graphics/surface.cpp +++ b/graphics/surface.cpp @@ -66,6 +66,11 @@ void Surface::free() { bytesPerPixel = 0; } +void Surface::copyFrom(const Surface &surf) { + create(surf.w, surf.h, surf.bytesPerPixel); + memcpy(pixels, surf.pixels, h * pitch); +} + void Surface::hLine(int x, int y, int x2, uint32 color) { // Clipping if (y < 0 || y >= h) diff --git a/graphics/surface.h b/graphics/surface.h index ff1ddda695..747bda9a26 100644 --- a/graphics/surface.h +++ b/graphics/surface.h @@ -30,7 +30,6 @@ namespace Graphics { - /** * An arbitrary graphics surface, which can be the target (or source) of blit * operations, font rendering, etc. @@ -67,6 +66,12 @@ struct Surface { */ void free(); + /** + * Copies data from another Surface, this calls *free* on the current surface, to assure + * it being clean. + */ + void copyFrom(const Surface &surf); + void drawLine(int x0, int y0, int x1, int y1, uint32 color); void hLine(int x, int y, int x2, uint32 color); void vLine(int x, int y, int y2, uint32 color); @@ -76,6 +81,18 @@ struct Surface { void move(int dx, int dy, int height); }; +/** + * For safe deletion of surface with SharedPtr. + * The deleter assures Surface::free is called on + * deletion. + */ +struct SharedPtrSurfaceDeleter { + void operator()(Surface *ptr) { + ptr->free(); + delete ptr; + } +}; + } // End of namespace Graphics diff --git a/graphics/thumbnail.cpp b/graphics/thumbnail.cpp new file mode 100644 index 0000000000..905fea3d93 --- /dev/null +++ b/graphics/thumbnail.cpp @@ -0,0 +1,174 @@ +/* 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. + * + * $URL$ + * $Id$ + */ + +#include "graphics/thumbnail.h" +#include "graphics/scaler.h" +#include "common/endian.h" +#include "common/system.h" + +namespace Graphics { + +namespace { +#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); +} + +bool loadHeader(Common::SeekableReadStream &in, ThumbnailHeader &header, bool outputWarnings) { + 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. + if (header.type != MKID_BE('THMB') && header.type != MKID_BE('BMHT')) { + if (outputWarnings) + warning("couldn't find thumbnail header type"); + return false; + } + + header.size = in.readUint32BE(); + header.version = in.readByte(); + + if (header.version > THMB_VERSION) { + if (outputWarnings) + warning("trying to load a newer thumbnail version: %d instead of %d", header.version, THMB_VERSION); + return false; + } + + header.width = in.readUint16BE(); + header.height = in.readUint16BE(); + header.bpp = in.readByte(); + + return true; +} +} // end of anonymous namespace + +bool checkThumbnailHeader(Common::SeekableReadStream &in) { + uint32 position = in.pos(); + ThumbnailHeader header; + + bool hasHeader = loadHeader(in, header, false); + + in.seek(position, SEEK_SET); + + return hasHeader; +} + +bool skipThumbnailHeader(Common::SeekableReadStream &in) { + uint32 position = in.pos(); + ThumbnailHeader header; + + if (!loadHeader(in, header, false)) { + in.seek(position, SEEK_SET); + return false; + } + + in.seek(header.size - (in.pos() - position), SEEK_CUR); + return true; +} + +bool loadThumbnail(Common::SeekableReadStream &in, Graphics::Surface &to) { + ThumbnailHeader header; + + if (!loadHeader(in, header, true)) + return false; + + if (header.bpp != 2) { + warning("trying to load thumbnail with unsupported bit depth %d", header.bpp); + return false; + } + + to.create(header.width, header.height, sizeof(OverlayColor)); + + OverlayColor *pixels = (OverlayColor *)to.pixels; + for (int y = 0; y < to.h; ++y) { + for (int x = 0; x < to.w; ++x) { + uint8 r, g, b; + colorToRGB(in.readUint16BE(), r, g, b); + + // converting to current OSystem Color + *pixels++ = g_system->RGBToColor(r, g, b); + } + } + + return true; +} + +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) { + if (thumb.bytesPerPixel != 2) { + warning("trying to save thumbnail with bpp different than 2"); + return false; + } + + 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; + + out.writeUint32BE(header.type); + out.writeUint32BE(header.size); + out.writeByte(header.version); + out.writeUint16BE(header.width); + out.writeUint16BE(header.height); + out.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) + out.writeUint16BE(*pixels); + + return true; +} + +} // end of namespace Graphics + diff --git a/graphics/thumbnail.h b/graphics/thumbnail.h new file mode 100644 index 0000000000..0553306519 --- /dev/null +++ b/graphics/thumbnail.h @@ -0,0 +1,69 @@ +/* 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. + * + * $URL$ + * $Id$ + */ + +#ifndef GRAPHICS_THUMBNAIL_H +#define GRAPHICS_THUMBNAIL_H + +#include "common/stream.h" +#include "graphics/surface.h" + +namespace Graphics { + +/** + * Checks for presence of the thumbnail save header. + * Seeks automatically back to start position after check. + * + * @param in stream to check for header + */ +bool checkThumbnailHeader(Common::SeekableReadStream &in); + +/** + * Skips a thumbnail header, if present. + * + * @param in stream to process + */ +bool skipThumbnailHeader(Common::SeekableReadStream &in); + +/** + * Lodas a thumbnail from the given input stream. + * The loaded thumbnail will be automatically converted to the + * current overlay pixelformat. + */ +bool loadThumbnail(Common::SeekableReadStream &in, Graphics::Surface &to); + +/** + * Saves a thumbnail to the given write stream. + * Automatically creates a thumbnail from screen contents. + */ +bool saveThumbnail(Common::WriteStream &out); + +/** + * Saves a (given) thumbnail to the given write stream. + */ +bool saveThumbnail(Common::WriteStream &out, const Graphics::Surface &thumb); + +} // end of namespace Graphics + +#endif + |