1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
3 Gwenview: an image viewer
4 Copyright 2008 Aurélien Gâteau <aurelien.gateau@free.fr>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
22 #include "iodevicejpegsourcemanager.h"
40 namespace IODeviceJpegSourceManager
{
42 #define SOURCE_MANAGER_BUFFER_SIZE 4096
43 struct IODeviceJpegSourceManager
: public jpeg_source_mgr
{
45 JOCTET mBuffer
[SOURCE_MANAGER_BUFFER_SIZE
];
49 static boolean
fill_input_buffer(j_decompress_ptr cinfo
) {
50 IODeviceJpegSourceManager
* src
= static_cast<IODeviceJpegSourceManager
*>(cinfo
->src
);
51 Q_ASSERT(src
->mIODevice
);
52 int readSize
= src
->mIODevice
->read((char*)src
->mBuffer
, SOURCE_MANAGER_BUFFER_SIZE
);
54 src
->next_input_byte
= src
->mBuffer
;
55 src
->bytes_in_buffer
= readSize
;
58 * JPEG file is broken. We feed the decoder with fake EOI, as specified
59 * in the libjpeg documentation.
61 static JOCTET fakeEOI
[2] = { JOCTET(0xFF), JOCTET(JPEG_EOI
)};
62 kWarning() << "Image is incomplete";
63 cinfo
->src
->next_input_byte
= fakeEOI
;
64 cinfo
->src
->bytes_in_buffer
= 2;
70 static void init_source(j_decompress_ptr cinfo
) {
71 fill_input_buffer(cinfo
);
75 static void skip_input_data(j_decompress_ptr cinfo
, long num_bytes
) {
77 while (num_bytes
> (long) cinfo
->src
->bytes_in_buffer
) {
78 num_bytes
-= (long) cinfo
->src
->bytes_in_buffer
;
79 fill_input_buffer(cinfo
);
81 * we assume that fill_input_buffer will never return FALSE, so
82 * suspension need not be handled.
85 cinfo
->src
->next_input_byte
+= (size_t) num_bytes
;
86 cinfo
->src
->bytes_in_buffer
-= (size_t) num_bytes
;
91 static void term_source(j_decompress_ptr
) {
95 void setup(j_decompress_ptr cinfo
, QIODevice
* ioDevice
) {
96 Q_ASSERT(!cinfo
->src
);
97 IODeviceJpegSourceManager
* src
= (IODeviceJpegSourceManager
*)
98 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
99 sizeof(IODeviceJpegSourceManager
));
102 src
->init_source
= init_source
;
103 src
->fill_input_buffer
= fill_input_buffer
;
104 src
->skip_input_data
= skip_input_data
;
105 src
->resync_to_restart
= jpeg_resync_to_restart
;
106 src
->term_source
= term_source
;
108 src
->mIODevice
= ioDevice
;
112 } // IODeviceJpegSourceManager namespace
113 } // Gwenview namespace