1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagjpeg.cpp
3 // Purpose: wxImage JPEG handler
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 #if wxUSE_IMAGE && wxUSE_LIBJPEG
19 #include "wx/imagjpeg.h"
25 #include "wx/bitmap.h"
26 #include "wx/module.h"
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.
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.
35 #if defined wxHACK_BOOLEAN || defined __RPCNDR_H__ || defined __WINE_RPCNDR_H
39 #define boolean wxHACK_BOOLEAN
47 #if defined(__WXMSW__)
53 #ifndef HAVE_WXJPEG_BOOLEAN
54 typedef boolean wxjpeg_boolean
;
57 #include "wx/filefn.h"
58 #include "wx/wfstream.h"
62 // For JPEG library error handling
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
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
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)
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
91 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
95 //------------- JPEG Data Source Manager
97 #define JPEG_IO_BUFFER_SIZE 2048
100 struct jpeg_source_mgr pub
; /* public fields */
102 JOCTET
* buffer
; /* start of buffer */
103 wxInputStream
*stream
;
106 typedef wx_source_mgr
* wx_src_ptr
;
108 CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
112 CPP_METHODDEF(wxjpeg_boolean
) wx_fill_input_buffer ( j_decompress_ptr cinfo
)
114 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
116 src
->pub
.next_input_byte
= src
->buffer
;
117 src
->pub
.bytes_in_buffer
= src
->stream
->Read(src
->buffer
, JPEG_IO_BUFFER_SIZE
).LastRead();
119 if (src
->pub
.bytes_in_buffer
== 0) // check for end-of-stream
121 // Insert a fake EOI marker
122 src
->buffer
[0] = 0xFF;
123 src
->buffer
[1] = JPEG_EOI
;
124 src
->pub
.bytes_in_buffer
= 2;
129 CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
133 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
135 while (num_bytes
> (long)src
->pub
.bytes_in_buffer
)
137 num_bytes
-= (long) src
->pub
.bytes_in_buffer
;
138 src
->pub
.fill_input_buffer(cinfo
);
140 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
141 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
145 CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo
)
147 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
149 if (src
->pub
.bytes_in_buffer
> 0)
150 src
->stream
->SeekI(-(long)src
->pub
.bytes_in_buffer
, wxFromCurrent
);
151 delete[] src
->buffer
;
155 // JPEG error manager:
157 struct wx_error_mgr
{
158 struct jpeg_error_mgr pub
; /* "public" fields */
160 jmp_buf setjmp_buffer
; /* for return to caller */
163 typedef struct wx_error_mgr
* wx_error_ptr
;
166 * Here's the routine that will replace the standard error_exit method:
169 CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo
)
171 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
172 wx_error_ptr myerr
= (wx_error_ptr
) cinfo
->err
;
174 /* Always display the message. */
175 /* We could postpone this until after returning, if we chose. */
176 (*cinfo
->err
->output_message
) (cinfo
);
178 /* Return control to the setjmp point */
179 longjmp(myerr
->setjmp_buffer
, 1);
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".
188 CPP_METHODDEF(void) wx_ignore_message (j_common_ptr
WXUNUSED(cinfo
))
192 void wx_jpeg_io_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
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
,
199 sizeof(wx_source_mgr
));
201 src
= (wx_src_ptr
) cinfo
->src
;
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
;
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
;
210 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
211 src
->pub
.term_source
= wx_term_source
;
214 static inline void wx_cmyk_to_rgb(unsigned char* rgb
, const unsigned char* cmyk
)
216 register int k
= 255 - cmyk
[3];
217 register int k2
= cmyk
[3];
220 c
= k
+ k2
* (255 - cmyk
[0]) / 255;
221 rgb
[0] = (unsigned char)((c
> 255) ? 0 : (255 - c
));
223 c
= k
+ k2
* (255 - cmyk
[1]) / 255;
224 rgb
[1] = (unsigned char)((c
> 255) ? 0 : (255 - c
));
226 c
= k
+ k2
* (255 - cmyk
[2]) / 255;
227 rgb
[2] = (unsigned char)((c
> 255) ? 0 : (255 - c
));
230 // temporarily disable the warning C4611 (interaction between '_setjmp' and
231 // C++ object destruction is non-portable) - I don't see any dtors here
233 #pragma warning(disable:4611)
236 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
238 struct jpeg_decompress_struct cinfo
;
239 struct wx_error_mgr jerr
;
243 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
244 jerr
.pub
.error_exit
= wx_error_exit
;
247 cinfo
.err
->output_message
= wx_ignore_message
;
249 /* Establish the setjmp return context for wx_error_exit to use. */
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.
255 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
256 (cinfo
.src
->term_source
)(&cinfo
);
257 jpeg_destroy_decompress(&cinfo
);
258 if (image
->Ok()) image
->Destroy();
262 jpeg_create_decompress( &cinfo
);
263 wx_jpeg_io_src( &cinfo
, stream
);
264 jpeg_read_header( &cinfo
, TRUE
);
267 if ((cinfo
.out_color_space
== JCS_CMYK
) || (cinfo
.out_color_space
== JCS_YCCK
))
269 cinfo
.out_color_space
= JCS_CMYK
;
272 else // all the rest is treated as RGB
274 cinfo
.out_color_space
= JCS_RGB
;
278 jpeg_start_decompress( &cinfo
);
280 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
282 jpeg_finish_decompress( &cinfo
);
283 jpeg_destroy_decompress( &cinfo
);
286 image
->SetMask( false );
287 ptr
= image
->GetData();
289 unsigned stride
= cinfo
.output_width
* bytesPerPixel
;
290 JSAMPARRAY tempbuf
= (*cinfo
.mem
->alloc_sarray
)
291 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
293 while ( cinfo
.output_scanline
< cinfo
.output_height
)
295 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
296 if (cinfo
.out_color_space
== JCS_RGB
)
298 memcpy( ptr
, tempbuf
[0], stride
);
303 const unsigned char* inptr
= (const unsigned char*) tempbuf
[0];
304 for (size_t i
= 0; i
< cinfo
.output_width
; i
++)
306 wx_cmyk_to_rgb(ptr
, inptr
);
313 jpeg_finish_decompress( &cinfo
);
314 jpeg_destroy_decompress( &cinfo
);
319 struct jpeg_destination_mgr pub
;
321 wxOutputStream
*stream
;
323 } wx_destination_mgr
;
325 typedef wx_destination_mgr
* wx_dest_ptr
;
327 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
329 CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo
)
331 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
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
;
341 CPP_METHODDEF(wxjpeg_boolean
) wx_empty_output_buffer (j_compress_ptr cinfo
)
343 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
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
;
351 CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo
)
353 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
354 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
355 /* Write any data remaining in the buffer */
357 dest
->stream
->Write(dest
->buffer
, datacount
);
360 GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
364 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
365 cinfo
->dest
= (struct jpeg_destination_mgr
*)
366 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
367 sizeof(wx_destination_mgr
));
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
;
374 dest
->stream
= &outfile
;
377 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
379 struct jpeg_compress_struct cinfo
;
380 struct wx_error_mgr jerr
;
381 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
382 JSAMPLE
*image_buffer
;
383 int stride
; /* physical row width in image buffer */
385 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
386 jerr
.pub
.error_exit
= wx_error_exit
;
389 cinfo
.err
->output_message
= wx_ignore_message
;
391 /* Establish the setjmp return context for wx_error_exit to use. */
392 if (setjmp(jerr
.setjmp_buffer
))
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.
398 wxLogError(_("JPEG: Couldn't save image."));
399 jpeg_destroy_compress(&cinfo
);
403 jpeg_create_compress(&cinfo
);
404 wx_jpeg_io_dest(&cinfo
, stream
);
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
);
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.
418 if (image
->HasOption(wxIMAGE_OPTION_QUALITY
))
419 jpeg_set_quality(&cinfo
, image
->GetOptionInt(wxIMAGE_OPTION_QUALITY
), TRUE
);
421 // set the resolution fields in the output file
424 if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
425 image
->HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
427 resX
= (UINT16
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
428 resY
= (UINT16
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
430 else if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTION
) )
433 resY
= (UINT16
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
443 cinfo
.X_density
= resX
;
444 cinfo
.Y_density
= resY
;
447 // sets the resolution unit field in the output file
448 // wxIMAGE_RESOLUTION_INCHES for inches
449 // wxIMAGE_RESOLUTION_CM for centimeters
450 if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT
) )
452 cinfo
.density_unit
= (UINT8
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
455 jpeg_start_compress(&cinfo
, TRUE
);
457 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
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 );
463 jpeg_finish_compress(&cinfo
);
464 jpeg_destroy_compress(&cinfo
);
470 #pragma warning(default:4611)
473 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
475 unsigned char hdr
[2];
477 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
480 return hdr
[0] == 0xFF && hdr
[1] == 0xD8;
483 #endif // wxUSE_STREAMS
485 #endif // wxUSE_LIBJPEG