1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage JPEG handler
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #if wxUSE_IMAGE && wxUSE_LIBJPEG
21 #include "wx/imagjpeg.h"
22 #include "wx/bitmap.h"
27 // NB: Some compilers define boolean type in Windows headers
28 // (e.g. Watcom C++, but not Open Watcom).
29 // This causes a conflict with jmorecfg.h header from libjpeg, so we have
30 // to make sure libjpeg won't try to define boolean itself. This is done by
31 // defining HAVE_BOOLEAN.
32 #if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__DIGITALMARS__) || (defined(__WATCOMC__) && __WATCOMC__ < 1200))
34 #include "wx/msw/wrapwin.h"
39 #if defined(__WXMSW__)
45 #include "wx/filefn.h"
46 #include "wx/wfstream.h"
48 #include "wx/module.h"
52 // For JPEG library error handling
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // the standard definition of METHODDEF(type) from jmorecfg.h is "static type"
64 // which means that we can't declare the method functions as extern "C" - the
65 // compiler (rightfully) complains about the multiple storage classes in
68 // so we only add extern "C" when using our own, modified, jmorecfg.h - and use
69 // whatever we have in the system headers if this is what we use hoping that it
70 // should be ok (can't do anything else)
71 #ifdef JPEG_METHOD_LINKAGE
72 #define CPP_METHODDEF(type) extern "C" METHODDEF(type)
73 #else // not using our jmorecfg.h header
74 #define CPP_METHODDEF(type) METHODDEF(type)
77 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
81 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
85 //------------- JPEG Data Source Manager
87 #define JPEG_IO_BUFFER_SIZE 2048
90 struct jpeg_source_mgr pub
; /* public fields */
92 JOCTET
* buffer
; /* start of buffer */
93 wxInputStream
*stream
;
96 typedef wx_source_mgr
* wx_src_ptr
;
98 CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
102 CPP_METHODDEF(boolean
) wx_fill_input_buffer ( j_decompress_ptr cinfo
)
104 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
106 src
->pub
.next_input_byte
= src
->buffer
;
107 src
->pub
.bytes_in_buffer
= src
->stream
->Read(src
->buffer
, JPEG_IO_BUFFER_SIZE
).LastRead();
109 if (src
->pub
.bytes_in_buffer
== 0) // check for end-of-stream
111 // Insert a fake EOI marker
112 src
->buffer
[0] = 0xFF;
113 src
->buffer
[1] = JPEG_EOI
;
114 src
->pub
.bytes_in_buffer
= 2;
119 CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
123 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
125 while (num_bytes
> (long)src
->pub
.bytes_in_buffer
)
127 num_bytes
-= (long) src
->pub
.bytes_in_buffer
;
128 src
->pub
.fill_input_buffer(cinfo
);
130 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
131 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
135 CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo
)
137 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
139 if (src
->pub
.bytes_in_buffer
> 0)
140 src
->stream
->SeekI(-(long)src
->pub
.bytes_in_buffer
, wxFromCurrent
);
141 delete[] src
->buffer
;
145 // JPEG error manager:
147 struct wx_error_mgr
{
148 struct jpeg_error_mgr pub
; /* "public" fields */
150 jmp_buf setjmp_buffer
; /* for return to caller */
153 typedef struct wx_error_mgr
* wx_error_ptr
;
156 * Here's the routine that will replace the standard error_exit method:
159 CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo
)
161 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
162 wx_error_ptr myerr
= (wx_error_ptr
) cinfo
->err
;
164 /* Always display the message. */
165 /* We could postpone this until after returning, if we chose. */
166 if (cinfo
->err
->output_message
) (*cinfo
->err
->output_message
) (cinfo
);
168 /* Return control to the setjmp point */
169 longjmp(myerr
->setjmp_buffer
, 1);
172 void wx_jpeg_io_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
176 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
177 cinfo
->src
= (struct jpeg_source_mgr
*)
178 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
179 sizeof(wx_source_mgr
));
181 src
= (wx_src_ptr
) cinfo
->src
;
182 src
->pub
.bytes_in_buffer
= 0; /* forces fill_input_buffer on first read */
183 src
->buffer
= new JOCTET
[JPEG_IO_BUFFER_SIZE
];
184 src
->pub
.next_input_byte
= NULL
; /* until buffer loaded */
185 src
->stream
= &infile
;
187 src
->pub
.init_source
= wx_init_source
;
188 src
->pub
.fill_input_buffer
= wx_fill_input_buffer
;
189 src
->pub
.skip_input_data
= wx_skip_input_data
;
190 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
191 src
->pub
.term_source
= wx_term_source
;
195 // temporarily disable the warning C4611 (interaction between '_setjmp' and
196 // C++ object destruction is non-portable) - I don't see any dtors here
198 #pragma warning(disable:4611)
201 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
203 struct jpeg_decompress_struct cinfo
;
204 struct wx_error_mgr jerr
;
210 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
211 jerr
.pub
.error_exit
= wx_error_exit
;
213 if (!verbose
) cinfo
.err
->output_message
=NULL
;
215 /* Establish the setjmp return context for wx_error_exit to use. */
216 if (setjmp(jerr
.setjmp_buffer
)) {
217 /* If we get here, the JPEG code has signaled an error.
218 * We need to clean up the JPEG object, close the input file, and return.
221 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
222 (cinfo
.src
->term_source
)(&cinfo
);
223 jpeg_destroy_decompress(&cinfo
);
224 if (image
->Ok()) image
->Destroy();
228 jpeg_create_decompress( &cinfo
);
229 wx_jpeg_io_src( &cinfo
, stream
);
230 jpeg_read_header( &cinfo
, TRUE
);
231 cinfo
.out_color_space
= JCS_RGB
;
232 jpeg_start_decompress( &cinfo
);
234 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
236 jpeg_finish_decompress( &cinfo
);
237 jpeg_destroy_decompress( &cinfo
);
240 image
->SetMask( false );
241 ptr
= image
->GetData();
242 stride
= cinfo
.output_width
* 3;
243 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
244 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
246 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
247 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
248 memcpy( ptr
, tempbuf
[0], stride
);
251 jpeg_finish_decompress( &cinfo
);
252 jpeg_destroy_decompress( &cinfo
);
257 struct jpeg_destination_mgr pub
;
259 wxOutputStream
*stream
;
261 } wx_destination_mgr
;
263 typedef wx_destination_mgr
* wx_dest_ptr
;
265 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
267 CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo
)
269 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
271 /* Allocate the output buffer --- it will be released when done with image */
272 dest
->buffer
= (JOCTET
*)
273 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
274 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
275 dest
->pub
.next_output_byte
= dest
->buffer
;
276 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
279 CPP_METHODDEF(boolean
) wx_empty_output_buffer (j_compress_ptr cinfo
)
281 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
283 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
284 dest
->pub
.next_output_byte
= dest
->buffer
;
285 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
289 CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo
)
291 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
292 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
293 /* Write any data remaining in the buffer */
295 dest
->stream
->Write(dest
->buffer
, datacount
);
298 GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
302 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
303 cinfo
->dest
= (struct jpeg_destination_mgr
*)
304 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
305 sizeof(wx_destination_mgr
));
308 dest
= (wx_dest_ptr
) cinfo
->dest
;
309 dest
->pub
.init_destination
= wx_init_destination
;
310 dest
->pub
.empty_output_buffer
= wx_empty_output_buffer
;
311 dest
->pub
.term_destination
= wx_term_destination
;
312 dest
->stream
= &outfile
;
315 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
317 struct jpeg_compress_struct cinfo
;
318 struct wx_error_mgr jerr
;
319 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
320 JSAMPLE
*image_buffer
;
321 int stride
; /* physical row width in image buffer */
323 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
324 jerr
.pub
.error_exit
= wx_error_exit
;
326 if (!verbose
) cinfo
.err
->output_message
=NULL
;
328 /* Establish the setjmp return context for wx_error_exit to use. */
329 if (setjmp(jerr
.setjmp_buffer
))
331 /* If we get here, the JPEG code has signaled an error.
332 * We need to clean up the JPEG object, close the input file, and return.
335 wxLogError(_("JPEG: Couldn't save image."));
336 jpeg_destroy_compress(&cinfo
);
340 jpeg_create_compress(&cinfo
);
341 wx_jpeg_io_dest(&cinfo
, stream
);
343 cinfo
.image_width
= image
->GetWidth();
344 cinfo
.image_height
= image
->GetHeight();
345 cinfo
.input_components
= 3;
346 cinfo
.in_color_space
= JCS_RGB
;
347 jpeg_set_defaults(&cinfo
);
349 // TODO: 3rd parameter is force_baseline, what value should this be?
350 // Code says: "If force_baseline is TRUE, the computed quantization table entries
351 // are limited to 1..255 for JPEG baseline compatibility."
352 // 'Quality' is a number between 0 (terrible) and 100 (very good).
353 // The default (in jcparam.c, jpeg_set_defaults) is 75,
354 // and force_baseline is TRUE.
355 if (image
->HasOption(wxIMAGE_OPTION_QUALITY
))
356 jpeg_set_quality(&cinfo
, image
->GetOptionInt(wxIMAGE_OPTION_QUALITY
), TRUE
);
358 // set the resolution fields in the output file
361 if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
362 image
->HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
364 resX
= (UINT16
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
365 resY
= (UINT16
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
367 else if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTION
) )
370 resY
= (UINT16
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
380 cinfo
.X_density
= resX
;
381 cinfo
.Y_density
= resY
;
384 // sets the resolution unit field in the output file
385 // wxIMAGE_RESOLUTION_INCHES for inches
386 // wxIMAGE_RESOLUTION_CM for centimeters
387 if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT
) )
389 cinfo
.density_unit
= (UINT8
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
392 jpeg_start_compress(&cinfo
, TRUE
);
394 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
395 image_buffer
= image
->GetData();
396 while (cinfo
.next_scanline
< cinfo
.image_height
) {
397 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
398 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
400 jpeg_finish_compress(&cinfo
);
401 jpeg_destroy_compress(&cinfo
);
407 #pragma warning(default:4611)
410 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
412 unsigned char hdr
[2];
414 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
417 return hdr
[0] == 0xFF && hdr
[1] == 0xD8;
420 #endif // wxUSE_STREAMS
422 #endif // wxUSE_LIBJPEG