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"
34 #include "wx/filefn.h"
35 #include "wx/wfstream.h"
37 #include "wx/module.h"
41 // For JPEG library error handling
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 #if !USE_SHARED_LIBRARIES
59 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
64 //------------- JPEG Data Source Manager
67 struct jpeg_source_mgr pub
; /* public fields */
69 JOCTET
* buffer
; /* start of buffer */
72 typedef my_source_mgr
* my_src_ptr
;
74 METHODDEF(void) my_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
78 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr
WXUNUSED(cinfo
) )
83 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
85 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
87 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
88 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
91 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
93 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
98 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
102 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
103 cinfo
->src
= (struct jpeg_source_mgr
*)
104 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
105 sizeof(my_source_mgr
));
106 src
= (my_src_ptr
) cinfo
->src
;
108 src
= (my_src_ptr
) cinfo
->src
;
109 src
->pub
.bytes_in_buffer
= infile
.GetSize(); /* forces fill_input_buffer on first read */
110 src
->buffer
= (JOCTET
*) malloc (infile
.GetSize());
111 src
->pub
.next_input_byte
= src
->buffer
; /* until buffer loaded */
112 infile
.Read(src
->buffer
, infile
.GetSize());
114 src
->pub
.init_source
= my_init_source
;
115 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
116 src
->pub
.skip_input_data
= my_skip_input_data
;
117 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
118 src
->pub
.term_source
= my_term_source
;
122 // JPEG error manager:
124 struct my_error_mgr
{
125 struct jpeg_error_mgr pub
; /* "public" fields */
127 jmp_buf setjmp_buffer
; /* for return to caller */
130 typedef struct my_error_mgr
* my_error_ptr
;
133 * Here's the routine that will replace the standard error_exit method:
137 my_error_exit (j_common_ptr cinfo
)
139 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
140 my_error_ptr myerr
= (my_error_ptr
) cinfo
->err
;
142 /* Always display the message. */
143 /* We could postpone this until after returning, if we chose. */
144 if (cinfo
->err
->output_message
) (*cinfo
->err
->output_message
) (cinfo
);
146 /* Return control to the setjmp point */
147 longjmp(myerr
->setjmp_buffer
, 1);
152 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
)
154 struct jpeg_decompress_struct cinfo
;
155 struct my_error_mgr jerr
;
161 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
162 jerr
.pub
.error_exit
= my_error_exit
;
164 if (!verbose
) cinfo
.err
->output_message
=NULL
;
166 /* Establish the setjmp return context for my_error_exit to use. */
167 if (setjmp(jerr
.setjmp_buffer
)) {
168 /* If we get here, the JPEG code has signaled an error.
169 * We need to clean up the JPEG object, close the input file, and return.
171 if (verbose
) wxLogError(_("Couldn't load a JPEG image - probably file is 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
)) {
283 /* If we get here, the JPEG code has signaled an error.
284 * We need to clean up the JPEG object, close the input file, and return.
286 if (verbose
) wxLogError(_("Couldn't save a JPEG image - probably file is corrupted."));
287 jpeg_destroy_compress(&cinfo
);
291 jpeg_create_compress(&cinfo
);
292 jpeg_wxio_dest(&cinfo
, stream
);
294 cinfo
.image_width
= image
->GetWidth();
295 cinfo
.image_height
= image
->GetHeight();
296 cinfo
.input_components
= 3;
297 cinfo
.in_color_space
= JCS_RGB
;
298 jpeg_set_defaults(&cinfo
);
299 jpeg_start_compress(&cinfo
, TRUE
);
301 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
302 image_buffer
= image
->GetData();
303 while (cinfo
.next_scanline
< cinfo
.image_height
) {
304 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
305 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
307 jpeg_finish_compress(&cinfo
);
308 jpeg_destroy_compress(&cinfo
);
314 bool wxJPEGHandler::CanRead( wxInputStream
& stream
)
316 unsigned char hdr
[2];
318 stream
.Read(&hdr
, 2);
319 stream
.SeekI(-2, wxFromCurrent
);
320 return (hdr
[0] == 0xFF && hdr
[1] == 0xD8);
323 #endif // wxUSE_STREAMS
325 #endif // wxUSE_LIBJPEG