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 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
27 #include "wx/bitmap.h"
35 #include "wx/filefn.h"
36 #include "wx/wfstream.h"
38 #include "wx/module.h"
42 // For JPEG library error handling
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
63 //------------- JPEG Data Source Manager
66 struct jpeg_source_mgr pub
; /* public fields */
68 JOCTET
* buffer
; /* start of buffer */
71 typedef my_source_mgr
* my_src_ptr
;
73 METHODDEF(void) my_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
77 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr
WXUNUSED(cinfo
) )
82 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
84 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
86 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
87 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
90 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
92 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
97 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
101 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
102 cinfo
->src
= (struct jpeg_source_mgr
*)
103 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
104 sizeof(my_source_mgr
));
105 src
= (my_src_ptr
) cinfo
->src
;
107 src
= (my_src_ptr
) cinfo
->src
;
108 src
->pub
.bytes_in_buffer
= infile
.GetSize(); /* forces fill_input_buffer on first read */
109 src
->buffer
= (JOCTET
*) malloc (infile
.GetSize());
110 src
->pub
.next_input_byte
= src
->buffer
; /* until buffer loaded */
111 infile
.Read(src
->buffer
, infile
.GetSize());
113 src
->pub
.init_source
= my_init_source
;
114 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
115 src
->pub
.skip_input_data
= my_skip_input_data
;
116 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
117 src
->pub
.term_source
= my_term_source
;
121 // JPEG error manager:
123 struct my_error_mgr
{
124 struct jpeg_error_mgr pub
; /* "public" fields */
126 jmp_buf setjmp_buffer
; /* for return to caller */
129 typedef struct my_error_mgr
* my_error_ptr
;
132 * Here's the routine that will replace the standard error_exit method:
136 my_error_exit (j_common_ptr cinfo
)
138 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
139 my_error_ptr myerr
= (my_error_ptr
) cinfo
->err
;
141 /* Always display the message. */
142 /* We could postpone this until after returning, if we chose. */
143 if (cinfo
->err
->output_message
) (*cinfo
->err
->output_message
) (cinfo
);
145 /* Return control to the setjmp point */
146 longjmp(myerr
->setjmp_buffer
, 1);
151 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
153 struct jpeg_decompress_struct cinfo
;
154 struct my_error_mgr jerr
;
160 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
161 jerr
.pub
.error_exit
= my_error_exit
;
163 if (!verbose
) cinfo
.err
->output_message
=NULL
;
165 /* Establish the setjmp return context for my_error_exit to use. */
166 if (setjmp(jerr
.setjmp_buffer
)) {
167 /* If we get here, the JPEG code has signaled an error.
168 * We need to clean up the JPEG object, close the input file, and return.
171 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
172 jpeg_destroy_decompress(&cinfo
);
173 if (image
->Ok()) image
->Destroy();
177 jpeg_create_decompress( &cinfo
);
178 jpeg_wxio_src( &cinfo
, stream
);
179 jpeg_read_header( &cinfo
, TRUE
);
180 cinfo
.out_color_space
= JCS_RGB
;
181 jpeg_start_decompress( &cinfo
);
183 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
185 jpeg_finish_decompress( &cinfo
);
186 jpeg_destroy_decompress( &cinfo
);
189 image
->SetMask( FALSE
);
190 ptr
= image
->GetData();
191 stride
= cinfo
.output_width
* 3;
192 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
193 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
195 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
196 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
197 memcpy( ptr
, tempbuf
[0], stride
);
200 jpeg_finish_decompress( &cinfo
);
201 jpeg_destroy_decompress( &cinfo
);
210 struct jpeg_destination_mgr pub
;
212 wxOutputStream
*stream
;
214 } my_destination_mgr
;
216 typedef my_destination_mgr
* my_dest_ptr
;
218 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
220 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
222 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
224 /* Allocate the output buffer --- it will be released when done with image */
225 dest
->buffer
= (JOCTET
*)
226 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
227 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
228 dest
->pub
.next_output_byte
= dest
->buffer
;
229 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
232 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
234 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
236 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
237 dest
->pub
.next_output_byte
= dest
->buffer
;
238 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
242 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
244 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
245 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
246 /* Write any data remaining in the buffer */
248 dest
->stream
->Write(dest
->buffer
, datacount
);
251 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
255 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
256 cinfo
->dest
= (struct jpeg_destination_mgr
*)
257 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
258 sizeof(my_destination_mgr
));
261 dest
= (my_dest_ptr
) cinfo
->dest
;
262 dest
->pub
.init_destination
= init_destination
;
263 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
264 dest
->pub
.term_destination
= term_destination
;
265 dest
->stream
= &outfile
;
268 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
270 struct jpeg_compress_struct cinfo
;
271 struct my_error_mgr jerr
;
272 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
273 JSAMPLE
*image_buffer
;
274 int stride
; /* physical row width in image buffer */
276 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
277 jerr
.pub
.error_exit
= my_error_exit
;
279 if (!verbose
) cinfo
.err
->output_message
=NULL
;
281 /* Establish the setjmp return context for my_error_exit to use. */
282 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.
288 wxLogError(_("JPEG: Couldn't save image."));
289 jpeg_destroy_compress(&cinfo
);
293 jpeg_create_compress(&cinfo
);
294 jpeg_wxio_dest(&cinfo
, stream
);
296 cinfo
.image_width
= image
->GetWidth();
297 cinfo
.image_height
= image
->GetHeight();
298 cinfo
.input_components
= 3;
299 cinfo
.in_color_space
= JCS_RGB
;
300 jpeg_set_defaults(&cinfo
);
301 jpeg_start_compress(&cinfo
, TRUE
);
303 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
304 image_buffer
= image
->GetData();
305 while (cinfo
.next_scanline
< cinfo
.image_height
) {
306 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
307 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
309 jpeg_finish_compress(&cinfo
);
310 jpeg_destroy_compress(&cinfo
);
316 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
318 unsigned char hdr
[2];
320 stream
.Read(&hdr
, 2);
321 stream
.SeekI(-2, wxFromCurrent
);
322 return (hdr
[0] == 0xFF && hdr
[1] == 0xD8);
325 #endif // wxUSE_STREAMS
327 #endif // wxUSE_LIBJPEG