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