1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagjpeg.cpp
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"
17 #if wxUSE_IMAGE && wxUSE_LIBJPEG
19 #include "wx/imagjpeg.h"
27 #include "wx/bitmap.h"
30 // NB: Some compilers define boolean type in Windows headers
31 // (e.g. Watcom C++, but not Open Watcom).
32 // This causes a conflict with jmorecfg.h header from libjpeg, so we have
33 // to make sure libjpeg won't try to define boolean itself. This is done by
34 // defining HAVE_BOOLEAN.
35 #if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__DIGITALMARS__))
37 #include "wx/msw/wrapwin.h"
42 #if defined(__WXMSW__)
48 #include "wx/filefn.h"
49 #include "wx/wfstream.h"
50 #include "wx/module.h"
54 // For JPEG library error handling
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // the standard definition of METHODDEF(type) from jmorecfg.h is "static type"
66 // which means that we can't declare the method functions as extern "C" - the
67 // compiler (rightfully) complains about the multiple storage classes in
70 // so we only add extern "C" when using our own, modified, jmorecfg.h - and use
71 // whatever we have in the system headers if this is what we use hoping that it
72 // should be ok (can't do anything else)
73 #ifdef JPEG_METHOD_LINKAGE
74 #define CPP_METHODDEF(type) extern "C" METHODDEF(type)
75 #else // not using our jmorecfg.h header
76 #define CPP_METHODDEF(type) METHODDEF(type)
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
83 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
87 //------------- JPEG Data Source Manager
89 #define JPEG_IO_BUFFER_SIZE 2048
92 struct jpeg_source_mgr pub
; /* public fields */
94 JOCTET
* buffer
; /* start of buffer */
95 wxInputStream
*stream
;
98 typedef wx_source_mgr
* wx_src_ptr
;
100 CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr
WXUNUSED(cinfo
) )
104 CPP_METHODDEF(boolean
) wx_fill_input_buffer ( j_decompress_ptr cinfo
)
106 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
108 src
->pub
.next_input_byte
= src
->buffer
;
109 src
->pub
.bytes_in_buffer
= src
->stream
->Read(src
->buffer
, JPEG_IO_BUFFER_SIZE
).LastRead();
111 if (src
->pub
.bytes_in_buffer
== 0) // check for end-of-stream
113 // Insert a fake EOI marker
114 src
->buffer
[0] = 0xFF;
115 src
->buffer
[1] = JPEG_EOI
;
116 src
->pub
.bytes_in_buffer
= 2;
121 CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
125 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
127 while (num_bytes
> (long)src
->pub
.bytes_in_buffer
)
129 num_bytes
-= (long) src
->pub
.bytes_in_buffer
;
130 src
->pub
.fill_input_buffer(cinfo
);
132 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
133 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
137 CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo
)
139 wx_src_ptr src
= (wx_src_ptr
) cinfo
->src
;
141 if (src
->pub
.bytes_in_buffer
> 0)
142 src
->stream
->SeekI(-(long)src
->pub
.bytes_in_buffer
, wxFromCurrent
);
143 delete[] src
->buffer
;
147 // JPEG error manager:
149 struct wx_error_mgr
{
150 struct jpeg_error_mgr pub
; /* "public" fields */
152 jmp_buf setjmp_buffer
; /* for return to caller */
155 typedef struct wx_error_mgr
* wx_error_ptr
;
158 * Here's the routine that will replace the standard error_exit method:
161 CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo
)
163 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
164 wx_error_ptr myerr
= (wx_error_ptr
) cinfo
->err
;
166 /* Always display the message. */
167 /* We could postpone this until after returning, if we chose. */
168 (*cinfo
->err
->output_message
) (cinfo
);
170 /* Return control to the setjmp point */
171 longjmp(myerr
->setjmp_buffer
, 1);
175 * This will replace the standard output_message method when the user
176 * wants us to be silent (verbose==false). We must have such method instead of
177 * simply using NULL for cinfo->err->output_message because it's called
178 * unconditionally from within libjpeg when there's "garbage input".
180 CPP_METHODDEF(void) wx_ignore_message (j_common_ptr
WXUNUSED(cinfo
))
184 void wx_jpeg_io_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
188 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
189 cinfo
->src
= (struct jpeg_source_mgr
*)
190 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
191 sizeof(wx_source_mgr
));
193 src
= (wx_src_ptr
) cinfo
->src
;
194 src
->pub
.bytes_in_buffer
= 0; /* forces fill_input_buffer on first read */
195 src
->buffer
= new JOCTET
[JPEG_IO_BUFFER_SIZE
];
196 src
->pub
.next_input_byte
= NULL
; /* until buffer loaded */
197 src
->stream
= &infile
;
199 src
->pub
.init_source
= wx_init_source
;
200 src
->pub
.fill_input_buffer
= wx_fill_input_buffer
;
201 src
->pub
.skip_input_data
= wx_skip_input_data
;
202 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
203 src
->pub
.term_source
= wx_term_source
;
207 // temporarily disable the warning C4611 (interaction between '_setjmp' and
208 // C++ object destruction is non-portable) - I don't see any dtors here
210 #pragma warning(disable:4611)
213 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
215 struct jpeg_decompress_struct cinfo
;
216 struct wx_error_mgr jerr
;
222 cinfo
.err
= jpeg_std_error( &jerr
.pub
);
223 jerr
.pub
.error_exit
= wx_error_exit
;
226 cinfo
.err
->output_message
= wx_ignore_message
;
228 /* Establish the setjmp return context for wx_error_exit to use. */
229 if (setjmp(jerr
.setjmp_buffer
)) {
230 /* If we get here, the JPEG code has signaled an error.
231 * We need to clean up the JPEG object, close the input file, and return.
234 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
235 (cinfo
.src
->term_source
)(&cinfo
);
236 jpeg_destroy_decompress(&cinfo
);
237 if (image
->Ok()) image
->Destroy();
241 jpeg_create_decompress( &cinfo
);
242 wx_jpeg_io_src( &cinfo
, stream
);
243 jpeg_read_header( &cinfo
, TRUE
);
244 cinfo
.out_color_space
= JCS_RGB
;
245 jpeg_start_decompress( &cinfo
);
247 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
249 jpeg_finish_decompress( &cinfo
);
250 jpeg_destroy_decompress( &cinfo
);
253 image
->SetMask( false );
254 ptr
= image
->GetData();
255 stride
= cinfo
.output_width
* 3;
256 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
257 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
259 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
260 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
261 memcpy( ptr
, tempbuf
[0], stride
);
264 jpeg_finish_decompress( &cinfo
);
265 jpeg_destroy_decompress( &cinfo
);
270 struct jpeg_destination_mgr pub
;
272 wxOutputStream
*stream
;
274 } wx_destination_mgr
;
276 typedef wx_destination_mgr
* wx_dest_ptr
;
278 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
280 CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo
)
282 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
284 /* Allocate the output buffer --- it will be released when done with image */
285 dest
->buffer
= (JOCTET
*)
286 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
287 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
288 dest
->pub
.next_output_byte
= dest
->buffer
;
289 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
292 CPP_METHODDEF(boolean
) wx_empty_output_buffer (j_compress_ptr cinfo
)
294 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
296 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
297 dest
->pub
.next_output_byte
= dest
->buffer
;
298 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
302 CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo
)
304 wx_dest_ptr dest
= (wx_dest_ptr
) cinfo
->dest
;
305 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
306 /* Write any data remaining in the buffer */
308 dest
->stream
->Write(dest
->buffer
, datacount
);
311 GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
315 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
316 cinfo
->dest
= (struct jpeg_destination_mgr
*)
317 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
318 sizeof(wx_destination_mgr
));
321 dest
= (wx_dest_ptr
) cinfo
->dest
;
322 dest
->pub
.init_destination
= wx_init_destination
;
323 dest
->pub
.empty_output_buffer
= wx_empty_output_buffer
;
324 dest
->pub
.term_destination
= wx_term_destination
;
325 dest
->stream
= &outfile
;
328 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
330 struct jpeg_compress_struct cinfo
;
331 struct wx_error_mgr jerr
;
332 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
333 JSAMPLE
*image_buffer
;
334 int stride
; /* physical row width in image buffer */
336 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
337 jerr
.pub
.error_exit
= wx_error_exit
;
340 cinfo
.err
->output_message
= wx_ignore_message
;
342 /* Establish the setjmp return context for wx_error_exit to use. */
343 if (setjmp(jerr
.setjmp_buffer
))
345 /* If we get here, the JPEG code has signaled an error.
346 * We need to clean up the JPEG object, close the input file, and return.
349 wxLogError(_("JPEG: Couldn't save image."));
350 jpeg_destroy_compress(&cinfo
);
354 jpeg_create_compress(&cinfo
);
355 wx_jpeg_io_dest(&cinfo
, stream
);
357 cinfo
.image_width
= image
->GetWidth();
358 cinfo
.image_height
= image
->GetHeight();
359 cinfo
.input_components
= 3;
360 cinfo
.in_color_space
= JCS_RGB
;
361 jpeg_set_defaults(&cinfo
);
363 // TODO: 3rd parameter is force_baseline, what value should this be?
364 // Code says: "If force_baseline is TRUE, the computed quantization table entries
365 // are limited to 1..255 for JPEG baseline compatibility."
366 // 'Quality' is a number between 0 (terrible) and 100 (very good).
367 // The default (in jcparam.c, jpeg_set_defaults) is 75,
368 // and force_baseline is TRUE.
369 if (image
->HasOption(wxIMAGE_OPTION_QUALITY
))
370 jpeg_set_quality(&cinfo
, image
->GetOptionInt(wxIMAGE_OPTION_QUALITY
), TRUE
);
372 // set the resolution fields in the output file
375 if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
376 image
->HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
378 resX
= (UINT16
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
379 resY
= (UINT16
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
381 else if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTION
) )
384 resY
= (UINT16
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
394 cinfo
.X_density
= resX
;
395 cinfo
.Y_density
= resY
;
398 // sets the resolution unit field in the output file
399 // wxIMAGE_RESOLUTION_INCHES for inches
400 // wxIMAGE_RESOLUTION_CM for centimeters
401 if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT
) )
403 cinfo
.density_unit
= (UINT8
)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
406 jpeg_start_compress(&cinfo
, TRUE
);
408 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
409 image_buffer
= image
->GetData();
410 while (cinfo
.next_scanline
< cinfo
.image_height
) {
411 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
412 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
414 jpeg_finish_compress(&cinfo
);
415 jpeg_destroy_compress(&cinfo
);
421 #pragma warning(default:4611)
424 bool wxJPEGHandler::DoCanRead( wxInputStream
& stream
)
426 unsigned char hdr
[2];
428 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
431 return hdr
[0] == 0xFF && hdr
[1] == 0xD8;
434 #endif // wxUSE_STREAMS
436 #endif // wxUSE_LIBJPEG