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
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // the standard definition of METHODDEF(type) from jmorecfg.h is "static type"
69 // which means that we can't declare the method functions as extern "C" - the
70 // compiler (rightfully) complains about the multiple storage classes in
73 // so we only add extern "C" when using our own, modified, jmorecfg.h - and use
74 // whatever we have in the system headers if this is what we use hoping that it
75 // should be ok (can't do anything else)
76 #ifdef JPEG_METHOD_LINKAGE
77 #define CPP_METHODDEF(type) extern "C" METHODDEF(type)
78 #else // not using our jmorecfg.h header
79 #define CPP_METHODDEF(type) METHODDEF(type)
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
90 //------------- JPEG Data Source Manager
92 #define JPEG_IO_BUFFER_SIZE 2048
95 struct jpeg_source_mgr pub
; /* public fields */
97 JOCTET
* buffer
; /* start of buffer */
98 wxInputStream
*stream
;
101 typedef wx_source_mgr
* wx_src_ptr
;
103 CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
107 CPP_METHODDEF(wxjpeg_boolean
) wx_fill_input_buffer ( j_decompress_ptr cinfo
)
109 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
111 src
->pub
.next_input_byte
= src
->buffer
;
112 src
->pub
.bytes_in_buffer
= src
->stream
->Read(src
->buffer
, JPEG_IO_BUFFER_SIZE
).LastRead();
114 if (src
->pub
.bytes_in_buffer
== 0) // check for end-of-stream
116 // Insert a fake EOI marker
117 src
->buffer
[0] = 0xFF;
118 src
->buffer
[1] = JPEG_EOI
;
119 src
->pub
.bytes_in_buffer
= 2;
124 CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
128 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
130 while (num_bytes
> (long)src
->pub
.bytes_in_buffer
)
132 num_bytes
-= (long) src
->pub
.bytes_in_buffer
;
133 src
->pub
.fill_input_buffer(cinfo
);
135 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
136 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
140 CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo
)
142 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
144 if (src
->pub
.bytes_in_buffer
> 0)
145 src
->stream
->SeekI(-(long)src
->pub
.bytes_in_buffer
, wxFromCurrent
);
146 delete[] src
->buffer
;
150 // JPEG error manager:
152 struct wx_error_mgr
{
153 struct jpeg_error_mgr pub
; /* "public" fields */
155 jmp_buf setjmp_buffer
; /* for return to caller */
158 typedef struct wx_error_mgr
* wx_error_ptr
;
161 * Here's the routine that will replace the standard error_exit method:
164 CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo
)
166 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
167 wx_error_ptr myerr
= (wx_error_ptr
) cinfo
->err
;
169 /* Always display the message. */
170 /* We could postpone this until after returning, if we chose. */
171 (*cinfo
->err
->output_message
) (cinfo
);
173 /* Return control to the setjmp point */
174 longjmp(myerr
->setjmp_buffer
, 1);
178 * This will replace the standard output_message method when the user
179 * wants us to be silent (verbose==false). We must have such method instead of
180 * simply using NULL for cinfo->err->output_message because it's called
181 * unconditionally from within libjpeg when there's "garbage input".
183 CPP_METHODDEF(void) wx_ignore_message (j_common_ptr
WXUNUSED(cinfo
))
187 void wx_jpeg_io_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
191 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
192 cinfo
->src
= (struct jpeg_source_mgr
*)
193 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
194 sizeof(wx_source_mgr
));
196 src
= (wx_src_ptr
) cinfo
->src
;
197 src
->pub
.bytes_in_buffer
= 0; /* forces fill_input_buffer on first read */
198 src
->buffer
= new JOCTET
[JPEG_IO_BUFFER_SIZE
];
199 src
->pub
.next_input_byte
= NULL
; /* until buffer loaded */
200 src
->stream
= &infile
;
202 src
->pub
.init_source
= wx_init_source
;
203 src
->pub
.fill_input_buffer
= wx_fill_input_buffer
;
204 src
->pub
.skip_input_data
= wx_skip_input_data
;
205 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
206 src
->pub
.term_source
= wx_term_source
;
209 static inline void wx_cmyk_to_rgb(unsigned char* rgb
, const unsigned char* cmyk
)
211 register int k
= 255 - cmyk
[3];
212 register int k2
= cmyk
[3];
215 c
= k
+ k2
* (255 - cmyk
[0]) / 255;
216 rgb
[0] = (unsigned char)((c
> 255) ? 0 : (255 - c
));
218 c
= k
+ k2
* (255 - cmyk
[1]) / 255;
219 rgb
[1] = (unsigned char)((c
> 255) ? 0 : (255 - c
));
221 c
= k
+ k2
* (255 - cmyk
[2]) / 255;
222 rgb
[2] = (unsigned char)((c
> 255) ? 0 : (255 - c
));
225 // temporarily disable the warning C4611 (interaction between '_setjmp' and
226 // C++ object destruction is non-portable) - I don't see any dtors here
228 #pragma warning(disable:4611)
231 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
233 struct jpeg_decompress_struct cinfo
;
234 struct wx_error_mgr jerr
;
238 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
239 jerr
.pub
.error_exit
= wx_error_exit
;
242 cinfo
.err
->output_message
= wx_ignore_message
;
244 /* Establish the setjmp return context for wx_error_exit to use. */
245 if (setjmp(jerr
.setjmp_buffer
)) {
246 /* If we get here, the JPEG code has signaled an error.
247 * We need to clean up the JPEG object, close the input file, and return.
250 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
251 (cinfo
.src
->term_source
)(&cinfo
);
252 jpeg_destroy_decompress(&cinfo
);
253 if (image
->Ok()) image
->Destroy();
257 jpeg_create_decompress( &cinfo
);
258 wx_jpeg_io_src( &cinfo
, stream
);
259 jpeg_read_header( &cinfo
, TRUE
);
262 if ((cinfo
.out_color_space
== JCS_CMYK
) || (cinfo
.out_color_space
== JCS_YCCK
))
264 cinfo
.out_color_space
= JCS_CMYK
;
267 else // all the rest is treated as RGB
269 cinfo
.out_color_space
= JCS_RGB
;
273 jpeg_start_decompress( &cinfo
);
275 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
277 jpeg_finish_decompress( &cinfo
);
278 jpeg_destroy_decompress( &cinfo
);
281 image
->SetMask( false );
282 ptr
= image
->GetData();
284 unsigned stride
= cinfo
.output_width
* bytesPerPixel
;
285 JSAMPARRAY tempbuf
= (*cinfo
.mem
->alloc_sarray
)
286 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
288 while ( cinfo
.output_scanline
< cinfo
.output_height
)
290 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
291 if (cinfo
.out_color_space
== JCS_RGB
)
293 memcpy( ptr
, tempbuf
[0], stride
);
298 const unsigned char* inptr
= (const unsigned char*) tempbuf
[0];
299 for (size_t i
= 0; i
< cinfo
.output_width
; i
++)
301 wx_cmyk_to_rgb(ptr
, inptr
);
308 jpeg_finish_decompress( &cinfo
);
309 jpeg_destroy_decompress( &cinfo
);
314 struct jpeg_destination_mgr pub
;
316 wxOutputStream
*stream
;
318 } wx_destination_mgr
;
320 typedef wx_destination_mgr
* wx_dest_ptr
;
322 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
324 CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo
)
326 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
328 /* Allocate the output buffer --- it will be released when done with image */
329 dest
->buffer
= (JOCTET
*)
330 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
331 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
332 dest
->pub
.next_output_byte
= dest
->buffer
;
333 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
336 CPP_METHODDEF(wxjpeg_boolean
) wx_empty_output_buffer (j_compress_ptr cinfo
)
338 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
340 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
341 dest
->pub
.next_output_byte
= dest
->buffer
;
342 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
346 CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo
)
348 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
349 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
350 /* Write any data remaining in the buffer */
352 dest
->stream
->Write(dest
->buffer
, datacount
);
355 GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
359 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
360 cinfo
->dest
= (struct jpeg_destination_mgr
*)
361 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
362 sizeof(wx_destination_mgr
));
365 dest
= (wx_dest_ptr
) cinfo
->dest
;
366 dest
->pub
.init_destination
= wx_init_destination
;
367 dest
->pub
.empty_output_buffer
= wx_empty_output_buffer
;
368 dest
->pub
.term_destination
= wx_term_destination
;
369 dest
->stream
= &outfile
;
372 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
374 struct jpeg_compress_struct cinfo
;
375 struct wx_error_mgr jerr
;
376 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
377 JSAMPLE
*image_buffer
;
378 int stride
; /* physical row width in image buffer */
380 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
381 jerr
.pub
.error_exit
= wx_error_exit
;
384 cinfo
.err
->output_message
= wx_ignore_message
;
386 /* Establish the setjmp return context for wx_error_exit to use. */
387 if (setjmp(jerr
.setjmp_buffer
))
389 /* If we get here, the JPEG code has signaled an error.
390 * We need to clean up the JPEG object, close the input file, and return.
393 wxLogError(_("JPEG: Couldn't save image."));
394 jpeg_destroy_compress(&cinfo
);
398 jpeg_create_compress(&cinfo
);
399 wx_jpeg_io_dest(&cinfo
, stream
);
401 cinfo
.image_width
= image
->GetWidth();
402 cinfo
.image_height
= image
->GetHeight();
403 cinfo
.input_components
= 3;
404 cinfo
.in_color_space
= JCS_RGB
;
405 jpeg_set_defaults(&cinfo
);
407 // TODO: 3rd parameter is force_baseline, what value should this be?
408 // Code says: "If force_baseline is TRUE, the computed quantization table entries
409 // are limited to 1..255 for JPEG baseline compatibility."
410 // 'Quality' is a number between 0 (terrible) and 100 (very good).
411 // The default (in jcparam.c, jpeg_set_defaults) is 75,
412 // and force_baseline is TRUE.
413 if (image
->HasOption(wxIMAGE_OPTION_QUALITY
))
414 jpeg_set_quality(&cinfo
, image
->GetOptionInt(wxIMAGE_OPTION_QUALITY
), TRUE
);
416 // set the resolution fields in the output file
418 wxImageResolution res
= GetResolutionFromOptions(*image
, &resX
, &resY
);
419 if ( res
!= wxIMAGE_RESOLUTION_NONE
)
421 cinfo
.X_density
= resX
;
422 cinfo
.Y_density
= resY
;
424 // it so happens that wxIMAGE_RESOLUTION_INCHES/CM values are the same
425 // ones as used by libjpeg, so we can assign them directly
426 cinfo
.density_unit
= res
;
429 jpeg_start_compress(&cinfo
, TRUE
);
431 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
432 image_buffer
= image
->GetData();
433 while (cinfo
.next_scanline
< cinfo
.image_height
) {
434 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
435 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
437 jpeg_finish_compress(&cinfo
);
438 jpeg_destroy_compress(&cinfo
);
444 #pragma warning(default:4611)
447 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
449 unsigned char hdr
[2];
451 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
454 return hdr
[0] == 0xFF && hdr
[1] == 0xD8;
457 #endif // wxUSE_STREAMS
459 #endif // wxUSE_LIBJPEG