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.
37 #define boolean wxHACK_BOOLEAN
42 #if defined(__WXMSW__)
48 #ifndef HAVE_WXJPEG_BOOLEAN
49 typedef boolean wxjpeg_boolean
;
52 #include "wx/filefn.h"
53 #include "wx/wfstream.h"
57 // For JPEG library error handling
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
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
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)
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
82 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
86 //------------- JPEG Data Source Manager
88 #define JPEG_IO_BUFFER_SIZE 2048
91 struct jpeg_source_mgr pub
; /* public fields */
93 JOCTET
* buffer
; /* start of buffer */
94 wxInputStream
*stream
;
97 typedef wx_source_mgr
* wx_src_ptr
;
102 CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
106 CPP_METHODDEF(wxjpeg_boolean
) wx_fill_input_buffer ( j_decompress_ptr cinfo
)
108 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
110 src
->pub
.next_input_byte
= src
->buffer
;
111 src
->pub
.bytes_in_buffer
= src
->stream
->Read(src
->buffer
, JPEG_IO_BUFFER_SIZE
).LastRead();
113 if (src
->pub
.bytes_in_buffer
== 0) // check for end-of-stream
115 // Insert a fake EOI marker
116 src
->buffer
[0] = 0xFF;
117 src
->buffer
[1] = JPEG_EOI
;
118 src
->pub
.bytes_in_buffer
= 2;
123 CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
127 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
129 while (num_bytes
> (long)src
->pub
.bytes_in_buffer
)
131 num_bytes
-= (long) src
->pub
.bytes_in_buffer
;
132 src
->pub
.fill_input_buffer(cinfo
);
134 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
135 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
139 CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo
)
141 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
143 if (src
->pub
.bytes_in_buffer
> 0)
144 src
->stream
->SeekI(-(long)src
->pub
.bytes_in_buffer
, wxFromCurrent
);
145 delete[] src
->buffer
;
149 // JPEG error manager:
151 struct wx_error_mgr
{
152 struct jpeg_error_mgr pub
; /* "public" fields */
154 jmp_buf setjmp_buffer
; /* for return to caller */
157 typedef struct wx_error_mgr
* wx_error_ptr
;
160 * Here's the routine that will replace the standard error_exit method:
163 CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo
)
165 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
166 wx_error_ptr myerr
= (wx_error_ptr
) cinfo
->err
;
168 /* Always display the message. */
169 /* We could postpone this until after returning, if we chose. */
170 (*cinfo
->err
->output_message
) (cinfo
);
172 /* Return control to the setjmp point */
173 longjmp(myerr
->setjmp_buffer
, 1);
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".
182 CPP_METHODDEF(void) wx_ignore_message (j_common_ptr
WXUNUSED(cinfo
))
186 void wx_jpeg_io_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
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
,
193 sizeof(wx_source_mgr
));
195 src
= (wx_src_ptr
) cinfo
->src
;
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
;
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
;
204 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
205 src
->pub
.term_source
= wx_term_source
;
210 static inline void wx_cmyk_to_rgb(unsigned char* rgb
, const unsigned char* cmyk
)
212 register int k
= 255 - cmyk
[3];
213 register int k2
= cmyk
[3];
216 c
= k
+ k2
* (255 - cmyk
[0]) / 255;
217 rgb
[0] = (unsigned char)((c
> 255) ? 0 : (255 - c
));
219 c
= k
+ k2
* (255 - cmyk
[1]) / 255;
220 rgb
[1] = (unsigned char)((c
> 255) ? 0 : (255 - c
));
222 c
= k
+ k2
* (255 - cmyk
[2]) / 255;
223 rgb
[2] = (unsigned char)((c
> 255) ? 0 : (255 - c
));
226 // temporarily disable the warning C4611 (interaction between '_setjmp' and
227 // C++ object destruction is non-portable) - I don't see any dtors here
229 #pragma warning(disable:4611)
232 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
234 struct jpeg_decompress_struct cinfo
;
235 struct wx_error_mgr jerr
;
239 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
240 jerr
.pub
.error_exit
= wx_error_exit
;
243 cinfo
.err
->output_message
= wx_ignore_message
;
245 /* Establish the setjmp return context for wx_error_exit to use. */
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.
251 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
252 (cinfo
.src
->term_source
)(&cinfo
);
253 jpeg_destroy_decompress(&cinfo
);
254 if (image
->Ok()) image
->Destroy();
258 jpeg_create_decompress( &cinfo
);
259 wx_jpeg_io_src( &cinfo
, stream
);
260 jpeg_read_header( &cinfo
, TRUE
);
263 if ((cinfo
.out_color_space
== JCS_CMYK
) || (cinfo
.out_color_space
== JCS_YCCK
))
265 cinfo
.out_color_space
= JCS_CMYK
;
268 else // all the rest is treated as RGB
270 cinfo
.out_color_space
= JCS_RGB
;
274 jpeg_start_decompress( &cinfo
);
276 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
278 jpeg_finish_decompress( &cinfo
);
279 jpeg_destroy_decompress( &cinfo
);
282 image
->SetMask( false );
283 ptr
= image
->GetData();
285 unsigned stride
= cinfo
.output_width
* bytesPerPixel
;
286 JSAMPARRAY tempbuf
= (*cinfo
.mem
->alloc_sarray
)
287 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
289 while ( cinfo
.output_scanline
< cinfo
.output_height
)
291 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
292 if (cinfo
.out_color_space
== JCS_RGB
)
294 memcpy( ptr
, tempbuf
[0], stride
);
299 const unsigned char* inptr
= (const unsigned char*) tempbuf
[0];
300 for (size_t i
= 0; i
< cinfo
.output_width
; i
++)
302 wx_cmyk_to_rgb(ptr
, inptr
);
309 // set up resolution if available: it's part of optional JFIF APP0 chunk
310 if ( cinfo
.saw_JFIF_marker
)
312 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONX
, cinfo
.X_density
);
313 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONY
, cinfo
.Y_density
);
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
);
320 jpeg_finish_decompress( &cinfo
);
321 jpeg_destroy_decompress( &cinfo
);
326 struct jpeg_destination_mgr pub
;
328 wxOutputStream
*stream
;
330 } wx_destination_mgr
;
332 typedef wx_destination_mgr
* wx_dest_ptr
;
334 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
339 CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo
)
341 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
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
;
351 CPP_METHODDEF(wxjpeg_boolean
) wx_empty_output_buffer (j_compress_ptr cinfo
)
353 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
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
;
361 CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo
)
363 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
364 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
365 /* Write any data remaining in the buffer */
367 dest
->stream
->Write(dest
->buffer
, datacount
);
370 GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
374 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
375 cinfo
->dest
= (struct jpeg_destination_mgr
*)
376 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
377 sizeof(wx_destination_mgr
));
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
;
384 dest
->stream
= &outfile
;
389 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
391 struct jpeg_compress_struct cinfo
;
392 struct wx_error_mgr jerr
;
393 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
394 JSAMPLE
*image_buffer
;
395 int stride
; /* physical row width in image buffer */
397 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
398 jerr
.pub
.error_exit
= wx_error_exit
;
401 cinfo
.err
->output_message
= wx_ignore_message
;
403 /* Establish the setjmp return context for wx_error_exit to use. */
404 if (setjmp(jerr
.setjmp_buffer
))
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.
410 wxLogError(_("JPEG: Couldn't save image."));
411 jpeg_destroy_compress(&cinfo
);
415 jpeg_create_compress(&cinfo
);
416 wx_jpeg_io_dest(&cinfo
, stream
);
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
);
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.
430 if (image
->HasOption(wxIMAGE_OPTION_QUALITY
))
431 jpeg_set_quality(&cinfo
, image
->GetOptionInt(wxIMAGE_OPTION_QUALITY
), TRUE
);
433 // set the resolution fields in the output file
435 wxImageResolution res
= GetResolutionFromOptions(*image
, &resX
, &resY
);
436 if ( res
!= wxIMAGE_RESOLUTION_NONE
)
438 cinfo
.X_density
= resX
;
439 cinfo
.Y_density
= resY
;
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
;
446 jpeg_start_compress(&cinfo
, TRUE
);
448 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
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 );
454 jpeg_finish_compress(&cinfo
);
455 jpeg_destroy_compress(&cinfo
);
461 #pragma warning(default:4611)
464 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
466 unsigned char hdr
[2];
468 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
471 return hdr
[0] == 0xFF && hdr
[1] == 0xD8;
474 #endif // wxUSE_STREAMS
476 #endif // wxUSE_LIBJPEG