]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
wxSystemSettings class API face lift: better names for its methods
[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
1aaaa712
VS
10#ifdef __GNUG__
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
VS
30
31// NB: Some compilers define boolean type in Windows headers (e.g. Watcom C++).
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.
e7ca6139
VS
35#if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__WATCOMC__))
36 #define HAVE_BOOLEAN
cb69afe0 37 #include <windows.h>
e7ca6139 38#endif
cb69afe0 39
995612e2
VZ
40extern "C"
41{
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__
56#ifdef FAR
57#undef FAR
58#endif
59#endif
60
e9c4b1a2
JS
61//-----------------------------------------------------------------------------
62// wxJPEGHandler
63//-----------------------------------------------------------------------------
64
e9c4b1a2 65IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
e9c4b1a2 66
9ab6ee85
GRG
67#if wxUSE_STREAMS
68
e9c4b1a2
JS
69//------------- JPEG Data Source Manager
70
1aaaa712
VS
71#define JPEG_IO_BUFFER_SIZE 2048
72
e9c4b1a2
JS
73typedef struct {
74 struct jpeg_source_mgr pub; /* public fields */
995612e2 75
e9c4b1a2 76 JOCTET* buffer; /* start of buffer */
1aaaa712 77 wxInputStream *stream;
e9c4b1a2
JS
78} my_source_mgr;
79
80typedef my_source_mgr * my_src_ptr;
81
74e3313b 82METHODDEF(void) my_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
e9c4b1a2
JS
83{
84}
85
1aaaa712 86METHODDEF(boolean) my_fill_input_buffer ( j_decompress_ptr cinfo )
e9c4b1a2 87{
1aaaa712
VS
88 my_src_ptr src = (my_src_ptr) cinfo->src;
89
90 src->pub.next_input_byte = src->buffer;
91 src->pub.bytes_in_buffer = src->stream->Read(src->buffer, JPEG_IO_BUFFER_SIZE).LastRead();
92
93 if (src->pub.bytes_in_buffer == 0) // check for end-of-stream
94 {
95 // Insert a fake EOI marker
96 src->buffer[0] = 0xFF;
97 src->buffer[1] = JPEG_EOI;
98 src->pub.bytes_in_buffer = 2;
99 }
e9c4b1a2
JS
100 return TRUE;
101}
102
103METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
104{
1aaaa712
VS
105 if (num_bytes > 0)
106 {
107 my_src_ptr src = (my_src_ptr) cinfo->src;
108
33ac7e6f 109 while (num_bytes > (long)src->pub.bytes_in_buffer)
1aaaa712
VS
110 {
111 num_bytes -= (long) src->pub.bytes_in_buffer;
112 src->pub.fill_input_buffer(cinfo);
113 }
114 src->pub.next_input_byte += (size_t) num_bytes;
115 src->pub.bytes_in_buffer -= (size_t) num_bytes;
116 }
e9c4b1a2
JS
117}
118
119METHODDEF(void) my_term_source ( j_decompress_ptr cinfo )
120{
121 my_src_ptr src = (my_src_ptr) cinfo->src;
995612e2 122
1aaaa712 123 if (src->pub.bytes_in_buffer > 0)
3ca6a5f0 124 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
1aaaa712 125 delete[] src->buffer;
e9c4b1a2
JS
126}
127
128void jpeg_wxio_src( j_decompress_ptr cinfo, wxInputStream& infile )
129{
130 my_src_ptr src;
995612e2
VZ
131
132 if (cinfo->src == NULL) { /* first time for this JPEG object? */
e9c4b1a2
JS
133 cinfo->src = (struct jpeg_source_mgr *)
134 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
135 sizeof(my_source_mgr));
136 src = (my_src_ptr) cinfo->src;
137 }
138 src = (my_src_ptr) cinfo->src;
1aaaa712
VS
139 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
140 src->buffer = new JOCTET[JPEG_IO_BUFFER_SIZE];
141 src->pub.next_input_byte = NULL; /* until buffer loaded */
142 src->stream = &infile;
995612e2 143
e9c4b1a2
JS
144 src->pub.init_source = my_init_source;
145 src->pub.fill_input_buffer = my_fill_input_buffer;
146 src->pub.skip_input_data = my_skip_input_data;
147 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
148 src->pub.term_source = my_term_source;
149}
150
151
b59ff3c9
VS
152// JPEG error manager:
153
154struct my_error_mgr {
995612e2 155 struct jpeg_error_mgr pub; /* "public" fields */
b59ff3c9 156
995612e2 157 jmp_buf setjmp_buffer; /* for return to caller */
b59ff3c9
VS
158};
159
160typedef struct my_error_mgr * my_error_ptr;
161
162/*
163 * Here's the routine that will replace the standard error_exit method:
164 */
165
166METHODDEF(void)
167my_error_exit (j_common_ptr cinfo)
168{
169 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
170 my_error_ptr myerr = (my_error_ptr) cinfo->err;
171
172 /* Always display the message. */
173 /* We could postpone this until after returning, if we chose. */
deb2fec0 174 if (cinfo->err->output_message) (*cinfo->err->output_message) (cinfo);
b59ff3c9
VS
175
176 /* Return control to the setjmp point */
177 longjmp(myerr->setjmp_buffer, 1);
178}
179
3ca6a5f0
BP
180// temporarily disable the warning C4611 (interaction between '_setjmp' and
181// C++ object destruction is non-portable) - I don't see any dtors here
182#ifdef __VISUALC__
183 #pragma warning(disable:4611)
184#endif /* VC++ */
e9c4b1a2 185
700ec454 186bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
e9c4b1a2
JS
187{
188 struct jpeg_decompress_struct cinfo;
b59ff3c9 189 struct my_error_mgr jerr;
e9c4b1a2
JS
190 JSAMPARRAY tempbuf;
191 unsigned char *ptr;
192 unsigned stride;
995612e2 193
e9c4b1a2 194 image->Destroy();
b59ff3c9
VS
195 cinfo.err = jpeg_std_error( &jerr.pub );
196 jerr.pub.error_exit = my_error_exit;
197
deb2fec0
SB
198 if (!verbose) cinfo.err->output_message=NULL;
199
b59ff3c9
VS
200 /* Establish the setjmp return context for my_error_exit to use. */
201 if (setjmp(jerr.setjmp_buffer)) {
202 /* If we get here, the JPEG code has signaled an error.
203 * We need to clean up the JPEG object, close the input file, and return.
204 */
33ac7e6f 205 if (verbose)
58c837a4 206 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
192644bf 207 (cinfo.src->term_source)(&cinfo);
b59ff3c9
VS
208 jpeg_destroy_decompress(&cinfo);
209 if (image->Ok()) image->Destroy();
210 return FALSE;
211 }
212
e9c4b1a2
JS
213 jpeg_create_decompress( &cinfo );
214 jpeg_wxio_src( &cinfo, stream );
215 jpeg_read_header( &cinfo, TRUE );
216 cinfo.out_color_space = JCS_RGB;
217 jpeg_start_decompress( &cinfo );
995612e2 218
e9c4b1a2
JS
219 image->Create( cinfo.image_width, cinfo.image_height );
220 if (!image->Ok()) {
221 jpeg_finish_decompress( &cinfo );
222 jpeg_destroy_decompress( &cinfo );
223 return FALSE;
224 }
225 image->SetMask( FALSE );
226 ptr = image->GetData();
227 stride = cinfo.output_width * 3;
228 tempbuf = (*cinfo.mem->alloc_sarray)
229 ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 );
995612e2 230
e9c4b1a2
JS
231 while ( cinfo.output_scanline < cinfo.output_height ) {
232 jpeg_read_scanlines( &cinfo, tempbuf, 1 );
233 memcpy( ptr, tempbuf[0], stride );
234 ptr += stride;
235 }
236 jpeg_finish_decompress( &cinfo );
237 jpeg_destroy_decompress( &cinfo );
238 return TRUE;
239}
240
e9c4b1a2
JS
241typedef struct {
242 struct jpeg_destination_mgr pub;
995612e2 243
e9c4b1a2
JS
244 wxOutputStream *stream;
245 JOCTET * buffer;
246} my_destination_mgr;
247
248typedef my_destination_mgr * my_dest_ptr;
249
995612e2 250#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
e9c4b1a2
JS
251
252METHODDEF(void) init_destination (j_compress_ptr cinfo)
253{
254 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
995612e2 255
e9c4b1a2
JS
256 /* Allocate the output buffer --- it will be released when done with image */
257 dest->buffer = (JOCTET *)
258 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
259 OUTPUT_BUF_SIZE * sizeof(JOCTET));
260 dest->pub.next_output_byte = dest->buffer;
261 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
262}
263
264METHODDEF(boolean) empty_output_buffer (j_compress_ptr cinfo)
265{
266 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
995612e2 267
e9c4b1a2
JS
268 dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE);
269 dest->pub.next_output_byte = dest->buffer;
270 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
271 return TRUE;
272}
273
274METHODDEF(void) term_destination (j_compress_ptr cinfo)
275{
276 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
277 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
278 /* Write any data remaining in the buffer */
279 if (datacount > 0)
280 dest->stream->Write(dest->buffer, datacount);
281}
282
283GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
284{
285 my_dest_ptr dest;
995612e2
VZ
286
287 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
e9c4b1a2
JS
288 cinfo->dest = (struct jpeg_destination_mgr *)
289 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
290 sizeof(my_destination_mgr));
291 }
995612e2 292
e9c4b1a2
JS
293 dest = (my_dest_ptr) cinfo->dest;
294 dest->pub.init_destination = init_destination;
295 dest->pub.empty_output_buffer = empty_output_buffer;
296 dest->pub.term_destination = term_destination;
297 dest->stream = &outfile;
298}
299
deb2fec0 300bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2
JS
301{
302 struct jpeg_compress_struct cinfo;
b59ff3c9 303 struct my_error_mgr jerr;
995612e2 304 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
e9c4b1a2 305 JSAMPLE *image_buffer;
995612e2
VZ
306 int stride; /* physical row width in image buffer */
307
b59ff3c9
VS
308 cinfo.err = jpeg_std_error(&jerr.pub);
309 jerr.pub.error_exit = my_error_exit;
310
deb2fec0
SB
311 if (!verbose) cinfo.err->output_message=NULL;
312
b59ff3c9 313 /* Establish the setjmp return context for my_error_exit to use. */
33ac7e6f 314 if (setjmp(jerr.setjmp_buffer))
58c837a4
RR
315 {
316 /* If we get here, the JPEG code has signaled an error.
317 * We need to clean up the JPEG object, close the input file, and return.
318 */
33ac7e6f 319 if (verbose)
58c837a4
RR
320 wxLogError(_("JPEG: Couldn't save image."));
321 jpeg_destroy_compress(&cinfo);
322 return FALSE;
b59ff3c9
VS
323 }
324
e9c4b1a2
JS
325 jpeg_create_compress(&cinfo);
326 jpeg_wxio_dest(&cinfo, stream);
995612e2 327
e9c4b1a2
JS
328 cinfo.image_width = image->GetWidth();
329 cinfo.image_height = image->GetHeight();
330 cinfo.input_components = 3;
331 cinfo.in_color_space = JCS_RGB;
332 jpeg_set_defaults(&cinfo);
5e5437e0
JS
333
334 // TODO: 3rd parameter is force_baseline, what value should this be?
335 // Code says: "If force_baseline is TRUE, the computed quantization table entries
336 // are limited to 1..255 for JPEG baseline compatibility."
337 // 'Quality' is a number between 0 (terrible) and 100 (very good).
338 // The default (in jcparam.c, jpeg_set_defaults) is 75,
339 // and force_baseline is TRUE.
340 if (image->HasOption(wxT("quality")))
341 jpeg_set_quality(&cinfo, image->GetOptionInt(wxT("quality")), TRUE);
342
e9c4b1a2 343 jpeg_start_compress(&cinfo, TRUE);
995612e2
VZ
344
345 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
e9c4b1a2
JS
346 image_buffer = image->GetData();
347 while (cinfo.next_scanline < cinfo.image_height) {
348 row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
349 jpeg_write_scanlines( &cinfo, row_pointer, 1 );
350 }
351 jpeg_finish_compress(&cinfo);
352 jpeg_destroy_compress(&cinfo);
995612e2 353
e9c4b1a2
JS
354 return TRUE;
355}
e9c4b1a2 356
3ca6a5f0
BP
357#ifdef __VISUALC__
358 #pragma warning(default:4611)
359#endif /* VC++ */
0828c087 360
995612e2 361bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
0828c087
VS
362{
363 unsigned char hdr[2];
995612e2 364
33ac7e6f 365 stream.Read(hdr, 2);
0828c087
VS
366 stream.SeekI(-2, wxFromCurrent);
367 return (hdr[0] == 0xFF && hdr[1] == 0xD8);
368}
369
9ab6ee85
GRG
370#endif // wxUSE_STREAMS
371
372#endif // wxUSE_LIBJPEG
b59ff3c9
VS
373
374
375
376
377
378