]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
fixed unused variable warning
[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"
0bca0373 25 #include "wx/bitmap.h"
02761f6c 26 #include "wx/module.h"
8898456d
WS
27#endif
28
995612e2
VZ
29extern "C"
30{
cd2a4e16
JS
31 #if defined(__WXMSW__)
32 #define XMD_H
33 #endif
995612e2 34 #include "jpeglib.h"
e9c4b1a2 35}
cb69afe0 36
e604ac79
MW
37#ifndef HAVE_WXJPEG_BOOLEAN
38typedef boolean wxjpeg_boolean;
39#endif
40
e9c4b1a2
JS
41#include "wx/filefn.h"
42#include "wx/wfstream.h"
e9c4b1a2
JS
43
44// For memcpy
45#include <string.h>
b59ff3c9
VS
46// For JPEG library error handling
47#include <setjmp.h>
e9c4b1a2
JS
48
49#ifdef __SALFORDC__
e9c4b1a2
JS
50#undef FAR
51#endif
e9c4b1a2 52
d7c42914
VZ
53// ----------------------------------------------------------------------------
54// types
55// ----------------------------------------------------------------------------
56
57// the standard definition of METHODDEF(type) from jmorecfg.h is "static type"
58// which means that we can't declare the method functions as extern "C" - the
59// compiler (rightfully) complains about the multiple storage classes in
60// declaration
61//
62// so we only add extern "C" when using our own, modified, jmorecfg.h - and use
63// whatever we have in the system headers if this is what we use hoping that it
64// should be ok (can't do anything else)
65#ifdef JPEG_METHOD_LINKAGE
66 #define CPP_METHODDEF(type) extern "C" METHODDEF(type)
67#else // not using our jmorecfg.h header
68 #define CPP_METHODDEF(type) METHODDEF(type)
69#endif
90350682 70
e9c4b1a2
JS
71//-----------------------------------------------------------------------------
72// wxJPEGHandler
73//-----------------------------------------------------------------------------
74
e9c4b1a2 75IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
e9c4b1a2 76
9ab6ee85
GRG
77#if wxUSE_STREAMS
78
e9c4b1a2
JS
79//------------- JPEG Data Source Manager
80
1aaaa712
VS
81#define JPEG_IO_BUFFER_SIZE 2048
82
e9c4b1a2
JS
83typedef struct {
84 struct jpeg_source_mgr pub; /* public fields */
995612e2 85
e9c4b1a2 86 JOCTET* buffer; /* start of buffer */
1aaaa712 87 wxInputStream *stream;
e9b964cf 88} wx_source_mgr;
e9c4b1a2 89
e9b964cf 90typedef wx_source_mgr * wx_src_ptr;
e9c4b1a2 91
e9b964cf 92CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
e9c4b1a2
JS
93{
94}
95
e604ac79 96CPP_METHODDEF(wxjpeg_boolean) wx_fill_input_buffer ( j_decompress_ptr cinfo )
e9c4b1a2 97{
e9b964cf 98 wx_src_ptr src = (wx_src_ptr) cinfo->src;
1aaaa712
VS
99
100 src->pub.next_input_byte = src->buffer;
101 src->pub.bytes_in_buffer = src->stream->Read(src->buffer, JPEG_IO_BUFFER_SIZE).LastRead();
102
103 if (src->pub.bytes_in_buffer == 0) // check for end-of-stream
104 {
105 // Insert a fake EOI marker
106 src->buffer[0] = 0xFF;
107 src->buffer[1] = JPEG_EOI;
108 src->pub.bytes_in_buffer = 2;
109 }
e9c4b1a2
JS
110 return TRUE;
111}
112
e9b964cf 113CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
e9c4b1a2 114{
1aaaa712
VS
115 if (num_bytes > 0)
116 {
e9b964cf 117 wx_src_ptr src = (wx_src_ptr) cinfo->src;
1aaaa712 118
33ac7e6f 119 while (num_bytes > (long)src->pub.bytes_in_buffer)
1aaaa712
VS
120 {
121 num_bytes -= (long) src->pub.bytes_in_buffer;
122 src->pub.fill_input_buffer(cinfo);
123 }
124 src->pub.next_input_byte += (size_t) num_bytes;
125 src->pub.bytes_in_buffer -= (size_t) num_bytes;
126 }
e9c4b1a2
JS
127}
128
e9b964cf 129CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo )
e9c4b1a2 130{
e9b964cf 131 wx_src_ptr src = (wx_src_ptr) cinfo->src;
995612e2 132
1aaaa712 133 if (src->pub.bytes_in_buffer > 0)
3ca6a5f0 134 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
1aaaa712 135 delete[] src->buffer;
e9c4b1a2
JS
136}
137
e9c4b1a2 138
b59ff3c9
VS
139// JPEG error manager:
140
e9b964cf 141struct wx_error_mgr {
995612e2 142 struct jpeg_error_mgr pub; /* "public" fields */
b59ff3c9 143
995612e2 144 jmp_buf setjmp_buffer; /* for return to caller */
b59ff3c9
VS
145};
146
e9b964cf 147typedef struct wx_error_mgr * wx_error_ptr;
b59ff3c9
VS
148
149/*
150 * Here's the routine that will replace the standard error_exit method:
151 */
152
e9b964cf 153CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo)
b59ff3c9 154{
e9b964cf
VS
155 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
156 wx_error_ptr myerr = (wx_error_ptr) cinfo->err;
b59ff3c9
VS
157
158 /* Always display the message. */
159 /* We could postpone this until after returning, if we chose. */
ef25e5a4 160 (*cinfo->err->output_message) (cinfo);
b59ff3c9
VS
161
162 /* Return control to the setjmp point */
163 longjmp(myerr->setjmp_buffer, 1);
164}
165
ef25e5a4
VZ
166/*
167 * This will replace the standard output_message method when the user
168 * wants us to be silent (verbose==false). We must have such method instead of
169 * simply using NULL for cinfo->err->output_message because it's called
170 * unconditionally from within libjpeg when there's "garbage input".
171 */
172CPP_METHODDEF(void) wx_ignore_message (j_common_ptr WXUNUSED(cinfo))
173{
174}
175
e9b964cf 176void wx_jpeg_io_src( j_decompress_ptr cinfo, wxInputStream& infile )
90350682 177{
e9b964cf 178 wx_src_ptr src;
90350682
VZ
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,
e9b964cf 183 sizeof(wx_source_mgr));
90350682 184 }
e9b964cf 185 src = (wx_src_ptr) cinfo->src;
90350682
VZ
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
e9b964cf
VS
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;
90350682 194 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
e9b964cf 195 src->pub.term_source = wx_term_source;
90350682
VZ
196}
197
234fedd2
VZ
198static inline void wx_cmyk_to_rgb(unsigned char* rgb, const unsigned char* cmyk)
199{
200 register int k = 255 - cmyk[3];
201 register int k2 = cmyk[3];
202 register int c;
203
204 c = k + k2 * (255 - cmyk[0]) / 255;
ff7d9066 205 rgb[0] = (unsigned char)((c > 255) ? 0 : (255 - c));
234fedd2
VZ
206
207 c = k + k2 * (255 - cmyk[1]) / 255;
ff7d9066 208 rgb[1] = (unsigned char)((c > 255) ? 0 : (255 - c));
234fedd2
VZ
209
210 c = k + k2 * (255 - cmyk[2]) / 255;
ff7d9066 211 rgb[2] = (unsigned char)((c > 255) ? 0 : (255 - c));
234fedd2 212}
90350682 213
3ca6a5f0
BP
214// temporarily disable the warning C4611 (interaction between '_setjmp' and
215// C++ object destruction is non-portable) - I don't see any dtors here
216#ifdef __VISUALC__
217 #pragma warning(disable:4611)
218#endif /* VC++ */
e9c4b1a2 219
700ec454 220bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
e9c4b1a2
JS
221{
222 struct jpeg_decompress_struct cinfo;
e9b964cf 223 struct wx_error_mgr jerr;
e9c4b1a2 224 unsigned char *ptr;
995612e2 225
e9c4b1a2 226 image->Destroy();
b59ff3c9 227 cinfo.err = jpeg_std_error( &jerr.pub );
e9b964cf 228 jerr.pub.error_exit = wx_error_exit;
b59ff3c9 229
ef25e5a4
VZ
230 if (!verbose)
231 cinfo.err->output_message = wx_ignore_message;
deb2fec0 232
e9b964cf 233 /* Establish the setjmp return context for wx_error_exit to use. */
b59ff3c9
VS
234 if (setjmp(jerr.setjmp_buffer)) {
235 /* If we get here, the JPEG code has signaled an error.
236 * We need to clean up the JPEG object, close the input file, and return.
237 */
33ac7e6f 238 if (verbose)
58c837a4 239 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
192644bf 240 (cinfo.src->term_source)(&cinfo);
b59ff3c9
VS
241 jpeg_destroy_decompress(&cinfo);
242 if (image->Ok()) image->Destroy();
7beb59f3 243 return false;
b59ff3c9
VS
244 }
245
e9c4b1a2 246 jpeg_create_decompress( &cinfo );
e9b964cf 247 wx_jpeg_io_src( &cinfo, stream );
e9c4b1a2 248 jpeg_read_header( &cinfo, TRUE );
234fedd2
VZ
249
250 int bytesPerPixel;
251 if ((cinfo.out_color_space == JCS_CMYK) || (cinfo.out_color_space == JCS_YCCK))
252 {
253 cinfo.out_color_space = JCS_CMYK;
254 bytesPerPixel = 4;
255 }
256 else // all the rest is treated as RGB
257 {
258 cinfo.out_color_space = JCS_RGB;
259 bytesPerPixel = 3;
260 }
261
e9c4b1a2 262 jpeg_start_decompress( &cinfo );
995612e2 263
e9c4b1a2
JS
264 image->Create( cinfo.image_width, cinfo.image_height );
265 if (!image->Ok()) {
266 jpeg_finish_decompress( &cinfo );
267 jpeg_destroy_decompress( &cinfo );
7beb59f3 268 return false;
e9c4b1a2 269 }
7beb59f3 270 image->SetMask( false );
e9c4b1a2 271 ptr = image->GetData();
995612e2 272
234fedd2
VZ
273 unsigned stride = cinfo.output_width * bytesPerPixel;
274 JSAMPARRAY tempbuf = (*cinfo.mem->alloc_sarray)
275 ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 );
276
277 while ( cinfo.output_scanline < cinfo.output_height )
278 {
e9c4b1a2 279 jpeg_read_scanlines( &cinfo, tempbuf, 1 );
234fedd2
VZ
280 if (cinfo.out_color_space == JCS_RGB)
281 {
282 memcpy( ptr, tempbuf[0], stride );
283 ptr += stride;
284 }
285 else // CMYK
286 {
287 const unsigned char* inptr = (const unsigned char*) tempbuf[0];
288 for (size_t i = 0; i < cinfo.output_width; i++)
289 {
290 wx_cmyk_to_rgb(ptr, inptr);
291 ptr += 3;
292 inptr += 4;
293 }
294 }
e9c4b1a2 295 }
234fedd2 296
e9c4b1a2
JS
297 jpeg_finish_decompress( &cinfo );
298 jpeg_destroy_decompress( &cinfo );
7beb59f3 299 return true;
e9c4b1a2
JS
300}
301
e9c4b1a2
JS
302typedef struct {
303 struct jpeg_destination_mgr pub;
995612e2 304
e9c4b1a2
JS
305 wxOutputStream *stream;
306 JOCTET * buffer;
e9b964cf 307} wx_destination_mgr;
e9c4b1a2 308
e9b964cf 309typedef wx_destination_mgr * wx_dest_ptr;
e9c4b1a2 310
995612e2 311#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
e9c4b1a2 312
e9b964cf 313CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo)
e9c4b1a2 314{
e9b964cf 315 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
995612e2 316
e9c4b1a2
JS
317 /* Allocate the output buffer --- it will be released when done with image */
318 dest->buffer = (JOCTET *)
319 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
320 OUTPUT_BUF_SIZE * sizeof(JOCTET));
321 dest->pub.next_output_byte = dest->buffer;
322 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
323}
324
e604ac79 325CPP_METHODDEF(wxjpeg_boolean) wx_empty_output_buffer (j_compress_ptr cinfo)
e9c4b1a2 326{
e9b964cf 327 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
995612e2 328
e9c4b1a2
JS
329 dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE);
330 dest->pub.next_output_byte = dest->buffer;
331 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
332 return TRUE;
333}
334
e9b964cf 335CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo)
e9c4b1a2 336{
e9b964cf 337 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
e9c4b1a2
JS
338 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
339 /* Write any data remaining in the buffer */
340 if (datacount > 0)
341 dest->stream->Write(dest->buffer, datacount);
342}
343
e9b964cf 344GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
e9c4b1a2 345{
e9b964cf 346 wx_dest_ptr dest;
995612e2
VZ
347
348 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
e9c4b1a2
JS
349 cinfo->dest = (struct jpeg_destination_mgr *)
350 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
e9b964cf 351 sizeof(wx_destination_mgr));
e9c4b1a2 352 }
995612e2 353
e9b964cf
VS
354 dest = (wx_dest_ptr) cinfo->dest;
355 dest->pub.init_destination = wx_init_destination;
356 dest->pub.empty_output_buffer = wx_empty_output_buffer;
357 dest->pub.term_destination = wx_term_destination;
e9c4b1a2
JS
358 dest->stream = &outfile;
359}
360
deb2fec0 361bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2
JS
362{
363 struct jpeg_compress_struct cinfo;
e9b964cf 364 struct wx_error_mgr jerr;
995612e2 365 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
e9c4b1a2 366 JSAMPLE *image_buffer;
995612e2
VZ
367 int stride; /* physical row width in image buffer */
368
b59ff3c9 369 cinfo.err = jpeg_std_error(&jerr.pub);
e9b964cf 370 jerr.pub.error_exit = wx_error_exit;
b59ff3c9 371
ef25e5a4
VZ
372 if (!verbose)
373 cinfo.err->output_message = wx_ignore_message;
deb2fec0 374
e9b964cf 375 /* Establish the setjmp return context for wx_error_exit to use. */
33ac7e6f 376 if (setjmp(jerr.setjmp_buffer))
58c837a4
RR
377 {
378 /* If we get here, the JPEG code has signaled an error.
379 * We need to clean up the JPEG object, close the input file, and return.
380 */
33ac7e6f 381 if (verbose)
58c837a4
RR
382 wxLogError(_("JPEG: Couldn't save image."));
383 jpeg_destroy_compress(&cinfo);
7beb59f3 384 return false;
b59ff3c9
VS
385 }
386
e9c4b1a2 387 jpeg_create_compress(&cinfo);
e9b964cf 388 wx_jpeg_io_dest(&cinfo, stream);
995612e2 389
e9c4b1a2
JS
390 cinfo.image_width = image->GetWidth();
391 cinfo.image_height = image->GetHeight();
392 cinfo.input_components = 3;
393 cinfo.in_color_space = JCS_RGB;
394 jpeg_set_defaults(&cinfo);
5e5437e0
JS
395
396 // TODO: 3rd parameter is force_baseline, what value should this be?
397 // Code says: "If force_baseline is TRUE, the computed quantization table entries
398 // are limited to 1..255 for JPEG baseline compatibility."
399 // 'Quality' is a number between 0 (terrible) and 100 (very good).
400 // The default (in jcparam.c, jpeg_set_defaults) is 75,
401 // and force_baseline is TRUE.
9a679df6
JS
402 if (image->HasOption(wxIMAGE_OPTION_QUALITY))
403 jpeg_set_quality(&cinfo, image->GetOptionInt(wxIMAGE_OPTION_QUALITY), TRUE);
5e5437e0 404
fe9308c6
VZ
405 // set the resolution fields in the output file
406 UINT16 resX,
407 resY;
408 if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) &&
409 image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) )
89d38f8c 410 {
4c2740ec
WS
411 resX = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX);
412 resY = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
fe9308c6
VZ
413 }
414 else if ( image->HasOption(wxIMAGE_OPTION_RESOLUTION) )
415 {
416 resX =
4c2740ec 417 resY = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION);
fe9308c6
VZ
418 }
419 else
420 {
421 resX =
422 resY = 0;
423 }
424
425 if ( resX && resY )
426 {
427 cinfo.X_density = resX;
428 cinfo.Y_density = resY;
89d38f8c
VS
429 }
430
431 // sets the resolution unit field in the output file
432 // wxIMAGE_RESOLUTION_INCHES for inches
433 // wxIMAGE_RESOLUTION_CM for centimeters
fe9308c6 434 if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT) )
89d38f8c 435 {
7ac31c42 436 cinfo.density_unit = (UINT8)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT);
89d38f8c
VS
437 }
438
e9c4b1a2 439 jpeg_start_compress(&cinfo, TRUE);
995612e2
VZ
440
441 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
e9c4b1a2
JS
442 image_buffer = image->GetData();
443 while (cinfo.next_scanline < cinfo.image_height) {
444 row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
445 jpeg_write_scanlines( &cinfo, row_pointer, 1 );
446 }
447 jpeg_finish_compress(&cinfo);
448 jpeg_destroy_compress(&cinfo);
995612e2 449
7beb59f3 450 return true;
e9c4b1a2 451}
e9c4b1a2 452
3ca6a5f0
BP
453#ifdef __VISUALC__
454 #pragma warning(default:4611)
455#endif /* VC++ */
0828c087 456
995612e2 457bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
0828c087
VS
458{
459 unsigned char hdr[2];
995612e2 460
79fa2374 461 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
7beb59f3 462 return false;
79fa2374 463
79fa2374 464 return hdr[0] == 0xFF && hdr[1] == 0xD8;
0828c087
VS
465}
466
9ab6ee85
GRG
467#endif // wxUSE_STREAMS
468
469#endif // wxUSE_LIBJPEG