]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/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 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
17 | #if wxUSE_IMAGE && wxUSE_LIBJPEG | |
18 | ||
19 | #include "wx/imagjpeg.h" | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/log.h" | |
23 | #include "wx/app.h" | |
24 | #include "wx/intl.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/bitmap.h" | |
28 | ||
29 | // NB: Some compilers define boolean type in Windows headers | |
30 | // (e.g. Watcom C++, but not Open Watcom). | |
31 | // This causes a conflict with jmorecfg.h header from libjpeg, so we have | |
32 | // to make sure libjpeg won't try to define boolean itself. This is done by | |
33 | // defining HAVE_BOOLEAN. | |
34 | #if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__DIGITALMARS__)) | |
35 | #define HAVE_BOOLEAN | |
36 | #include "wx/msw/wrapwin.h" | |
37 | #endif | |
38 | ||
39 | extern "C" | |
40 | { | |
41 | #if defined(__WXMSW__) | |
42 | #define XMD_H | |
43 | #endif | |
44 | #include "jpeglib.h" | |
45 | } | |
46 | ||
47 | #include "wx/filefn.h" | |
48 | #include "wx/wfstream.h" | |
49 | #include "wx/module.h" | |
50 | ||
51 | // For memcpy | |
52 | #include <string.h> | |
53 | // For JPEG library error handling | |
54 | #include <setjmp.h> | |
55 | ||
56 | #ifdef __SALFORDC__ | |
57 | #undef FAR | |
58 | #endif | |
59 | ||
60 | // ---------------------------------------------------------------------------- | |
61 | // types | |
62 | // ---------------------------------------------------------------------------- | |
63 | ||
64 | // the standard definition of METHODDEF(type) from jmorecfg.h is "static type" | |
65 | // which means that we can't declare the method functions as extern "C" - the | |
66 | // compiler (rightfully) complains about the multiple storage classes in | |
67 | // declaration | |
68 | // | |
69 | // so we only add extern "C" when using our own, modified, jmorecfg.h - and use | |
70 | // whatever we have in the system headers if this is what we use hoping that it | |
71 | // should be ok (can't do anything else) | |
72 | #ifdef JPEG_METHOD_LINKAGE | |
73 | #define CPP_METHODDEF(type) extern "C" METHODDEF(type) | |
74 | #else // not using our jmorecfg.h header | |
75 | #define CPP_METHODDEF(type) METHODDEF(type) | |
76 | #endif | |
77 | ||
78 | //----------------------------------------------------------------------------- | |
79 | // wxJPEGHandler | |
80 | //----------------------------------------------------------------------------- | |
81 | ||
82 | IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler) | |
83 | ||
84 | #if wxUSE_STREAMS | |
85 | ||
86 | //------------- JPEG Data Source Manager | |
87 | ||
88 | #define JPEG_IO_BUFFER_SIZE 2048 | |
89 | ||
90 | typedef struct { | |
91 | struct jpeg_source_mgr pub; /* public fields */ | |
92 | ||
93 | JOCTET* buffer; /* start of buffer */ | |
94 | wxInputStream *stream; | |
95 | } wx_source_mgr; | |
96 | ||
97 | typedef wx_source_mgr * wx_src_ptr; | |
98 | ||
99 | CPP_METHODDEF(void) wx_init_source ( j_decompress_ptr WXUNUSED(cinfo) ) | |
100 | { | |
101 | } | |
102 | ||
103 | CPP_METHODDEF(boolean) wx_fill_input_buffer ( j_decompress_ptr cinfo ) | |
104 | { | |
105 | wx_src_ptr src = (wx_src_ptr) cinfo->src; | |
106 | ||
107 | src->pub.next_input_byte = src->buffer; | |
108 | src->pub.bytes_in_buffer = src->stream->Read(src->buffer, JPEG_IO_BUFFER_SIZE).LastRead(); | |
109 | ||
110 | if (src->pub.bytes_in_buffer == 0) // check for end-of-stream | |
111 | { | |
112 | // Insert a fake EOI marker | |
113 | src->buffer[0] = 0xFF; | |
114 | src->buffer[1] = JPEG_EOI; | |
115 | src->pub.bytes_in_buffer = 2; | |
116 | } | |
117 | return TRUE; | |
118 | } | |
119 | ||
120 | CPP_METHODDEF(void) wx_skip_input_data ( j_decompress_ptr cinfo, long num_bytes ) | |
121 | { | |
122 | if (num_bytes > 0) | |
123 | { | |
124 | wx_src_ptr src = (wx_src_ptr) cinfo->src; | |
125 | ||
126 | while (num_bytes > (long)src->pub.bytes_in_buffer) | |
127 | { | |
128 | num_bytes -= (long) src->pub.bytes_in_buffer; | |
129 | src->pub.fill_input_buffer(cinfo); | |
130 | } | |
131 | src->pub.next_input_byte += (size_t) num_bytes; | |
132 | src->pub.bytes_in_buffer -= (size_t) num_bytes; | |
133 | } | |
134 | } | |
135 | ||
136 | CPP_METHODDEF(void) wx_term_source ( j_decompress_ptr cinfo ) | |
137 | { | |
138 | wx_src_ptr src = (wx_src_ptr) cinfo->src; | |
139 | ||
140 | if (src->pub.bytes_in_buffer > 0) | |
141 | src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent); | |
142 | delete[] src->buffer; | |
143 | } | |
144 | ||
145 | ||
146 | // JPEG error manager: | |
147 | ||
148 | struct wx_error_mgr { | |
149 | struct jpeg_error_mgr pub; /* "public" fields */ | |
150 | ||
151 | jmp_buf setjmp_buffer; /* for return to caller */ | |
152 | }; | |
153 | ||
154 | typedef struct wx_error_mgr * wx_error_ptr; | |
155 | ||
156 | /* | |
157 | * Here's the routine that will replace the standard error_exit method: | |
158 | */ | |
159 | ||
160 | CPP_METHODDEF(void) wx_error_exit (j_common_ptr cinfo) | |
161 | { | |
162 | /* cinfo->err really points to a wx_error_mgr struct, so coerce pointer */ | |
163 | wx_error_ptr myerr = (wx_error_ptr) cinfo->err; | |
164 | ||
165 | /* Always display the message. */ | |
166 | /* We could postpone this until after returning, if we chose. */ | |
167 | (*cinfo->err->output_message) (cinfo); | |
168 | ||
169 | /* Return control to the setjmp point */ | |
170 | longjmp(myerr->setjmp_buffer, 1); | |
171 | } | |
172 | ||
173 | /* | |
174 | * This will replace the standard output_message method when the user | |
175 | * wants us to be silent (verbose==false). We must have such method instead of | |
176 | * simply using NULL for cinfo->err->output_message because it's called | |
177 | * unconditionally from within libjpeg when there's "garbage input". | |
178 | */ | |
179 | CPP_METHODDEF(void) wx_ignore_message (j_common_ptr WXUNUSED(cinfo)) | |
180 | { | |
181 | } | |
182 | ||
183 | void wx_jpeg_io_src( j_decompress_ptr cinfo, wxInputStream& infile ) | |
184 | { | |
185 | wx_src_ptr src; | |
186 | ||
187 | if (cinfo->src == NULL) { /* first time for this JPEG object? */ | |
188 | cinfo->src = (struct jpeg_source_mgr *) | |
189 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
190 | sizeof(wx_source_mgr)); | |
191 | } | |
192 | src = (wx_src_ptr) cinfo->src; | |
193 | src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ | |
194 | src->buffer = new JOCTET[JPEG_IO_BUFFER_SIZE]; | |
195 | src->pub.next_input_byte = NULL; /* until buffer loaded */ | |
196 | src->stream = &infile; | |
197 | ||
198 | src->pub.init_source = wx_init_source; | |
199 | src->pub.fill_input_buffer = wx_fill_input_buffer; | |
200 | src->pub.skip_input_data = wx_skip_input_data; | |
201 | src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ | |
202 | src->pub.term_source = wx_term_source; | |
203 | } | |
204 | ||
205 | static inline void wx_cmyk_to_rgb(unsigned char* rgb, const unsigned char* cmyk) | |
206 | { | |
207 | register int k = 255 - cmyk[3]; | |
208 | register int k2 = cmyk[3]; | |
209 | register int c; | |
210 | ||
211 | c = k + k2 * (255 - cmyk[0]) / 255; | |
212 | rgb[0] = (unsigned char)((c > 255) ? 0 : (255 - c)); | |
213 | ||
214 | c = k + k2 * (255 - cmyk[1]) / 255; | |
215 | rgb[1] = (unsigned char)((c > 255) ? 0 : (255 - c)); | |
216 | ||
217 | c = k + k2 * (255 - cmyk[2]) / 255; | |
218 | rgb[2] = (unsigned char)((c > 255) ? 0 : (255 - c)); | |
219 | } | |
220 | ||
221 | // temporarily disable the warning C4611 (interaction between '_setjmp' and | |
222 | // C++ object destruction is non-portable) - I don't see any dtors here | |
223 | #ifdef __VISUALC__ | |
224 | #pragma warning(disable:4611) | |
225 | #endif /* VC++ */ | |
226 | ||
227 | bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) ) | |
228 | { | |
229 | struct jpeg_decompress_struct cinfo; | |
230 | struct wx_error_mgr jerr; | |
231 | unsigned char *ptr; | |
232 | ||
233 | image->Destroy(); | |
234 | cinfo.err = jpeg_std_error( &jerr.pub ); | |
235 | jerr.pub.error_exit = wx_error_exit; | |
236 | ||
237 | if (!verbose) | |
238 | cinfo.err->output_message = wx_ignore_message; | |
239 | ||
240 | /* Establish the setjmp return context for wx_error_exit to use. */ | |
241 | if (setjmp(jerr.setjmp_buffer)) { | |
242 | /* If we get here, the JPEG code has signaled an error. | |
243 | * We need to clean up the JPEG object, close the input file, and return. | |
244 | */ | |
245 | if (verbose) | |
246 | wxLogError(_("JPEG: Couldn't load - file is probably corrupted.")); | |
247 | (cinfo.src->term_source)(&cinfo); | |
248 | jpeg_destroy_decompress(&cinfo); | |
249 | if (image->Ok()) image->Destroy(); | |
250 | return false; | |
251 | } | |
252 | ||
253 | jpeg_create_decompress( &cinfo ); | |
254 | wx_jpeg_io_src( &cinfo, stream ); | |
255 | jpeg_read_header( &cinfo, TRUE ); | |
256 | ||
257 | int bytesPerPixel; | |
258 | if ((cinfo.out_color_space == JCS_CMYK) || (cinfo.out_color_space == JCS_YCCK)) | |
259 | { | |
260 | cinfo.out_color_space = JCS_CMYK; | |
261 | bytesPerPixel = 4; | |
262 | } | |
263 | else // all the rest is treated as RGB | |
264 | { | |
265 | cinfo.out_color_space = JCS_RGB; | |
266 | bytesPerPixel = 3; | |
267 | } | |
268 | ||
269 | jpeg_start_decompress( &cinfo ); | |
270 | ||
271 | image->Create( cinfo.image_width, cinfo.image_height ); | |
272 | if (!image->Ok()) { | |
273 | jpeg_finish_decompress( &cinfo ); | |
274 | jpeg_destroy_decompress( &cinfo ); | |
275 | return false; | |
276 | } | |
277 | image->SetMask( false ); | |
278 | ptr = image->GetData(); | |
279 | ||
280 | unsigned stride = cinfo.output_width * bytesPerPixel; | |
281 | JSAMPARRAY tempbuf = (*cinfo.mem->alloc_sarray) | |
282 | ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 ); | |
283 | ||
284 | while ( cinfo.output_scanline < cinfo.output_height ) | |
285 | { | |
286 | jpeg_read_scanlines( &cinfo, tempbuf, 1 ); | |
287 | if (cinfo.out_color_space == JCS_RGB) | |
288 | { | |
289 | memcpy( ptr, tempbuf[0], stride ); | |
290 | ptr += stride; | |
291 | } | |
292 | else // CMYK | |
293 | { | |
294 | const unsigned char* inptr = (const unsigned char*) tempbuf[0]; | |
295 | for (size_t i = 0; i < cinfo.output_width; i++) | |
296 | { | |
297 | wx_cmyk_to_rgb(ptr, inptr); | |
298 | ptr += 3; | |
299 | inptr += 4; | |
300 | } | |
301 | } | |
302 | } | |
303 | ||
304 | jpeg_finish_decompress( &cinfo ); | |
305 | jpeg_destroy_decompress( &cinfo ); | |
306 | return true; | |
307 | } | |
308 | ||
309 | typedef struct { | |
310 | struct jpeg_destination_mgr pub; | |
311 | ||
312 | wxOutputStream *stream; | |
313 | JOCTET * buffer; | |
314 | } wx_destination_mgr; | |
315 | ||
316 | typedef wx_destination_mgr * wx_dest_ptr; | |
317 | ||
318 | #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ | |
319 | ||
320 | CPP_METHODDEF(void) wx_init_destination (j_compress_ptr cinfo) | |
321 | { | |
322 | wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest; | |
323 | ||
324 | /* Allocate the output buffer --- it will be released when done with image */ | |
325 | dest->buffer = (JOCTET *) | |
326 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | |
327 | OUTPUT_BUF_SIZE * sizeof(JOCTET)); | |
328 | dest->pub.next_output_byte = dest->buffer; | |
329 | dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; | |
330 | } | |
331 | ||
332 | CPP_METHODDEF(boolean) wx_empty_output_buffer (j_compress_ptr cinfo) | |
333 | { | |
334 | wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest; | |
335 | ||
336 | dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE); | |
337 | dest->pub.next_output_byte = dest->buffer; | |
338 | dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; | |
339 | return TRUE; | |
340 | } | |
341 | ||
342 | CPP_METHODDEF(void) wx_term_destination (j_compress_ptr cinfo) | |
343 | { | |
344 | wx_dest_ptr dest = (wx_dest_ptr) cinfo->dest; | |
345 | size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer; | |
346 | /* Write any data remaining in the buffer */ | |
347 | if (datacount > 0) | |
348 | dest->stream->Write(dest->buffer, datacount); | |
349 | } | |
350 | ||
351 | GLOBAL(void) wx_jpeg_io_dest (j_compress_ptr cinfo, wxOutputStream& outfile) | |
352 | { | |
353 | wx_dest_ptr dest; | |
354 | ||
355 | if (cinfo->dest == NULL) { /* first time for this JPEG object? */ | |
356 | cinfo->dest = (struct jpeg_destination_mgr *) | |
357 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
358 | sizeof(wx_destination_mgr)); | |
359 | } | |
360 | ||
361 | dest = (wx_dest_ptr) cinfo->dest; | |
362 | dest->pub.init_destination = wx_init_destination; | |
363 | dest->pub.empty_output_buffer = wx_empty_output_buffer; | |
364 | dest->pub.term_destination = wx_term_destination; | |
365 | dest->stream = &outfile; | |
366 | } | |
367 | ||
368 | bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) | |
369 | { | |
370 | struct jpeg_compress_struct cinfo; | |
371 | struct wx_error_mgr jerr; | |
372 | JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ | |
373 | JSAMPLE *image_buffer; | |
374 | int stride; /* physical row width in image buffer */ | |
375 | ||
376 | cinfo.err = jpeg_std_error(&jerr.pub); | |
377 | jerr.pub.error_exit = wx_error_exit; | |
378 | ||
379 | if (!verbose) | |
380 | cinfo.err->output_message = wx_ignore_message; | |
381 | ||
382 | /* Establish the setjmp return context for wx_error_exit to use. */ | |
383 | if (setjmp(jerr.setjmp_buffer)) | |
384 | { | |
385 | /* If we get here, the JPEG code has signaled an error. | |
386 | * We need to clean up the JPEG object, close the input file, and return. | |
387 | */ | |
388 | if (verbose) | |
389 | wxLogError(_("JPEG: Couldn't save image.")); | |
390 | jpeg_destroy_compress(&cinfo); | |
391 | return false; | |
392 | } | |
393 | ||
394 | jpeg_create_compress(&cinfo); | |
395 | wx_jpeg_io_dest(&cinfo, stream); | |
396 | ||
397 | cinfo.image_width = image->GetWidth(); | |
398 | cinfo.image_height = image->GetHeight(); | |
399 | cinfo.input_components = 3; | |
400 | cinfo.in_color_space = JCS_RGB; | |
401 | jpeg_set_defaults(&cinfo); | |
402 | ||
403 | // TODO: 3rd parameter is force_baseline, what value should this be? | |
404 | // Code says: "If force_baseline is TRUE, the computed quantization table entries | |
405 | // are limited to 1..255 for JPEG baseline compatibility." | |
406 | // 'Quality' is a number between 0 (terrible) and 100 (very good). | |
407 | // The default (in jcparam.c, jpeg_set_defaults) is 75, | |
408 | // and force_baseline is TRUE. | |
409 | if (image->HasOption(wxIMAGE_OPTION_QUALITY)) | |
410 | jpeg_set_quality(&cinfo, image->GetOptionInt(wxIMAGE_OPTION_QUALITY), TRUE); | |
411 | ||
412 | // set the resolution fields in the output file | |
413 | UINT16 resX, | |
414 | resY; | |
415 | if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) && | |
416 | image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) ) | |
417 | { | |
418 | resX = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX); | |
419 | resY = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY); | |
420 | } | |
421 | else if ( image->HasOption(wxIMAGE_OPTION_RESOLUTION) ) | |
422 | { | |
423 | resX = | |
424 | resY = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION); | |
425 | } | |
426 | else | |
427 | { | |
428 | resX = | |
429 | resY = 0; | |
430 | } | |
431 | ||
432 | if ( resX && resY ) | |
433 | { | |
434 | cinfo.X_density = resX; | |
435 | cinfo.Y_density = resY; | |
436 | } | |
437 | ||
438 | // sets the resolution unit field in the output file | |
439 | // wxIMAGE_RESOLUTION_INCHES for inches | |
440 | // wxIMAGE_RESOLUTION_CM for centimeters | |
441 | if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT) ) | |
442 | { | |
443 | cinfo.density_unit = (UINT8)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT); | |
444 | } | |
445 | ||
446 | jpeg_start_compress(&cinfo, TRUE); | |
447 | ||
448 | stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */ | |
449 | image_buffer = image->GetData(); | |
450 | while (cinfo.next_scanline < cinfo.image_height) { | |
451 | row_pointer[0] = &image_buffer[cinfo.next_scanline * stride]; | |
452 | jpeg_write_scanlines( &cinfo, row_pointer, 1 ); | |
453 | } | |
454 | jpeg_finish_compress(&cinfo); | |
455 | jpeg_destroy_compress(&cinfo); | |
456 | ||
457 | return true; | |
458 | } | |
459 | ||
460 | #ifdef __VISUALC__ | |
461 | #pragma warning(default:4611) | |
462 | #endif /* VC++ */ | |
463 | ||
464 | bool wxJPEGHandler::DoCanRead( wxInputStream& stream ) | |
465 | { | |
466 | unsigned char hdr[2]; | |
467 | ||
468 | if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) | |
469 | return false; | |
470 | ||
471 | return hdr[0] == 0xFF && hdr[1] == 0xD8; | |
472 | } | |
473 | ||
474 | #endif // wxUSE_STREAMS | |
475 | ||
476 | #endif // wxUSE_LIBJPEG |