]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
Avoid using wxHtmlTag::HasParam() unnecessarily.
[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
b59ff3c9 5// Copyright: (c) Vaclav Slavik
65571936 6// Licence: wxWindows licence
e9c4b1a2
JS
7/////////////////////////////////////////////////////////////////////////////
8
e9c4b1a2
JS
9// For compilers that support precompilation, includes "wx.h".
10#include "wx/wxprec.h"
11
12#ifdef __BORLANDC__
88a7a4e1 13 #pragma hdrstop
e9c4b1a2
JS
14#endif
15
c96ea657 16#if wxUSE_IMAGE && wxUSE_LIBJPEG
ce4169a4 17
88a7a4e1 18#include "wx/imagjpeg.h"
ccec9093 19#include "wx/versioninfo.h"
88a7a4e1 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.
03647350 31//
a4cc2d09
MW
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
5c69ef61 151struct wx_error_mgr : public jpeg_error_mgr
87d4e271 152{
995612e2 153 jmp_buf setjmp_buffer; /* for return to caller */
b59ff3c9
VS
154};
155
b59ff3c9
VS
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 162 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
87d4e271 163 wx_error_mgr * const jerr = (wx_error_mgr *) 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 */
87d4e271 170 longjmp(jerr->setjmp_buffer, 1);
b59ff3c9
VS
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
b1f8fd3b
VZ
205} // extern "C"
206
234fedd2
VZ
207static inline void wx_cmyk_to_rgb(unsigned char* rgb, const unsigned char* cmyk)
208{
209 register int k = 255 - cmyk[3];
210 register int k2 = cmyk[3];
211 register int c;
212
213 c = k + k2 * (255 - cmyk[0]) / 255;
ff7d9066 214 rgb[0] = (unsigned char)((c > 255) ? 0 : (255 - c));
234fedd2
VZ
215
216 c = k + k2 * (255 - cmyk[1]) / 255;
ff7d9066 217 rgb[1] = (unsigned char)((c > 255) ? 0 : (255 - c));
234fedd2
VZ
218
219 c = k + k2 * (255 - cmyk[2]) / 255;
ff7d9066 220 rgb[2] = (unsigned char)((c > 255) ? 0 : (255 - c));
234fedd2 221}
90350682 222
3ca6a5f0
BP
223// temporarily disable the warning C4611 (interaction between '_setjmp' and
224// C++ object destruction is non-portable) - I don't see any dtors here
225#ifdef __VISUALC__
226 #pragma warning(disable:4611)
227#endif /* VC++ */
e9c4b1a2 228
700ec454 229bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
e9c4b1a2 230{
36abe9d4
VZ
231 wxCHECK_MSG( image, false, "NULL image pointer" );
232
e9c4b1a2 233 struct jpeg_decompress_struct cinfo;
87d4e271 234 wx_error_mgr jerr;
e9c4b1a2 235 unsigned char *ptr;
995612e2 236
36abe9d4
VZ
237 // save this before calling Destroy()
238 const unsigned maxWidth = image->GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH),
239 maxHeight = image->GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT);
e9c4b1a2 240 image->Destroy();
36abe9d4 241
87d4e271
VZ
242 cinfo.err = jpeg_std_error( &jerr );
243 jerr.error_exit = wx_error_exit;
b59ff3c9 244
ef25e5a4
VZ
245 if (!verbose)
246 cinfo.err->output_message = wx_ignore_message;
deb2fec0 247
e9b964cf 248 /* Establish the setjmp return context for wx_error_exit to use. */
b59ff3c9
VS
249 if (setjmp(jerr.setjmp_buffer)) {
250 /* If we get here, the JPEG code has signaled an error.
251 * We need to clean up the JPEG object, close the input file, and return.
252 */
33ac7e6f 253 if (verbose)
af588446 254 {
58c837a4 255 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
af588446 256 }
192644bf 257 (cinfo.src->term_source)(&cinfo);
b59ff3c9 258 jpeg_destroy_decompress(&cinfo);
a1b806b9 259 if (image->IsOk()) image->Destroy();
7beb59f3 260 return false;
b59ff3c9
VS
261 }
262
e9c4b1a2 263 jpeg_create_decompress( &cinfo );
e9b964cf 264 wx_jpeg_io_src( &cinfo, stream );
e9c4b1a2 265 jpeg_read_header( &cinfo, TRUE );
234fedd2
VZ
266
267 int bytesPerPixel;
268 if ((cinfo.out_color_space == JCS_CMYK) || (cinfo.out_color_space == JCS_YCCK))
269 {
270 cinfo.out_color_space = JCS_CMYK;
271 bytesPerPixel = 4;
272 }
273 else // all the rest is treated as RGB
274 {
275 cinfo.out_color_space = JCS_RGB;
276 bytesPerPixel = 3;
277 }
278
36abe9d4
VZ
279 // scale the picture to fit in the specified max size if necessary
280 if ( maxWidth > 0 || maxHeight > 0 )
281 {
282 unsigned& scale = cinfo.scale_denom;
283 while ( (maxWidth && (cinfo.image_width / scale > maxWidth)) ||
284 (maxHeight && (cinfo.image_height / scale > maxHeight)) )
285 {
286 scale *= 2;
287 }
288 }
289
e9c4b1a2 290 jpeg_start_decompress( &cinfo );
995612e2 291
36abe9d4 292 image->Create( cinfo.output_width, cinfo.output_height );
a1b806b9 293 if (!image->IsOk()) {
e9c4b1a2
JS
294 jpeg_finish_decompress( &cinfo );
295 jpeg_destroy_decompress( &cinfo );
7beb59f3 296 return false;
e9c4b1a2 297 }
7beb59f3 298 image->SetMask( false );
e9c4b1a2 299 ptr = image->GetData();
995612e2 300
234fedd2
VZ
301 unsigned stride = cinfo.output_width * bytesPerPixel;
302 JSAMPARRAY tempbuf = (*cinfo.mem->alloc_sarray)
303 ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 );
304
305 while ( cinfo.output_scanline < cinfo.output_height )
306 {
e9c4b1a2 307 jpeg_read_scanlines( &cinfo, tempbuf, 1 );
234fedd2
VZ
308 if (cinfo.out_color_space == JCS_RGB)
309 {
310 memcpy( ptr, tempbuf[0], stride );
311 ptr += stride;
312 }
313 else // CMYK
314 {
315 const unsigned char* inptr = (const unsigned char*) tempbuf[0];
316 for (size_t i = 0; i < cinfo.output_width; i++)
317 {
318 wx_cmyk_to_rgb(ptr, inptr);
319 ptr += 3;
320 inptr += 4;
321 }
322 }
e9c4b1a2 323 }
234fedd2 324
37ba70a5
VZ
325 // set up resolution if available: it's part of optional JFIF APP0 chunk
326 if ( cinfo.saw_JFIF_marker )
327 {
328 image->SetOption(wxIMAGE_OPTION_RESOLUTIONX, cinfo.X_density);
329 image->SetOption(wxIMAGE_OPTION_RESOLUTIONY, cinfo.Y_density);
330
331 // we use the same values for this option as libjpeg so we don't need
332 // any conversion here
333 image->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT, cinfo.density_unit);
334 }
b6963858
VZ
335
336 if ( cinfo.image_width != cinfo.output_width || cinfo.image_height != cinfo.output_height )
337 {
338 // save the original image size
339 image->SetOption(wxIMAGE_OPTION_ORIGINAL_WIDTH, cinfo.image_width);
340 image->SetOption(wxIMAGE_OPTION_ORIGINAL_HEIGHT, cinfo.image_height);
341 }
37ba70a5 342
e9c4b1a2
JS
343 jpeg_finish_decompress( &cinfo );
344 jpeg_destroy_decompress( &cinfo );
7beb59f3 345 return true;
e9c4b1a2
JS
346}
347
e9c4b1a2
JS
348typedef struct {
349 struct jpeg_destination_mgr pub;
995612e2 350
e9c4b1a2
JS
351 wxOutputStream *stream;
352 JOCTET * buffer;
e9b964cf 353} wx_destination_mgr;
e9c4b1a2 354
e9b964cf 355typedef wx_destination_mgr * wx_dest_ptr;
e9c4b1a2 356
995612e2 357#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
e9c4b1a2 358
b1f8fd3b
VZ
359extern "C"
360{
361
e9b964cf 362CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo)
e9c4b1a2 363{
e9b964cf 364 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
995612e2 365
e9c4b1a2
JS
366 /* Allocate the output buffer --- it will be released when done with image */
367 dest->buffer = (JOCTET *)
368 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
369 OUTPUT_BUF_SIZE * sizeof(JOCTET));
370 dest->pub.next_output_byte = dest->buffer;
371 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
372}
373
e604ac79 374CPP_METHODDEF(wxjpeg_boolean) wx_empty_output_buffer (j_compress_ptr cinfo)
e9c4b1a2 375{
e9b964cf 376 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
995612e2 377
e9c4b1a2
JS
378 dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE);
379 dest->pub.next_output_byte = dest->buffer;
380 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
381 return TRUE;
382}
383
e9b964cf 384CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo)
e9c4b1a2 385{
e9b964cf 386 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
e9c4b1a2
JS
387 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
388 /* Write any data remaining in the buffer */
389 if (datacount > 0)
390 dest->stream->Write(dest->buffer, datacount);
391}
392
e9b964cf 393GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
e9c4b1a2 394{
e9b964cf 395 wx_dest_ptr dest;
995612e2
VZ
396
397 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
e9c4b1a2
JS
398 cinfo->dest = (struct jpeg_destination_mgr *)
399 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
e9b964cf 400 sizeof(wx_destination_mgr));
e9c4b1a2 401 }
995612e2 402
e9b964cf
VS
403 dest = (wx_dest_ptr) cinfo->dest;
404 dest->pub.init_destination = wx_init_destination;
405 dest->pub.empty_output_buffer = wx_empty_output_buffer;
406 dest->pub.term_destination = wx_term_destination;
e9c4b1a2
JS
407 dest->stream = &outfile;
408}
409
b1f8fd3b
VZ
410} // extern "C"
411
deb2fec0 412bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2
JS
413{
414 struct jpeg_compress_struct cinfo;
87d4e271 415 wx_error_mgr jerr;
995612e2 416 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
e9c4b1a2 417 JSAMPLE *image_buffer;
995612e2
VZ
418 int stride; /* physical row width in image buffer */
419
87d4e271
VZ
420 cinfo.err = jpeg_std_error(&jerr);
421 jerr.error_exit = wx_error_exit;
b59ff3c9 422
ef25e5a4
VZ
423 if (!verbose)
424 cinfo.err->output_message = wx_ignore_message;
deb2fec0 425
e9b964cf 426 /* Establish the setjmp return context for wx_error_exit to use. */
33ac7e6f 427 if (setjmp(jerr.setjmp_buffer))
58c837a4
RR
428 {
429 /* If we get here, the JPEG code has signaled an error.
430 * We need to clean up the JPEG object, close the input file, and return.
431 */
33ac7e6f 432 if (verbose)
af588446 433 {
58c837a4 434 wxLogError(_("JPEG: Couldn't save image."));
af588446 435 }
58c837a4 436 jpeg_destroy_compress(&cinfo);
7beb59f3 437 return false;
b59ff3c9
VS
438 }
439
e9c4b1a2 440 jpeg_create_compress(&cinfo);
e9b964cf 441 wx_jpeg_io_dest(&cinfo, stream);
995612e2 442
e9c4b1a2
JS
443 cinfo.image_width = image->GetWidth();
444 cinfo.image_height = image->GetHeight();
445 cinfo.input_components = 3;
446 cinfo.in_color_space = JCS_RGB;
447 jpeg_set_defaults(&cinfo);
5e5437e0
JS
448
449 // TODO: 3rd parameter is force_baseline, what value should this be?
450 // Code says: "If force_baseline is TRUE, the computed quantization table entries
451 // are limited to 1..255 for JPEG baseline compatibility."
452 // 'Quality' is a number between 0 (terrible) and 100 (very good).
453 // The default (in jcparam.c, jpeg_set_defaults) is 75,
454 // and force_baseline is TRUE.
9a679df6
JS
455 if (image->HasOption(wxIMAGE_OPTION_QUALITY))
456 jpeg_set_quality(&cinfo, image->GetOptionInt(wxIMAGE_OPTION_QUALITY), TRUE);
5e5437e0 457
fe9308c6 458 // set the resolution fields in the output file
361f4288
VZ
459 int resX, resY;
460 wxImageResolution res = GetResolutionFromOptions(*image, &resX, &resY);
461 if ( res != wxIMAGE_RESOLUTION_NONE )
fe9308c6
VZ
462 {
463 cinfo.X_density = resX;
464 cinfo.Y_density = resY;
89d38f8c 465
361f4288
VZ
466 // it so happens that wxIMAGE_RESOLUTION_INCHES/CM values are the same
467 // ones as used by libjpeg, so we can assign them directly
468 cinfo.density_unit = res;
89d38f8c
VS
469 }
470
e9c4b1a2 471 jpeg_start_compress(&cinfo, TRUE);
995612e2
VZ
472
473 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
e9c4b1a2
JS
474 image_buffer = image->GetData();
475 while (cinfo.next_scanline < cinfo.image_height) {
476 row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
477 jpeg_write_scanlines( &cinfo, row_pointer, 1 );
478 }
479 jpeg_finish_compress(&cinfo);
480 jpeg_destroy_compress(&cinfo);
995612e2 481
7beb59f3 482 return true;
e9c4b1a2 483}
e9c4b1a2 484
3ca6a5f0
BP
485#ifdef __VISUALC__
486 #pragma warning(default:4611)
487#endif /* VC++ */
0828c087 488
995612e2 489bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
0828c087
VS
490{
491 unsigned char hdr[2];
995612e2 492
8faef7cc 493 if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) // it's ok to modify the stream position here
7beb59f3 494 return false;
79fa2374 495
79fa2374 496 return hdr[0] == 0xFF && hdr[1] == 0xD8;
0828c087
VS
497}
498
9ab6ee85
GRG
499#endif // wxUSE_STREAMS
500
ccec9093
VZ
501/*static*/ wxVersionInfo wxJPEGHandler::GetLibraryVersionInfo()
502{
503 return wxVersionInfo("libjpeg", JPEG_LIB_VERSION/10, JPEG_LIB_VERSION%10);
504}
505
9ab6ee85 506#endif // wxUSE_LIBJPEG