]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
Applied patch [ 864469 ] WaitForAccept(): 100% CPU Usage (NON-GUI application)
[wxWidgets.git] / src / common / imagjpeg.cpp
CommitLineData
e9c4b1a2
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: imagjpeg.cpp
3// Purpose: wxImage JPEG handler
b59ff3c9 4// Author: Vaclav Slavik
e9c4b1a2 5// RCS-ID: $Id$
b59ff3c9 6// Copyright: (c) Vaclav Slavik
e9c4b1a2
JS
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
1aaaa712
VS
11#pragma implementation "imagjpeg.h"
12#endif
e9c4b1a2
JS
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
ce4169a4
RR
21#include "wx/defs.h"
22
c96ea657 23#if wxUSE_IMAGE && wxUSE_LIBJPEG
ce4169a4 24
1aaaa712 25#include "wx/imagjpeg.h"
e9c4b1a2
JS
26#include "wx/bitmap.h"
27#include "wx/debug.h"
28#include "wx/log.h"
29#include "wx/app.h"
cb69afe0 30
4c87eb44
VZ
31// NB: Some compilers define boolean type in Windows headers
32// (e.g. Watcom C++, but not Open Watcom).
cb69afe0
VS
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.
7d584866 36#if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__DIGITALMARS__) || (defined(__WATCOMC__) && __WATCOMC__ < 1200))
e7ca6139 37 #define HAVE_BOOLEAN
9ed0d735 38 #include "wx/msw/wrapwin.h"
e7ca6139 39#endif
cb69afe0 40
995612e2
VZ
41extern "C"
42{
cd2a4e16
JS
43 #if defined(__WXMSW__)
44 #define XMD_H
45 #endif
995612e2 46 #include "jpeglib.h"
e9c4b1a2 47}
cb69afe0 48
e9c4b1a2
JS
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>
b59ff3c9
VS
56// For JPEG library error handling
57#include <setjmp.h>
e9c4b1a2
JS
58
59#ifdef __SALFORDC__
e9c4b1a2
JS
60#undef FAR
61#endif
e9c4b1a2 62
d7c42914
VZ
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
90350682 80
e9c4b1a2
JS
81//-----------------------------------------------------------------------------
82// wxJPEGHandler
83//-----------------------------------------------------------------------------
84
e9c4b1a2 85IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
e9c4b1a2 86
9ab6ee85
GRG
87#if wxUSE_STREAMS
88
e9c4b1a2
JS
89//------------- JPEG Data Source Manager
90
1aaaa712
VS
91#define JPEG_IO_BUFFER_SIZE 2048
92
e9c4b1a2
JS
93typedef struct {
94 struct jpeg_source_mgr pub; /* public fields */
995612e2 95
e9c4b1a2 96 JOCTET* buffer; /* start of buffer */
1aaaa712 97 wxInputStream *stream;
e9c4b1a2
JS
98} my_source_mgr;
99
100typedef my_source_mgr * my_src_ptr;
101
d7c42914 102CPP_METHODDEF(void) my_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
e9c4b1a2
JS
103{
104}
105
d7c42914 106CPP_METHODDEF(boolean) my_fill_input_buffer ( j_decompress_ptr cinfo )
e9c4b1a2 107{
1aaaa712
VS
108 my_src_ptr src = (my_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 }
e9c4b1a2
JS
120 return TRUE;
121}
122
d7c42914 123CPP_METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
e9c4b1a2 124{
1aaaa712
VS
125 if (num_bytes > 0)
126 {
127 my_src_ptr src = (my_src_ptr) cinfo->src;
128
33ac7e6f 129 while (num_bytes > (long)src->pub.bytes_in_buffer)
1aaaa712
VS
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 }
e9c4b1a2
JS
137}
138
d7c42914 139CPP_METHODDEF(void) my_term_source ( j_decompress_ptr cinfo )
e9c4b1a2
JS
140{
141 my_src_ptr src = (my_src_ptr) cinfo->src;
995612e2 142
1aaaa712 143 if (src->pub.bytes_in_buffer > 0)
3ca6a5f0 144 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
1aaaa712 145 delete[] src->buffer;
e9c4b1a2
JS
146}
147
e9c4b1a2 148
b59ff3c9
VS
149// JPEG error manager:
150
151struct my_error_mgr {
995612e2 152 struct jpeg_error_mgr pub; /* "public" fields */
b59ff3c9 153
995612e2 154 jmp_buf setjmp_buffer; /* for return to caller */
b59ff3c9
VS
155};
156
157typedef struct my_error_mgr * my_error_ptr;
158
159/*
160 * Here's the routine that will replace the standard error_exit method:
161 */
162
d7c42914 163CPP_METHODDEF(void) my_error_exit (j_common_ptr cinfo)
b59ff3c9
VS
164{
165 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
166 my_error_ptr myerr = (my_error_ptr) cinfo->err;
167
168 /* Always display the message. */
169 /* We could postpone this until after returning, if we chose. */
deb2fec0 170 if (cinfo->err->output_message) (*cinfo->err->output_message) (cinfo);
b59ff3c9
VS
171
172 /* Return control to the setjmp point */
173 longjmp(myerr->setjmp_buffer, 1);
174}
175
90350682
VZ
176void jpeg_wxio_src( j_decompress_ptr cinfo, wxInputStream& infile )
177{
178 my_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(my_source_mgr));
90350682
VZ
184 }
185 src = (my_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 = my_init_source;
192 src->pub.fill_input_buffer = my_fill_input_buffer;
193 src->pub.skip_input_data = my_skip_input_data;
194 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
195 src->pub.term_source = my_term_source;
196}
197
198
3ca6a5f0
BP
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++ */
e9c4b1a2 204
700ec454 205bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
e9c4b1a2
JS
206{
207 struct jpeg_decompress_struct cinfo;
b59ff3c9 208 struct my_error_mgr jerr;
e9c4b1a2
JS
209 JSAMPARRAY tempbuf;
210 unsigned char *ptr;
211 unsigned stride;
995612e2 212
e9c4b1a2 213 image->Destroy();
b59ff3c9
VS
214 cinfo.err = jpeg_std_error( &jerr.pub );
215 jerr.pub.error_exit = my_error_exit;
216
deb2fec0
SB
217 if (!verbose) cinfo.err->output_message=NULL;
218
b59ff3c9
VS
219 /* Establish the setjmp return context for my_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 */
33ac7e6f 224 if (verbose)
58c837a4 225 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
192644bf 226 (cinfo.src->term_source)(&cinfo);
b59ff3c9
VS
227 jpeg_destroy_decompress(&cinfo);
228 if (image->Ok()) image->Destroy();
229 return FALSE;
230 }
231
e9c4b1a2
JS
232 jpeg_create_decompress( &cinfo );
233 jpeg_wxio_src( &cinfo, stream );
234 jpeg_read_header( &cinfo, TRUE );
235 cinfo.out_color_space = JCS_RGB;
236 jpeg_start_decompress( &cinfo );
995612e2 237
e9c4b1a2
JS
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 );
995612e2 249
e9c4b1a2
JS
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
e9c4b1a2
JS
260typedef struct {
261 struct jpeg_destination_mgr pub;
995612e2 262
e9c4b1a2
JS
263 wxOutputStream *stream;
264 JOCTET * buffer;
265} my_destination_mgr;
266
267typedef my_destination_mgr * my_dest_ptr;
268
995612e2 269#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
e9c4b1a2 270
d7c42914 271CPP_METHODDEF(void) init_destination (j_compress_ptr cinfo)
e9c4b1a2
JS
272{
273 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
995612e2 274
e9c4b1a2
JS
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
d7c42914 283CPP_METHODDEF(boolean) empty_output_buffer (j_compress_ptr cinfo)
e9c4b1a2
JS
284{
285 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
995612e2 286
e9c4b1a2
JS
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
d7c42914 293CPP_METHODDEF(void) term_destination (j_compress_ptr cinfo)
e9c4b1a2
JS
294{
295 my_dest_ptr dest = (my_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
302GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
303{
304 my_dest_ptr dest;
995612e2
VZ
305
306 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
e9c4b1a2
JS
307 cinfo->dest = (struct jpeg_destination_mgr *)
308 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
309 sizeof(my_destination_mgr));
310 }
995612e2 311
e9c4b1a2
JS
312 dest = (my_dest_ptr) cinfo->dest;
313 dest->pub.init_destination = init_destination;
314 dest->pub.empty_output_buffer = empty_output_buffer;
315 dest->pub.term_destination = term_destination;
316 dest->stream = &outfile;
317}
318
deb2fec0 319bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2
JS
320{
321 struct jpeg_compress_struct cinfo;
b59ff3c9 322 struct my_error_mgr jerr;
995612e2 323 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
e9c4b1a2 324 JSAMPLE *image_buffer;
995612e2
VZ
325 int stride; /* physical row width in image buffer */
326
b59ff3c9
VS
327 cinfo.err = jpeg_std_error(&jerr.pub);
328 jerr.pub.error_exit = my_error_exit;
329
deb2fec0
SB
330 if (!verbose) cinfo.err->output_message=NULL;
331
b59ff3c9 332 /* Establish the setjmp return context for my_error_exit to use. */
33ac7e6f 333 if (setjmp(jerr.setjmp_buffer))
58c837a4
RR
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 */
33ac7e6f 338 if (verbose)
58c837a4
RR
339 wxLogError(_("JPEG: Couldn't save image."));
340 jpeg_destroy_compress(&cinfo);
341 return FALSE;
b59ff3c9
VS
342 }
343
e9c4b1a2
JS
344 jpeg_create_compress(&cinfo);
345 jpeg_wxio_dest(&cinfo, stream);
995612e2 346
e9c4b1a2
JS
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);
5e5437e0
JS
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
89d38f8c
VS
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
e9c4b1a2 377 jpeg_start_compress(&cinfo, TRUE);
995612e2
VZ
378
379 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
e9c4b1a2
JS
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);
995612e2 387
e9c4b1a2
JS
388 return TRUE;
389}
e9c4b1a2 390
3ca6a5f0
BP
391#ifdef __VISUALC__
392 #pragma warning(default:4611)
393#endif /* VC++ */
0828c087 394
995612e2 395bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
0828c087
VS
396{
397 unsigned char hdr[2];
995612e2 398
79fa2374
VZ
399 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
400 return FALSE;
401
79fa2374 402 return hdr[0] == 0xFF && hdr[1] == 0xD8;
0828c087
VS
403}
404
9ab6ee85
GRG
405#endif // wxUSE_STREAMS
406
407#endif // wxUSE_LIBJPEG
b59ff3c9
VS
408
409
410
411
412
413