JPEGImageDecoder.cpp [plain text]
#include "config.h"
#include "JPEGImageDecoder.h"
#include <assert.h>
#include <stdio.h> // Needed by jpeglib.h for FILE.
extern "C" {
#include "jpeglib.h"
}
#include <setjmp.h>
namespace WebCore {
struct decoder_error_mgr {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
enum jstate {
JPEG_HEADER,
JPEG_START_DECOMPRESS,
JPEG_DECOMPRESS_PROGRESSIVE,
JPEG_DECOMPRESS_SEQUENTIAL,
JPEG_DONE,
JPEG_SINK_NON_JPEG_TRAILER,
JPEG_ERROR
};
void init_source(j_decompress_ptr jd);
boolean fill_input_buffer(j_decompress_ptr jd);
void skip_input_data(j_decompress_ptr jd, long num_bytes);
void term_source(j_decompress_ptr jd);
void error_exit(j_common_ptr cinfo);
struct decoder_source_mgr {
struct jpeg_source_mgr pub;
JPEGImageReader *decoder;
};
class JPEGImageReader
{
public:
JPEGImageReader(JPEGImageDecoder* decoder)
: m_decoder(decoder)
, m_bufferLength(0)
, m_bytesToSkip(0)
, m_state(JPEG_HEADER)
, m_samples(0)
{
memset(&m_info, 0, sizeof(jpeg_decompress_struct));
m_info.err = jpeg_std_error(&m_err.pub);
m_err.pub.error_exit = error_exit;
jpeg_create_decompress(&m_info);
decoder_source_mgr* src = 0;
if (!m_info.src) {
src = (decoder_source_mgr*)fastCalloc(sizeof(decoder_source_mgr), 1);
if (!src) {
m_state = JPEG_ERROR;
return;
}
}
m_info.src = (jpeg_source_mgr*)src;
src->pub.init_source = init_source;
src->pub.fill_input_buffer = fill_input_buffer;
src->pub.skip_input_data = skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart;
src->pub.term_source = term_source;
src->decoder = this;
}
~JPEGImageReader()
{
close();
}
void close() {
decoder_source_mgr* src = (decoder_source_mgr*)m_info.src;
if (src)
fastFree(src);
m_info.src = 0;
jpeg_destroy_decompress(&m_info);
}
void skipBytes(long num_bytes) {
decoder_source_mgr* src = (decoder_source_mgr*)m_info.src;
long bytesToSkip = std::min(num_bytes, (long)src->pub.bytes_in_buffer);
src->pub.bytes_in_buffer -= (size_t)bytesToSkip;
src->pub.next_input_byte += bytesToSkip;
if (num_bytes > bytesToSkip)
m_bytesToSkip = (size_t)(num_bytes - bytesToSkip);
else
m_bytesToSkip = 0;
}
bool decode(const Vector<char>& data, bool sizeOnly) {
m_decodingSizeOnly = sizeOnly;
unsigned newByteCount = data.size() - m_bufferLength;
unsigned readOffset = m_bufferLength - m_info.src->bytes_in_buffer;
m_info.src->bytes_in_buffer += newByteCount;
m_info.src->next_input_byte = (JOCTET*)(data.data()) + readOffset;
if (m_bytesToSkip)
skipBytes(m_bytesToSkip);
m_bufferLength = data.size();
if (setjmp(m_err.setjmp_buffer)) {
m_state = JPEG_SINK_NON_JPEG_TRAILER;
close();
return false;
}
switch (m_state) {
case JPEG_HEADER:
{
if (jpeg_read_header(&m_info, true) == JPEG_SUSPENDED)
return true;
switch (m_info.jpeg_color_space) {
case JCS_GRAYSCALE:
case JCS_RGB:
case JCS_YCbCr:
m_info.out_color_space = JCS_RGB;
break;
case JCS_CMYK:
case JCS_YCCK:
default:
m_state = JPEG_ERROR;
return false;
}
m_info.buffered_image = jpeg_has_multiple_scans(&m_info);
jpeg_calc_output_dimensions(&m_info);
int row_stride = m_info.output_width * 4;
m_samples = (*m_info.mem->alloc_sarray)((j_common_ptr) &m_info,
JPOOL_IMAGE,
row_stride, 1);
m_state = JPEG_START_DECOMPRESS;
if (!m_decoder->setSize(m_info.image_width, m_info.image_height)) {
m_state = JPEG_ERROR;
return false;
}
if (m_decodingSizeOnly) {
m_bufferLength -= m_info.src->bytes_in_buffer;
m_info.src->bytes_in_buffer = 0;
return true;
}
}
case JPEG_START_DECOMPRESS:
{
m_info.dct_method = JDCT_ISLOW;
m_info.dither_mode = JDITHER_FS;
m_info.do_fancy_upsampling = true;
m_info.enable_2pass_quant = false;
m_info.do_block_smoothing = true;
if (!jpeg_start_decompress(&m_info))
return true;
m_state = (m_info.buffered_image) ? JPEG_DECOMPRESS_PROGRESSIVE : JPEG_DECOMPRESS_SEQUENTIAL;
}
case JPEG_DECOMPRESS_SEQUENTIAL:
{
if (m_state == JPEG_DECOMPRESS_SEQUENTIAL) {
if (!m_decoder->outputScanlines())
return true;
assert(m_info.output_scanline == m_info.output_height);
m_state = JPEG_DONE;
}
}
case JPEG_DECOMPRESS_PROGRESSIVE:
{
if (m_state == JPEG_DECOMPRESS_PROGRESSIVE) {
int status;
do {
status = jpeg_consume_input(&m_info);
} while ((status != JPEG_SUSPENDED) &&
(status != JPEG_REACHED_EOI));
for (;;) {
if (m_info.output_scanline == 0) {
int scan = m_info.input_scan_number;
if ((m_info.output_scan_number == 0) &&
(scan > 1) &&
(status != JPEG_REACHED_EOI))
scan--;
if (!jpeg_start_output(&m_info, scan))
return true;
}
if (m_info.output_scanline == 0xffffff)
m_info.output_scanline = 0;
if (!m_decoder->outputScanlines()) {
if (m_info.output_scanline == 0)
m_info.output_scanline = 0xffffff;
return true;
}
if (m_info.output_scanline == m_info.output_height) {
if (!jpeg_finish_output(&m_info))
return true;
if (jpeg_input_complete(&m_info) &&
(m_info.input_scan_number == m_info.output_scan_number))
break;
m_info.output_scanline = 0;
}
}
m_state = JPEG_DONE;
}
}
case JPEG_DONE:
{
if (!jpeg_finish_decompress(&m_info))
return true;
m_state = JPEG_SINK_NON_JPEG_TRAILER;
break;
}
case JPEG_SINK_NON_JPEG_TRAILER:
break;
case JPEG_ERROR:
break;
}
return true;
}
jpeg_decompress_struct* info() { return &m_info; }
JSAMPARRAY samples() const { return m_samples; }
JPEGImageDecoder* decoder() { return m_decoder; }
private:
JPEGImageDecoder* m_decoder;
unsigned m_bufferLength;
int m_bytesToSkip;
bool m_decodingSizeOnly;
bool m_initialized;
jpeg_decompress_struct m_info;
decoder_error_mgr m_err;
jstate m_state;
JSAMPARRAY m_samples;
};
void error_exit(j_common_ptr cinfo)
{
decoder_error_mgr *err = (decoder_error_mgr *) cinfo->err;
longjmp(err->setjmp_buffer, -1);
}
void init_source(j_decompress_ptr jd)
{
}
void skip_input_data(j_decompress_ptr jd, long num_bytes)
{
decoder_source_mgr *src = (decoder_source_mgr *)jd->src;
src->decoder->skipBytes(num_bytes);
}
boolean fill_input_buffer(j_decompress_ptr jd)
{
return false;
}
void term_source (j_decompress_ptr jd)
{
decoder_source_mgr *src = (decoder_source_mgr *)jd->src;
src->decoder->decoder()->jpegComplete();
}
JPEGImageDecoder::JPEGImageDecoder()
: m_reader(0)
{}
JPEGImageDecoder::~JPEGImageDecoder()
{
delete m_reader;
}
void JPEGImageDecoder::setData(SharedBuffer* data, bool allDataReceived)
{
if (m_failed)
return;
ImageDecoder::setData(data, allDataReceived);
if (!m_reader && !m_failed)
m_reader = new JPEGImageReader(this);
}
bool JPEGImageDecoder::isSizeAvailable()
{
if (!ImageDecoder::isSizeAvailable() && !failed() && m_reader)
decode(true);
return ImageDecoder::isSizeAvailable();
}
RGBA32Buffer* JPEGImageDecoder::frameBufferAtIndex(size_t index)
{
if (index)
return 0;
if (m_frameBufferCache.isEmpty())
m_frameBufferCache.resize(1);
RGBA32Buffer& frame = m_frameBufferCache[0];
if (frame.status() != RGBA32Buffer::FrameComplete && m_reader)
decode();
return &frame;
}
void JPEGImageDecoder::decode(bool sizeOnly)
{
if (m_failed)
return;
m_failed = !m_reader->decode(m_data->buffer(), sizeOnly);
if (m_failed || (!m_frameBufferCache.isEmpty() && m_frameBufferCache[0].status() == RGBA32Buffer::FrameComplete)) {
delete m_reader;
m_reader = 0;
}
}
bool JPEGImageDecoder::outputScanlines()
{
if (m_frameBufferCache.isEmpty())
return false;
RGBA32Buffer& buffer = m_frameBufferCache[0];
if (buffer.status() == RGBA32Buffer::FrameEmpty) {
if (!buffer.setSize(size().width(), size().height())) {
m_failed = true;
return false;
}
buffer.setStatus(RGBA32Buffer::FramePartial);
buffer.setHasAlpha(false);
buffer.setRect(IntRect(IntPoint(), size()));
}
jpeg_decompress_struct* info = m_reader->info();
JSAMPARRAY samples = m_reader->samples();
while (info->output_scanline < info->output_height) {
if (jpeg_read_scanlines(info, samples, 1) != 1)
return false;
JSAMPLE *j1 = samples[0];
for (unsigned x = 0; x < info->output_width; ++x) {
unsigned r = *j1++;
unsigned g = *j1++;
unsigned b = *j1++;
buffer.setRGBA(x, info->output_scanline - 1, r, g, b, 0xFF);
}
}
return true;
}
void JPEGImageDecoder::jpegComplete()
{
if (m_frameBufferCache.isEmpty())
return;
RGBA32Buffer& buffer = m_frameBufferCache[0];
buffer.setStatus(RGBA32Buffer::FrameComplete);
}
}