]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
scroll of subwindow confined to rect param
[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;
e9b964cf 98} wx_source_mgr;
e9c4b1a2 99
e9b964cf 100typedef wx_source_mgr * wx_src_ptr;
e9c4b1a2 101
e9b964cf 102CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
e9c4b1a2
JS
103{
104}
105
e9b964cf 106CPP_METHODDEF(boolean) wx_fill_input_buffer ( j_decompress_ptr cinfo )
e9c4b1a2 107{
e9b964cf 108 wx_src_ptr src = (wx_src_ptr) cinfo->src;
1aaaa712
VS
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
e9b964cf 123CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
e9c4b1a2 124{
1aaaa712
VS
125 if (num_bytes > 0)
126 {
e9b964cf 127 wx_src_ptr src = (wx_src_ptr) cinfo->src;
1aaaa712 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
e9b964cf 139CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo )
e9c4b1a2 140{
e9b964cf 141 wx_src_ptr src = (wx_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
e9b964cf 151struct wx_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
e9b964cf 157typedef struct wx_error_mgr * wx_error_ptr;
b59ff3c9
VS
158
159/*
160 * Here's the routine that will replace the standard error_exit method:
161 */
162
e9b964cf 163CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo)
b59ff3c9 164{
e9b964cf
VS
165 /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */
166 wx_error_ptr myerr = (wx_error_ptr) cinfo->err;
b59ff3c9
VS
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
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
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;
e9b964cf 208 struct wx_error_mgr jerr;
e9c4b1a2
JS
209 JSAMPARRAY tempbuf;
210 unsigned char *ptr;
211 unsigned stride;
995612e2 212
e9c4b1a2 213 image->Destroy();
b59ff3c9 214 cinfo.err = jpeg_std_error( &jerr.pub );
e9b964cf 215 jerr.pub.error_exit = wx_error_exit;
b59ff3c9 216
deb2fec0
SB
217 if (!verbose) cinfo.err->output_message=NULL;
218
e9b964cf 219 /* Establish the setjmp return context for wx_error_exit to use. */
b59ff3c9
VS
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 232 jpeg_create_decompress( &cinfo );
e9b964cf 233 wx_jpeg_io_src( &cinfo, stream );
e9c4b1a2
JS
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;
e9b964cf 265} wx_destination_mgr;
e9c4b1a2 266
e9b964cf 267typedef wx_destination_mgr * wx_dest_ptr;
e9c4b1a2 268
995612e2 269#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
e9c4b1a2 270
e9b964cf 271CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo)
e9c4b1a2 272{
e9b964cf 273 wx_dest_ptr dest = (wx_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
e9b964cf 283CPP_METHODDEF(boolean) wx_empty_output_buffer (j_compress_ptr cinfo)
e9c4b1a2 284{
e9b964cf 285 wx_dest_ptr dest = (wx_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
e9b964cf 293CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo)
e9c4b1a2 294{
e9b964cf 295 wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest;
e9c4b1a2
JS
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
e9b964cf 302GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
e9c4b1a2 303{
e9b964cf 304 wx_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,
e9b964cf 309 sizeof(wx_destination_mgr));
e9c4b1a2 310 }
995612e2 311
e9b964cf
VS
312 dest = (wx_dest_ptr) cinfo->dest;
313 dest->pub.init_destination = wx_init_destination;
314 dest->pub.empty_output_buffer = wx_empty_output_buffer;
315 dest->pub.term_destination = wx_term_destination;
e9c4b1a2
JS
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;
e9b964cf 322 struct wx_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 327 cinfo.err = jpeg_std_error(&jerr.pub);
e9b964cf 328 jerr.pub.error_exit = wx_error_exit;
b59ff3c9 329
deb2fec0
SB
330 if (!verbose) cinfo.err->output_message=NULL;
331
e9b964cf 332 /* Establish the setjmp return context for wx_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 344 jpeg_create_compress(&cinfo);
e9b964cf 345 wx_jpeg_io_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