Skip to content

Commit

Permalink
IMAGE: added GIFDecoder wrapping stb_image
Browse files Browse the repository at this point in the history
  • Loading branch information
mgerhardy committed Apr 4, 2021
1 parent 5fe4209 commit 214bf0f
Show file tree
Hide file tree
Showing 7 changed files with 7,950 additions and 2 deletions.
92 changes: 92 additions & 0 deletions image/gif.cpp
@@ -0,0 +1,92 @@
/* 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.
*
*/

#include "image/gif.h"

#include "common/stream.h"
#include "common/textconsole.h"
#include "common/util.h"
#include "graphics/surface.h"

#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_GIF
#define STBI_NO_STDIO
#include "image/stb_image.h"

namespace Image {

GIFDecoder::GIFDecoder() : _outputSurface(0) {
}

GIFDecoder::~GIFDecoder() {
destroy();
}

bool GIFDecoder::loadStream(Common::SeekableReadStream &stream) {
destroy();
const int32 size = stream.size();
if (size == -1) {
return false;
}
uint8 *buf = (uint8 *)malloc(size);
if ((uint32)size != stream.read(buf, size)) {
return false;
}
int w = 0;
int h = 0;
int n = 0;
int z = 0;
int *delays = 0;
uint8 *data = stbi_load_gif_from_memory(buf, size, &delays, &w, &h, &z, &n, 0);
if (data == 0) {
return false;
}

if (z > 1) {
stbi_image_free(data);
warning("GIF has more than 1 frame: %i", z);
return false;
}
_outputSurface = new Graphics::Surface();
_outputSurface->create(w, h, getByteOrderRgbaPixelFormat());
memcpy(_outputSurface->getBasePtr(0, 0), data, w * h * _outputSurface->format.bytesPerPixel);
stbi_image_free(data);
return true;
}

Graphics::PixelFormat GIFDecoder::getByteOrderRgbaPixelFormat() const {
#ifdef SCUMM_BIG_ENDIAN
return Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
#else
return Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24);
#endif
}

void GIFDecoder::destroy() {
if (_outputSurface) {
_outputSurface->free();
delete _outputSurface;
_outputSurface = 0;
}
}

} // End of namespace Image
65 changes: 65 additions & 0 deletions image/gif.h
@@ -0,0 +1,65 @@
/* 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.
*
*/

#ifndef IMAGE_GIF_H
#define IMAGE_GIF_H

#include "graphics/pixelformat.h"
#include "image/image_decoder.h"

namespace Common {
class SeekableReadStream;
}

namespace Graphics {
struct Surface;
}

namespace Image {

/**
* @defgroup image_gif GIF decoder
* @ingroup image
*
* @brief Decoder for images encoded as Graphics Interchange Format (GIF).
*
* Used in engines:
* - TwinE
* @{
*/
class GIFDecoder : public ImageDecoder {
public:
GIFDecoder();
~GIFDecoder();

bool loadStream(Common::SeekableReadStream &stream) override;
void destroy() override;
const Graphics::Surface *getSurface() const override { return _outputSurface; }
private:
Graphics::Surface *_outputSurface;
Graphics::PixelFormat getByteOrderRgbaPixelFormat() const;
};

/** @} */
} // End of namespace Image

#endif
1 change: 1 addition & 0 deletions image/module.mk
Expand Up @@ -3,6 +3,7 @@ MODULE := image
MODULE_OBJS := \
bmp.o \
cel_3do.o \
gif.o \
iff.o \
jpeg.o \
pcx.o \
Expand Down

0 comments on commit 214bf0f

Please sign in to comment.