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