]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/imagjpeg.cpp
fixing osx_cocoa
[wxWidgets.git] / src / common / imagjpeg.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/common/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// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
14 #pragma hdrstop
15#endif
16
17#if wxUSE_IMAGE && wxUSE_LIBJPEG
18
19#include "wx/imagjpeg.h"
20#include "wx/versioninfo.h"
21
22#ifndef WX_PRECOMP
23 #include "wx/log.h"
24 #include "wx/app.h"
25 #include "wx/intl.h"
26 #include "wx/bitmap.h"
27 #include "wx/module.h"
28#endif
29
30// A hack based on one from tif_jpeg.c to overcome the problem on Windows
31// of rpcndr.h defining boolean with a different type to the jpeg headers.
32//
33// This hack is only necessary for an external jpeg library, the builtin one
34// usually used on Windows doesn't use the type boolean, so always works.
35//
36#ifdef wxHACK_BOOLEAN
37 #define HAVE_BOOLEAN
38 #define boolean wxHACK_BOOLEAN
39#endif
40
41extern "C"
42{
43 #if defined(__WXMSW__)
44 #define XMD_H
45 #endif
46 #include "jpeglib.h"
47}
48
49#ifndef HAVE_WXJPEG_BOOLEAN
50typedef boolean wxjpeg_boolean;
51#endif
52
53#include "wx/filefn.h"
54#include "wx/wfstream.h"
55
56// For memcpy
57#include <string.h>
58// For JPEG library error handling
59#include <setjmp.h>
60
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
78
79//-----------------------------------------------------------------------------
80// wxJPEGHandler
81//-----------------------------------------------------------------------------
82
83IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
84
85#if wxUSE_STREAMS
86
87//------------- JPEG Data Source Manager
88
89#define JPEG_IO_BUFFER_SIZE 2048
90
91typedef struct {
92 struct jpeg_source_mgr pub; /* public fields */
93
94 JOCTET* buffer; /* start of buffer */
95 wxInputStream *stream;
96} wx_source_mgr;
97
98typedef wx_source_mgr * wx_src_ptr;
99
100extern "C"
101{
102
103CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
104{
105}
106
107CPP_METHODDEF(wxjpeg_boolean) wx_fill_input_buffer ( j_decompress_ptr cinfo )
108{
109 wx_src_ptr src = (wx_src_ptr) cinfo->src;
110
111 src->pub.next_input_byte = src->buffer;
112 src->pub.bytes_in_buffer = src->stream->Read(src->buffer, JPEG_IO_BUFFER_SIZE).LastRead();
113
114 if (src->pub.bytes_in_buffer == 0) // check for end-of-stream
115 {
116 // Insert a fake EOI marker
117 src->buffer[0] = 0xFF;
118 src->buffer[1] = JPEG_EOI;
119 src->pub.bytes_in_buffer = 2;
120 }
121 return TRUE;
122}
123
124CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
125{
126 if (num_bytes > 0)
127 {
128 wx_src_ptr src = (wx_src_ptr) cinfo->src;
129
130 while (num_bytes > (long)src->pub.bytes_in_buffer)
131 {
132 num_bytes -= (long) src->pub.bytes_in_buffer;
133 src->pub.fill_input_buffer(cinfo);
134 }
135 src->pub.next_input_byte += (size_t) num_bytes;
136 src->pub.bytes_in_buffer -= (size_t) num_bytes;
137 }
138}
139
140CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo )
141{
142 wx_src_ptr src = (wx_src_ptr) cinfo->src;
143
144 if (src->pub.bytes_in_buffer > 0)
145 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
146 delete[] src->buffer;
147}
148
149
150// JPEG error manager:
151
152struct wx_error_mgr : public jpeg_error_mgr
153{
154 jmp_buf setjmp_buffer; /* for return to caller */
155};
156
157/*
158 * Here's the routine that will replace the standard error_exit method:
159 */
160
161CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo)
162{
163 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
164 wx_error_mgr * const jerr = (wx_error_mgr *) cinfo->err;
165
166 /* Always display the message. */
167 /* We could postpone this until after returning, if we chose. */
168 (*cinfo->err->output_message) (cinfo);
169
170 /* Return control to the setjmp point */
171 longjmp(jerr->setjmp_buffer, 1);
172}
173
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
184void wx_jpeg_io_src( j_decompress_ptr cinfo, wxInputStream& infile )
185{
186 wx_src_ptr src;
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,
191 sizeof(wx_source_mgr));
192 }
193 src = (wx_src_ptr) cinfo->src;
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
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;
202 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
203 src->pub.term_source = wx_term_source;
204}
205
206} // extern "C"
207
208static inline void wx_cmyk_to_rgb(unsigned char* rgb, const unsigned char* cmyk)
209{
210 register int k = 255 - cmyk[3];
211 register int k2 = cmyk[3];
212 register int c;
213
214 c = k + k2 * (255 - cmyk[0]) / 255;
215 rgb[0] = (unsigned char)((c > 255) ? 0 : (255 - c));
216
217 c = k + k2 * (255 - cmyk[1]) / 255;
218 rgb[1] = (unsigned char)((c > 255) ? 0 : (255 - c));
219
220 c = k + k2 * (255 - cmyk[2]) / 255;
221 rgb[2] = (unsigned char)((c > 255) ? 0 : (255 - c));
222}
223
224// temporarily disable the warning C4611 (interaction between '_setjmp' and
225// C++ object destruction is non-portable) - I don't see any dtors here
226#ifdef __VISUALC__
227 #pragma warning(disable:4611)
228#endif /* VC++ */
229
230bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
231{
232 wxCHECK_MSG( image, false, "NULL image pointer" );
233
234 struct jpeg_decompress_struct cinfo;
235 wx_error_mgr jerr;
236 unsigned char *ptr;
237
238 // save this before calling Destroy()
239 const unsigned maxWidth = image->GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH),
240 maxHeight = image->GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT);
241 image->Destroy();
242
243 cinfo.err = jpeg_std_error( &jerr );
244 jerr.error_exit = wx_error_exit;
245
246 if (!verbose)
247 cinfo.err->output_message = wx_ignore_message;
248
249 /* Establish the setjmp return context for wx_error_exit to use. */
250 if (setjmp(jerr.setjmp_buffer)) {
251 /* If we get here, the JPEG code has signaled an error.
252 * We need to clean up the JPEG object, close the input file, and return.
253 */
254 if (verbose)
255 {
256 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
257 }
258 (cinfo.src->term_source)(&cinfo);
259 jpeg_destroy_decompress(&cinfo);
260 if (image->IsOk()) image->Destroy();
261 return false;
262 }
263
264 jpeg_create_decompress( &cinfo );
265 wx_jpeg_io_src( &cinfo, stream );
266 jpeg_read_header( &cinfo, TRUE );
267
268 int bytesPerPixel;
269 if ((cinfo.out_color_space == JCS_CMYK) || (cinfo.out_color_space == JCS_YCCK))
270 {
271 cinfo.out_color_space = JCS_CMYK;
272 bytesPerPixel = 4;
273 }
274 else // all the rest is treated as RGB
275 {
276 cinfo.out_color_space = JCS_RGB;
277 bytesPerPixel = 3;
278 }
279
280 // scale the picture to fit in the specified max size if necessary
281 if ( maxWidth > 0 || maxHeight > 0 )
282 {
283 unsigned& scale = cinfo.scale_denom;
284 while ( (maxWidth && (cinfo.image_width / scale > maxWidth)) ||
285 (maxHeight && (cinfo.image_height / scale > maxHeight)) )
286 {
287 scale *= 2;
288 }
289 }
290
291 jpeg_start_decompress( &cinfo );
292
293 image->Create( cinfo.output_width, cinfo.output_height );
294 if (!image->IsOk()) {
295 jpeg_finish_decompress( &cinfo );
296 jpeg_destroy_decompress( &cinfo );
297 return false;
298 }
299 image->SetMask( false );
300 ptr = image->GetData();
301
302 unsigned stride = cinfo.output_width * bytesPerPixel;
303 JSAMPARRAY tempbuf = (*cinfo.mem->alloc_sarray)
304 ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 );
305
306 while ( cinfo.output_scanline < cinfo.output_height )
307 {
308 jpeg_read_scanlines( &cinfo, tempbuf, 1 );
309 if (cinfo.out_color_space == JCS_RGB)
310 {
311 memcpy( ptr, tempbuf[0], stride );
312 ptr += stride;
313 }
314 else // CMYK
315 {
316 const unsigned char* inptr = (const unsigned char*) tempbuf[0];
317 for (size_t i = 0; i < cinfo.output_width; i++)
318 {
319 wx_cmyk_to_rgb(ptr, inptr);
320 ptr += 3;
321 inptr += 4;
322 }
323 }
324 }
325
326 // set up resolution if available: it's part of optional JFIF APP0 chunk
327 if ( cinfo.saw_JFIF_marker )
328 {
329 image->SetOption(wxIMAGE_OPTION_RESOLUTIONX, cinfo.X_density);
330 image->SetOption(wxIMAGE_OPTION_RESOLUTIONY, cinfo.Y_density);
331
332 // we use the same values for this option as libjpeg so we don't need
333 // any conversion here
334 image->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT, cinfo.density_unit);
335 }
336
337 jpeg_finish_decompress( &cinfo );
338 jpeg_destroy_decompress( &cinfo );
339 return true;
340}
341
342typedef struct {
343 struct jpeg_destination_mgr pub;
344
345 wxOutputStream *stream;
346 JOCTET * buffer;
347} wx_destination_mgr;
348
349typedef wx_destination_mgr * wx_dest_ptr;
350
351#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
352
353extern "C"
354{
355
356CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo)
357{
358 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
359
360 /* Allocate the output buffer --- it will be released when done with image */
361 dest->buffer = (JOCTET *)
362 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
363 OUTPUT_BUF_SIZE * sizeof(JOCTET));
364 dest->pub.next_output_byte = dest->buffer;
365 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
366}
367
368CPP_METHODDEF(wxjpeg_boolean) wx_empty_output_buffer (j_compress_ptr cinfo)
369{
370 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
371
372 dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE);
373 dest->pub.next_output_byte = dest->buffer;
374 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
375 return TRUE;
376}
377
378CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo)
379{
380 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
381 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
382 /* Write any data remaining in the buffer */
383 if (datacount > 0)
384 dest->stream->Write(dest->buffer, datacount);
385}
386
387GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
388{
389 wx_dest_ptr dest;
390
391 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
392 cinfo->dest = (struct jpeg_destination_mgr *)
393 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
394 sizeof(wx_destination_mgr));
395 }
396
397 dest = (wx_dest_ptr) cinfo->dest;
398 dest->pub.init_destination = wx_init_destination;
399 dest->pub.empty_output_buffer = wx_empty_output_buffer;
400 dest->pub.term_destination = wx_term_destination;
401 dest->stream = &outfile;
402}
403
404} // extern "C"
405
406bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
407{
408 struct jpeg_compress_struct cinfo;
409 wx_error_mgr jerr;
410 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
411 JSAMPLE *image_buffer;
412 int stride; /* physical row width in image buffer */
413
414 cinfo.err = jpeg_std_error(&jerr);
415 jerr.error_exit = wx_error_exit;
416
417 if (!verbose)
418 cinfo.err->output_message = wx_ignore_message;
419
420 /* Establish the setjmp return context for wx_error_exit to use. */
421 if (setjmp(jerr.setjmp_buffer))
422 {
423 /* If we get here, the JPEG code has signaled an error.
424 * We need to clean up the JPEG object, close the input file, and return.
425 */
426 if (verbose)
427 {
428 wxLogError(_("JPEG: Couldn't save image."));
429 }
430 jpeg_destroy_compress(&cinfo);
431 return false;
432 }
433
434 jpeg_create_compress(&cinfo);
435 wx_jpeg_io_dest(&cinfo, stream);
436
437 cinfo.image_width = image->GetWidth();
438 cinfo.image_height = image->GetHeight();
439 cinfo.input_components = 3;
440 cinfo.in_color_space = JCS_RGB;
441 jpeg_set_defaults(&cinfo);
442
443 // TODO: 3rd parameter is force_baseline, what value should this be?
444 // Code says: "If force_baseline is TRUE, the computed quantization table entries
445 // are limited to 1..255 for JPEG baseline compatibility."
446 // 'Quality' is a number between 0 (terrible) and 100 (very good).
447 // The default (in jcparam.c, jpeg_set_defaults) is 75,
448 // and force_baseline is TRUE.
449 if (image->HasOption(wxIMAGE_OPTION_QUALITY))
450 jpeg_set_quality(&cinfo, image->GetOptionInt(wxIMAGE_OPTION_QUALITY), TRUE);
451
452 // set the resolution fields in the output file
453 int resX, resY;
454 wxImageResolution res = GetResolutionFromOptions(*image, &resX, &resY);
455 if ( res != wxIMAGE_RESOLUTION_NONE )
456 {
457 cinfo.X_density = resX;
458 cinfo.Y_density = resY;
459
460 // it so happens that wxIMAGE_RESOLUTION_INCHES/CM values are the same
461 // ones as used by libjpeg, so we can assign them directly
462 cinfo.density_unit = res;
463 }
464
465 jpeg_start_compress(&cinfo, TRUE);
466
467 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
468 image_buffer = image->GetData();
469 while (cinfo.next_scanline < cinfo.image_height) {
470 row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
471 jpeg_write_scanlines( &cinfo, row_pointer, 1 );
472 }
473 jpeg_finish_compress(&cinfo);
474 jpeg_destroy_compress(&cinfo);
475
476 return true;
477}
478
479#ifdef __VISUALC__
480 #pragma warning(default:4611)
481#endif /* VC++ */
482
483bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
484{
485 unsigned char hdr[2];
486
487 if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) // it's ok to modify the stream position here
488 return false;
489
490 return hdr[0] == 0xFF && hdr[1] == 0xD8;
491}
492
493#endif // wxUSE_STREAMS
494
495/*static*/ wxVersionInfo wxJPEGHandler::GetLibraryVersionInfo()
496{
497 return wxVersionInfo("libjpeg", JPEG_LIB_VERSION/10, JPEG_LIB_VERSION%10);
498}
499
500#endif // wxUSE_LIBJPEG