1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage JPEG handler
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "imagjpeg.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
23 #if wxUSE_IMAGE && wxUSE_LIBJPEG
25 #include "wx/imagjpeg.h"
26 #include "wx/bitmap.h"
31 // NB: Some compilers define boolean type in Windows headers
32 // (e.g. Watcom C++, but not Open Watcom).
33 // This causes a conflict with jmorecfg.h header from libjpeg, so we have
34 // to make sure libjpeg won't try to define boolean itself. This is done by
35 // defining HAVE_BOOLEAN.
36 #if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__DIGITALMARS__) || (defined(__WATCOMC__) && __WATCOMC__ < 1200))
38 #include "wx/msw/wrapwin.h"
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 my_source_mgr
* my_src_ptr
;
99 CPP_METHODDEF(void) my_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
103 CPP_METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr cinfo
)
105 my_src_ptr src
= (my_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) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
124 my_src_ptr src
= (my_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) my_term_source ( j_decompress_ptr cinfo
)
138 my_src_ptr src
= (my_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 my_error_mgr
{
149 struct jpeg_error_mgr pub
; /* "public" fields */
151 jmp_buf setjmp_buffer
; /* for return to caller */
154 typedef struct my_error_mgr
* my_error_ptr
;
157 * Here's the routine that will replace the standard error_exit method:
160 CPP_METHODDEF(void) my_error_exit (j_common_ptr cinfo
)
162 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
163 my_error_ptr myerr
= (my_error_ptr
) cinfo
->err
;
165 /* Always display the message. */
166 /* We could postpone this until after returning, if we chose. */
167 if (cinfo
->err
->output_message
) (*cinfo
->err
->output_message
) (cinfo
);
169 /* Return control to the setjmp point */
170 longjmp(myerr
->setjmp_buffer
, 1);
173 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
177 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
178 cinfo
->src
= (struct jpeg_source_mgr
*)
179 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
180 sizeof(my_source_mgr
));
182 src
= (my_src_ptr
) cinfo
->src
;
183 src
->pub
.bytes_in_buffer
= 0; /* forces fill_input_buffer on first read */
184 src
->buffer
= new JOCTET
[JPEG_IO_BUFFER_SIZE
];
185 src
->pub
.next_input_byte
= NULL
; /* until buffer loaded */
186 src
->stream
= &infile
;
188 src
->pub
.init_source
= my_init_source
;
189 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
190 src
->pub
.skip_input_data
= my_skip_input_data
;
191 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
192 src
->pub
.term_source
= my_term_source
;
196 // temporarily disable the warning C4611 (interaction between '_setjmp' and
197 // C++ object destruction is non-portable) - I don't see any dtors here
199 #pragma warning(disable:4611)
202 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
204 struct jpeg_decompress_struct cinfo
;
205 struct my_error_mgr jerr
;
211 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
212 jerr
.pub
.error_exit
= my_error_exit
;
214 if (!verbose
) cinfo
.err
->output_message
=NULL
;
216 /* Establish the setjmp return context for my_error_exit to use. */
217 if (setjmp(jerr
.setjmp_buffer
)) {
218 /* If we get here, the JPEG code has signaled an error.
219 * We need to clean up the JPEG object, close the input file, and return.
222 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
223 (cinfo
.src
->term_source
)(&cinfo
);
224 jpeg_destroy_decompress(&cinfo
);
225 if (image
->Ok()) image
->Destroy();
229 jpeg_create_decompress( &cinfo
);
230 jpeg_wxio_src( &cinfo
, stream
);
231 jpeg_read_header( &cinfo
, TRUE
);
232 cinfo
.out_color_space
= JCS_RGB
;
233 jpeg_start_decompress( &cinfo
);
235 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
237 jpeg_finish_decompress( &cinfo
);
238 jpeg_destroy_decompress( &cinfo
);
241 image
->SetMask( FALSE
);
242 ptr
= image
->GetData();
243 stride
= cinfo
.output_width
* 3;
244 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
245 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
247 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
248 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
249 memcpy( ptr
, tempbuf
[0], stride
);
252 jpeg_finish_decompress( &cinfo
);
253 jpeg_destroy_decompress( &cinfo
);
258 struct jpeg_destination_mgr pub
;
260 wxOutputStream
*stream
;
262 } my_destination_mgr
;
264 typedef my_destination_mgr
* my_dest_ptr
;
266 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
268 CPP_METHODDEF(void) init_destination (j_compress_ptr cinfo
)
270 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
272 /* Allocate the output buffer --- it will be released when done with image */
273 dest
->buffer
= (JOCTET
*)
274 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
275 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
276 dest
->pub
.next_output_byte
= dest
->buffer
;
277 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
280 CPP_METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
282 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
284 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
285 dest
->pub
.next_output_byte
= dest
->buffer
;
286 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
290 CPP_METHODDEF(void) term_destination (j_compress_ptr cinfo
)
292 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
293 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
294 /* Write any data remaining in the buffer */
296 dest
->stream
->Write(dest
->buffer
, datacount
);
299 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
303 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
304 cinfo
->dest
= (struct jpeg_destination_mgr
*)
305 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
306 sizeof(my_destination_mgr
));
309 dest
= (my_dest_ptr
) cinfo
->dest
;
310 dest
->pub
.init_destination
= init_destination
;
311 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
312 dest
->pub
.term_destination
= term_destination
;
313 dest
->stream
= &outfile
;
316 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
318 struct jpeg_compress_struct cinfo
;
319 struct my_error_mgr jerr
;
320 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
321 JSAMPLE
*image_buffer
;
322 int stride
; /* physical row width in image buffer */
324 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
325 jerr
.pub
.error_exit
= my_error_exit
;
327 if (!verbose
) cinfo
.err
->output_message
=NULL
;
329 /* Establish the setjmp return context for my_error_exit to use. */
330 if (setjmp(jerr
.setjmp_buffer
))
332 /* If we get here, the JPEG code has signaled an error.
333 * We need to clean up the JPEG object, close the input file, and return.
336 wxLogError(_("JPEG: Couldn't save image."));
337 jpeg_destroy_compress(&cinfo
);
341 jpeg_create_compress(&cinfo
);
342 jpeg_wxio_dest(&cinfo
, stream
);
344 cinfo
.image_width
= image
->GetWidth();
345 cinfo
.image_height
= image
->GetHeight();
346 cinfo
.input_components
= 3;
347 cinfo
.in_color_space
= JCS_RGB
;
348 jpeg_set_defaults(&cinfo
);
350 // TODO: 3rd parameter is force_baseline, what value should this be?
351 // Code says: "If force_baseline is TRUE, the computed quantization table entries
352 // are limited to 1..255 for JPEG baseline compatibility."
353 // 'Quality' is a number between 0 (terrible) and 100 (very good).
354 // The default (in jcparam.c, jpeg_set_defaults) is 75,
355 // and force_baseline is TRUE.
356 if (image
->HasOption(wxT("quality")))
357 jpeg_set_quality(&cinfo
, image
->GetOptionInt(wxT("quality")), TRUE
);
359 // sets the resolution fields in the output file
360 if (image
->HasOption(wxIMAGE_OPTION_RESOLUTION
))
363 cinfo
.Y_density
= image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
366 // sets the resolution unit field in the output file
367 // wxIMAGE_RESOLUTION_INCHES for inches
368 // wxIMAGE_RESOLUTION_CM for centimeters
369 if (image
->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT
))
371 cinfo
.density_unit
= image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
374 jpeg_start_compress(&cinfo
, TRUE
);
376 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
377 image_buffer
= image
->GetData();
378 while (cinfo
.next_scanline
< cinfo
.image_height
) {
379 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
380 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
382 jpeg_finish_compress(&cinfo
);
383 jpeg_destroy_compress(&cinfo
);
389 #pragma warning(default:4611)
392 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
394 unsigned char hdr
[2];
396 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
399 return hdr
[0] == 0xFF && hdr
[1] == 0xD8;
402 #endif // wxUSE_STREAMS
404 #endif // wxUSE_LIBJPEG