📄 loader_jpeg.cpp
字号:
/* This file is part of the KDE libraries Copyright (C) 2000 Dirk Mueller (mueller@kde.org) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/#ifdef HAVE_CONFIG_H#include <config.h>#endif#ifdef HAVE_LIBJPEG// on some systems, libjpeg installs its config.h file which causes a conflict// and makes the compiler barf with "XXX already defined".#ifdef HAVE_STDLIB_H#undef HAVE_STDLIB_H#endif#include "loader_jpeg.h"#include <stdio.h>#include <setjmp.h>#include <qdatetime.h>#include <kglobal.h>extern "C" {#define XMD_H#include <jpeglib.h>#undef const}#undef BUFFER_DEBUG//#define BUFFER_DEBUG#undef JPEG_DEBUG//#define JPEG_DEBUG// -----------------------------------------------------------------------------struct khtml_error_mgr : public jpeg_error_mgr { jmp_buf setjmp_buffer;};extern "C" { static void khtml_error_exit (j_common_ptr cinfo) { khtml_error_mgr* myerr = (khtml_error_mgr*) cinfo->err; char buffer[JMSG_LENGTH_MAX]; (*cinfo->err->format_message)(cinfo, buffer);#ifdef JPEG_DEBUG qWarning("%s", buffer);#endif longjmp(myerr->setjmp_buffer, 1); }}static const int max_buf = 32768;static const int max_consumingtime = 2000;struct khtml_jpeg_source_mgr : public jpeg_source_mgr { JOCTET buffer[max_buf]; int valid_buffer_len; size_t skip_input_bytes; int ateof; QRect change_rect; QRect old_change_rect; QTime decoder_timestamp; bool final_pass; bool decoding_done; bool do_progressive;public: khtml_jpeg_source_mgr() KDE_NO_EXPORT;};extern "C" { static void khtml_j_decompress_dummy(j_decompress_ptr) { } static boolean khtml_fill_input_buffer(j_decompress_ptr cinfo) {#ifdef BUFFER_DEBUG qDebug("khtml_fill_input_buffer called!");#endif khtml_jpeg_source_mgr* src = (khtml_jpeg_source_mgr*)cinfo->src; if ( src->ateof ) { /* Insert a fake EOI marker - as per jpeglib recommendation */ src->buffer[0] = (JOCTET) 0xFF; src->buffer[1] = (JOCTET) JPEG_EOI; src->bytes_in_buffer = 2; src->next_input_byte = (JOCTET *) src->buffer;#ifdef BUFFER_DEBUG qDebug("...returning true!");#endif return true; } else return false; /* I/O suspension mode */ } static void khtml_skip_input_data(j_decompress_ptr cinfo, long num_bytes) { if(num_bytes <= 0) return; /* required noop */#ifdef BUFFER_DEBUG qDebug("khtml_skip_input_data (%d) called!", num_bytes);#endif khtml_jpeg_source_mgr* src = (khtml_jpeg_source_mgr*)cinfo->src; src->skip_input_bytes += num_bytes; unsigned int skipbytes = kMin(src->bytes_in_buffer, src->skip_input_bytes);#ifdef BUFFER_DEBUG qDebug("skip_input_bytes is now %d", src->skip_input_bytes); qDebug("skipbytes is now %d", skipbytes); qDebug("valid_buffer_len is before %d", src->valid_buffer_len); qDebug("bytes_in_buffer is %d", src->bytes_in_buffer);#endif if(skipbytes < src->bytes_in_buffer) memmove(src->buffer, src->next_input_byte+skipbytes, src->bytes_in_buffer - skipbytes); src->bytes_in_buffer -= skipbytes; src->valid_buffer_len = src->bytes_in_buffer; src->skip_input_bytes -= skipbytes; /* adjust data for jpeglib */ cinfo->src->next_input_byte = (JOCTET *) src->buffer; cinfo->src->bytes_in_buffer = (size_t) src->valid_buffer_len;#ifdef BUFFER_DEBUG qDebug("valid_buffer_len is afterwards %d", src->valid_buffer_len); qDebug("skip_input_bytes is now %d", src->skip_input_bytes);#endif }}khtml_jpeg_source_mgr::khtml_jpeg_source_mgr(){ jpeg_source_mgr::init_source = khtml_j_decompress_dummy; jpeg_source_mgr::fill_input_buffer = khtml_fill_input_buffer; jpeg_source_mgr::skip_input_data = khtml_skip_input_data; jpeg_source_mgr::resync_to_restart = jpeg_resync_to_restart; jpeg_source_mgr::term_source = khtml_j_decompress_dummy; bytes_in_buffer = 0; valid_buffer_len = 0; skip_input_bytes = 0; ateof = 0; next_input_byte = buffer; final_pass = false; decoding_done = false;}// -----------------------------------------------------------------------------class KJPEGFormat : public QImageFormat{public: KJPEGFormat(); virtual ~KJPEGFormat(); virtual int decode(QImage& img, QImageConsumer* consumer, const uchar* buffer, int length);private: enum { Init, readHeader, startDecompress, decompressStarted, consumeInput, prepareOutputScan, doOutputScan, readDone, invalid } state; // structs for the jpeglib struct jpeg_decompress_struct cinfo; struct khtml_error_mgr jerr; struct khtml_jpeg_source_mgr jsrc;};// -----------------------------------------------------------------------------KJPEGFormat::KJPEGFormat(){ memset(&cinfo, 0, sizeof(cinfo)); cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); cinfo.err = jpeg_std_error(&jerr); jerr.error_exit = khtml_error_exit; cinfo.src = &jsrc; state = Init;}KJPEGFormat::~KJPEGFormat(){ (void) jpeg_destroy_decompress(&cinfo);}/* * return > 0 means "consumed x bytes, need more" * return == 0 means "end of frame reached" * return < 0 means "fatal error in image decoding, don't call me ever again" */int KJPEGFormat::decode(QImage& image, QImageConsumer* consumer, const uchar* buffer, int length){#ifdef JPEG_DEBUG qDebug("KJPEGFormat::decode(%08lx, %08lx, %08lx, %d)", &image, consumer, buffer, length);#endif if(jsrc.ateof) {#ifdef JPEG_DEBUG qDebug("ateof, eating");#endif return length; } if(setjmp(jerr.setjmp_buffer)) {#ifdef JPEG_DEBUG qDebug("jump into state invalid");#endif if(consumer) consumer->end(); // this is fatal return -1; } int consumed = kMin(length, max_buf - jsrc.valid_buffer_len);#ifdef BUFFER_DEBUG qDebug("consuming %d bytes", consumed);#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -