1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage JPEG handler
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 We don't put pragma implement in this file because it is already present in
15 #pragma implementation "image.h"
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
27 #include "wx/bitmap.h"
36 #include "wx/filefn.h"
37 #include "wx/wfstream.h"
39 #include "wx/module.h"
43 // For JPEG library error handling
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
62 #if !USE_SHARED_LIBRARIES
63 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
69 //------------- JPEG Data Source Manager
72 struct jpeg_source_mgr pub
; /* public fields */
74 JOCTET
* buffer
; /* start of buffer */
77 typedef my_source_mgr
* my_src_ptr
;
79 METHODDEF(void) my_init_source ( j_decompress_ptr cinfo
)
83 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr cinfo
)
88 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
90 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
92 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
93 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
96 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
98 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
103 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
107 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
108 cinfo
->src
= (struct jpeg_source_mgr
*)
109 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
110 sizeof(my_source_mgr
));
111 src
= (my_src_ptr
) cinfo
->src
;
113 src
= (my_src_ptr
) cinfo
->src
;
114 src
->pub
.bytes_in_buffer
= infile
.StreamSize(); /* forces fill_input_buffer on first read */
115 src
->buffer
= (JOCTET
*) malloc (infile
.StreamSize());
116 src
->pub
.next_input_byte
= src
->buffer
; /* until buffer loaded */
117 infile
.Read(src
->buffer
, infile
.StreamSize());
119 src
->pub
.init_source
= my_init_source
;
120 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
121 src
->pub
.skip_input_data
= my_skip_input_data
;
122 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
123 src
->pub
.term_source
= my_term_source
;
127 // JPEG error manager:
129 struct my_error_mgr
{
130 struct jpeg_error_mgr pub
; /* "public" fields */
132 jmp_buf setjmp_buffer
; /* for return to caller */
135 typedef struct my_error_mgr
* my_error_ptr
;
138 * Here's the routine that will replace the standard error_exit method:
142 my_error_exit (j_common_ptr cinfo
)
144 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
145 my_error_ptr myerr
= (my_error_ptr
) cinfo
->err
;
147 /* Always display the message. */
148 /* We could postpone this until after returning, if we chose. */
149 (*cinfo
->err
->output_message
) (cinfo
);
151 /* Return control to the setjmp point */
152 longjmp(myerr
->setjmp_buffer
, 1);
157 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
159 struct jpeg_decompress_struct cinfo
;
160 struct my_error_mgr jerr
;
166 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
167 jerr
.pub
.error_exit
= my_error_exit
;
169 /* Establish the setjmp return context for my_error_exit to use. */
170 if (setjmp(jerr
.setjmp_buffer
)) {
171 /* If we get here, the JPEG code has signaled an error.
172 * We need to clean up the JPEG object, close the input file, and return.
174 wxLogError(_("Couldn't load a JPEG image - probably file is corrupted."));
175 jpeg_destroy_decompress(&cinfo
);
176 if (image
->Ok()) image
->Destroy();
180 jpeg_create_decompress( &cinfo
);
181 jpeg_wxio_src( &cinfo
, stream
);
182 jpeg_read_header( &cinfo
, TRUE
);
183 cinfo
.out_color_space
= JCS_RGB
;
184 jpeg_start_decompress( &cinfo
);
186 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
188 jpeg_finish_decompress( &cinfo
);
189 jpeg_destroy_decompress( &cinfo
);
192 image
->SetMask( FALSE
);
193 ptr
= image
->GetData();
194 stride
= cinfo
.output_width
* 3;
195 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
196 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
198 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
199 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
200 memcpy( ptr
, tempbuf
[0], stride
);
203 jpeg_finish_decompress( &cinfo
);
204 jpeg_destroy_decompress( &cinfo
);
213 struct jpeg_destination_mgr pub
;
215 wxOutputStream
*stream
;
217 } my_destination_mgr
;
219 typedef my_destination_mgr
* my_dest_ptr
;
221 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
223 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
225 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
227 /* Allocate the output buffer --- it will be released when done with image */
228 dest
->buffer
= (JOCTET
*)
229 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
230 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
231 dest
->pub
.next_output_byte
= dest
->buffer
;
232 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
235 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
237 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
239 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
240 dest
->pub
.next_output_byte
= dest
->buffer
;
241 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
245 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
247 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
248 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
249 /* Write any data remaining in the buffer */
251 dest
->stream
->Write(dest
->buffer
, datacount
);
254 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
258 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
259 cinfo
->dest
= (struct jpeg_destination_mgr
*)
260 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
261 sizeof(my_destination_mgr
));
264 dest
= (my_dest_ptr
) cinfo
->dest
;
265 dest
->pub
.init_destination
= init_destination
;
266 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
267 dest
->pub
.term_destination
= term_destination
;
268 dest
->stream
= &outfile
;
271 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
273 struct jpeg_compress_struct cinfo
;
274 struct my_error_mgr jerr
;
275 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
276 JSAMPLE
*image_buffer
;
277 int stride
; /* physical row width in image buffer */
279 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
280 jerr
.pub
.error_exit
= my_error_exit
;
282 /* Establish the setjmp return context for my_error_exit to use. */
283 if (setjmp(jerr
.setjmp_buffer
)) {
284 /* If we get here, the JPEG code has signaled an error.
285 * We need to clean up the JPEG object, close the input file, and return.
287 wxLogError(_("Couldn't save a JPEG image - probably file is corrupted."));
288 jpeg_destroy_compress(&cinfo
);
292 jpeg_create_compress(&cinfo
);
293 jpeg_wxio_dest(&cinfo
, stream
);
295 cinfo
.image_width
= image
->GetWidth();
296 cinfo
.image_height
= image
->GetHeight();
297 cinfo
.input_components
= 3;
298 cinfo
.in_color_space
= JCS_RGB
;
299 jpeg_set_defaults(&cinfo
);
300 jpeg_start_compress(&cinfo
, TRUE
);
302 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
303 image_buffer
= image
->GetData();
304 while (cinfo
.next_scanline
< cinfo
.image_height
) {
305 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
306 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
308 jpeg_finish_compress(&cinfo
);
309 jpeg_destroy_compress(&cinfo
);
313 #endif // wxUSE_STREAMS