]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagjpeg.cpp
expand . into cwd
[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 //-----------------------------------------------------------------------------
62 // wxJPEGHandler
63 //-----------------------------------------------------------------------------
64
65 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
66
67 #if wxUSE_STREAMS
68
69 //------------- JPEG Data Source Manager
70
71 #define JPEG_IO_BUFFER_SIZE 2048
72
73 typedef struct {
74 struct jpeg_source_mgr pub; /* public fields */
75
76 JOCTET* buffer; /* start of buffer */
77 wxInputStream *stream;
78 } my_source_mgr;
79
80 typedef my_source_mgr * my_src_ptr;
81
82 METHODDEF(void) my_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
83 {
84 }
85
86 METHODDEF(boolean) my_fill_input_buffer ( j_decompress_ptr cinfo )
87 {
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 }
100 return TRUE;
101 }
102
103 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
104 {
105 if (num_bytes > 0)
106 {
107 my_src_ptr src = (my_src_ptr) cinfo->src;
108
109 while (num_bytes > (long)src->pub.bytes_in_buffer)
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 }
117 }
118
119 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo )
120 {
121 my_src_ptr src = (my_src_ptr) cinfo->src;
122
123 if (src->pub.bytes_in_buffer > 0)
124 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
125 delete[] src->buffer;
126 }
127
128 void jpeg_wxio_src( j_decompress_ptr cinfo, wxInputStream& infile )
129 {
130 my_src_ptr src;
131
132 if (cinfo->src == NULL) { /* first time for this JPEG object? */
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;
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;
143
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
152 // JPEG error manager:
153
154 struct my_error_mgr {
155 struct jpeg_error_mgr pub; /* "public" fields */
156
157 jmp_buf setjmp_buffer; /* for return to caller */
158 };
159
160 typedef struct my_error_mgr * my_error_ptr;
161
162 /*
163 * Here's the routine that will replace the standard error_exit method:
164 */
165
166 METHODDEF(void)
167 my_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. */
174 if (cinfo->err->output_message) (*cinfo->err->output_message) (cinfo);
175
176 /* Return control to the setjmp point */
177 longjmp(myerr->setjmp_buffer, 1);
178 }
179
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++ */
185
186 bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
187 {
188 struct jpeg_decompress_struct cinfo;
189 struct my_error_mgr jerr;
190 JSAMPARRAY tempbuf;
191 unsigned char *ptr;
192 unsigned stride;
193
194 image->Destroy();
195 cinfo.err = jpeg_std_error( &jerr.pub );
196 jerr.pub.error_exit = my_error_exit;
197
198 if (!verbose) cinfo.err->output_message=NULL;
199
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 */
205 if (verbose)
206 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
207 (cinfo.src->term_source)(&cinfo);
208 jpeg_destroy_decompress(&cinfo);
209 if (image->Ok()) image->Destroy();
210 return FALSE;
211 }
212
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 );
218
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 );
230
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
241 typedef struct {
242 struct jpeg_destination_mgr pub;
243
244 wxOutputStream *stream;
245 JOCTET * buffer;
246 } my_destination_mgr;
247
248 typedef my_destination_mgr * my_dest_ptr;
249
250 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
251
252 METHODDEF(void) init_destination (j_compress_ptr cinfo)
253 {
254 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
255
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
264 METHODDEF(boolean) empty_output_buffer (j_compress_ptr cinfo)
265 {
266 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
267
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
274 METHODDEF(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
283 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
284 {
285 my_dest_ptr dest;
286
287 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
288 cinfo->dest = (struct jpeg_destination_mgr *)
289 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
290 sizeof(my_destination_mgr));
291 }
292
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
300 bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
301 {
302 struct jpeg_compress_struct cinfo;
303 struct my_error_mgr jerr;
304 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
305 JSAMPLE *image_buffer;
306 int stride; /* physical row width in image buffer */
307
308 cinfo.err = jpeg_std_error(&jerr.pub);
309 jerr.pub.error_exit = my_error_exit;
310
311 if (!verbose) cinfo.err->output_message=NULL;
312
313 /* Establish the setjmp return context for my_error_exit to use. */
314 if (setjmp(jerr.setjmp_buffer))
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 */
319 if (verbose)
320 wxLogError(_("JPEG: Couldn't save image."));
321 jpeg_destroy_compress(&cinfo);
322 return FALSE;
323 }
324
325 jpeg_create_compress(&cinfo);
326 jpeg_wxio_dest(&cinfo, stream);
327
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);
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
343 jpeg_start_compress(&cinfo, TRUE);
344
345 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
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);
353
354 return TRUE;
355 }
356
357 #ifdef __VISUALC__
358 #pragma warning(default:4611)
359 #endif /* VC++ */
360
361 bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
362 {
363 unsigned char hdr[2];
364
365 stream.Read(hdr, 2);
366 stream.SeekI(-2, wxFromCurrent);
367 return (hdr[0] == 0xFF && hdr[1] == 0xD8);
368 }
369
370 #endif // wxUSE_STREAMS
371
372 #endif // wxUSE_LIBJPEG
373
374
375
376
377
378