]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
don't compile in wx hash code unless we really use it (#9532:12)
[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 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
b1f8fd3b
VZ
99extern "C"
100{
101
e9b964cf 102CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
e9c4b1a2
JS
103{
104}
105
e604ac79 106CPP_METHODDEF(wxjpeg_boolean) wx_fill_input_buffer ( j_decompress_ptr cinfo )
e9c4b1a2 107{
e9b964cf 108 wx_src_ptr src = (wx_src_ptr) cinfo->src;
1aaaa712
VS
109
110 src->pub.next_input_byte = src->buffer;
111 src->pub.bytes_in_buffer = src->stream->Read(src->buffer, JPEG_IO_BUFFER_SIZE).LastRead();
112
113 if (src->pub.bytes_in_buffer == 0) // check for end-of-stream
114 {
115 // Insert a fake EOI marker
116 src->buffer[0] = 0xFF;
117 src->buffer[1] = JPEG_EOI;
118 src->pub.bytes_in_buffer = 2;
119 }
e9c4b1a2
JS
120 return TRUE;
121}
122
e9b964cf 123CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
e9c4b1a2 124{
1aaaa712
VS
125 if (num_bytes > 0)
126 {
e9b964cf 127 wx_src_ptr src = (wx_src_ptr) cinfo->src;
1aaaa712 128
33ac7e6f 129 while (num_bytes > (long)src->pub.bytes_in_buffer)
1aaaa712
VS
130 {
131 num_bytes -= (long) src->pub.bytes_in_buffer;
132 src->pub.fill_input_buffer(cinfo);
133 }
134 src->pub.next_input_byte += (size_t) num_bytes;
135 src->pub.bytes_in_buffer -= (size_t) num_bytes;
136 }
e9c4b1a2
JS
137}
138
e9b964cf 139CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo )
e9c4b1a2 140{
e9b964cf 141 wx_src_ptr src = (wx_src_ptr) cinfo->src;
995612e2 142
1aaaa712 143 if (src->pub.bytes_in_buffer > 0)
3ca6a5f0 144 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
1aaaa712 145 delete[] src->buffer;
e9c4b1a2
JS
146}
147
e9c4b1a2 148
b59ff3c9
VS
149// JPEG error manager:
150
e9b964cf 151struct wx_error_mgr {
995612e2 152 struct jpeg_error_mgr pub; /* "public" fields */
b59ff3c9 153
995612e2 154 jmp_buf setjmp_buffer; /* for return to caller */
b59ff3c9
VS
155};
156
e9b964cf 157typedef struct wx_error_mgr * wx_error_ptr;
b59ff3c9
VS
158
159/*
160 * Here's the routine that will replace the standard error_exit method:
161 */
162
e9b964cf 163CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo)
b59ff3c9 164{
e9b964cf
VS
165 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
166 wx_error_ptr myerr = (wx_error_ptr) cinfo->err;
b59ff3c9
VS
167
168 /* Always display the message. */
169 /* We could postpone this until after returning, if we chose. */
ef25e5a4 170 (*cinfo->err->output_message) (cinfo);
b59ff3c9
VS
171
172 /* Return control to the setjmp point */
173 longjmp(myerr->setjmp_buffer, 1);
174}
175
ef25e5a4
VZ
176/*
177 * This will replace the standard output_message method when the user
178 * wants us to be silent (verbose==false). We must have such method instead of
179 * simply using NULL for cinfo->err->output_message because it's called
180 * unconditionally from within libjpeg when there's "garbage input".
181 */
182CPP_METHODDEF(void) wx_ignore_message (j_common_ptr WXUNUSED(cinfo))
183{
184}
185
e9b964cf 186void wx_jpeg_io_src( j_decompress_ptr cinfo, wxInputStream& infile )
90350682 187{
e9b964cf 188 wx_src_ptr src;
90350682
VZ
189
190 if (cinfo->src == NULL) { /* first time for this JPEG object? */
191 cinfo->src = (struct jpeg_source_mgr *)
192 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
e9b964cf 193 sizeof(wx_source_mgr));
90350682 194 }
e9b964cf 195 src = (wx_src_ptr) cinfo->src;
90350682
VZ
196 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
197 src->buffer = new JOCTET[JPEG_IO_BUFFER_SIZE];
198 src->pub.next_input_byte = NULL; /* until buffer loaded */
199 src->stream = &infile;
200
e9b964cf
VS
201 src->pub.init_source = wx_init_source;
202 src->pub.fill_input_buffer = wx_fill_input_buffer;
203 src->pub.skip_input_data = wx_skip_input_data;
90350682 204 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
e9b964cf 205 src->pub.term_source = wx_term_source;
90350682
VZ
206}
207
b1f8fd3b
VZ
208} // extern "C"
209
234fedd2
VZ
210static inline void wx_cmyk_to_rgb(unsigned char* rgb, const unsigned char* cmyk)
211{
212 register int k = 255 - cmyk[3];
213 register int k2 = cmyk[3];
214 register int c;
215
216 c = k + k2 * (255 - cmyk[0]) / 255;
ff7d9066 217 rgb[0] = (unsigned char)((c > 255) ? 0 : (255 - c));
234fedd2
VZ
218
219 c = k + k2 * (255 - cmyk[1]) / 255;
ff7d9066 220 rgb[1] = (unsigned char)((c > 255) ? 0 : (255 - c));
234fedd2
VZ
221
222 c = k + k2 * (255 - cmyk[2]) / 255;
ff7d9066 223 rgb[2] = (unsigned char)((c > 255) ? 0 : (255 - c));
234fedd2 224}
90350682 225
3ca6a5f0
BP
226// temporarily disable the warning C4611 (interaction between '_setjmp' and
227// C++ object destruction is non-portable) - I don't see any dtors here
228#ifdef __VISUALC__
229 #pragma warning(disable:4611)
230#endif /* VC++ */
e9c4b1a2 231
700ec454 232bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
e9c4b1a2
JS
233{
234 struct jpeg_decompress_struct cinfo;
e9b964cf 235 struct wx_error_mgr jerr;
e9c4b1a2 236 unsigned char *ptr;
995612e2 237
e9c4b1a2 238 image->Destroy();
b59ff3c9 239 cinfo.err = jpeg_std_error( &jerr.pub );
e9b964cf 240 jerr.pub.error_exit = wx_error_exit;
b59ff3c9 241
ef25e5a4
VZ
242 if (!verbose)
243 cinfo.err->output_message = wx_ignore_message;
deb2fec0 244
e9b964cf 245 /* Establish the setjmp return context for wx_error_exit to use. */
b59ff3c9
VS
246 if (setjmp(jerr.setjmp_buffer)) {
247 /* If we get here, the JPEG code has signaled an error.
248 * We need to clean up the JPEG object, close the input file, and return.
249 */
33ac7e6f 250 if (verbose)
58c837a4 251 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
192644bf 252 (cinfo.src->term_source)(&cinfo);
b59ff3c9
VS
253 jpeg_destroy_decompress(&cinfo);
254 if (image->Ok()) image->Destroy();
7beb59f3 255 return false;
b59ff3c9
VS
256 }
257
e9c4b1a2 258 jpeg_create_decompress( &cinfo );
e9b964cf 259 wx_jpeg_io_src( &cinfo, stream );
e9c4b1a2 260 jpeg_read_header( &cinfo, TRUE );
234fedd2
VZ
261
262 int bytesPerPixel;
263 if ((cinfo.out_color_space == JCS_CMYK) || (cinfo.out_color_space == JCS_YCCK))
264 {
265 cinfo.out_color_space = JCS_CMYK;
266 bytesPerPixel = 4;
267 }
268 else // all the rest is treated as RGB
269 {
270 cinfo.out_color_space = JCS_RGB;
271 bytesPerPixel = 3;
272 }
273
e9c4b1a2 274 jpeg_start_decompress( &cinfo );
995612e2 275
e9c4b1a2
JS
276 image->Create( cinfo.image_width, cinfo.image_height );
277 if (!image->Ok()) {
278 jpeg_finish_decompress( &cinfo );
279 jpeg_destroy_decompress( &cinfo );
7beb59f3 280 return false;
e9c4b1a2 281 }
7beb59f3 282 image->SetMask( false );
e9c4b1a2 283 ptr = image->GetData();
995612e2 284
234fedd2
VZ
285 unsigned stride = cinfo.output_width * bytesPerPixel;
286 JSAMPARRAY tempbuf = (*cinfo.mem->alloc_sarray)
287 ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 );
288
289 while ( cinfo.output_scanline < cinfo.output_height )
290 {
e9c4b1a2 291 jpeg_read_scanlines( &cinfo, tempbuf, 1 );
234fedd2
VZ
292 if (cinfo.out_color_space == JCS_RGB)
293 {
294 memcpy( ptr, tempbuf[0], stride );
295 ptr += stride;
296 }
297 else // CMYK
298 {
299 const unsigned char* inptr = (const unsigned char*) tempbuf[0];
300 for (size_t i = 0; i < cinfo.output_width; i++)
301 {
302 wx_cmyk_to_rgb(ptr, inptr);
303 ptr += 3;
304 inptr += 4;
305 }
306 }
e9c4b1a2 307 }
234fedd2 308
37ba70a5
VZ
309 // set up resolution if available: it's part of optional JFIF APP0 chunk
310 if ( cinfo.saw_JFIF_marker )
311 {
312 image->SetOption(wxIMAGE_OPTION_RESOLUTIONX, cinfo.X_density);
313 image->SetOption(wxIMAGE_OPTION_RESOLUTIONY, cinfo.Y_density);
314
315 // we use the same values for this option as libjpeg so we don't need
316 // any conversion here
317 image->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT, cinfo.density_unit);
318 }
319
e9c4b1a2
JS
320 jpeg_finish_decompress( &cinfo );
321 jpeg_destroy_decompress( &cinfo );
7beb59f3 322 return true;
e9c4b1a2
JS
323}
324
e9c4b1a2
JS
325typedef struct {
326 struct jpeg_destination_mgr pub;
995612e2 327
e9c4b1a2
JS
328 wxOutputStream *stream;
329 JOCTET * buffer;
e9b964cf 330} wx_destination_mgr;
e9c4b1a2 331
e9b964cf 332typedef wx_destination_mgr * wx_dest_ptr;
e9c4b1a2 333
995612e2 334#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
e9c4b1a2 335
b1f8fd3b
VZ
336extern "C"
337{
338
e9b964cf 339CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo)
e9c4b1a2 340{
e9b964cf 341 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
995612e2 342
e9c4b1a2
JS
343 /* Allocate the output buffer --- it will be released when done with image */
344 dest->buffer = (JOCTET *)
345 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
346 OUTPUT_BUF_SIZE * sizeof(JOCTET));
347 dest->pub.next_output_byte = dest->buffer;
348 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
349}
350
e604ac79 351CPP_METHODDEF(wxjpeg_boolean) wx_empty_output_buffer (j_compress_ptr cinfo)
e9c4b1a2 352{
e9b964cf 353 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
995612e2 354
e9c4b1a2
JS
355 dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE);
356 dest->pub.next_output_byte = dest->buffer;
357 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
358 return TRUE;
359}
360
e9b964cf 361CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo)
e9c4b1a2 362{
e9b964cf 363 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
e9c4b1a2
JS
364 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
365 /* Write any data remaining in the buffer */
366 if (datacount > 0)
367 dest->stream->Write(dest->buffer, datacount);
368}
369
e9b964cf 370GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
e9c4b1a2 371{
e9b964cf 372 wx_dest_ptr dest;
995612e2
VZ
373
374 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
e9c4b1a2
JS
375 cinfo->dest = (struct jpeg_destination_mgr *)
376 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
e9b964cf 377 sizeof(wx_destination_mgr));
e9c4b1a2 378 }
995612e2 379
e9b964cf
VS
380 dest = (wx_dest_ptr) cinfo->dest;
381 dest->pub.init_destination = wx_init_destination;
382 dest->pub.empty_output_buffer = wx_empty_output_buffer;
383 dest->pub.term_destination = wx_term_destination;
e9c4b1a2
JS
384 dest->stream = &outfile;
385}
386
b1f8fd3b
VZ
387} // extern "C"
388
deb2fec0 389bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2
JS
390{
391 struct jpeg_compress_struct cinfo;
e9b964cf 392 struct wx_error_mgr jerr;
995612e2 393 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
e9c4b1a2 394 JSAMPLE *image_buffer;
995612e2
VZ
395 int stride; /* physical row width in image buffer */
396
b59ff3c9 397 cinfo.err = jpeg_std_error(&jerr.pub);
e9b964cf 398 jerr.pub.error_exit = wx_error_exit;
b59ff3c9 399
ef25e5a4
VZ
400 if (!verbose)
401 cinfo.err->output_message = wx_ignore_message;
deb2fec0 402
e9b964cf 403 /* Establish the setjmp return context for wx_error_exit to use. */
33ac7e6f 404 if (setjmp(jerr.setjmp_buffer))
58c837a4
RR
405 {
406 /* If we get here, the JPEG code has signaled an error.
407 * We need to clean up the JPEG object, close the input file, and return.
408 */
33ac7e6f 409 if (verbose)
58c837a4
RR
410 wxLogError(_("JPEG: Couldn't save image."));
411 jpeg_destroy_compress(&cinfo);
7beb59f3 412 return false;
b59ff3c9
VS
413 }
414
e9c4b1a2 415 jpeg_create_compress(&cinfo);
e9b964cf 416 wx_jpeg_io_dest(&cinfo, stream);
995612e2 417
e9c4b1a2
JS
418 cinfo.image_width = image->GetWidth();
419 cinfo.image_height = image->GetHeight();
420 cinfo.input_components = 3;
421 cinfo.in_color_space = JCS_RGB;
422 jpeg_set_defaults(&cinfo);
5e5437e0
JS
423
424 // TODO: 3rd parameter is force_baseline, what value should this be?
425 // Code says: "If force_baseline is TRUE, the computed quantization table entries
426 // are limited to 1..255 for JPEG baseline compatibility."
427 // 'Quality' is a number between 0 (terrible) and 100 (very good).
428 // The default (in jcparam.c, jpeg_set_defaults) is 75,
429 // and force_baseline is TRUE.
9a679df6
JS
430 if (image->HasOption(wxIMAGE_OPTION_QUALITY))
431 jpeg_set_quality(&cinfo, image->GetOptionInt(wxIMAGE_OPTION_QUALITY), TRUE);
5e5437e0 432
fe9308c6 433 // set the resolution fields in the output file
361f4288
VZ
434 int resX, resY;
435 wxImageResolution res = GetResolutionFromOptions(*image, &resX, &resY);
436 if ( res != wxIMAGE_RESOLUTION_NONE )
fe9308c6
VZ
437 {
438 cinfo.X_density = resX;
439 cinfo.Y_density = resY;
89d38f8c 440
361f4288
VZ
441 // it so happens that wxIMAGE_RESOLUTION_INCHES/CM values are the same
442 // ones as used by libjpeg, so we can assign them directly
443 cinfo.density_unit = res;
89d38f8c
VS
444 }
445
e9c4b1a2 446 jpeg_start_compress(&cinfo, TRUE);
995612e2
VZ
447
448 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
e9c4b1a2
JS
449 image_buffer = image->GetData();
450 while (cinfo.next_scanline < cinfo.image_height) {
451 row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
452 jpeg_write_scanlines( &cinfo, row_pointer, 1 );
453 }
454 jpeg_finish_compress(&cinfo);
455 jpeg_destroy_compress(&cinfo);
995612e2 456
7beb59f3 457 return true;
e9c4b1a2 458}
e9c4b1a2 459
3ca6a5f0
BP
460#ifdef __VISUALC__
461 #pragma warning(default:4611)
462#endif /* VC++ */
0828c087 463
995612e2 464bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
0828c087
VS
465{
466 unsigned char hdr[2];
995612e2 467
79fa2374 468 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
7beb59f3 469 return false;
79fa2374 470
79fa2374 471 return hdr[0] == 0xFF && hdr[1] == 0xD8;
0828c087
VS
472}
473
9ab6ee85
GRG
474#endif // wxUSE_STREAMS
475
476#endif // wxUSE_LIBJPEG