]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
Include wx/brush.h according to precompiled headers of wx/wx.h (with other minor...
[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__
88a7a4e1 14 #pragma hdrstop
e9c4b1a2
JS
15#endif
16
c96ea657 17#if wxUSE_IMAGE && wxUSE_LIBJPEG
ce4169a4 18
88a7a4e1
WS
19#include "wx/imagjpeg.h"
20
8898456d
WS
21#ifndef WX_PRECOMP
22 #include "wx/log.h"
23 #include "wx/app.h"
88a7a4e1 24 #include "wx/intl.h"
8898456d
WS
25#endif
26
e9c4b1a2
JS
27#include "wx/bitmap.h"
28#include "wx/debug.h"
cb69afe0 29
7beb59f3 30// NB: Some compilers define boolean type in Windows headers
4c87eb44 31// (e.g. Watcom C++, but not Open Watcom).
cb69afe0
VS
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.
6d3d756a 35#if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__DIGITALMARS__))
e7ca6139 36 #define HAVE_BOOLEAN
9ed0d735 37 #include "wx/msw/wrapwin.h"
e7ca6139 38#endif
cb69afe0 39
995612e2
VZ
40extern "C"
41{
cd2a4e16
JS
42 #if defined(__WXMSW__)
43 #define XMD_H
44 #endif
995612e2 45 #include "jpeglib.h"
e9c4b1a2 46}
cb69afe0 47
e9c4b1a2
JS
48#include "wx/filefn.h"
49#include "wx/wfstream.h"
e9c4b1a2
JS
50#include "wx/module.h"
51
52// For memcpy
53#include <string.h>
b59ff3c9
VS
54// For JPEG library error handling
55#include <setjmp.h>
e9c4b1a2
JS
56
57#ifdef __SALFORDC__
e9c4b1a2
JS
58#undef FAR
59#endif
e9c4b1a2 60
d7c42914
VZ
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
90350682 78
e9c4b1a2
JS
79//-----------------------------------------------------------------------------
80// wxJPEGHandler
81//-----------------------------------------------------------------------------
82
e9c4b1a2 83IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
e9c4b1a2 84
9ab6ee85
GRG
85#if wxUSE_STREAMS
86
e9c4b1a2
JS
87//------------- JPEG Data Source Manager
88
1aaaa712
VS
89#define JPEG_IO_BUFFER_SIZE 2048
90
e9c4b1a2
JS
91typedef struct {
92 struct jpeg_source_mgr pub; /* public fields */
995612e2 93
e9c4b1a2 94 JOCTET* buffer; /* start of buffer */
1aaaa712 95 wxInputStream *stream;
e9b964cf 96} wx_source_mgr;
e9c4b1a2 97
e9b964cf 98typedef wx_source_mgr * wx_src_ptr;
e9c4b1a2 99
e9b964cf 100CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
e9c4b1a2
JS
101{
102}
103
e9b964cf 104CPP_METHODDEF(boolean) wx_fill_input_buffer ( j_decompress_ptr cinfo )
e9c4b1a2 105{
e9b964cf 106 wx_src_ptr src = (wx_src_ptr) cinfo->src;
1aaaa712
VS
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 }
e9c4b1a2
JS
118 return TRUE;
119}
120
e9b964cf 121CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
e9c4b1a2 122{
1aaaa712
VS
123 if (num_bytes > 0)
124 {
e9b964cf 125 wx_src_ptr src = (wx_src_ptr) cinfo->src;
1aaaa712 126
33ac7e6f 127 while (num_bytes > (long)src->pub.bytes_in_buffer)
1aaaa712
VS
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 }
e9c4b1a2
JS
135}
136
e9b964cf 137CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo )
e9c4b1a2 138{
e9b964cf 139 wx_src_ptr src = (wx_src_ptr) cinfo->src;
995612e2 140
1aaaa712 141 if (src->pub.bytes_in_buffer > 0)
3ca6a5f0 142 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
1aaaa712 143 delete[] src->buffer;
e9c4b1a2
JS
144}
145
e9c4b1a2 146
b59ff3c9
VS
147// JPEG error manager:
148
e9b964cf 149struct wx_error_mgr {
995612e2 150 struct jpeg_error_mgr pub; /* "public" fields */
b59ff3c9 151
995612e2 152 jmp_buf setjmp_buffer; /* for return to caller */
b59ff3c9
VS
153};
154
e9b964cf 155typedef struct wx_error_mgr * wx_error_ptr;
b59ff3c9
VS
156
157/*
158 * Here's the routine that will replace the standard error_exit method:
159 */
160
e9b964cf 161CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo)
b59ff3c9 162{
e9b964cf
VS
163 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
164 wx_error_ptr myerr = (wx_error_ptr) cinfo->err;
b59ff3c9
VS
165
166 /* Always display the message. */
167 /* We could postpone this until after returning, if we chose. */
ef25e5a4 168 (*cinfo->err->output_message) (cinfo);
b59ff3c9
VS
169
170 /* Return control to the setjmp point */
171 longjmp(myerr->setjmp_buffer, 1);
172}
173
ef25e5a4
VZ
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 */
180CPP_METHODDEF(void) wx_ignore_message (j_common_ptr WXUNUSED(cinfo))
181{
182}
183
e9b964cf 184void wx_jpeg_io_src( j_decompress_ptr cinfo, wxInputStream& infile )
90350682 185{
e9b964cf 186 wx_src_ptr src;
90350682
VZ
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,
e9b964cf 191 sizeof(wx_source_mgr));
90350682 192 }
e9b964cf 193 src = (wx_src_ptr) cinfo->src;
90350682
VZ
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
e9b964cf
VS
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;
90350682 202 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
e9b964cf 203 src->pub.term_source = wx_term_source;
90350682
VZ
204}
205
206
3ca6a5f0
BP
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++ */
e9c4b1a2 212
700ec454 213bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
e9c4b1a2
JS
214{
215 struct jpeg_decompress_struct cinfo;
e9b964cf 216 struct wx_error_mgr jerr;
e9c4b1a2
JS
217 JSAMPARRAY tempbuf;
218 unsigned char *ptr;
219 unsigned stride;
995612e2 220
e9c4b1a2 221 image->Destroy();
b59ff3c9 222 cinfo.err = jpeg_std_error( &jerr.pub );
e9b964cf 223 jerr.pub.error_exit = wx_error_exit;
b59ff3c9 224
ef25e5a4
VZ
225 if (!verbose)
226 cinfo.err->output_message = wx_ignore_message;
deb2fec0 227
e9b964cf 228 /* Establish the setjmp return context for wx_error_exit to use. */
b59ff3c9
VS
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 */
33ac7e6f 233 if (verbose)
58c837a4 234 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
192644bf 235 (cinfo.src->term_source)(&cinfo);
b59ff3c9
VS
236 jpeg_destroy_decompress(&cinfo);
237 if (image->Ok()) image->Destroy();
7beb59f3 238 return false;
b59ff3c9
VS
239 }
240
e9c4b1a2 241 jpeg_create_decompress( &cinfo );
e9b964cf 242 wx_jpeg_io_src( &cinfo, stream );
e9c4b1a2
JS
243 jpeg_read_header( &cinfo, TRUE );
244 cinfo.out_color_space = JCS_RGB;
245 jpeg_start_decompress( &cinfo );
995612e2 246
e9c4b1a2
JS
247 image->Create( cinfo.image_width, cinfo.image_height );
248 if (!image->Ok()) {
249 jpeg_finish_decompress( &cinfo );
250 jpeg_destroy_decompress( &cinfo );
7beb59f3 251 return false;
e9c4b1a2 252 }
7beb59f3 253 image->SetMask( false );
e9c4b1a2
JS
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 );
995612e2 258
e9c4b1a2
JS
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 );
7beb59f3 266 return true;
e9c4b1a2
JS
267}
268
e9c4b1a2
JS
269typedef struct {
270 struct jpeg_destination_mgr pub;
995612e2 271
e9c4b1a2
JS
272 wxOutputStream *stream;
273 JOCTET * buffer;
e9b964cf 274} wx_destination_mgr;
e9c4b1a2 275
e9b964cf 276typedef wx_destination_mgr * wx_dest_ptr;
e9c4b1a2 277
995612e2 278#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
e9c4b1a2 279
e9b964cf 280CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo)
e9c4b1a2 281{
e9b964cf 282 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
995612e2 283
e9c4b1a2
JS
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
e9b964cf 292CPP_METHODDEF(boolean) wx_empty_output_buffer (j_compress_ptr cinfo)
e9c4b1a2 293{
e9b964cf 294 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
995612e2 295
e9c4b1a2
JS
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
e9b964cf 302CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo)
e9c4b1a2 303{
e9b964cf 304 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
e9c4b1a2
JS
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
e9b964cf 311GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
e9c4b1a2 312{
e9b964cf 313 wx_dest_ptr dest;
995612e2
VZ
314
315 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
e9c4b1a2
JS
316 cinfo->dest = (struct jpeg_destination_mgr *)
317 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
e9b964cf 318 sizeof(wx_destination_mgr));
e9c4b1a2 319 }
995612e2 320
e9b964cf
VS
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;
e9c4b1a2
JS
325 dest->stream = &outfile;
326}
327
deb2fec0 328bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2
JS
329{
330 struct jpeg_compress_struct cinfo;
e9b964cf 331 struct wx_error_mgr jerr;
995612e2 332 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
e9c4b1a2 333 JSAMPLE *image_buffer;
995612e2
VZ
334 int stride; /* physical row width in image buffer */
335
b59ff3c9 336 cinfo.err = jpeg_std_error(&jerr.pub);
e9b964cf 337 jerr.pub.error_exit = wx_error_exit;
b59ff3c9 338
ef25e5a4
VZ
339 if (!verbose)
340 cinfo.err->output_message = wx_ignore_message;
deb2fec0 341
e9b964cf 342 /* Establish the setjmp return context for wx_error_exit to use. */
33ac7e6f 343 if (setjmp(jerr.setjmp_buffer))
58c837a4
RR
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 */
33ac7e6f 348 if (verbose)
58c837a4
RR
349 wxLogError(_("JPEG: Couldn't save image."));
350 jpeg_destroy_compress(&cinfo);
7beb59f3 351 return false;
b59ff3c9
VS
352 }
353
e9c4b1a2 354 jpeg_create_compress(&cinfo);
e9b964cf 355 wx_jpeg_io_dest(&cinfo, stream);
995612e2 356
e9c4b1a2
JS
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);
5e5437e0
JS
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.
9a679df6
JS
369 if (image->HasOption(wxIMAGE_OPTION_QUALITY))
370 jpeg_set_quality(&cinfo, image->GetOptionInt(wxIMAGE_OPTION_QUALITY), TRUE);
5e5437e0 371
fe9308c6
VZ
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) )
89d38f8c 377 {
4c2740ec
WS
378 resX = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX);
379 resY = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
fe9308c6
VZ
380 }
381 else if ( image->HasOption(wxIMAGE_OPTION_RESOLUTION) )
382 {
383 resX =
4c2740ec 384 resY = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION);
fe9308c6
VZ
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;
89d38f8c
VS
396 }
397
398 // sets the resolution unit field in the output file
399 // wxIMAGE_RESOLUTION_INCHES for inches
400 // wxIMAGE_RESOLUTION_CM for centimeters
fe9308c6 401 if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT) )
89d38f8c 402 {
7ac31c42 403 cinfo.density_unit = (UINT8)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT);
89d38f8c
VS
404 }
405
e9c4b1a2 406 jpeg_start_compress(&cinfo, TRUE);
995612e2
VZ
407
408 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
e9c4b1a2
JS
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);
995612e2 416
7beb59f3 417 return true;
e9c4b1a2 418}
e9c4b1a2 419
3ca6a5f0
BP
420#ifdef __VISUALC__
421 #pragma warning(default:4611)
422#endif /* VC++ */
0828c087 423
995612e2 424bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
0828c087
VS
425{
426 unsigned char hdr[2];
995612e2 427
79fa2374 428 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
7beb59f3 429 return false;
79fa2374 430
79fa2374 431 return hdr[0] == 0xFF && hdr[1] == 0xD8;
0828c087
VS
432}
433
9ab6ee85
GRG
434#endif // wxUSE_STREAMS
435
436#endif // wxUSE_LIBJPEG