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