]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
wizards not using sizers for the page layout now work again
[wxWidgets.git] / src / common / imagjpeg.cpp
CommitLineData
e9c4b1a2 1/////////////////////////////////////////////////////////////////////////////
6d3d756a 2// Name: src/common/imagjpeg.cpp
e9c4b1a2 3// Purpose: wxImage JPEG handler
b59ff3c9 4// Author: Vaclav Slavik
e9c4b1a2 5// RCS-ID: $Id$
b59ff3c9 6// Copyright: (c) Vaclav Slavik
65571936 7// Licence: wxWindows licence
e9c4b1a2
JS
8/////////////////////////////////////////////////////////////////////////////
9
e9c4b1a2
JS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
88a7a4e1 14 #pragma hdrstop
e9c4b1a2
JS
15#endif
16
c96ea657 17#if wxUSE_IMAGE && wxUSE_LIBJPEG
ce4169a4 18
88a7a4e1
WS
19#include "wx/imagjpeg.h"
20
8898456d
WS
21#ifndef WX_PRECOMP
22 #include "wx/log.h"
23 #include "wx/app.h"
88a7a4e1 24 #include "wx/intl.h"
8898456d
WS
25#endif
26
e9c4b1a2 27#include "wx/bitmap.h"
cb69afe0 28
7beb59f3 29// NB: Some compilers define boolean type in Windows headers
4c87eb44 30// (e.g. Watcom C++, but not Open Watcom).
cb69afe0
VS
31// This causes a conflict with jmorecfg.h header from libjpeg, so we have
32// to make sure libjpeg won't try to define boolean itself. This is done by
33// defining HAVE_BOOLEAN.
6d3d756a 34#if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__DIGITALMARS__))
e7ca6139 35 #define HAVE_BOOLEAN
9ed0d735 36 #include "wx/msw/wrapwin.h"
e7ca6139 37#endif
cb69afe0 38
995612e2
VZ
39extern "C"
40{
cd2a4e16
JS
41 #if defined(__WXMSW__)
42 #define XMD_H
43 #endif
995612e2 44 #include "jpeglib.h"
e9c4b1a2 45}
cb69afe0 46
e9c4b1a2
JS
47#include "wx/filefn.h"
48#include "wx/wfstream.h"
e9c4b1a2
JS
49#include "wx/module.h"
50
51// For memcpy
52#include <string.h>
b59ff3c9
VS
53// For JPEG library error handling
54#include <setjmp.h>
e9c4b1a2
JS
55
56#ifdef __SALFORDC__
e9c4b1a2
JS
57#undef FAR
58#endif
e9c4b1a2 59
d7c42914
VZ
60// ----------------------------------------------------------------------------
61// types
62// ----------------------------------------------------------------------------
63
64// the standard definition of METHODDEF(type) from jmorecfg.h is "static type"
65// which means that we can't declare the method functions as extern "C" - the
66// compiler (rightfully) complains about the multiple storage classes in
67// declaration
68//
69// so we only add extern "C" when using our own, modified, jmorecfg.h - and use
70// whatever we have in the system headers if this is what we use hoping that it
71// should be ok (can't do anything else)
72#ifdef JPEG_METHOD_LINKAGE
73 #define CPP_METHODDEF(type) extern "C" METHODDEF(type)
74#else // not using our jmorecfg.h header
75 #define CPP_METHODDEF(type) METHODDEF(type)
76#endif
90350682 77
e9c4b1a2
JS
78//-----------------------------------------------------------------------------
79// wxJPEGHandler
80//-----------------------------------------------------------------------------
81
e9c4b1a2 82IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
e9c4b1a2 83
9ab6ee85
GRG
84#if wxUSE_STREAMS
85
e9c4b1a2
JS
86//------------- JPEG Data Source Manager
87
1aaaa712
VS
88#define JPEG_IO_BUFFER_SIZE 2048
89
e9c4b1a2
JS
90typedef struct {
91 struct jpeg_source_mgr pub; /* public fields */
995612e2 92
e9c4b1a2 93 JOCTET* buffer; /* start of buffer */
1aaaa712 94 wxInputStream *stream;
e9b964cf 95} wx_source_mgr;
e9c4b1a2 96
e9b964cf 97typedef wx_source_mgr * wx_src_ptr;
e9c4b1a2 98
e9b964cf 99CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
e9c4b1a2
JS
100{
101}
102
e9b964cf 103CPP_METHODDEF(boolean) wx_fill_input_buffer ( j_decompress_ptr cinfo )
e9c4b1a2 104{
e9b964cf 105 wx_src_ptr src = (wx_src_ptr) cinfo->src;
1aaaa712
VS
106
107 src->pub.next_input_byte = src->buffer;
108 src->pub.bytes_in_buffer = src->stream->Read(src->buffer, JPEG_IO_BUFFER_SIZE).LastRead();
109
110 if (src->pub.bytes_in_buffer == 0) // check for end-of-stream
111 {
112 // Insert a fake EOI marker
113 src->buffer[0] = 0xFF;
114 src->buffer[1] = JPEG_EOI;
115 src->pub.bytes_in_buffer = 2;
116 }
e9c4b1a2
JS
117 return TRUE;
118}
119
e9b964cf 120CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
e9c4b1a2 121{
1aaaa712
VS
122 if (num_bytes > 0)
123 {
e9b964cf 124 wx_src_ptr src = (wx_src_ptr) cinfo->src;
1aaaa712 125
33ac7e6f 126 while (num_bytes > (long)src->pub.bytes_in_buffer)
1aaaa712
VS
127 {
128 num_bytes -= (long) src->pub.bytes_in_buffer;
129 src->pub.fill_input_buffer(cinfo);
130 }
131 src->pub.next_input_byte += (size_t) num_bytes;
132 src->pub.bytes_in_buffer -= (size_t) num_bytes;
133 }
e9c4b1a2
JS
134}
135
e9b964cf 136CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo )
e9c4b1a2 137{
e9b964cf 138 wx_src_ptr src = (wx_src_ptr) cinfo->src;
995612e2 139
1aaaa712 140 if (src->pub.bytes_in_buffer > 0)
3ca6a5f0 141 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
1aaaa712 142 delete[] src->buffer;
e9c4b1a2
JS
143}
144
e9c4b1a2 145
b59ff3c9
VS
146// JPEG error manager:
147
e9b964cf 148struct wx_error_mgr {
995612e2 149 struct jpeg_error_mgr pub; /* "public" fields */
b59ff3c9 150
995612e2 151 jmp_buf setjmp_buffer; /* for return to caller */
b59ff3c9
VS
152};
153
e9b964cf 154typedef struct wx_error_mgr * wx_error_ptr;
b59ff3c9
VS
155
156/*
157 * Here's the routine that will replace the standard error_exit method:
158 */
159
e9b964cf 160CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo)
b59ff3c9 161{
e9b964cf
VS
162 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
163 wx_error_ptr myerr = (wx_error_ptr) cinfo->err;
b59ff3c9
VS
164
165 /* Always display the message. */
166 /* We could postpone this until after returning, if we chose. */
ef25e5a4 167 (*cinfo->err->output_message) (cinfo);
b59ff3c9
VS
168
169 /* Return control to the setjmp point */
170 longjmp(myerr->setjmp_buffer, 1);
171}
172
ef25e5a4
VZ
173/*
174 * This will replace the standard output_message method when the user
175 * wants us to be silent (verbose==false). We must have such method instead of
176 * simply using NULL for cinfo->err->output_message because it's called
177 * unconditionally from within libjpeg when there's "garbage input".
178 */
179CPP_METHODDEF(void) wx_ignore_message (j_common_ptr WXUNUSED(cinfo))
180{
181}
182
e9b964cf 183void wx_jpeg_io_src( j_decompress_ptr cinfo, wxInputStream& infile )
90350682 184{
e9b964cf 185 wx_src_ptr src;
90350682
VZ
186
187 if (cinfo->src == NULL) { /* first time for this JPEG object? */
188 cinfo->src = (struct jpeg_source_mgr *)
189 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
e9b964cf 190 sizeof(wx_source_mgr));
90350682 191 }
e9b964cf 192 src = (wx_src_ptr) cinfo->src;
90350682
VZ
193 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
194 src->buffer = new JOCTET[JPEG_IO_BUFFER_SIZE];
195 src->pub.next_input_byte = NULL; /* until buffer loaded */
196 src->stream = &infile;
197
e9b964cf
VS
198 src->pub.init_source = wx_init_source;
199 src->pub.fill_input_buffer = wx_fill_input_buffer;
200 src->pub.skip_input_data = wx_skip_input_data;
90350682 201 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
e9b964cf 202 src->pub.term_source = wx_term_source;
90350682
VZ
203}
204
205
3ca6a5f0
BP
206// temporarily disable the warning C4611 (interaction between '_setjmp' and
207// C++ object destruction is non-portable) - I don't see any dtors here
208#ifdef __VISUALC__
209 #pragma warning(disable:4611)
210#endif /* VC++ */
e9c4b1a2 211
700ec454 212bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
e9c4b1a2
JS
213{
214 struct jpeg_decompress_struct cinfo;
e9b964cf 215 struct wx_error_mgr jerr;
e9c4b1a2
JS
216 JSAMPARRAY tempbuf;
217 unsigned char *ptr;
218 unsigned stride;
995612e2 219
e9c4b1a2 220 image->Destroy();
b59ff3c9 221 cinfo.err = jpeg_std_error( &jerr.pub );
e9b964cf 222 jerr.pub.error_exit = wx_error_exit;
b59ff3c9 223
ef25e5a4
VZ
224 if (!verbose)
225 cinfo.err->output_message = wx_ignore_message;
deb2fec0 226
e9b964cf 227 /* Establish the setjmp return context for wx_error_exit to use. */
b59ff3c9
VS
228 if (setjmp(jerr.setjmp_buffer)) {
229 /* If we get here, the JPEG code has signaled an error.
230 * We need to clean up the JPEG object, close the input file, and return.
231 */
33ac7e6f 232 if (verbose)
58c837a4 233 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
192644bf 234 (cinfo.src->term_source)(&cinfo);
b59ff3c9
VS
235 jpeg_destroy_decompress(&cinfo);
236 if (image->Ok()) image->Destroy();
7beb59f3 237 return false;
b59ff3c9
VS
238 }
239
e9c4b1a2 240 jpeg_create_decompress( &cinfo );
e9b964cf 241 wx_jpeg_io_src( &cinfo, stream );
e9c4b1a2
JS
242 jpeg_read_header( &cinfo, TRUE );
243 cinfo.out_color_space = JCS_RGB;
244 jpeg_start_decompress( &cinfo );
995612e2 245
e9c4b1a2
JS
246 image->Create( cinfo.image_width, cinfo.image_height );
247 if (!image->Ok()) {
248 jpeg_finish_decompress( &cinfo );
249 jpeg_destroy_decompress( &cinfo );
7beb59f3 250 return false;
e9c4b1a2 251 }
7beb59f3 252 image->SetMask( false );
e9c4b1a2
JS
253 ptr = image->GetData();
254 stride = cinfo.output_width * 3;
255 tempbuf = (*cinfo.mem->alloc_sarray)
256 ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 );
995612e2 257
e9c4b1a2
JS
258 while ( cinfo.output_scanline < cinfo.output_height ) {
259 jpeg_read_scanlines( &cinfo, tempbuf, 1 );
260 memcpy( ptr, tempbuf[0], stride );
261 ptr += stride;
262 }
263 jpeg_finish_decompress( &cinfo );
264 jpeg_destroy_decompress( &cinfo );
7beb59f3 265 return true;
e9c4b1a2
JS
266}
267
e9c4b1a2
JS
268typedef struct {
269 struct jpeg_destination_mgr pub;
995612e2 270
e9c4b1a2
JS
271 wxOutputStream *stream;
272 JOCTET * buffer;
e9b964cf 273} wx_destination_mgr;
e9c4b1a2 274
e9b964cf 275typedef wx_destination_mgr * wx_dest_ptr;
e9c4b1a2 276
995612e2 277#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
e9c4b1a2 278
e9b964cf 279CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo)
e9c4b1a2 280{
e9b964cf 281 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
995612e2 282
e9c4b1a2
JS
283 /* Allocate the output buffer --- it will be released when done with image */
284 dest->buffer = (JOCTET *)
285 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
286 OUTPUT_BUF_SIZE * sizeof(JOCTET));
287 dest->pub.next_output_byte = dest->buffer;
288 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
289}
290
e9b964cf 291CPP_METHODDEF(boolean) wx_empty_output_buffer (j_compress_ptr cinfo)
e9c4b1a2 292{
e9b964cf 293 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
995612e2 294
e9c4b1a2
JS
295 dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE);
296 dest->pub.next_output_byte = dest->buffer;
297 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
298 return TRUE;
299}
300
e9b964cf 301CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo)
e9c4b1a2 302{
e9b964cf 303 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
e9c4b1a2
JS
304 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
305 /* Write any data remaining in the buffer */
306 if (datacount > 0)
307 dest->stream->Write(dest->buffer, datacount);
308}
309
e9b964cf 310GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
e9c4b1a2 311{
e9b964cf 312 wx_dest_ptr dest;
995612e2
VZ
313
314 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
e9c4b1a2
JS
315 cinfo->dest = (struct jpeg_destination_mgr *)
316 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
e9b964cf 317 sizeof(wx_destination_mgr));
e9c4b1a2 318 }
995612e2 319
e9b964cf
VS
320 dest = (wx_dest_ptr) cinfo->dest;
321 dest->pub.init_destination = wx_init_destination;
322 dest->pub.empty_output_buffer = wx_empty_output_buffer;
323 dest->pub.term_destination = wx_term_destination;
e9c4b1a2
JS
324 dest->stream = &outfile;
325}
326
deb2fec0 327bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2
JS
328{
329 struct jpeg_compress_struct cinfo;
e9b964cf 330 struct wx_error_mgr jerr;
995612e2 331 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
e9c4b1a2 332 JSAMPLE *image_buffer;
995612e2
VZ
333 int stride; /* physical row width in image buffer */
334
b59ff3c9 335 cinfo.err = jpeg_std_error(&jerr.pub);
e9b964cf 336 jerr.pub.error_exit = wx_error_exit;
b59ff3c9 337
ef25e5a4
VZ
338 if (!verbose)
339 cinfo.err->output_message = wx_ignore_message;
deb2fec0 340
e9b964cf 341 /* Establish the setjmp return context for wx_error_exit to use. */
33ac7e6f 342 if (setjmp(jerr.setjmp_buffer))
58c837a4
RR
343 {
344 /* If we get here, the JPEG code has signaled an error.
345 * We need to clean up the JPEG object, close the input file, and return.
346 */
33ac7e6f 347 if (verbose)
58c837a4
RR
348 wxLogError(_("JPEG: Couldn't save image."));
349 jpeg_destroy_compress(&cinfo);
7beb59f3 350 return false;
b59ff3c9
VS
351 }
352
e9c4b1a2 353 jpeg_create_compress(&cinfo);
e9b964cf 354 wx_jpeg_io_dest(&cinfo, stream);
995612e2 355
e9c4b1a2
JS
356 cinfo.image_width = image->GetWidth();
357 cinfo.image_height = image->GetHeight();
358 cinfo.input_components = 3;
359 cinfo.in_color_space = JCS_RGB;
360 jpeg_set_defaults(&cinfo);
5e5437e0
JS
361
362 // TODO: 3rd parameter is force_baseline, what value should this be?
363 // Code says: "If force_baseline is TRUE, the computed quantization table entries
364 // are limited to 1..255 for JPEG baseline compatibility."
365 // 'Quality' is a number between 0 (terrible) and 100 (very good).
366 // The default (in jcparam.c, jpeg_set_defaults) is 75,
367 // and force_baseline is TRUE.
9a679df6
JS
368 if (image->HasOption(wxIMAGE_OPTION_QUALITY))
369 jpeg_set_quality(&cinfo, image->GetOptionInt(wxIMAGE_OPTION_QUALITY), TRUE);
5e5437e0 370
fe9308c6
VZ
371 // set the resolution fields in the output file
372 UINT16 resX,
373 resY;
374 if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) &&
375 image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) )
89d38f8c 376 {
4c2740ec
WS
377 resX = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX);
378 resY = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
fe9308c6
VZ
379 }
380 else if ( image->HasOption(wxIMAGE_OPTION_RESOLUTION) )
381 {
382 resX =
4c2740ec 383 resY = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION);
fe9308c6
VZ
384 }
385 else
386 {
387 resX =
388 resY = 0;
389 }
390
391 if ( resX && resY )
392 {
393 cinfo.X_density = resX;
394 cinfo.Y_density = resY;
89d38f8c
VS
395 }
396
397 // sets the resolution unit field in the output file
398 // wxIMAGE_RESOLUTION_INCHES for inches
399 // wxIMAGE_RESOLUTION_CM for centimeters
fe9308c6 400 if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT) )
89d38f8c 401 {
7ac31c42 402 cinfo.density_unit = (UINT8)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT);
89d38f8c
VS
403 }
404
e9c4b1a2 405 jpeg_start_compress(&cinfo, TRUE);
995612e2
VZ
406
407 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
e9c4b1a2
JS
408 image_buffer = image->GetData();
409 while (cinfo.next_scanline < cinfo.image_height) {
410 row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
411 jpeg_write_scanlines( &cinfo, row_pointer, 1 );
412 }
413 jpeg_finish_compress(&cinfo);
414 jpeg_destroy_compress(&cinfo);
995612e2 415
7beb59f3 416 return true;
e9c4b1a2 417}
e9c4b1a2 418
3ca6a5f0
BP
419#ifdef __VISUALC__
420 #pragma warning(default:4611)
421#endif /* VC++ */
0828c087 422
995612e2 423bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
0828c087
VS
424{
425 unsigned char hdr[2];
995612e2 426
79fa2374 427 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
7beb59f3 428 return false;
79fa2374 429
79fa2374 430 return hdr[0] == 0xFF && hdr[1] == 0xD8;
0828c087
VS
431}
432
9ab6ee85
GRG
433#endif // wxUSE_STREAMS
434
435#endif // wxUSE_LIBJPEG