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"
25 #include "wx/imagjpeg.h"
26 #include "wx/bitmap.h"
37 #include "wx/filefn.h"
38 #include "wx/wfstream.h"
40 #include "wx/module.h"
44 // For JPEG library error handling
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
65 //------------- JPEG Data Source Manager
67 #define JPEG_IO_BUFFER_SIZE 2048
70 struct jpeg_source_mgr pub
; /* public fields */
72 JOCTET
* buffer
; /* start of buffer */
73 wxInputStream
*stream
;
76 typedef my_source_mgr
* my_src_ptr
;
78 METHODDEF(void) my_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
82 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr cinfo
)
84 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
86 src
->pub
.next_input_byte
= src
->buffer
;
87 src
->pub
.bytes_in_buffer
= src
->stream
->Read(src
->buffer
, JPEG_IO_BUFFER_SIZE
).LastRead();
89 if (src
->pub
.bytes_in_buffer
== 0) // check for end-of-stream
91 // Insert a fake EOI marker
92 src
->buffer
[0] = 0xFF;
93 src
->buffer
[1] = JPEG_EOI
;
94 src
->pub
.bytes_in_buffer
= 2;
99 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
103 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
105 while (num_bytes
> (long)src
->pub
.bytes_in_buffer
)
107 num_bytes
-= (long) src
->pub
.bytes_in_buffer
;
108 src
->pub
.fill_input_buffer(cinfo
);
110 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
111 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
115 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
117 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
119 if (src
->pub
.bytes_in_buffer
> 0)
120 src
->stream
->SeekI(-(long)src
->pub
.bytes_in_buffer
, wxFromCurrent
);
121 delete[] src
->buffer
;
124 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
128 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
129 cinfo
->src
= (struct jpeg_source_mgr
*)
130 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
131 sizeof(my_source_mgr
));
132 src
= (my_src_ptr
) cinfo
->src
;
134 src
= (my_src_ptr
) cinfo
->src
;
135 src
->pub
.bytes_in_buffer
= 0; /* forces fill_input_buffer on first read */
136 src
->buffer
= new JOCTET
[JPEG_IO_BUFFER_SIZE
];
137 src
->pub
.next_input_byte
= NULL
; /* until buffer loaded */
138 src
->stream
= &infile
;
140 src
->pub
.init_source
= my_init_source
;
141 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
142 src
->pub
.skip_input_data
= my_skip_input_data
;
143 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
144 src
->pub
.term_source
= my_term_source
;
148 // JPEG error manager:
150 struct my_error_mgr
{
151 struct jpeg_error_mgr pub
; /* "public" fields */
153 jmp_buf setjmp_buffer
; /* for return to caller */
156 typedef struct my_error_mgr
* my_error_ptr
;
159 * Here's the routine that will replace the standard error_exit method:
163 my_error_exit (j_common_ptr cinfo
)
165 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
166 my_error_ptr myerr
= (my_error_ptr
) cinfo
->err
;
168 /* Always display the message. */
169 /* We could postpone this until after returning, if we chose. */
170 if (cinfo
->err
->output_message
) (*cinfo
->err
->output_message
) (cinfo
);
172 /* Return control to the setjmp point */
173 longjmp(myerr
->setjmp_buffer
, 1);
176 // temporarily disable the warning C4611 (interaction between '_setjmp' and
177 // C++ object destruction is non-portable) - I don't see any dtors here
179 #pragma warning(disable:4611)
182 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
184 struct jpeg_decompress_struct cinfo
;
185 struct my_error_mgr jerr
;
191 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
192 jerr
.pub
.error_exit
= my_error_exit
;
194 if (!verbose
) cinfo
.err
->output_message
=NULL
;
196 /* Establish the setjmp return context for my_error_exit to use. */
197 if (setjmp(jerr
.setjmp_buffer
)) {
198 /* If we get here, the JPEG code has signaled an error.
199 * We need to clean up the JPEG object, close the input file, and return.
202 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
203 jpeg_destroy_decompress(&cinfo
);
204 if (image
->Ok()) image
->Destroy();
208 jpeg_create_decompress( &cinfo
);
209 jpeg_wxio_src( &cinfo
, stream
);
210 jpeg_read_header( &cinfo
, TRUE
);
211 cinfo
.out_color_space
= JCS_RGB
;
212 jpeg_start_decompress( &cinfo
);
214 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
216 jpeg_finish_decompress( &cinfo
);
217 jpeg_destroy_decompress( &cinfo
);
220 image
->SetMask( FALSE
);
221 ptr
= image
->GetData();
222 stride
= cinfo
.output_width
* 3;
223 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
224 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
226 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
227 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
228 memcpy( ptr
, tempbuf
[0], stride
);
231 jpeg_finish_decompress( &cinfo
);
232 jpeg_destroy_decompress( &cinfo
);
237 struct jpeg_destination_mgr pub
;
239 wxOutputStream
*stream
;
241 } my_destination_mgr
;
243 typedef my_destination_mgr
* my_dest_ptr
;
245 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
247 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
249 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
251 /* Allocate the output buffer --- it will be released when done with image */
252 dest
->buffer
= (JOCTET
*)
253 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
254 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
255 dest
->pub
.next_output_byte
= dest
->buffer
;
256 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
259 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
261 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
263 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
264 dest
->pub
.next_output_byte
= dest
->buffer
;
265 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
269 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
271 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
272 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
273 /* Write any data remaining in the buffer */
275 dest
->stream
->Write(dest
->buffer
, datacount
);
278 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
282 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
283 cinfo
->dest
= (struct jpeg_destination_mgr
*)
284 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
285 sizeof(my_destination_mgr
));
288 dest
= (my_dest_ptr
) cinfo
->dest
;
289 dest
->pub
.init_destination
= init_destination
;
290 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
291 dest
->pub
.term_destination
= term_destination
;
292 dest
->stream
= &outfile
;
295 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
297 struct jpeg_compress_struct cinfo
;
298 struct my_error_mgr jerr
;
299 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
300 JSAMPLE
*image_buffer
;
301 int stride
; /* physical row width in image buffer */
303 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
304 jerr
.pub
.error_exit
= my_error_exit
;
306 if (!verbose
) cinfo
.err
->output_message
=NULL
;
308 /* Establish the setjmp return context for my_error_exit to use. */
309 if (setjmp(jerr
.setjmp_buffer
))
311 /* If we get here, the JPEG code has signaled an error.
312 * We need to clean up the JPEG object, close the input file, and return.
315 wxLogError(_("JPEG: Couldn't save image."));
316 jpeg_destroy_compress(&cinfo
);
320 jpeg_create_compress(&cinfo
);
321 jpeg_wxio_dest(&cinfo
, stream
);
323 cinfo
.image_width
= image
->GetWidth();
324 cinfo
.image_height
= image
->GetHeight();
325 cinfo
.input_components
= 3;
326 cinfo
.in_color_space
= JCS_RGB
;
327 jpeg_set_defaults(&cinfo
);
329 // TODO: 3rd parameter is force_baseline, what value should this be?
330 // Code says: "If force_baseline is TRUE, the computed quantization table entries
331 // are limited to 1..255 for JPEG baseline compatibility."
332 // 'Quality' is a number between 0 (terrible) and 100 (very good).
333 // The default (in jcparam.c, jpeg_set_defaults) is 75,
334 // and force_baseline is TRUE.
335 if (image
->HasOption(wxT("quality")))
336 jpeg_set_quality(&cinfo
, image
->GetOptionInt(wxT("quality")), TRUE
);
338 jpeg_start_compress(&cinfo
, TRUE
);
340 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
341 image_buffer
= image
->GetData();
342 while (cinfo
.next_scanline
< cinfo
.image_height
) {
343 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
344 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
346 jpeg_finish_compress(&cinfo
);
347 jpeg_destroy_compress(&cinfo
);
353 #pragma warning(default:4611)
356 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
358 unsigned char hdr
[2];
360 stream
.Read(&hdr
, 2);
361 stream
.SeekI(-2, wxFromCurrent
);
362 return (hdr
[0] == 0xFF && hdr
[1] == 0xD8);
365 #endif // wxUSE_STREAMS
367 #endif // wxUSE_LIBJPEG