2009-08-21 13:52:43 +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.
|
|
|
|
*
|
|
|
|
* 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.
|
2014-02-18 01:34:20 +00:00
|
|
|
*
|
2009-08-21 13:52:43 +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.
|
2014-02-18 01:34:20 +00:00
|
|
|
*
|
2009-08-21 13:52:43 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
// libjpeg uses forbidden symbols in its header. Thus, we need to allow them
|
|
|
|
// here.
|
|
|
|
#define FORBIDDEN_SYMBOL_ALLOW_ALL
|
|
|
|
|
2014-02-28 02:27:23 +00:00
|
|
|
#include "image/jpeg.h"
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/debug.h"
|
2009-08-21 13:52:43 +00:00
|
|
|
#include "common/endian.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"
|
2014-02-28 02:27:23 +00:00
|
|
|
#include "graphics/pixelformat.h"
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
#ifdef USE_JPEG
|
|
|
|
// The original release of libjpeg v6b did not contain any extern "C" in case
|
|
|
|
// its header files are included in a C++ environment. To avoid any linking
|
|
|
|
// issues we need to add it on our own.
|
|
|
|
extern "C" {
|
|
|
|
#include <jpeglib.h>
|
|
|
|
#include <jerror.h>
|
|
|
|
}
|
|
|
|
#endif
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2014-02-28 02:27:23 +00:00
|
|
|
namespace Image {
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 22:53:34 +00:00
|
|
|
JPEGDecoder::JPEGDecoder() : ImageDecoder(), _surface(), _colorSpace(kColorSpaceRGBA) {
|
2009-08-21 13:52:43 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 02:05:45 +00:00
|
|
|
JPEGDecoder::~JPEGDecoder() {
|
|
|
|
destroy();
|
2009-08-21 13:52:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 02:27:23 +00:00
|
|
|
const Graphics::Surface *JPEGDecoder::getSurface() const {
|
2013-08-11 22:53:34 +00:00
|
|
|
return &_surface;
|
2011-01-18 16:18:10 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 02:05:45 +00:00
|
|
|
void JPEGDecoder::destroy() {
|
2013-08-11 22:53:34 +00:00
|
|
|
_surface.free();
|
2009-08-21 13:52:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
#ifdef USE_JPEG
|
|
|
|
namespace {
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
#define JPEG_BUFFER_SIZE 4096
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
struct StreamSource : public jpeg_source_mgr {
|
|
|
|
Common::SeekableReadStream *stream;
|
|
|
|
bool startOfFile;
|
|
|
|
JOCTET buffer[JPEG_BUFFER_SIZE];
|
|
|
|
};
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
void initSource(j_decompress_ptr cinfo) {
|
|
|
|
StreamSource *source = (StreamSource *)cinfo->src;
|
|
|
|
source->startOfFile = true;
|
2009-08-21 13:52:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
boolean fillInputBuffer(j_decompress_ptr cinfo) {
|
|
|
|
StreamSource *source = (StreamSource *)cinfo->src;
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
uint32 bufferSize = source->stream->read((byte *)source->buffer, sizeof(source->buffer));
|
|
|
|
if (bufferSize == 0) {
|
|
|
|
if (source->startOfFile) {
|
|
|
|
// An empty file is a fatal error
|
|
|
|
ERREXIT(cinfo, JERR_INPUT_EMPTY);
|
|
|
|
} else {
|
|
|
|
// Otherwise we insert an EOF marker
|
|
|
|
WARNMS(cinfo, JWRN_JPEG_EOF);
|
|
|
|
source->buffer[0] = (JOCTET)0xFF;
|
|
|
|
source->buffer[1] = (JOCTET)JPEG_EOI;
|
|
|
|
bufferSize = 2;
|
2009-08-21 13:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
source->next_input_byte = source->buffer;
|
|
|
|
source->bytes_in_buffer = bufferSize;
|
|
|
|
source->startOfFile = false;
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2012-02-08 08:45:50 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
void skipInputData(j_decompress_ptr cinfo, long numBytes) {
|
|
|
|
StreamSource *source = (StreamSource *)cinfo->src;
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
if (numBytes > 0) {
|
|
|
|
if (numBytes > (long)source->bytes_in_buffer) {
|
|
|
|
// In case we need to skip more bytes than there are in the buffer
|
|
|
|
// we will skip the remaining data and fill the buffer again
|
|
|
|
numBytes -= (long)source->bytes_in_buffer;
|
2012-02-08 08:45:50 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
// Skip the remaining bytes
|
|
|
|
source->stream->skip(numBytes);
|
2012-02-08 08:45:50 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
// Fill up the buffer again
|
|
|
|
(*source->fill_input_buffer)(cinfo);
|
|
|
|
} else {
|
|
|
|
source->next_input_byte += (size_t)numBytes;
|
|
|
|
source->bytes_in_buffer -= (size_t)numBytes;
|
2012-02-08 08:45:50 +00:00
|
|
|
}
|
|
|
|
|
2009-12-24 08:26:13 +00:00
|
|
|
}
|
2009-08-21 13:52:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
void termSource(j_decompress_ptr cinfo) {
|
2009-08-21 13:52:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
void jpeg_scummvm_src(j_decompress_ptr cinfo, Common::SeekableReadStream *stream) {
|
|
|
|
StreamSource *source;
|
2012-02-08 08:45:50 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
// Initialize the source in case it has not been done yet.
|
|
|
|
if (cinfo->src == NULL) {
|
|
|
|
cinfo->src = (jpeg_source_mgr *)(*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT, sizeof(StreamSource));
|
2012-02-08 08:45:50 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
source = (StreamSource *)cinfo->src;
|
|
|
|
source->init_source = &initSource;
|
|
|
|
source->fill_input_buffer = &fillInputBuffer;
|
|
|
|
source->skip_input_data = &skipInputData;
|
|
|
|
source->resync_to_restart = &jpeg_resync_to_restart;
|
|
|
|
source->term_source = &termSource;
|
|
|
|
source->bytes_in_buffer = 0;
|
|
|
|
source->next_input_byte = NULL;
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
source->stream = stream;
|
|
|
|
}
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
void errorExit(j_common_ptr cinfo) {
|
|
|
|
char buffer[JMSG_LENGTH_MAX];
|
|
|
|
(*cinfo->err->format_message)(cinfo, buffer);
|
|
|
|
// This function is not allowed to return to the caller, thus we simply
|
|
|
|
// error out with our error handling here.
|
|
|
|
error("%s", buffer);
|
2009-08-21 13:52:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
void outputMessage(j_common_ptr cinfo) {
|
|
|
|
char buffer[JMSG_LENGTH_MAX];
|
|
|
|
(*cinfo->err->format_message)(cinfo, buffer);
|
|
|
|
// Is using debug here a good idea? Or do we want to ignore all libjpeg
|
|
|
|
// messages?
|
|
|
|
debug(3, "libjpeg: %s", buffer);
|
2012-02-09 14:59:19 +00:00
|
|
|
}
|
2011-02-06 13:43:40 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
} // End of anonymous namespace
|
|
|
|
#endif
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
bool JPEGDecoder::loadStream(Common::SeekableReadStream &stream) {
|
|
|
|
#ifdef USE_JPEG
|
|
|
|
// Reset member variables from previous decodings
|
|
|
|
destroy();
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
jpeg_decompress_struct cinfo;
|
|
|
|
jpeg_error_mgr jerr;
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
// Initialize error handling callbacks
|
|
|
|
cinfo.err = jpeg_std_error(&jerr);
|
|
|
|
cinfo.err->error_exit = &errorExit;
|
|
|
|
cinfo.err->output_message = &outputMessage;
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
// Initialize the decompression structure
|
|
|
|
jpeg_create_decompress(&cinfo);
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
// Initialize our buffer handling
|
|
|
|
jpeg_scummvm_src(&cinfo, &stream);
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
// Read the file header
|
|
|
|
jpeg_read_header(&cinfo, TRUE);
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 22:53:34 +00:00
|
|
|
// We can request YUV output because Groovie requires it
|
|
|
|
switch (_colorSpace) {
|
|
|
|
case kColorSpaceRGBA:
|
|
|
|
cinfo.out_color_space = JCS_RGB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kColorSpaceYUV:
|
|
|
|
cinfo.out_color_space = JCS_YCbCr;
|
|
|
|
break;
|
|
|
|
}
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
// Actually start decompressing the image
|
|
|
|
jpeg_start_decompress(&cinfo);
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 22:53:34 +00:00
|
|
|
// Allocate buffers for the output data
|
|
|
|
switch (_colorSpace) {
|
|
|
|
case kColorSpaceRGBA:
|
|
|
|
// We use RGBA8888 in this scenario
|
|
|
|
_surface.create(cinfo.output_width, cinfo.output_height, Graphics::PixelFormat(4, 8, 8, 8, 0, 24, 16, 8, 0));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kColorSpaceYUV:
|
|
|
|
// We use YUV with 3 bytes per pixel otherwise.
|
|
|
|
// This is pretty ugly since our PixelFormat cannot express YUV...
|
|
|
|
_surface.create(cinfo.output_width, cinfo.output_height, Graphics::PixelFormat(3, 0, 0, 0, 0, 0, 0, 0, 0));
|
|
|
|
break;
|
|
|
|
}
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
// Allocate buffer for one scanline
|
2013-08-18 16:21:19 +00:00
|
|
|
assert(cinfo.output_components == 3);
|
2013-08-11 21:36:57 +00:00
|
|
|
JDIMENSION pitch = cinfo.output_width * cinfo.output_components;
|
2013-08-18 16:21:19 +00:00
|
|
|
assert(_surface.pitch >= pitch);
|
2013-08-11 21:36:57 +00:00
|
|
|
JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, pitch, 1);
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
// Go through the image data scanline by scanline
|
|
|
|
while (cinfo.output_scanline < cinfo.output_height) {
|
2013-08-11 22:53:34 +00:00
|
|
|
byte *dst = (byte *)_surface.getBasePtr(0, cinfo.output_scanline);
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
jpeg_read_scanlines(&cinfo, buffer, 1);
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
const byte *src = buffer[0];
|
2013-08-11 22:53:34 +00:00
|
|
|
switch (_colorSpace) {
|
|
|
|
case kColorSpaceRGBA: {
|
|
|
|
for (int remaining = cinfo.output_width; remaining > 0; --remaining) {
|
|
|
|
byte r = *src++;
|
|
|
|
byte g = *src++;
|
|
|
|
byte b = *src++;
|
|
|
|
// We need to insert a alpha value of 255 (opaque) here.
|
|
|
|
#ifdef SCUMM_BIG_ENDIAN
|
|
|
|
*dst++ = r;
|
|
|
|
*dst++ = g;
|
|
|
|
*dst++ = b;
|
|
|
|
*dst++ = 0xFF;
|
|
|
|
#else
|
|
|
|
*dst++ = 0xFF;
|
|
|
|
*dst++ = b;
|
|
|
|
*dst++ = g;
|
|
|
|
*dst++ = r;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case kColorSpaceYUV:
|
2013-08-18 16:21:19 +00:00
|
|
|
memcpy(dst, src, pitch);
|
2013-08-11 22:53:34 +00:00
|
|
|
break;
|
2009-08-21 13:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
// We are done with decompressing, thus free all the data
|
|
|
|
jpeg_finish_decompress(&cinfo);
|
|
|
|
jpeg_destroy_decompress(&cinfo);
|
2009-08-21 13:52:43 +00:00
|
|
|
|
2013-08-11 21:36:57 +00:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2009-08-21 13:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End of Graphics namespace
|