]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
Calculate correct client size for windows that are using deferred sizing.
[wxWidgets.git] / src / common / imagjpeg.cpp
CommitLineData
e9c4b1a2 1/////////////////////////////////////////////////////////////////////////////
6d3d756a 2// Name: src/common/imagjpeg.cpp
e9c4b1a2 3// Purpose: wxImage JPEG handler
b59ff3c9 4// Author: Vaclav Slavik
e9c4b1a2 5// RCS-ID: $Id$
b59ff3c9 6// Copyright: (c) Vaclav Slavik
65571936 7// Licence: wxWindows licence
e9c4b1a2
JS
8/////////////////////////////////////////////////////////////////////////////
9
e9c4b1a2
JS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
14#pragma hdrstop
15#endif
16
ce4169a4
RR
17#include "wx/defs.h"
18
c96ea657 19#if wxUSE_IMAGE && wxUSE_LIBJPEG
ce4169a4 20
1aaaa712 21#include "wx/imagjpeg.h"
e9c4b1a2
JS
22#include "wx/bitmap.h"
23#include "wx/debug.h"
24#include "wx/log.h"
25#include "wx/app.h"
cb69afe0 26
7beb59f3 27// NB: Some compilers define boolean type in Windows headers
4c87eb44 28// (e.g. Watcom C++, but not Open Watcom).
cb69afe0
VS
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.
6d3d756a 32#if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__DIGITALMARS__))
e7ca6139 33 #define HAVE_BOOLEAN
9ed0d735 34 #include "wx/msw/wrapwin.h"
e7ca6139 35#endif
cb69afe0 36
995612e2
VZ
37extern "C"
38{
cd2a4e16
JS
39 #if defined(__WXMSW__)
40 #define XMD_H
41 #endif
995612e2 42 #include "jpeglib.h"
e9c4b1a2 43}
cb69afe0 44
e9c4b1a2
JS
45#include "wx/filefn.h"
46#include "wx/wfstream.h"
47#include "wx/intl.h"
48#include "wx/module.h"
49
50// For memcpy
51#include <string.h>
b59ff3c9
VS
52// For JPEG library error handling
53#include <setjmp.h>
e9c4b1a2
JS
54
55#ifdef __SALFORDC__
e9c4b1a2
JS
56#undef FAR
57#endif
e9c4b1a2 58
d7c42914
VZ
59// ----------------------------------------------------------------------------
60// types
61// ----------------------------------------------------------------------------
62
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
66// declaration
67//
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)
75#endif
90350682 76
e9c4b1a2
JS
77//-----------------------------------------------------------------------------
78// wxJPEGHandler
79//-----------------------------------------------------------------------------
80
e9c4b1a2 81IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
e9c4b1a2 82
9ab6ee85
GRG
83#if wxUSE_STREAMS
84
e9c4b1a2
JS
85//------------- JPEG Data Source Manager
86
1aaaa712
VS
87#define JPEG_IO_BUFFER_SIZE 2048
88
e9c4b1a2
JS
89typedef struct {
90 struct jpeg_source_mgr pub; /* public fields */
995612e2 91
e9c4b1a2 92 JOCTET* buffer; /* start of buffer */
1aaaa712 93 wxInputStream *stream;
e9b964cf 94} wx_source_mgr;
e9c4b1a2 95
e9b964cf 96typedef wx_source_mgr * wx_src_ptr;
e9c4b1a2 97
e9b964cf 98CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
e9c4b1a2
JS
99{
100}
101
e9b964cf 102CPP_METHODDEF(boolean) wx_fill_input_buffer ( j_decompress_ptr cinfo )
e9c4b1a2 103{
e9b964cf 104 wx_src_ptr src = (wx_src_ptr) cinfo->src;
1aaaa712
VS
105
106 src->pub.next_input_byte = src->buffer;
107 src->pub.bytes_in_buffer = src->stream->Read(src->buffer, JPEG_IO_BUFFER_SIZE).LastRead();
108
109 if (src->pub.bytes_in_buffer == 0) // check for end-of-stream
110 {
111 // Insert a fake EOI marker
112 src->buffer[0] = 0xFF;
113 src->buffer[1] = JPEG_EOI;
114 src->pub.bytes_in_buffer = 2;
115 }
e9c4b1a2
JS
116 return TRUE;
117}
118
e9b964cf 119CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
e9c4b1a2 120{
1aaaa712
VS
121 if (num_bytes > 0)
122 {
e9b964cf 123 wx_src_ptr src = (wx_src_ptr) cinfo->src;
1aaaa712 124
33ac7e6f 125 while (num_bytes > (long)src->pub.bytes_in_buffer)
1aaaa712
VS
126 {
127 num_bytes -= (long) src->pub.bytes_in_buffer;
128 src->pub.fill_input_buffer(cinfo);
129 }
130 src->pub.next_input_byte += (size_t) num_bytes;
131 src->pub.bytes_in_buffer -= (size_t) num_bytes;
132 }
e9c4b1a2
JS
133}
134
e9b964cf 135CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo )
e9c4b1a2 136{
e9b964cf 137 wx_src_ptr src = (wx_src_ptr) cinfo->src;
995612e2 138
1aaaa712 139 if (src->pub.bytes_in_buffer > 0)
3ca6a5f0 140 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
1aaaa712 141 delete[] src->buffer;
e9c4b1a2
JS
142}
143
e9c4b1a2 144
b59ff3c9
VS
145// JPEG error manager:
146
e9b964cf 147struct wx_error_mgr {
995612e2 148 struct jpeg_error_mgr pub; /* "public" fields */
b59ff3c9 149
995612e2 150 jmp_buf setjmp_buffer; /* for return to caller */
b59ff3c9
VS
151};
152
e9b964cf 153typedef struct wx_error_mgr * wx_error_ptr;
b59ff3c9
VS
154
155/*
156 * Here's the routine that will replace the standard error_exit method:
157 */
158
e9b964cf 159CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo)
b59ff3c9 160{
e9b964cf
VS
161 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
162 wx_error_ptr myerr = (wx_error_ptr) cinfo->err;
b59ff3c9
VS
163
164 /* Always display the message. */
165 /* We could postpone this until after returning, if we chose. */
deb2fec0 166 if (cinfo->err->output_message) (*cinfo->err->output_message) (cinfo);
b59ff3c9
VS
167
168 /* Return control to the setjmp point */
169 longjmp(myerr->setjmp_buffer, 1);
170}
171
e9b964cf 172void wx_jpeg_io_src( j_decompress_ptr cinfo, wxInputStream& infile )
90350682 173{
e9b964cf 174 wx_src_ptr src;
90350682
VZ
175
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,
e9b964cf 179 sizeof(wx_source_mgr));
90350682 180 }
e9b964cf 181 src = (wx_src_ptr) cinfo->src;
90350682
VZ
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;
186
e9b964cf
VS
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;
90350682 190 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
e9b964cf 191 src->pub.term_source = wx_term_source;
90350682
VZ
192}
193
194
3ca6a5f0
BP
195// temporarily disable the warning C4611 (interaction between '_setjmp' and
196// C++ object destruction is non-portable) - I don't see any dtors here
197#ifdef __VISUALC__
198 #pragma warning(disable:4611)
199#endif /* VC++ */
e9c4b1a2 200
700ec454 201bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
e9c4b1a2
JS
202{
203 struct jpeg_decompress_struct cinfo;
e9b964cf 204 struct wx_error_mgr jerr;
e9c4b1a2
JS
205 JSAMPARRAY tempbuf;
206 unsigned char *ptr;
207 unsigned stride;
995612e2 208
e9c4b1a2 209 image->Destroy();
b59ff3c9 210 cinfo.err = jpeg_std_error( &jerr.pub );
e9b964cf 211 jerr.pub.error_exit = wx_error_exit;
b59ff3c9 212
deb2fec0
SB
213 if (!verbose) cinfo.err->output_message=NULL;
214
e9b964cf 215 /* Establish the setjmp return context for wx_error_exit to use. */
b59ff3c9
VS
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.
219 */
33ac7e6f 220 if (verbose)
58c837a4 221 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
192644bf 222 (cinfo.src->term_source)(&cinfo);
b59ff3c9
VS
223 jpeg_destroy_decompress(&cinfo);
224 if (image->Ok()) image->Destroy();
7beb59f3 225 return false;
b59ff3c9
VS
226 }
227
e9c4b1a2 228 jpeg_create_decompress( &cinfo );
e9b964cf 229 wx_jpeg_io_src( &cinfo, stream );
e9c4b1a2
JS
230 jpeg_read_header( &cinfo, TRUE );
231 cinfo.out_color_space = JCS_RGB;
232 jpeg_start_decompress( &cinfo );
995612e2 233
e9c4b1a2
JS
234 image->Create( cinfo.image_width, cinfo.image_height );
235 if (!image->Ok()) {
236 jpeg_finish_decompress( &cinfo );
237 jpeg_destroy_decompress( &cinfo );
7beb59f3 238 return false;
e9c4b1a2 239 }
7beb59f3 240 image->SetMask( false );
e9c4b1a2
JS
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 );
995612e2 245
e9c4b1a2
JS
246 while ( cinfo.output_scanline < cinfo.output_height ) {
247 jpeg_read_scanlines( &cinfo, tempbuf, 1 );
248 memcpy( ptr, tempbuf[0], stride );
249 ptr += stride;
250 }
251 jpeg_finish_decompress( &cinfo );
252 jpeg_destroy_decompress( &cinfo );
7beb59f3 253 return true;
e9c4b1a2
JS
254}
255
e9c4b1a2
JS
256typedef struct {
257 struct jpeg_destination_mgr pub;
995612e2 258
e9c4b1a2
JS
259 wxOutputStream *stream;
260 JOCTET * buffer;
e9b964cf 261} wx_destination_mgr;
e9c4b1a2 262
e9b964cf 263typedef wx_destination_mgr * wx_dest_ptr;
e9c4b1a2 264
995612e2 265#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
e9c4b1a2 266
e9b964cf 267CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo)
e9c4b1a2 268{
e9b964cf 269 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
995612e2 270
e9c4b1a2
JS
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;
277}
278
e9b964cf 279CPP_METHODDEF(boolean) wx_empty_output_buffer (j_compress_ptr cinfo)
e9c4b1a2 280{
e9b964cf 281 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
995612e2 282
e9c4b1a2
JS
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;
286 return TRUE;
287}
288
e9b964cf 289CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo)
e9c4b1a2 290{
e9b964cf 291 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
e9c4b1a2
JS
292 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
293 /* Write any data remaining in the buffer */
294 if (datacount > 0)
295 dest->stream->Write(dest->buffer, datacount);
296}
297
e9b964cf 298GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
e9c4b1a2 299{
e9b964cf 300 wx_dest_ptr dest;
995612e2
VZ
301
302 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
e9c4b1a2
JS
303 cinfo->dest = (struct jpeg_destination_mgr *)
304 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
e9b964cf 305 sizeof(wx_destination_mgr));
e9c4b1a2 306 }
995612e2 307
e9b964cf
VS
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;
e9c4b1a2
JS
312 dest->stream = &outfile;
313}
314
deb2fec0 315bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2
JS
316{
317 struct jpeg_compress_struct cinfo;
e9b964cf 318 struct wx_error_mgr jerr;
995612e2 319 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
e9c4b1a2 320 JSAMPLE *image_buffer;
995612e2
VZ
321 int stride; /* physical row width in image buffer */
322
b59ff3c9 323 cinfo.err = jpeg_std_error(&jerr.pub);
e9b964cf 324 jerr.pub.error_exit = wx_error_exit;
b59ff3c9 325
deb2fec0
SB
326 if (!verbose) cinfo.err->output_message=NULL;
327
e9b964cf 328 /* Establish the setjmp return context for wx_error_exit to use. */
33ac7e6f 329 if (setjmp(jerr.setjmp_buffer))
58c837a4
RR
330 {
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.
333 */
33ac7e6f 334 if (verbose)
58c837a4
RR
335 wxLogError(_("JPEG: Couldn't save image."));
336 jpeg_destroy_compress(&cinfo);
7beb59f3 337 return false;
b59ff3c9
VS
338 }
339
e9c4b1a2 340 jpeg_create_compress(&cinfo);
e9b964cf 341 wx_jpeg_io_dest(&cinfo, stream);
995612e2 342
e9c4b1a2
JS
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);
5e5437e0
JS
348
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.
9a679df6
JS
355 if (image->HasOption(wxIMAGE_OPTION_QUALITY))
356 jpeg_set_quality(&cinfo, image->GetOptionInt(wxIMAGE_OPTION_QUALITY), TRUE);
5e5437e0 357
fe9308c6
VZ
358 // set the resolution fields in the output file
359 UINT16 resX,
360 resY;
361 if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) &&
362 image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) )
89d38f8c 363 {
4c2740ec
WS
364 resX = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX);
365 resY = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
fe9308c6
VZ
366 }
367 else if ( image->HasOption(wxIMAGE_OPTION_RESOLUTION) )
368 {
369 resX =
4c2740ec 370 resY = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION);
fe9308c6
VZ
371 }
372 else
373 {
374 resX =
375 resY = 0;
376 }
377
378 if ( resX && resY )
379 {
380 cinfo.X_density = resX;
381 cinfo.Y_density = resY;
89d38f8c
VS
382 }
383
384 // sets the resolution unit field in the output file
385 // wxIMAGE_RESOLUTION_INCHES for inches
386 // wxIMAGE_RESOLUTION_CM for centimeters
fe9308c6 387 if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT) )
89d38f8c 388 {
7ac31c42 389 cinfo.density_unit = (UINT8)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT);
89d38f8c
VS
390 }
391
e9c4b1a2 392 jpeg_start_compress(&cinfo, TRUE);
995612e2
VZ
393
394 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
e9c4b1a2
JS
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 );
399 }
400 jpeg_finish_compress(&cinfo);
401 jpeg_destroy_compress(&cinfo);
995612e2 402
7beb59f3 403 return true;
e9c4b1a2 404}
e9c4b1a2 405
3ca6a5f0
BP
406#ifdef __VISUALC__
407 #pragma warning(default:4611)
408#endif /* VC++ */
0828c087 409
995612e2 410bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
0828c087
VS
411{
412 unsigned char hdr[2];
995612e2 413
79fa2374 414 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
7beb59f3 415 return false;
79fa2374 416
79fa2374 417 return hdr[0] == 0xFF && hdr[1] == 0xD8;
0828c087
VS
418}
419
9ab6ee85
GRG
420#endif // wxUSE_STREAMS
421
422#endif // wxUSE_LIBJPEG