]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagjpeg.cpp
don't just drop click events resulting from triple clicks
[wxWidgets.git] / src / common / imagjpeg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "imagjpeg.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #include "wx/defs.h"
22
23 #if wxUSE_IMAGE && wxUSE_LIBJPEG
24
25 #include "wx/imagjpeg.h"
26 #include "wx/bitmap.h"
27 #include "wx/debug.h"
28 #include "wx/log.h"
29 #include "wx/app.h"
30
31 // NB: Some compilers define boolean type in Windows headers
32 // (e.g. Watcom C++, but not Open Watcom).
33 // This causes a conflict with jmorecfg.h header from libjpeg, so we have
34 // to make sure libjpeg won't try to define boolean itself. This is done by
35 // defining HAVE_BOOLEAN.
36 #if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__DIGITALMARS__) || (defined(__WATCOMC__) && __WATCOMC__ < 1200))
37 #define HAVE_BOOLEAN
38 #include "wx/msw/wrapwin.h"
39 #endif
40
41 extern "C"
42 {
43 #if defined(__WXMSW__)
44 #define XMD_H
45 #endif
46 #include "jpeglib.h"
47 }
48
49 #include "wx/filefn.h"
50 #include "wx/wfstream.h"
51 #include "wx/intl.h"
52 #include "wx/module.h"
53
54 // For memcpy
55 #include <string.h>
56 // For JPEG library error handling
57 #include <setjmp.h>
58
59 #ifdef __SALFORDC__
60 #undef FAR
61 #endif
62
63 // ----------------------------------------------------------------------------
64 // types
65 // ----------------------------------------------------------------------------
66
67 // the standard definition of METHODDEF(type) from jmorecfg.h is "static type"
68 // which means that we can't declare the method functions as extern "C" - the
69 // compiler (rightfully) complains about the multiple storage classes in
70 // declaration
71 //
72 // so we only add extern "C" when using our own, modified, jmorecfg.h - and use
73 // whatever we have in the system headers if this is what we use hoping that it
74 // should be ok (can't do anything else)
75 #ifdef JPEG_METHOD_LINKAGE
76 #define CPP_METHODDEF(type) extern "C" METHODDEF(type)
77 #else // not using our jmorecfg.h header
78 #define CPP_METHODDEF(type) METHODDEF(type)
79 #endif
80
81 //-----------------------------------------------------------------------------
82 // wxJPEGHandler
83 //-----------------------------------------------------------------------------
84
85 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
86
87 #if wxUSE_STREAMS
88
89 //------------- JPEG Data Source Manager
90
91 #define JPEG_IO_BUFFER_SIZE 2048
92
93 typedef struct {
94 struct jpeg_source_mgr pub; /* public fields */
95
96 JOCTET* buffer; /* start of buffer */
97 wxInputStream *stream;
98 } wx_source_mgr;
99
100 typedef wx_source_mgr * wx_src_ptr;
101
102 CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
103 {
104 }
105
106 CPP_METHODDEF(boolean) wx_fill_input_buffer ( j_decompress_ptr cinfo )
107 {
108 wx_src_ptr src = (wx_src_ptr) cinfo->src;
109
110 src->pub.next_input_byte = src->buffer;
111 src->pub.bytes_in_buffer = src->stream->Read(src->buffer, JPEG_IO_BUFFER_SIZE).LastRead();
112
113 if (src->pub.bytes_in_buffer == 0) // check for end-of-stream
114 {
115 // Insert a fake EOI marker
116 src->buffer[0] = 0xFF;
117 src->buffer[1] = JPEG_EOI;
118 src->pub.bytes_in_buffer = 2;
119 }
120 return TRUE;
121 }
122
123 CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
124 {
125 if (num_bytes > 0)
126 {
127 wx_src_ptr src = (wx_src_ptr) cinfo->src;
128
129 while (num_bytes > (long)src->pub.bytes_in_buffer)
130 {
131 num_bytes -= (long) src->pub.bytes_in_buffer;
132 src->pub.fill_input_buffer(cinfo);
133 }
134 src->pub.next_input_byte += (size_t) num_bytes;
135 src->pub.bytes_in_buffer -= (size_t) num_bytes;
136 }
137 }
138
139 CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo )
140 {
141 wx_src_ptr src = (wx_src_ptr) cinfo->src;
142
143 if (src->pub.bytes_in_buffer > 0)
144 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
145 delete[] src->buffer;
146 }
147
148
149 // JPEG error manager:
150
151 struct wx_error_mgr {
152 struct jpeg_error_mgr pub; /* "public" fields */
153
154 jmp_buf setjmp_buffer; /* for return to caller */
155 };
156
157 typedef struct wx_error_mgr * wx_error_ptr;
158
159 /*
160 * Here's the routine that will replace the standard error_exit method:
161 */
162
163 CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo)
164 {
165 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
166 wx_error_ptr myerr = (wx_error_ptr) cinfo->err;
167
168 /* Always display the message. */
169 /* We could postpone this until after returning, if we chose. */
170 if (cinfo->err->output_message) (*cinfo->err->output_message) (cinfo);
171
172 /* Return control to the setjmp point */
173 longjmp(myerr->setjmp_buffer, 1);
174 }
175
176 void wx_jpeg_io_src( j_decompress_ptr cinfo, wxInputStream& infile )
177 {
178 wx_src_ptr src;
179
180 if (cinfo->src == NULL) { /* first time for this JPEG object? */
181 cinfo->src = (struct jpeg_source_mgr *)
182 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
183 sizeof(wx_source_mgr));
184 }
185 src = (wx_src_ptr) cinfo->src;
186 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
187 src->buffer = new JOCTET[JPEG_IO_BUFFER_SIZE];
188 src->pub.next_input_byte = NULL; /* until buffer loaded */
189 src->stream = &infile;
190
191 src->pub.init_source = wx_init_source;
192 src->pub.fill_input_buffer = wx_fill_input_buffer;
193 src->pub.skip_input_data = wx_skip_input_data;
194 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
195 src->pub.term_source = wx_term_source;
196 }
197
198
199 // temporarily disable the warning C4611 (interaction between '_setjmp' and
200 // C++ object destruction is non-portable) - I don't see any dtors here
201 #ifdef __VISUALC__
202 #pragma warning(disable:4611)
203 #endif /* VC++ */
204
205 bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
206 {
207 struct jpeg_decompress_struct cinfo;
208 struct wx_error_mgr jerr;
209 JSAMPARRAY tempbuf;
210 unsigned char *ptr;
211 unsigned stride;
212
213 image->Destroy();
214 cinfo.err = jpeg_std_error( &jerr.pub );
215 jerr.pub.error_exit = wx_error_exit;
216
217 if (!verbose) cinfo.err->output_message=NULL;
218
219 /* Establish the setjmp return context for wx_error_exit to use. */
220 if (setjmp(jerr.setjmp_buffer)) {
221 /* If we get here, the JPEG code has signaled an error.
222 * We need to clean up the JPEG object, close the input file, and return.
223 */
224 if (verbose)
225 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
226 (cinfo.src->term_source)(&cinfo);
227 jpeg_destroy_decompress(&cinfo);
228 if (image->Ok()) image->Destroy();
229 return FALSE;
230 }
231
232 jpeg_create_decompress( &cinfo );
233 wx_jpeg_io_src( &cinfo, stream );
234 jpeg_read_header( &cinfo, TRUE );
235 cinfo.out_color_space = JCS_RGB;
236 jpeg_start_decompress( &cinfo );
237
238 image->Create( cinfo.image_width, cinfo.image_height );
239 if (!image->Ok()) {
240 jpeg_finish_decompress( &cinfo );
241 jpeg_destroy_decompress( &cinfo );
242 return FALSE;
243 }
244 image->SetMask( FALSE );
245 ptr = image->GetData();
246 stride = cinfo.output_width * 3;
247 tempbuf = (*cinfo.mem->alloc_sarray)
248 ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 );
249
250 while ( cinfo.output_scanline < cinfo.output_height ) {
251 jpeg_read_scanlines( &cinfo, tempbuf, 1 );
252 memcpy( ptr, tempbuf[0], stride );
253 ptr += stride;
254 }
255 jpeg_finish_decompress( &cinfo );
256 jpeg_destroy_decompress( &cinfo );
257 return TRUE;
258 }
259
260 typedef struct {
261 struct jpeg_destination_mgr pub;
262
263 wxOutputStream *stream;
264 JOCTET * buffer;
265 } wx_destination_mgr;
266
267 typedef wx_destination_mgr * wx_dest_ptr;
268
269 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
270
271 CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo)
272 {
273 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
274
275 /* Allocate the output buffer --- it will be released when done with image */
276 dest->buffer = (JOCTET *)
277 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
278 OUTPUT_BUF_SIZE * sizeof(JOCTET));
279 dest->pub.next_output_byte = dest->buffer;
280 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
281 }
282
283 CPP_METHODDEF(boolean) wx_empty_output_buffer (j_compress_ptr cinfo)
284 {
285 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
286
287 dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE);
288 dest->pub.next_output_byte = dest->buffer;
289 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
290 return TRUE;
291 }
292
293 CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo)
294 {
295 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
296 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
297 /* Write any data remaining in the buffer */
298 if (datacount > 0)
299 dest->stream->Write(dest->buffer, datacount);
300 }
301
302 GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
303 {
304 wx_dest_ptr dest;
305
306 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
307 cinfo->dest = (struct jpeg_destination_mgr *)
308 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
309 sizeof(wx_destination_mgr));
310 }
311
312 dest = (wx_dest_ptr) cinfo->dest;
313 dest->pub.init_destination = wx_init_destination;
314 dest->pub.empty_output_buffer = wx_empty_output_buffer;
315 dest->pub.term_destination = wx_term_destination;
316 dest->stream = &outfile;
317 }
318
319 bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
320 {
321 struct jpeg_compress_struct cinfo;
322 struct wx_error_mgr jerr;
323 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
324 JSAMPLE *image_buffer;
325 int stride; /* physical row width in image buffer */
326
327 cinfo.err = jpeg_std_error(&jerr.pub);
328 jerr.pub.error_exit = wx_error_exit;
329
330 if (!verbose) cinfo.err->output_message=NULL;
331
332 /* Establish the setjmp return context for wx_error_exit to use. */
333 if (setjmp(jerr.setjmp_buffer))
334 {
335 /* If we get here, the JPEG code has signaled an error.
336 * We need to clean up the JPEG object, close the input file, and return.
337 */
338 if (verbose)
339 wxLogError(_("JPEG: Couldn't save image."));
340 jpeg_destroy_compress(&cinfo);
341 return FALSE;
342 }
343
344 jpeg_create_compress(&cinfo);
345 wx_jpeg_io_dest(&cinfo, stream);
346
347 cinfo.image_width = image->GetWidth();
348 cinfo.image_height = image->GetHeight();
349 cinfo.input_components = 3;
350 cinfo.in_color_space = JCS_RGB;
351 jpeg_set_defaults(&cinfo);
352
353 // TODO: 3rd parameter is force_baseline, what value should this be?
354 // Code says: "If force_baseline is TRUE, the computed quantization table entries
355 // are limited to 1..255 for JPEG baseline compatibility."
356 // 'Quality' is a number between 0 (terrible) and 100 (very good).
357 // The default (in jcparam.c, jpeg_set_defaults) is 75,
358 // and force_baseline is TRUE.
359 if (image->HasOption(wxT("quality")))
360 jpeg_set_quality(&cinfo, image->GetOptionInt(wxT("quality")), TRUE);
361
362 // sets the resolution fields in the output file
363 if (image->HasOption(wxIMAGE_OPTION_RESOLUTION))
364 {
365 cinfo.X_density =
366 cinfo.Y_density = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION);
367 }
368
369 // sets the resolution unit field in the output file
370 // wxIMAGE_RESOLUTION_INCHES for inches
371 // wxIMAGE_RESOLUTION_CM for centimeters
372 if (image->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT))
373 {
374 cinfo.density_unit = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT);
375 }
376
377 jpeg_start_compress(&cinfo, TRUE);
378
379 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
380 image_buffer = image->GetData();
381 while (cinfo.next_scanline < cinfo.image_height) {
382 row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
383 jpeg_write_scanlines( &cinfo, row_pointer, 1 );
384 }
385 jpeg_finish_compress(&cinfo);
386 jpeg_destroy_compress(&cinfo);
387
388 return TRUE;
389 }
390
391 #ifdef __VISUALC__
392 #pragma warning(default:4611)
393 #endif /* VC++ */
394
395 bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
396 {
397 unsigned char hdr[2];
398
399 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
400 return FALSE;
401
402 return hdr[0] == 0xFF && hdr[1] == 0xD8;
403 }
404
405 #endif // wxUSE_STREAMS
406
407 #endif // wxUSE_LIBJPEG
408
409
410
411
412
413