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