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