1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage JPEG handler
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
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 (e.g. Watcom C++).
32 // This causes a conflict with jmorecfg.h header from libjpeg, so we have
33 // to make sure libjpeg won't try to define boolean itself. This is done by
34 // defining HAVE_BOOLEAN.
35 #if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__WATCOMC__))
45 #include "wx/filefn.h"
46 #include "wx/wfstream.h"
48 #include "wx/module.h"
52 // For JPEG library error handling
61 // we can't use METHODDEF here as it includes static yet the functions must be
62 // extern "C" and these can't be used together!
64 #define METHODDEF(type) extern "C" type wxC_CALLING_CONV
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
70 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
74 //------------- JPEG Data Source Manager
76 #define JPEG_IO_BUFFER_SIZE 2048
79 struct jpeg_source_mgr pub
; /* public fields */
81 JOCTET
* buffer
; /* start of buffer */
82 wxInputStream
*stream
;
85 typedef my_source_mgr
* my_src_ptr
;
87 METHODDEF(void) my_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
91 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr cinfo
)
93 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
95 src
->pub
.next_input_byte
= src
->buffer
;
96 src
->pub
.bytes_in_buffer
= src
->stream
->Read(src
->buffer
, JPEG_IO_BUFFER_SIZE
).LastRead();
98 if (src
->pub
.bytes_in_buffer
== 0) // check for end-of-stream
100 // Insert a fake EOI marker
101 src
->buffer
[0] = 0xFF;
102 src
->buffer
[1] = JPEG_EOI
;
103 src
->pub
.bytes_in_buffer
= 2;
108 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
112 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
114 while (num_bytes
> (long)src
->pub
.bytes_in_buffer
)
116 num_bytes
-= (long) src
->pub
.bytes_in_buffer
;
117 src
->pub
.fill_input_buffer(cinfo
);
119 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
120 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
124 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
126 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
128 if (src
->pub
.bytes_in_buffer
> 0)
129 src
->stream
->SeekI(-(long)src
->pub
.bytes_in_buffer
, wxFromCurrent
);
130 delete[] src
->buffer
;
134 // JPEG error manager:
136 struct my_error_mgr
{
137 struct jpeg_error_mgr pub
; /* "public" fields */
139 jmp_buf setjmp_buffer
; /* for return to caller */
142 typedef struct my_error_mgr
* my_error_ptr
;
145 * Here's the routine that will replace the standard error_exit method:
148 METHODDEF(void) my_error_exit (j_common_ptr cinfo
)
150 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
151 my_error_ptr myerr
= (my_error_ptr
) cinfo
->err
;
153 /* Always display the message. */
154 /* We could postpone this until after returning, if we chose. */
155 if (cinfo
->err
->output_message
) (*cinfo
->err
->output_message
) (cinfo
);
157 /* Return control to the setjmp point */
158 longjmp(myerr
->setjmp_buffer
, 1);
161 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
165 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
166 cinfo
->src
= (struct jpeg_source_mgr
*)
167 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
168 sizeof(my_source_mgr
));
169 src
= (my_src_ptr
) cinfo
->src
;
171 src
= (my_src_ptr
) cinfo
->src
;
172 src
->pub
.bytes_in_buffer
= 0; /* forces fill_input_buffer on first read */
173 src
->buffer
= new JOCTET
[JPEG_IO_BUFFER_SIZE
];
174 src
->pub
.next_input_byte
= NULL
; /* until buffer loaded */
175 src
->stream
= &infile
;
177 src
->pub
.init_source
= my_init_source
;
178 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
179 src
->pub
.skip_input_data
= my_skip_input_data
;
180 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
181 src
->pub
.term_source
= my_term_source
;
185 // temporarily disable the warning C4611 (interaction between '_setjmp' and
186 // C++ object destruction is non-portable) - I don't see any dtors here
188 #pragma warning(disable:4611)
191 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
193 struct jpeg_decompress_struct cinfo
;
194 struct my_error_mgr jerr
;
200 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
201 jerr
.pub
.error_exit
= my_error_exit
;
203 if (!verbose
) cinfo
.err
->output_message
=NULL
;
205 /* Establish the setjmp return context for my_error_exit to use. */
206 if (setjmp(jerr
.setjmp_buffer
)) {
207 /* If we get here, the JPEG code has signaled an error.
208 * We need to clean up the JPEG object, close the input file, and return.
211 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
212 (cinfo
.src
->term_source
)(&cinfo
);
213 jpeg_destroy_decompress(&cinfo
);
214 if (image
->Ok()) image
->Destroy();
218 jpeg_create_decompress( &cinfo
);
219 jpeg_wxio_src( &cinfo
, stream
);
220 jpeg_read_header( &cinfo
, TRUE
);
221 cinfo
.out_color_space
= JCS_RGB
;
222 jpeg_start_decompress( &cinfo
);
224 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
226 jpeg_finish_decompress( &cinfo
);
227 jpeg_destroy_decompress( &cinfo
);
230 image
->SetMask( FALSE
);
231 ptr
= image
->GetData();
232 stride
= cinfo
.output_width
* 3;
233 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
234 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
236 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
237 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
238 memcpy( ptr
, tempbuf
[0], stride
);
241 jpeg_finish_decompress( &cinfo
);
242 jpeg_destroy_decompress( &cinfo
);
247 struct jpeg_destination_mgr pub
;
249 wxOutputStream
*stream
;
251 } my_destination_mgr
;
253 typedef my_destination_mgr
* my_dest_ptr
;
255 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
257 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
259 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
261 /* Allocate the output buffer --- it will be released when done with image */
262 dest
->buffer
= (JOCTET
*)
263 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
264 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
265 dest
->pub
.next_output_byte
= dest
->buffer
;
266 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
269 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
271 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
273 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
274 dest
->pub
.next_output_byte
= dest
->buffer
;
275 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
279 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
281 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
282 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
283 /* Write any data remaining in the buffer */
285 dest
->stream
->Write(dest
->buffer
, datacount
);
288 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
292 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
293 cinfo
->dest
= (struct jpeg_destination_mgr
*)
294 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
295 sizeof(my_destination_mgr
));
298 dest
= (my_dest_ptr
) cinfo
->dest
;
299 dest
->pub
.init_destination
= init_destination
;
300 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
301 dest
->pub
.term_destination
= term_destination
;
302 dest
->stream
= &outfile
;
305 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
307 struct jpeg_compress_struct cinfo
;
308 struct my_error_mgr jerr
;
309 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
310 JSAMPLE
*image_buffer
;
311 int stride
; /* physical row width in image buffer */
313 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
314 jerr
.pub
.error_exit
= my_error_exit
;
316 if (!verbose
) cinfo
.err
->output_message
=NULL
;
318 /* Establish the setjmp return context for my_error_exit to use. */
319 if (setjmp(jerr
.setjmp_buffer
))
321 /* If we get here, the JPEG code has signaled an error.
322 * We need to clean up the JPEG object, close the input file, and return.
325 wxLogError(_("JPEG: Couldn't save image."));
326 jpeg_destroy_compress(&cinfo
);
330 jpeg_create_compress(&cinfo
);
331 jpeg_wxio_dest(&cinfo
, stream
);
333 cinfo
.image_width
= image
->GetWidth();
334 cinfo
.image_height
= image
->GetHeight();
335 cinfo
.input_components
= 3;
336 cinfo
.in_color_space
= JCS_RGB
;
337 jpeg_set_defaults(&cinfo
);
339 // TODO: 3rd parameter is force_baseline, what value should this be?
340 // Code says: "If force_baseline is TRUE, the computed quantization table entries
341 // are limited to 1..255 for JPEG baseline compatibility."
342 // 'Quality' is a number between 0 (terrible) and 100 (very good).
343 // The default (in jcparam.c, jpeg_set_defaults) is 75,
344 // and force_baseline is TRUE.
345 if (image
->HasOption(wxT("quality")))
346 jpeg_set_quality(&cinfo
, image
->GetOptionInt(wxT("quality")), TRUE
);
348 jpeg_start_compress(&cinfo
, TRUE
);
350 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
351 image_buffer
= image
->GetData();
352 while (cinfo
.next_scanline
< cinfo
.image_height
) {
353 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
354 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
356 jpeg_finish_compress(&cinfo
);
357 jpeg_destroy_compress(&cinfo
);
363 #pragma warning(default:4611)
366 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
368 unsigned char hdr
[2];
371 stream
.SeekI(-2, wxFromCurrent
);
372 return (hdr
[0] == 0xFF && hdr
[1] == 0xD8);
375 #endif // wxUSE_STREAMS
377 #endif // wxUSE_LIBJPEG