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 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
69 //------------- JPEG Data Source Manager
71 #define JPEG_IO_BUFFER_SIZE 2048
74 struct jpeg_source_mgr pub
; /* public fields */
76 JOCTET
* buffer
; /* start of buffer */
77 wxInputStream
*stream
;
80 typedef my_source_mgr
* my_src_ptr
;
82 METHODDEF(void) my_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
86 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr cinfo
)
88 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
90 src
->pub
.next_input_byte
= src
->buffer
;
91 src
->pub
.bytes_in_buffer
= src
->stream
->Read(src
->buffer
, JPEG_IO_BUFFER_SIZE
).LastRead();
93 if (src
->pub
.bytes_in_buffer
== 0) // check for end-of-stream
95 // Insert a fake EOI marker
96 src
->buffer
[0] = 0xFF;
97 src
->buffer
[1] = JPEG_EOI
;
98 src
->pub
.bytes_in_buffer
= 2;
103 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
107 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
109 while (num_bytes
> (long)src
->pub
.bytes_in_buffer
)
111 num_bytes
-= (long) src
->pub
.bytes_in_buffer
;
112 src
->pub
.fill_input_buffer(cinfo
);
114 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
115 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
119 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
121 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
123 if (src
->pub
.bytes_in_buffer
> 0)
124 src
->stream
->SeekI(-(long)src
->pub
.bytes_in_buffer
, wxFromCurrent
);
125 delete[] src
->buffer
;
128 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
132 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
133 cinfo
->src
= (struct jpeg_source_mgr
*)
134 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
135 sizeof(my_source_mgr
));
136 src
= (my_src_ptr
) cinfo
->src
;
138 src
= (my_src_ptr
) cinfo
->src
;
139 src
->pub
.bytes_in_buffer
= 0; /* forces fill_input_buffer on first read */
140 src
->buffer
= new JOCTET
[JPEG_IO_BUFFER_SIZE
];
141 src
->pub
.next_input_byte
= NULL
; /* until buffer loaded */
142 src
->stream
= &infile
;
144 src
->pub
.init_source
= my_init_source
;
145 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
146 src
->pub
.skip_input_data
= my_skip_input_data
;
147 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
148 src
->pub
.term_source
= my_term_source
;
152 // JPEG error manager:
154 struct my_error_mgr
{
155 struct jpeg_error_mgr pub
; /* "public" fields */
157 jmp_buf setjmp_buffer
; /* for return to caller */
160 typedef struct my_error_mgr
* my_error_ptr
;
163 * Here's the routine that will replace the standard error_exit method:
167 my_error_exit (j_common_ptr cinfo
)
169 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
170 my_error_ptr myerr
= (my_error_ptr
) cinfo
->err
;
172 /* Always display the message. */
173 /* We could postpone this until after returning, if we chose. */
174 if (cinfo
->err
->output_message
) (*cinfo
->err
->output_message
) (cinfo
);
176 /* Return control to the setjmp point */
177 longjmp(myerr
->setjmp_buffer
, 1);
180 // temporarily disable the warning C4611 (interaction between '_setjmp' and
181 // C++ object destruction is non-portable) - I don't see any dtors here
183 #pragma warning(disable:4611)
186 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
188 struct jpeg_decompress_struct cinfo
;
189 struct my_error_mgr jerr
;
195 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
196 jerr
.pub
.error_exit
= my_error_exit
;
198 if (!verbose
) cinfo
.err
->output_message
=NULL
;
200 /* Establish the setjmp return context for my_error_exit to use. */
201 if (setjmp(jerr
.setjmp_buffer
)) {
202 /* If we get here, the JPEG code has signaled an error.
203 * We need to clean up the JPEG object, close the input file, and return.
206 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
207 (cinfo
.src
->term_source
)(&cinfo
);
208 jpeg_destroy_decompress(&cinfo
);
209 if (image
->Ok()) image
->Destroy();
213 jpeg_create_decompress( &cinfo
);
214 jpeg_wxio_src( &cinfo
, stream
);
215 jpeg_read_header( &cinfo
, TRUE
);
216 cinfo
.out_color_space
= JCS_RGB
;
217 jpeg_start_decompress( &cinfo
);
219 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
221 jpeg_finish_decompress( &cinfo
);
222 jpeg_destroy_decompress( &cinfo
);
225 image
->SetMask( FALSE
);
226 ptr
= image
->GetData();
227 stride
= cinfo
.output_width
* 3;
228 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
229 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
231 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
232 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
233 memcpy( ptr
, tempbuf
[0], stride
);
236 jpeg_finish_decompress( &cinfo
);
237 jpeg_destroy_decompress( &cinfo
);
242 struct jpeg_destination_mgr pub
;
244 wxOutputStream
*stream
;
246 } my_destination_mgr
;
248 typedef my_destination_mgr
* my_dest_ptr
;
250 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
252 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
254 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
256 /* Allocate the output buffer --- it will be released when done with image */
257 dest
->buffer
= (JOCTET
*)
258 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
259 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
260 dest
->pub
.next_output_byte
= dest
->buffer
;
261 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
264 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
266 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
268 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
269 dest
->pub
.next_output_byte
= dest
->buffer
;
270 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
274 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
276 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
277 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
278 /* Write any data remaining in the buffer */
280 dest
->stream
->Write(dest
->buffer
, datacount
);
283 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
287 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
288 cinfo
->dest
= (struct jpeg_destination_mgr
*)
289 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
290 sizeof(my_destination_mgr
));
293 dest
= (my_dest_ptr
) cinfo
->dest
;
294 dest
->pub
.init_destination
= init_destination
;
295 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
296 dest
->pub
.term_destination
= term_destination
;
297 dest
->stream
= &outfile
;
300 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
302 struct jpeg_compress_struct cinfo
;
303 struct my_error_mgr jerr
;
304 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
305 JSAMPLE
*image_buffer
;
306 int stride
; /* physical row width in image buffer */
308 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
309 jerr
.pub
.error_exit
= my_error_exit
;
311 if (!verbose
) cinfo
.err
->output_message
=NULL
;
313 /* Establish the setjmp return context for my_error_exit to use. */
314 if (setjmp(jerr
.setjmp_buffer
))
316 /* If we get here, the JPEG code has signaled an error.
317 * We need to clean up the JPEG object, close the input file, and return.
320 wxLogError(_("JPEG: Couldn't save image."));
321 jpeg_destroy_compress(&cinfo
);
325 jpeg_create_compress(&cinfo
);
326 jpeg_wxio_dest(&cinfo
, stream
);
328 cinfo
.image_width
= image
->GetWidth();
329 cinfo
.image_height
= image
->GetHeight();
330 cinfo
.input_components
= 3;
331 cinfo
.in_color_space
= JCS_RGB
;
332 jpeg_set_defaults(&cinfo
);
334 // TODO: 3rd parameter is force_baseline, what value should this be?
335 // Code says: "If force_baseline is TRUE, the computed quantization table entries
336 // are limited to 1..255 for JPEG baseline compatibility."
337 // 'Quality' is a number between 0 (terrible) and 100 (very good).
338 // The default (in jcparam.c, jpeg_set_defaults) is 75,
339 // and force_baseline is TRUE.
340 if (image
->HasOption(wxT("quality")))
341 jpeg_set_quality(&cinfo
, image
->GetOptionInt(wxT("quality")), TRUE
);
343 jpeg_start_compress(&cinfo
, TRUE
);
345 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
346 image_buffer
= image
->GetData();
347 while (cinfo
.next_scanline
< cinfo
.image_height
) {
348 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
349 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
351 jpeg_finish_compress(&cinfo
);
352 jpeg_destroy_compress(&cinfo
);
358 #pragma warning(default:4611)
361 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
363 unsigned char hdr
[2];
366 stream
.SeekI(-2, wxFromCurrent
);
367 return (hdr
[0] == 0xFF && hdr
[1] == 0xD8);
370 #endif // wxUSE_STREAMS
372 #endif // wxUSE_LIBJPEG