]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagjpeg.cpp
Include wx/dcmemory.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / common / imagjpeg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagjpeg.cpp
3 // Purpose: wxImage JPEG handler
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #if wxUSE_IMAGE && wxUSE_LIBJPEG
18
19 #include "wx/imagjpeg.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/log.h"
23 #include "wx/app.h"
24 #include "wx/intl.h"
25 #endif
26
27 #include "wx/bitmap.h"
28 #include "wx/debug.h"
29
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__))
36 #define HAVE_BOOLEAN
37 #include "wx/msw/wrapwin.h"
38 #endif
39
40 extern "C"
41 {
42 #if defined(__WXMSW__)
43 #define XMD_H
44 #endif
45 #include "jpeglib.h"
46 }
47
48 #include "wx/filefn.h"
49 #include "wx/wfstream.h"
50 #include "wx/module.h"
51
52 // For memcpy
53 #include <string.h>
54 // For JPEG library error handling
55 #include <setjmp.h>
56
57 #ifdef __SALFORDC__
58 #undef FAR
59 #endif
60
61 // ----------------------------------------------------------------------------
62 // types
63 // ----------------------------------------------------------------------------
64
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
68 // declaration
69 //
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)
77 #endif
78
79 //-----------------------------------------------------------------------------
80 // wxJPEGHandler
81 //-----------------------------------------------------------------------------
82
83 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
84
85 #if wxUSE_STREAMS
86
87 //------------- JPEG Data Source Manager
88
89 #define JPEG_IO_BUFFER_SIZE 2048
90
91 typedef struct {
92 struct jpeg_source_mgr pub; /* public fields */
93
94 JOCTET* buffer; /* start of buffer */
95 wxInputStream *stream;
96 } wx_source_mgr;
97
98 typedef wx_source_mgr * wx_src_ptr;
99
100 CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
101 {
102 }
103
104 CPP_METHODDEF(boolean) wx_fill_input_buffer ( j_decompress_ptr cinfo )
105 {
106 wx_src_ptr src = (wx_src_ptr) cinfo->src;
107
108 src->pub.next_input_byte = src->buffer;
109 src->pub.bytes_in_buffer = src->stream->Read(src->buffer, JPEG_IO_BUFFER_SIZE).LastRead();
110
111 if (src->pub.bytes_in_buffer == 0) // check for end-of-stream
112 {
113 // Insert a fake EOI marker
114 src->buffer[0] = 0xFF;
115 src->buffer[1] = JPEG_EOI;
116 src->pub.bytes_in_buffer = 2;
117 }
118 return TRUE;
119 }
120
121 CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
122 {
123 if (num_bytes > 0)
124 {
125 wx_src_ptr src = (wx_src_ptr) cinfo->src;
126
127 while (num_bytes > (long)src->pub.bytes_in_buffer)
128 {
129 num_bytes -= (long) src->pub.bytes_in_buffer;
130 src->pub.fill_input_buffer(cinfo);
131 }
132 src->pub.next_input_byte += (size_t) num_bytes;
133 src->pub.bytes_in_buffer -= (size_t) num_bytes;
134 }
135 }
136
137 CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo )
138 {
139 wx_src_ptr src = (wx_src_ptr) cinfo->src;
140
141 if (src->pub.bytes_in_buffer > 0)
142 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
143 delete[] src->buffer;
144 }
145
146
147 // JPEG error manager:
148
149 struct wx_error_mgr {
150 struct jpeg_error_mgr pub; /* "public" fields */
151
152 jmp_buf setjmp_buffer; /* for return to caller */
153 };
154
155 typedef struct wx_error_mgr * wx_error_ptr;
156
157 /*
158 * Here's the routine that will replace the standard error_exit method:
159 */
160
161 CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo)
162 {
163 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
164 wx_error_ptr myerr = (wx_error_ptr) cinfo->err;
165
166 /* Always display the message. */
167 /* We could postpone this until after returning, if we chose. */
168 (*cinfo->err->output_message) (cinfo);
169
170 /* Return control to the setjmp point */
171 longjmp(myerr->setjmp_buffer, 1);
172 }
173
174 /*
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".
179 */
180 CPP_METHODDEF(void) wx_ignore_message (j_common_ptr WXUNUSED(cinfo))
181 {
182 }
183
184 void wx_jpeg_io_src( j_decompress_ptr cinfo, wxInputStream& infile )
185 {
186 wx_src_ptr src;
187
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));
192 }
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;
198
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;
204 }
205
206
207 // temporarily disable the warning C4611 (interaction between '_setjmp' and
208 // C++ object destruction is non-portable) - I don't see any dtors here
209 #ifdef __VISUALC__
210 #pragma warning(disable:4611)
211 #endif /* VC++ */
212
213 bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
214 {
215 struct jpeg_decompress_struct cinfo;
216 struct wx_error_mgr jerr;
217 JSAMPARRAY tempbuf;
218 unsigned char *ptr;
219 unsigned stride;
220
221 image->Destroy();
222 cinfo.err = jpeg_std_error( &jerr.pub );
223 jerr.pub.error_exit = wx_error_exit;
224
225 if (!verbose)
226 cinfo.err->output_message = wx_ignore_message;
227
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.
232 */
233 if (verbose)
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();
238 return false;
239 }
240
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 );
246
247 image->Create( cinfo.image_width, cinfo.image_height );
248 if (!image->Ok()) {
249 jpeg_finish_decompress( &cinfo );
250 jpeg_destroy_decompress( &cinfo );
251 return false;
252 }
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 );
258
259 while ( cinfo.output_scanline < cinfo.output_height ) {
260 jpeg_read_scanlines( &cinfo, tempbuf, 1 );
261 memcpy( ptr, tempbuf[0], stride );
262 ptr += stride;
263 }
264 jpeg_finish_decompress( &cinfo );
265 jpeg_destroy_decompress( &cinfo );
266 return true;
267 }
268
269 typedef struct {
270 struct jpeg_destination_mgr pub;
271
272 wxOutputStream *stream;
273 JOCTET * buffer;
274 } wx_destination_mgr;
275
276 typedef wx_destination_mgr * wx_dest_ptr;
277
278 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
279
280 CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo)
281 {
282 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
283
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;
290 }
291
292 CPP_METHODDEF(boolean) wx_empty_output_buffer (j_compress_ptr cinfo)
293 {
294 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
295
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;
299 return TRUE;
300 }
301
302 CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo)
303 {
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 */
307 if (datacount > 0)
308 dest->stream->Write(dest->buffer, datacount);
309 }
310
311 GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
312 {
313 wx_dest_ptr dest;
314
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));
319 }
320
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;
326 }
327
328 bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
329 {
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 */
335
336 cinfo.err = jpeg_std_error(&jerr.pub);
337 jerr.pub.error_exit = wx_error_exit;
338
339 if (!verbose)
340 cinfo.err->output_message = wx_ignore_message;
341
342 /* Establish the setjmp return context for wx_error_exit to use. */
343 if (setjmp(jerr.setjmp_buffer))
344 {
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.
347 */
348 if (verbose)
349 wxLogError(_("JPEG: Couldn't save image."));
350 jpeg_destroy_compress(&cinfo);
351 return false;
352 }
353
354 jpeg_create_compress(&cinfo);
355 wx_jpeg_io_dest(&cinfo, stream);
356
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);
362
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);
371
372 // set the resolution fields in the output file
373 UINT16 resX,
374 resY;
375 if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) &&
376 image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) )
377 {
378 resX = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX);
379 resY = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
380 }
381 else if ( image->HasOption(wxIMAGE_OPTION_RESOLUTION) )
382 {
383 resX =
384 resY = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION);
385 }
386 else
387 {
388 resX =
389 resY = 0;
390 }
391
392 if ( resX && resY )
393 {
394 cinfo.X_density = resX;
395 cinfo.Y_density = resY;
396 }
397
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) )
402 {
403 cinfo.density_unit = (UINT8)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT);
404 }
405
406 jpeg_start_compress(&cinfo, TRUE);
407
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 );
413 }
414 jpeg_finish_compress(&cinfo);
415 jpeg_destroy_compress(&cinfo);
416
417 return true;
418 }
419
420 #ifdef __VISUALC__
421 #pragma warning(default:4611)
422 #endif /* VC++ */
423
424 bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
425 {
426 unsigned char hdr[2];
427
428 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
429 return false;
430
431 return hdr[0] == 0xFF && hdr[1] == 0xD8;
432 }
433
434 #endif // wxUSE_STREAMS
435
436 #endif // wxUSE_LIBJPEG