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