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