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.
36 // TODO: include windows.h for MetroWerks and Watcom only after 2.3.2 release!
37 #if defined(__WXMSW__)
38 #if defined(__MWERKS__) || defined(__WATCOMC__)
49 #include "wx/filefn.h"
50 #include "wx/wfstream.h"
52 #include "wx/module.h"
56 // For JPEG library error handling
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
69 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
73 //------------- JPEG Data Source Manager
75 #define JPEG_IO_BUFFER_SIZE 2048
78 struct jpeg_source_mgr pub
; /* public fields */
80 JOCTET
* buffer
; /* start of buffer */
81 wxInputStream
*stream
;
84 typedef my_source_mgr
* my_src_ptr
;
86 METHODDEF(void) my_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
90 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr cinfo
)
92 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
94 src
->pub
.next_input_byte
= src
->buffer
;
95 src
->pub
.bytes_in_buffer
= src
->stream
->Read(src
->buffer
, JPEG_IO_BUFFER_SIZE
).LastRead();
97 if (src
->pub
.bytes_in_buffer
== 0) // check for end-of-stream
99 // Insert a fake EOI marker
100 src
->buffer
[0] = 0xFF;
101 src
->buffer
[1] = JPEG_EOI
;
102 src
->pub
.bytes_in_buffer
= 2;
107 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
111 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
113 while (num_bytes
> (long)src
->pub
.bytes_in_buffer
)
115 num_bytes
-= (long) src
->pub
.bytes_in_buffer
;
116 src
->pub
.fill_input_buffer(cinfo
);
118 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
119 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
123 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
125 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
127 if (src
->pub
.bytes_in_buffer
> 0)
128 src
->stream
->SeekI(-(long)src
->pub
.bytes_in_buffer
, wxFromCurrent
);
129 delete[] src
->buffer
;
132 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
136 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
137 cinfo
->src
= (struct jpeg_source_mgr
*)
138 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
139 sizeof(my_source_mgr
));
140 src
= (my_src_ptr
) cinfo
->src
;
142 src
= (my_src_ptr
) cinfo
->src
;
143 src
->pub
.bytes_in_buffer
= 0; /* forces fill_input_buffer on first read */
144 src
->buffer
= new JOCTET
[JPEG_IO_BUFFER_SIZE
];
145 src
->pub
.next_input_byte
= NULL
; /* until buffer loaded */
146 src
->stream
= &infile
;
148 src
->pub
.init_source
= my_init_source
;
149 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
150 src
->pub
.skip_input_data
= my_skip_input_data
;
151 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
152 src
->pub
.term_source
= my_term_source
;
156 // JPEG error manager:
158 struct my_error_mgr
{
159 struct jpeg_error_mgr pub
; /* "public" fields */
161 jmp_buf setjmp_buffer
; /* for return to caller */
164 typedef struct my_error_mgr
* my_error_ptr
;
167 * Here's the routine that will replace the standard error_exit method:
171 my_error_exit (j_common_ptr cinfo
)
173 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
174 my_error_ptr myerr
= (my_error_ptr
) cinfo
->err
;
176 /* Always display the message. */
177 /* We could postpone this until after returning, if we chose. */
178 if (cinfo
->err
->output_message
) (*cinfo
->err
->output_message
) (cinfo
);
180 /* Return control to the setjmp point */
181 longjmp(myerr
->setjmp_buffer
, 1);
184 // temporarily disable the warning C4611 (interaction between '_setjmp' and
185 // C++ object destruction is non-portable) - I don't see any dtors here
187 #pragma warning(disable:4611)
190 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
192 struct jpeg_decompress_struct cinfo
;
193 struct my_error_mgr jerr
;
199 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
200 jerr
.pub
.error_exit
= my_error_exit
;
202 if (!verbose
) cinfo
.err
->output_message
=NULL
;
204 /* Establish the setjmp return context for my_error_exit to use. */
205 if (setjmp(jerr
.setjmp_buffer
)) {
206 /* If we get here, the JPEG code has signaled an error.
207 * We need to clean up the JPEG object, close the input file, and return.
210 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
211 (cinfo
.src
->term_source
)(&cinfo
);
212 jpeg_destroy_decompress(&cinfo
);
213 if (image
->Ok()) image
->Destroy();
217 jpeg_create_decompress( &cinfo
);
218 jpeg_wxio_src( &cinfo
, stream
);
219 jpeg_read_header( &cinfo
, TRUE
);
220 cinfo
.out_color_space
= JCS_RGB
;
221 jpeg_start_decompress( &cinfo
);
223 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
225 jpeg_finish_decompress( &cinfo
);
226 jpeg_destroy_decompress( &cinfo
);
229 image
->SetMask( FALSE
);
230 ptr
= image
->GetData();
231 stride
= cinfo
.output_width
* 3;
232 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
233 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
235 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
236 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
237 memcpy( ptr
, tempbuf
[0], stride
);
240 jpeg_finish_decompress( &cinfo
);
241 jpeg_destroy_decompress( &cinfo
);
246 struct jpeg_destination_mgr pub
;
248 wxOutputStream
*stream
;
250 } my_destination_mgr
;
252 typedef my_destination_mgr
* my_dest_ptr
;
254 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
256 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
258 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
260 /* Allocate the output buffer --- it will be released when done with image */
261 dest
->buffer
= (JOCTET
*)
262 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
263 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
264 dest
->pub
.next_output_byte
= dest
->buffer
;
265 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
268 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
270 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
272 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
273 dest
->pub
.next_output_byte
= dest
->buffer
;
274 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
278 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
280 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
281 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
282 /* Write any data remaining in the buffer */
284 dest
->stream
->Write(dest
->buffer
, datacount
);
287 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
291 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
292 cinfo
->dest
= (struct jpeg_destination_mgr
*)
293 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
294 sizeof(my_destination_mgr
));
297 dest
= (my_dest_ptr
) cinfo
->dest
;
298 dest
->pub
.init_destination
= init_destination
;
299 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
300 dest
->pub
.term_destination
= term_destination
;
301 dest
->stream
= &outfile
;
304 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
306 struct jpeg_compress_struct cinfo
;
307 struct my_error_mgr jerr
;
308 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
309 JSAMPLE
*image_buffer
;
310 int stride
; /* physical row width in image buffer */
312 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
313 jerr
.pub
.error_exit
= my_error_exit
;
315 if (!verbose
) cinfo
.err
->output_message
=NULL
;
317 /* Establish the setjmp return context for my_error_exit to use. */
318 if (setjmp(jerr
.setjmp_buffer
))
320 /* If we get here, the JPEG code has signaled an error.
321 * We need to clean up the JPEG object, close the input file, and return.
324 wxLogError(_("JPEG: Couldn't save image."));
325 jpeg_destroy_compress(&cinfo
);
329 jpeg_create_compress(&cinfo
);
330 jpeg_wxio_dest(&cinfo
, stream
);
332 cinfo
.image_width
= image
->GetWidth();
333 cinfo
.image_height
= image
->GetHeight();
334 cinfo
.input_components
= 3;
335 cinfo
.in_color_space
= JCS_RGB
;
336 jpeg_set_defaults(&cinfo
);
338 // TODO: 3rd parameter is force_baseline, what value should this be?
339 // Code says: "If force_baseline is TRUE, the computed quantization table entries
340 // are limited to 1..255 for JPEG baseline compatibility."
341 // 'Quality' is a number between 0 (terrible) and 100 (very good).
342 // The default (in jcparam.c, jpeg_set_defaults) is 75,
343 // and force_baseline is TRUE.
344 if (image
->HasOption(wxT("quality")))
345 jpeg_set_quality(&cinfo
, image
->GetOptionInt(wxT("quality")), TRUE
);
347 jpeg_start_compress(&cinfo
, TRUE
);
349 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
350 image_buffer
= image
->GetData();
351 while (cinfo
.next_scanline
< cinfo
.image_height
) {
352 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
353 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
355 jpeg_finish_compress(&cinfo
);
356 jpeg_destroy_compress(&cinfo
);
362 #pragma warning(default:4611)
365 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
367 unsigned char hdr
[2];
370 stream
.SeekI(-2, wxFromCurrent
);
371 return (hdr
[0] == 0xFF && hdr
[1] == 0xD8);
374 #endif // wxUSE_STREAMS
376 #endif // wxUSE_LIBJPEG