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
24 #include "wx/imagjpeg.h"
25 #include "wx/bitmap.h"
28 // NB: Some compilers define boolean type in Windows headers
29 // (e.g. Watcom C++, but not Open Watcom).
30 // This causes a conflict with jmorecfg.h header from libjpeg, so we have
31 // to make sure libjpeg won't try to define boolean itself. This is done by
32 // defining HAVE_BOOLEAN.
33 #if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__DIGITALMARS__))
35 #include "wx/msw/wrapwin.h"
40 #if defined(__WXMSW__)
46 #include "wx/filefn.h"
47 #include "wx/wfstream.h"
49 #include "wx/module.h"
53 // 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
;
99 CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
103 CPP_METHODDEF(boolean
) wx_fill_input_buffer ( j_decompress_ptr cinfo
)
105 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
107 src
->pub
.next_input_byte
= src
->buffer
;
108 src
->pub
.bytes_in_buffer
= src
->stream
->Read(src
->buffer
, JPEG_IO_BUFFER_SIZE
).LastRead();
110 if (src
->pub
.bytes_in_buffer
== 0) // check for end-of-stream
112 // Insert a fake EOI marker
113 src
->buffer
[0] = 0xFF;
114 src
->buffer
[1] = JPEG_EOI
;
115 src
->pub
.bytes_in_buffer
= 2;
120 CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
124 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
126 while (num_bytes
> (long)src
->pub
.bytes_in_buffer
)
128 num_bytes
-= (long) src
->pub
.bytes_in_buffer
;
129 src
->pub
.fill_input_buffer(cinfo
);
131 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
132 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
136 CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo
)
138 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
140 if (src
->pub
.bytes_in_buffer
> 0)
141 src
->stream
->SeekI(-(long)src
->pub
.bytes_in_buffer
, wxFromCurrent
);
142 delete[] src
->buffer
;
146 // JPEG error manager:
148 struct wx_error_mgr
{
149 struct jpeg_error_mgr pub
; /* "public" fields */
151 jmp_buf setjmp_buffer
; /* for return to caller */
154 typedef struct wx_error_mgr
* wx_error_ptr
;
157 * Here's the routine that will replace the standard error_exit method:
160 CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo
)
162 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
163 wx_error_ptr myerr
= (wx_error_ptr
) cinfo
->err
;
165 /* Always display the message. */
166 /* We could postpone this until after returning, if we chose. */
167 (*cinfo
->err
->output_message
) (cinfo
);
169 /* Return control to the setjmp point */
170 longjmp(myerr
->setjmp_buffer
, 1);
174 * This will replace the standard output_message method when the user
175 * wants us to be silent (verbose==false). We must have such method instead of
176 * simply using NULL for cinfo->err->output_message because it's called
177 * unconditionally from within libjpeg when there's "garbage input".
179 CPP_METHODDEF(void) wx_ignore_message (j_common_ptr
WXUNUSED(cinfo
))
183 void wx_jpeg_io_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
187 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
188 cinfo
->src
= (struct jpeg_source_mgr
*)
189 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
190 sizeof(wx_source_mgr
));
192 src
= (wx_src_ptr
) cinfo
->src
;
193 src
->pub
.bytes_in_buffer
= 0; /* forces fill_input_buffer on first read */
194 src
->buffer
= new JOCTET
[JPEG_IO_BUFFER_SIZE
];
195 src
->pub
.next_input_byte
= NULL
; /* until buffer loaded */
196 src
->stream
= &infile
;
198 src
->pub
.init_source
= wx_init_source
;
199 src
->pub
.fill_input_buffer
= wx_fill_input_buffer
;
200 src
->pub
.skip_input_data
= wx_skip_input_data
;
201 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
202 src
->pub
.term_source
= wx_term_source
;
206 // temporarily disable the warning C4611 (interaction between '_setjmp' and
207 // C++ object destruction is non-portable) - I don't see any dtors here
209 #pragma warning(disable:4611)
212 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
214 struct jpeg_decompress_struct cinfo
;
215 struct wx_error_mgr jerr
;
221 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
222 jerr
.pub
.error_exit
= wx_error_exit
;
225 cinfo
.err
->output_message
= wx_ignore_message
;
227 /* Establish the setjmp return context for wx_error_exit to use. */
228 if (setjmp(jerr
.setjmp_buffer
)) {
229 /* If we get here, the JPEG code has signaled an error.
230 * We need to clean up the JPEG object, close the input file, and return.
233 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
234 (cinfo
.src
->term_source
)(&cinfo
);
235 jpeg_destroy_decompress(&cinfo
);
236 if (image
->Ok()) image
->Destroy();
240 jpeg_create_decompress( &cinfo
);
241 wx_jpeg_io_src( &cinfo
, stream
);
242 jpeg_read_header( &cinfo
, TRUE
);
243 cinfo
.out_color_space
= JCS_RGB
;
244 jpeg_start_decompress( &cinfo
);
246 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
248 jpeg_finish_decompress( &cinfo
);
249 jpeg_destroy_decompress( &cinfo
);
252 image
->SetMask( false );
253 ptr
= image
->GetData();
254 stride
= cinfo
.output_width
* 3;
255 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
256 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
258 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
259 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
260 memcpy( ptr
, tempbuf
[0], stride
);
263 jpeg_finish_decompress( &cinfo
);
264 jpeg_destroy_decompress( &cinfo
);
269 struct jpeg_destination_mgr pub
;
271 wxOutputStream
*stream
;
273 } wx_destination_mgr
;
275 typedef wx_destination_mgr
* wx_dest_ptr
;
277 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
279 CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo
)
281 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
283 /* Allocate the output buffer --- it will be released when done with image */
284 dest
->buffer
= (JOCTET
*)
285 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
286 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
287 dest
->pub
.next_output_byte
= dest
->buffer
;
288 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
291 CPP_METHODDEF(boolean
) wx_empty_output_buffer (j_compress_ptr cinfo
)
293 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
295 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
296 dest
->pub
.next_output_byte
= dest
->buffer
;
297 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
301 CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo
)
303 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
304 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
305 /* Write any data remaining in the buffer */
307 dest
->stream
->Write(dest
->buffer
, datacount
);
310 GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
314 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
315 cinfo
->dest
= (struct jpeg_destination_mgr
*)
316 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
317 sizeof(wx_destination_mgr
));
320 dest
= (wx_dest_ptr
) cinfo
->dest
;
321 dest
->pub
.init_destination
= wx_init_destination
;
322 dest
->pub
.empty_output_buffer
= wx_empty_output_buffer
;
323 dest
->pub
.term_destination
= wx_term_destination
;
324 dest
->stream
= &outfile
;
327 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
329 struct jpeg_compress_struct cinfo
;
330 struct wx_error_mgr jerr
;
331 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
332 JSAMPLE
*image_buffer
;
333 int stride
; /* physical row width in image buffer */
335 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
336 jerr
.pub
.error_exit
= wx_error_exit
;
339 cinfo
.err
->output_message
= wx_ignore_message
;
341 /* Establish the setjmp return context for wx_error_exit to use. */
342 if (setjmp(jerr
.setjmp_buffer
))
344 /* If we get here, the JPEG code has signaled an error.
345 * We need to clean up the JPEG object, close the input file, and return.
348 wxLogError(_("JPEG: Couldn't save image."));
349 jpeg_destroy_compress(&cinfo
);
353 jpeg_create_compress(&cinfo
);
354 wx_jpeg_io_dest(&cinfo
, stream
);
356 cinfo
.image_width
= image
->GetWidth();
357 cinfo
.image_height
= image
->GetHeight();
358 cinfo
.input_components
= 3;
359 cinfo
.in_color_space
= JCS_RGB
;
360 jpeg_set_defaults(&cinfo
);
362 // TODO: 3rd parameter is force_baseline, what value should this be?
363 // Code says: "If force_baseline is TRUE, the computed quantization table entries
364 // are limited to 1..255 for JPEG baseline compatibility."
365 // 'Quality' is a number between 0 (terrible) and 100 (very good).
366 // The default (in jcparam.c, jpeg_set_defaults) is 75,
367 // and force_baseline is TRUE.
368 if (image
->HasOption(wxIMAGE_OPTION_QUALITY
))
369 jpeg_set_quality(&cinfo
, image
->GetOptionInt(wxIMAGE_OPTION_QUALITY
), TRUE
);
371 // set the resolution fields in the output file
374 if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
375 image
->HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
377 resX
= (UINT16
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
378 resY
= (UINT16
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
380 else if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTION
) )
383 resY
= (UINT16
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
393 cinfo
.X_density
= resX
;
394 cinfo
.Y_density
= resY
;
397 // sets the resolution unit field in the output file
398 // wxIMAGE_RESOLUTION_INCHES for inches
399 // wxIMAGE_RESOLUTION_CM for centimeters
400 if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT
) )
402 cinfo
.density_unit
= (UINT8
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
405 jpeg_start_compress(&cinfo
, TRUE
);
407 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
408 image_buffer
= image
->GetData();
409 while (cinfo
.next_scanline
< cinfo
.image_height
) {
410 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
411 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
413 jpeg_finish_compress(&cinfo
);
414 jpeg_destroy_compress(&cinfo
);
420 #pragma warning(default:4611)
423 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
425 unsigned char hdr
[2];
427 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
430 return hdr
[0] == 0xFF && hdr
[1] == 0xD8;
433 #endif // wxUSE_STREAMS
435 #endif // wxUSE_LIBJPEG