]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
wchar_t byte-swapping is not necessary if __STDC_ISO_10646__ is defined.
[wxWidgets.git] / src / common / imagjpeg.cpp
CommitLineData
e9c4b1a2
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: imagjpeg.cpp
3// Purpose: wxImage JPEG handler
b59ff3c9 4// Author: Vaclav Slavik
e9c4b1a2 5// RCS-ID: $Id$
b59ff3c9 6// Copyright: (c) Vaclav Slavik
e9c4b1a2
JS
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
1aaaa712
VS
10#ifdef __GNUG__
11#pragma implementation "imagjpeg.h"
12#endif
e9c4b1a2
JS
13
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18#pragma hdrstop
19#endif
20
ce4169a4
RR
21#include "wx/defs.h"
22
9ab6ee85 23#if wxUSE_LIBJPEG
ce4169a4 24
1aaaa712 25#include "wx/imagjpeg.h"
e9c4b1a2
JS
26#include "wx/bitmap.h"
27#include "wx/debug.h"
28#include "wx/log.h"
29#include "wx/app.h"
995612e2
VZ
30extern "C"
31{
f6bcfd97
BP
32#ifdef __WATCOMC__
33 #define HAVE_BOOLEAN
34#endif
995612e2 35 #include "jpeglib.h"
e9c4b1a2 36}
e9c4b1a2
JS
37#include "wx/filefn.h"
38#include "wx/wfstream.h"
39#include "wx/intl.h"
40#include "wx/module.h"
41
42// For memcpy
43#include <string.h>
b59ff3c9
VS
44// For JPEG library error handling
45#include <setjmp.h>
e9c4b1a2
JS
46
47#ifdef __SALFORDC__
48#ifdef FAR
49#undef FAR
50#endif
51#endif
52
53#ifdef __WXMSW__
54#include <windows.h>
55#endif
56
57//-----------------------------------------------------------------------------
58// wxJPEGHandler
59//-----------------------------------------------------------------------------
60
e9c4b1a2 61IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
e9c4b1a2 62
9ab6ee85
GRG
63#if wxUSE_STREAMS
64
e9c4b1a2
JS
65//------------- JPEG Data Source Manager
66
1aaaa712
VS
67#define JPEG_IO_BUFFER_SIZE 2048
68
e9c4b1a2
JS
69typedef struct {
70 struct jpeg_source_mgr pub; /* public fields */
995612e2 71
e9c4b1a2 72 JOCTET* buffer; /* start of buffer */
1aaaa712 73 wxInputStream *stream;
e9c4b1a2
JS
74} my_source_mgr;
75
76typedef my_source_mgr * my_src_ptr;
77
74e3313b 78METHODDEF(void) my_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
e9c4b1a2
JS
79{
80}
81
1aaaa712 82METHODDEF(boolean) my_fill_input_buffer ( j_decompress_ptr cinfo )
e9c4b1a2 83{
1aaaa712
VS
84 my_src_ptr src = (my_src_ptr) cinfo->src;
85
86 src->pub.next_input_byte = src->buffer;
87 src->pub.bytes_in_buffer = src->stream->Read(src->buffer, JPEG_IO_BUFFER_SIZE).LastRead();
88
89 if (src->pub.bytes_in_buffer == 0) // check for end-of-stream
90 {
91 // Insert a fake EOI marker
92 src->buffer[0] = 0xFF;
93 src->buffer[1] = JPEG_EOI;
94 src->pub.bytes_in_buffer = 2;
95 }
e9c4b1a2
JS
96 return TRUE;
97}
98
99METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
100{
1aaaa712
VS
101 if (num_bytes > 0)
102 {
103 my_src_ptr src = (my_src_ptr) cinfo->src;
104
105 while (num_bytes > (long)src->pub.bytes_in_buffer)
106 {
107 num_bytes -= (long) src->pub.bytes_in_buffer;
108 src->pub.fill_input_buffer(cinfo);
109 }
110 src->pub.next_input_byte += (size_t) num_bytes;
111 src->pub.bytes_in_buffer -= (size_t) num_bytes;
112 }
e9c4b1a2
JS
113}
114
115METHODDEF(void) my_term_source ( j_decompress_ptr cinfo )
116{
117 my_src_ptr src = (my_src_ptr) cinfo->src;
995612e2 118
1aaaa712 119 if (src->pub.bytes_in_buffer > 0)
3ca6a5f0 120 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
1aaaa712 121 delete[] src->buffer;
e9c4b1a2
JS
122}
123
124void jpeg_wxio_src( j_decompress_ptr cinfo, wxInputStream& infile )
125{
126 my_src_ptr src;
995612e2
VZ
127
128 if (cinfo->src == NULL) { /* first time for this JPEG object? */
e9c4b1a2
JS
129 cinfo->src = (struct jpeg_source_mgr *)
130 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
131 sizeof(my_source_mgr));
132 src = (my_src_ptr) cinfo->src;
133 }
134 src = (my_src_ptr) cinfo->src;
1aaaa712
VS
135 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
136 src->buffer = new JOCTET[JPEG_IO_BUFFER_SIZE];
137 src->pub.next_input_byte = NULL; /* until buffer loaded */
138 src->stream = &infile;
995612e2 139
e9c4b1a2
JS
140 src->pub.init_source = my_init_source;
141 src->pub.fill_input_buffer = my_fill_input_buffer;
142 src->pub.skip_input_data = my_skip_input_data;
143 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
144 src->pub.term_source = my_term_source;
145}
146
147
b59ff3c9
VS
148// JPEG error manager:
149
150struct my_error_mgr {
995612e2 151 struct jpeg_error_mgr pub; /* "public" fields */
b59ff3c9 152
995612e2 153 jmp_buf setjmp_buffer; /* for return to caller */
b59ff3c9
VS
154};
155
156typedef struct my_error_mgr * my_error_ptr;
157
158/*
159 * Here's the routine that will replace the standard error_exit method:
160 */
161
162METHODDEF(void)
163my_error_exit (j_common_ptr cinfo)
164{
165 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
166 my_error_ptr myerr = (my_error_ptr) cinfo->err;
167
168 /* Always display the message. */
169 /* We could postpone this until after returning, if we chose. */
deb2fec0 170 if (cinfo->err->output_message) (*cinfo->err->output_message) (cinfo);
b59ff3c9
VS
171
172 /* Return control to the setjmp point */
173 longjmp(myerr->setjmp_buffer, 1);
174}
175
3ca6a5f0
BP
176// temporarily disable the warning C4611 (interaction between '_setjmp' and
177// C++ object destruction is non-portable) - I don't see any dtors here
178#ifdef __VISUALC__
179 #pragma warning(disable:4611)
180#endif /* VC++ */
e9c4b1a2 181
700ec454 182bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
e9c4b1a2
JS
183{
184 struct jpeg_decompress_struct cinfo;
b59ff3c9 185 struct my_error_mgr jerr;
e9c4b1a2
JS
186 JSAMPARRAY tempbuf;
187 unsigned char *ptr;
188 unsigned stride;
995612e2 189
e9c4b1a2 190 image->Destroy();
b59ff3c9
VS
191 cinfo.err = jpeg_std_error( &jerr.pub );
192 jerr.pub.error_exit = my_error_exit;
193
deb2fec0
SB
194 if (!verbose) cinfo.err->output_message=NULL;
195
b59ff3c9
VS
196 /* Establish the setjmp return context for my_error_exit to use. */
197 if (setjmp(jerr.setjmp_buffer)) {
198 /* If we get here, the JPEG code has signaled an error.
199 * We need to clean up the JPEG object, close the input file, and return.
200 */
58c837a4
RR
201 if (verbose)
202 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
b59ff3c9
VS
203 jpeg_destroy_decompress(&cinfo);
204 if (image->Ok()) image->Destroy();
205 return FALSE;
206 }
207
e9c4b1a2
JS
208 jpeg_create_decompress( &cinfo );
209 jpeg_wxio_src( &cinfo, stream );
210 jpeg_read_header( &cinfo, TRUE );
211 cinfo.out_color_space = JCS_RGB;
212 jpeg_start_decompress( &cinfo );
995612e2 213
e9c4b1a2
JS
214 image->Create( cinfo.image_width, cinfo.image_height );
215 if (!image->Ok()) {
216 jpeg_finish_decompress( &cinfo );
217 jpeg_destroy_decompress( &cinfo );
218 return FALSE;
219 }
220 image->SetMask( FALSE );
221 ptr = image->GetData();
222 stride = cinfo.output_width * 3;
223 tempbuf = (*cinfo.mem->alloc_sarray)
224 ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 );
995612e2 225
e9c4b1a2
JS
226 while ( cinfo.output_scanline < cinfo.output_height ) {
227 jpeg_read_scanlines( &cinfo, tempbuf, 1 );
228 memcpy( ptr, tempbuf[0], stride );
229 ptr += stride;
230 }
231 jpeg_finish_decompress( &cinfo );
232 jpeg_destroy_decompress( &cinfo );
233 return TRUE;
234}
235
e9c4b1a2
JS
236typedef struct {
237 struct jpeg_destination_mgr pub;
995612e2 238
e9c4b1a2
JS
239 wxOutputStream *stream;
240 JOCTET * buffer;
241} my_destination_mgr;
242
243typedef my_destination_mgr * my_dest_ptr;
244
995612e2 245#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
e9c4b1a2
JS
246
247METHODDEF(void) init_destination (j_compress_ptr cinfo)
248{
249 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
995612e2 250
e9c4b1a2
JS
251 /* Allocate the output buffer --- it will be released when done with image */
252 dest->buffer = (JOCTET *)
253 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
254 OUTPUT_BUF_SIZE * sizeof(JOCTET));
255 dest->pub.next_output_byte = dest->buffer;
256 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
257}
258
259METHODDEF(boolean) empty_output_buffer (j_compress_ptr cinfo)
260{
261 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
995612e2 262
e9c4b1a2
JS
263 dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE);
264 dest->pub.next_output_byte = dest->buffer;
265 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
266 return TRUE;
267}
268
269METHODDEF(void) term_destination (j_compress_ptr cinfo)
270{
271 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
272 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
273 /* Write any data remaining in the buffer */
274 if (datacount > 0)
275 dest->stream->Write(dest->buffer, datacount);
276}
277
278GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
279{
280 my_dest_ptr dest;
995612e2
VZ
281
282 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
e9c4b1a2
JS
283 cinfo->dest = (struct jpeg_destination_mgr *)
284 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
285 sizeof(my_destination_mgr));
286 }
995612e2 287
e9c4b1a2
JS
288 dest = (my_dest_ptr) cinfo->dest;
289 dest->pub.init_destination = init_destination;
290 dest->pub.empty_output_buffer = empty_output_buffer;
291 dest->pub.term_destination = term_destination;
292 dest->stream = &outfile;
293}
294
deb2fec0 295bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2
JS
296{
297 struct jpeg_compress_struct cinfo;
b59ff3c9 298 struct my_error_mgr jerr;
995612e2 299 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
e9c4b1a2 300 JSAMPLE *image_buffer;
995612e2
VZ
301 int stride; /* physical row width in image buffer */
302
b59ff3c9
VS
303 cinfo.err = jpeg_std_error(&jerr.pub);
304 jerr.pub.error_exit = my_error_exit;
305
deb2fec0
SB
306 if (!verbose) cinfo.err->output_message=NULL;
307
b59ff3c9 308 /* Establish the setjmp return context for my_error_exit to use. */
58c837a4
RR
309 if (setjmp(jerr.setjmp_buffer))
310 {
311 /* If we get here, the JPEG code has signaled an error.
312 * We need to clean up the JPEG object, close the input file, and return.
313 */
314 if (verbose)
315 wxLogError(_("JPEG: Couldn't save image."));
316 jpeg_destroy_compress(&cinfo);
317 return FALSE;
b59ff3c9
VS
318 }
319
e9c4b1a2
JS
320 jpeg_create_compress(&cinfo);
321 jpeg_wxio_dest(&cinfo, stream);
995612e2 322
e9c4b1a2
JS
323 cinfo.image_width = image->GetWidth();
324 cinfo.image_height = image->GetHeight();
325 cinfo.input_components = 3;
326 cinfo.in_color_space = JCS_RGB;
327 jpeg_set_defaults(&cinfo);
5e5437e0
JS
328
329 // TODO: 3rd parameter is force_baseline, what value should this be?
330 // Code says: "If force_baseline is TRUE, the computed quantization table entries
331 // are limited to 1..255 for JPEG baseline compatibility."
332 // 'Quality' is a number between 0 (terrible) and 100 (very good).
333 // The default (in jcparam.c, jpeg_set_defaults) is 75,
334 // and force_baseline is TRUE.
335 if (image->HasOption(wxT("quality")))
336 jpeg_set_quality(&cinfo, image->GetOptionInt(wxT("quality")), TRUE);
337
e9c4b1a2 338 jpeg_start_compress(&cinfo, TRUE);
995612e2
VZ
339
340 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
e9c4b1a2
JS
341 image_buffer = image->GetData();
342 while (cinfo.next_scanline < cinfo.image_height) {
343 row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
344 jpeg_write_scanlines( &cinfo, row_pointer, 1 );
345 }
346 jpeg_finish_compress(&cinfo);
347 jpeg_destroy_compress(&cinfo);
995612e2 348
e9c4b1a2
JS
349 return TRUE;
350}
e9c4b1a2 351
3ca6a5f0
BP
352#ifdef __VISUALC__
353 #pragma warning(default:4611)
354#endif /* VC++ */
0828c087 355
995612e2 356bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
0828c087
VS
357{
358 unsigned char hdr[2];
995612e2 359
0828c087
VS
360 stream.Read(&hdr, 2);
361 stream.SeekI(-2, wxFromCurrent);
362 return (hdr[0] == 0xFF && hdr[1] == 0xD8);
363}
364
9ab6ee85
GRG
365#endif // wxUSE_STREAMS
366
367#endif // wxUSE_LIBJPEG
b59ff3c9
VS
368
369
370
371
372
373