1
/****************************************************************************
3
** This file is based on sources of the Qt GUI Toolkit, used under the terms
4
** of the GNU General Public License version 2 (see the original copyright
6
** All further contributions to this file are (and are required to be)
7
** licensed under the terms of the GNU General Public License as published by
8
** the Free Software Foundation; either version 2 of the License, or
9
** (at your option) any later version.
11
** The original Qt license header follows:
14
** Implementation of JPEG QImage IOHandler
18
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
20
** This file is part of the kernel module of the Qt GUI Toolkit.
22
** This file may be used under the terms of the GNU General
23
** Public License versions 2.0 or 3.0 as published by the Free
24
** Software Foundation and appearing in the files LICENSE.GPL2
25
** and LICENSE.GPL3 included in the packaging of this file.
26
** Alternatively you may (at your option) use any later version
27
** of the GNU General Public License if such license has been
28
** publicly approved by Trolltech ASA (or its successors, if any)
29
** and the KDE Free Qt Foundation.
31
** Please review the following information to ensure GNU General
32
** Public Licensing requirements will be met:
33
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
34
** If you are unsure which license is appropriate for your use, please
35
** review the following information:
36
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
37
** or contact the sales department at sales@trolltech.com.
39
** This file may be used under the terms of the Q Public License as
40
** defined by Trolltech ASA and appearing in the file LICENSE.QPL
41
** included in the packaging of this file. Licensees holding valid Qt
42
** Commercial licenses may use this file in accordance with the Qt
43
** Commercial License Agreement provided with the Software.
45
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
46
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
47
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
50
**********************************************************************/
52
#ifndef QT_CLEAN_NAMESPACE
53
#define QT_CLEAN_NAMESPACE
59
#ifndef QT_NO_IMAGEIO_JPEG
61
#include <stdio.h> // jpeglib needs this to be pre-included
65
// including jpeglib.h seems to be a little messy
67
#define XMD_H // shut JPEGlib up
68
#if defined(Q_OS_UNIXWARE)
69
# define HAVE_BOOLEAN // libjpeg under Unixware seems to need this
73
# undef const // remove crazy C hackery in jconfig.h
78
struct my_error_mgr : public jpeg_error_mgr {
79
jmp_buf setjmp_buffer;
82
#if defined(Q_C_CALLBACKS)
87
void my_error_exit (j_common_ptr cinfo)
89
my_error_mgr* myerr = (my_error_mgr*) cinfo->err;
90
char buffer[JMSG_LENGTH_MAX];
91
(*cinfo->err->format_message)(cinfo, buffer);
93
longjmp(myerr->setjmp_buffer, 1);
96
#if defined(Q_C_CALLBACKS)
101
static const int max_buf = 4096;
103
struct my_jpeg_source_mgr : public jpeg_source_mgr {
104
// Nothing dynamic - cannot rely on destruction over longjump
106
JOCTET buffer[max_buf];
109
my_jpeg_source_mgr(FILE* f);
112
#if defined(Q_C_CALLBACKS)
117
void qt_init_source(j_decompress_ptr)
122
boolean qt_fill_input_buffer(j_decompress_ptr cinfo)
125
my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src;
126
src->next_input_byte = src->buffer;
127
num_read = fread( (char*)src->buffer, 1, max_buf, src->f );
129
if ( num_read <= 0 ) {
130
// Insert a fake EOI marker - as per jpeglib recommendation
131
src->buffer[0] = (JOCTET) 0xFF;
132
src->buffer[1] = (JOCTET) JPEG_EOI;
133
src->bytes_in_buffer = 2;
135
src->bytes_in_buffer = num_read;
137
#if defined(Q_OS_UNIXWARE)
145
void qt_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
147
my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src;
149
// `dumb' implementation from jpeglib
151
/* Just a dumb implementation for now. Could use fseek() except
152
* it doesn't work on pipes. Not clear that being smart is worth
153
* any trouble anyway --- large skips are infrequent.
156
while (num_bytes > (long) src->bytes_in_buffer) {
157
num_bytes -= (long) src->bytes_in_buffer;
158
(void) qt_fill_input_buffer(cinfo);
159
/* note we assume that qt_fill_input_buffer will never return FALSE,
160
* so suspension need not be handled.
163
src->next_input_byte += (size_t) num_bytes;
164
src->bytes_in_buffer -= (size_t) num_bytes;
169
void qt_term_source(j_decompress_ptr)
173
#if defined(Q_C_CALLBACKS)
178
inline my_jpeg_source_mgr::my_jpeg_source_mgr(FILE* fptr)
180
jpeg_source_mgr::init_source = qt_init_source;
181
jpeg_source_mgr::fill_input_buffer = qt_fill_input_buffer;
182
jpeg_source_mgr::skip_input_data = qt_skip_input_data;
183
jpeg_source_mgr::resync_to_restart = jpeg_resync_to_restart;
184
jpeg_source_mgr::term_source = qt_term_source;
187
next_input_byte = buffer;
191
QImage splash_read_jpeg_image(FILE* f)
195
struct jpeg_decompress_struct cinfo;
197
struct my_jpeg_source_mgr *iod_src = new my_jpeg_source_mgr(f);
198
struct my_error_mgr jerr;
200
jpeg_create_decompress(&cinfo);
204
cinfo.err = jpeg_std_error(&jerr);
205
jerr.error_exit = my_error_exit;
207
if (!setjmp(jerr.setjmp_buffer)) {
208
#if defined(Q_OS_UNIXWARE)
209
(void) jpeg_read_header(&cinfo, B_TRUE);
211
(void) jpeg_read_header(&cinfo, TRUE);
214
(void) jpeg_start_decompress(&cinfo);
217
bool created = FALSE;
218
if ( cinfo.output_components == 3 || cinfo.output_components == 4) {
219
created = image.create( cinfo.output_width, cinfo.output_height, 32 );
220
} else if ( cinfo.output_components == 1 ) {
221
created = image.create( cinfo.output_width, cinfo.output_height, 8, 256 );
222
for (int i=0; i<256; i++)
223
image.setColor(i, qRgb(i,i,i));
225
// Unsupported format
230
if (!image.isNull()) {
231
uchar** lines = image.jumpTable();
232
while (cinfo.output_scanline < cinfo.output_height)
233
(void) jpeg_read_scanlines(&cinfo,
234
lines + cinfo.output_scanline,
235
cinfo.output_height);
236
(void) jpeg_finish_decompress(&cinfo);
238
if ( cinfo.output_components == 3 ) {
239
// Expand 24->32 bpp.
240
for (uint j=0; j<cinfo.output_height; j++) {
241
uchar *in = image.scanLine(j) + cinfo.output_width * 3;
242
QRgb *out = (QRgb*)image.scanLine(j);
244
for (uint i=cinfo.output_width; i--; ) {
246
out[i] = qRgb(in[0], in[1], in[2]);
253
if (!image.isNull()) {
254
if ( cinfo.density_unit == 1 ) {
255
image.setDotsPerMeterX( int(100. * cinfo.X_density / 2.54) );
256
image.setDotsPerMeterY( int(100. * cinfo.Y_density / 2.54) );
257
} else if ( cinfo.density_unit == 2 ) {
258
image.setDotsPerMeterX( int(100. * cinfo.X_density) );
259
image.setDotsPerMeterY( int(100. * cinfo.Y_density) );
265
jpeg_destroy_decompress(&cinfo);