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 #if !USE_SHARED_LIBRARIES
60 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
65 //------------- JPEG Data Source Manager
68 struct jpeg_source_mgr pub
; /* public fields */
70 JOCTET
* buffer
; /* start of buffer */
73 typedef my_source_mgr
* my_src_ptr
;
75 METHODDEF(void) my_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
79 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr
WXUNUSED(cinfo
) )
84 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
86 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
88 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
89 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
92 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
94 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
99 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
103 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
104 cinfo
->src
= (struct jpeg_source_mgr
*)
105 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
106 sizeof(my_source_mgr
));
107 src
= (my_src_ptr
) cinfo
->src
;
109 src
= (my_src_ptr
) cinfo
->src
;
110 src
->pub
.bytes_in_buffer
= infile
.GetSize(); /* forces fill_input_buffer on first read */
111 src
->buffer
= (JOCTET
*) malloc (infile
.GetSize());
112 src
->pub
.next_input_byte
= src
->buffer
; /* until buffer loaded */
113 infile
.Read(src
->buffer
, infile
.GetSize());
115 src
->pub
.init_source
= my_init_source
;
116 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
117 src
->pub
.skip_input_data
= my_skip_input_data
;
118 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
119 src
->pub
.term_source
= my_term_source
;
123 // JPEG error manager:
125 struct my_error_mgr
{
126 struct jpeg_error_mgr pub
; /* "public" fields */
128 jmp_buf setjmp_buffer
; /* for return to caller */
131 typedef struct my_error_mgr
* my_error_ptr
;
134 * Here's the routine that will replace the standard error_exit method:
138 my_error_exit (j_common_ptr cinfo
)
140 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
141 my_error_ptr myerr
= (my_error_ptr
) cinfo
->err
;
143 /* Always display the message. */
144 /* We could postpone this until after returning, if we chose. */
145 if (cinfo
->err
->output_message
) (*cinfo
->err
->output_message
) (cinfo
);
147 /* Return control to the setjmp point */
148 longjmp(myerr
->setjmp_buffer
, 1);
153 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
155 struct jpeg_decompress_struct cinfo
;
156 struct my_error_mgr jerr
;
162 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
163 jerr
.pub
.error_exit
= my_error_exit
;
165 if (!verbose
) cinfo
.err
->output_message
=NULL
;
167 /* Establish the setjmp return context for my_error_exit to use. */
168 if (setjmp(jerr
.setjmp_buffer
)) {
169 /* If we get here, the JPEG code has signaled an error.
170 * We need to clean up the JPEG object, close the input file, and return.
172 if (verbose
) wxLogError(_("Couldn't load a JPEG image - probably file is corrupted."));
173 jpeg_destroy_decompress(&cinfo
);
174 if (image
->Ok()) image
->Destroy();
178 jpeg_create_decompress( &cinfo
);
179 jpeg_wxio_src( &cinfo
, stream
);
180 jpeg_read_header( &cinfo
, TRUE
);
181 cinfo
.out_color_space
= JCS_RGB
;
182 jpeg_start_decompress( &cinfo
);
184 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
186 jpeg_finish_decompress( &cinfo
);
187 jpeg_destroy_decompress( &cinfo
);
190 image
->SetMask( FALSE
);
191 ptr
= image
->GetData();
192 stride
= cinfo
.output_width
* 3;
193 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
194 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
196 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
197 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
198 memcpy( ptr
, tempbuf
[0], stride
);
201 jpeg_finish_decompress( &cinfo
);
202 jpeg_destroy_decompress( &cinfo
);
211 struct jpeg_destination_mgr pub
;
213 wxOutputStream
*stream
;
215 } my_destination_mgr
;
217 typedef my_destination_mgr
* my_dest_ptr
;
219 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
221 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
223 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
225 /* Allocate the output buffer --- it will be released when done with image */
226 dest
->buffer
= (JOCTET
*)
227 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
228 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
229 dest
->pub
.next_output_byte
= dest
->buffer
;
230 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
233 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
235 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
237 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
238 dest
->pub
.next_output_byte
= dest
->buffer
;
239 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
243 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
245 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
246 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
247 /* Write any data remaining in the buffer */
249 dest
->stream
->Write(dest
->buffer
, datacount
);
252 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
256 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
257 cinfo
->dest
= (struct jpeg_destination_mgr
*)
258 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
259 sizeof(my_destination_mgr
));
262 dest
= (my_dest_ptr
) cinfo
->dest
;
263 dest
->pub
.init_destination
= init_destination
;
264 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
265 dest
->pub
.term_destination
= term_destination
;
266 dest
->stream
= &outfile
;
269 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
271 struct jpeg_compress_struct cinfo
;
272 struct my_error_mgr jerr
;
273 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
274 JSAMPLE
*image_buffer
;
275 int stride
; /* physical row width in image buffer */
277 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
278 jerr
.pub
.error_exit
= my_error_exit
;
280 if (!verbose
) cinfo
.err
->output_message
=NULL
;
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 if (verbose
) 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
);
315 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
317 unsigned char hdr
[2];
319 stream
.Read(&hdr
, 2);
320 stream
.SeekI(-2, wxFromCurrent
);
321 return (hdr
[0] == 0xFF && hdr
[1] == 0xD8);
324 #endif // wxUSE_STREAMS
326 #endif // wxUSE_LIBJPEG