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 (*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
)
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 /* Establish the setjmp return context for my_error_exit to use. */
165 if (setjmp(jerr
.setjmp_buffer
)) {
166 /* If we get here, the JPEG code has signaled an error.
167 * We need to clean up the JPEG object, close the input file, and return.
169 wxLogError(_("Couldn't load a JPEG image - probably file is corrupted."));
170 jpeg_destroy_decompress(&cinfo
);
171 if (image
->Ok()) image
->Destroy();
175 jpeg_create_decompress( &cinfo
);
176 jpeg_wxio_src( &cinfo
, stream
);
177 jpeg_read_header( &cinfo
, TRUE
);
178 cinfo
.out_color_space
= JCS_RGB
;
179 jpeg_start_decompress( &cinfo
);
181 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
183 jpeg_finish_decompress( &cinfo
);
184 jpeg_destroy_decompress( &cinfo
);
187 image
->SetMask( FALSE
);
188 ptr
= image
->GetData();
189 stride
= cinfo
.output_width
* 3;
190 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
191 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
193 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
194 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
195 memcpy( ptr
, tempbuf
[0], stride
);
198 jpeg_finish_decompress( &cinfo
);
199 jpeg_destroy_decompress( &cinfo
);
208 struct jpeg_destination_mgr pub
;
210 wxOutputStream
*stream
;
212 } my_destination_mgr
;
214 typedef my_destination_mgr
* my_dest_ptr
;
216 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
218 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
220 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
222 /* Allocate the output buffer --- it will be released when done with image */
223 dest
->buffer
= (JOCTET
*)
224 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
225 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
226 dest
->pub
.next_output_byte
= dest
->buffer
;
227 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
230 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
232 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
234 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
235 dest
->pub
.next_output_byte
= dest
->buffer
;
236 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
240 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
242 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
243 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
244 /* Write any data remaining in the buffer */
246 dest
->stream
->Write(dest
->buffer
, datacount
);
249 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
253 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
254 cinfo
->dest
= (struct jpeg_destination_mgr
*)
255 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
256 sizeof(my_destination_mgr
));
259 dest
= (my_dest_ptr
) cinfo
->dest
;
260 dest
->pub
.init_destination
= init_destination
;
261 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
262 dest
->pub
.term_destination
= term_destination
;
263 dest
->stream
= &outfile
;
266 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
268 struct jpeg_compress_struct cinfo
;
269 struct my_error_mgr jerr
;
270 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
271 JSAMPLE
*image_buffer
;
272 int stride
; /* physical row width in image buffer */
274 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
275 jerr
.pub
.error_exit
= my_error_exit
;
277 /* Establish the setjmp return context for my_error_exit to use. */
278 if (setjmp(jerr
.setjmp_buffer
)) {
279 /* If we get here, the JPEG code has signaled an error.
280 * We need to clean up the JPEG object, close the input file, and return.
282 wxLogError(_("Couldn't save a JPEG image - probably file is corrupted."));
283 jpeg_destroy_compress(&cinfo
);
287 jpeg_create_compress(&cinfo
);
288 jpeg_wxio_dest(&cinfo
, stream
);
290 cinfo
.image_width
= image
->GetWidth();
291 cinfo
.image_height
= image
->GetHeight();
292 cinfo
.input_components
= 3;
293 cinfo
.in_color_space
= JCS_RGB
;
294 jpeg_set_defaults(&cinfo
);
295 jpeg_start_compress(&cinfo
, TRUE
);
297 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
298 image_buffer
= image
->GetData();
299 while (cinfo
.next_scanline
< cinfo
.image_height
) {
300 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
301 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
303 jpeg_finish_compress(&cinfo
);
304 jpeg_destroy_compress(&cinfo
);