]>
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 | ||
9ab6ee85 | 23 | #if 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" | |
995612e2 VZ |
30 | extern "C" |
31 | { | |
f6bcfd97 BP |
32 | #ifdef __WATCOMC__ |
33 | #define HAVE_BOOLEAN | |
34 | #endif | |
995612e2 | 35 | #include "jpeglib.h" |
e9c4b1a2 | 36 | } |
e9c4b1a2 JS |
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> | |
b59ff3c9 VS |
44 | // For JPEG library error handling |
45 | #include <setjmp.h> | |
e9c4b1a2 JS |
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 | ||
e9c4b1a2 | 61 | IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler) |
e9c4b1a2 | 62 | |
9ab6ee85 GRG |
63 | #if wxUSE_STREAMS |
64 | ||
e9c4b1a2 JS |
65 | //------------- JPEG Data Source Manager |
66 | ||
1aaaa712 VS |
67 | #define JPEG_IO_BUFFER_SIZE 2048 |
68 | ||
e9c4b1a2 JS |
69 | typedef struct { |
70 | struct jpeg_source_mgr pub; /* public fields */ | |
995612e2 | 71 | |
e9c4b1a2 | 72 | JOCTET* buffer; /* start of buffer */ |
1aaaa712 | 73 | wxInputStream *stream; |
e9c4b1a2 JS |
74 | } my_source_mgr; |
75 | ||
76 | typedef my_source_mgr * my_src_ptr; | |
77 | ||
74e3313b | 78 | METHODDEF(void) my_init_source ( j_decompress_ptr WXUNUSED(cinfo) ) |
e9c4b1a2 JS |
79 | { |
80 | } | |
81 | ||
1aaaa712 | 82 | METHODDEF(boolean) my_fill_input_buffer ( j_decompress_ptr cinfo ) |
e9c4b1a2 | 83 | { |
1aaaa712 VS |
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 | } | |
e9c4b1a2 JS |
96 | return TRUE; |
97 | } | |
98 | ||
99 | METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo, long num_bytes ) | |
100 | { | |
1aaaa712 VS |
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 | } | |
e9c4b1a2 JS |
113 | } |
114 | ||
115 | METHODDEF(void) my_term_source ( j_decompress_ptr cinfo ) | |
116 | { | |
117 | my_src_ptr src = (my_src_ptr) cinfo->src; | |
995612e2 | 118 | |
1aaaa712 VS |
119 | if (src->pub.bytes_in_buffer > 0) |
120 | src->stream->SeekI(-src->pub.bytes_in_buffer, wxFromCurrent); | |
121 | delete[] src->buffer; | |
e9c4b1a2 JS |
122 | } |
123 | ||
124 | void jpeg_wxio_src( j_decompress_ptr cinfo, wxInputStream& infile ) | |
125 | { | |
126 | my_src_ptr src; | |
995612e2 VZ |
127 | |
128 | if (cinfo->src == NULL) { /* first time for this JPEG object? */ | |
e9c4b1a2 JS |
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; | |
1aaaa712 VS |
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; | |
995612e2 | 139 | |
e9c4b1a2 JS |
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 | ||
b59ff3c9 VS |
148 | // JPEG error manager: |
149 | ||
150 | struct my_error_mgr { | |
995612e2 | 151 | struct jpeg_error_mgr pub; /* "public" fields */ |
b59ff3c9 | 152 | |
995612e2 | 153 | jmp_buf setjmp_buffer; /* for return to caller */ |
b59ff3c9 VS |
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. */ | |
deb2fec0 | 170 | if (cinfo->err->output_message) (*cinfo->err->output_message) (cinfo); |
b59ff3c9 VS |
171 | |
172 | /* Return control to the setjmp point */ | |
173 | longjmp(myerr->setjmp_buffer, 1); | |
174 | } | |
175 | ||
176 | ||
e9c4b1a2 | 177 | |
700ec454 | 178 | bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) ) |
e9c4b1a2 JS |
179 | { |
180 | struct jpeg_decompress_struct cinfo; | |
b59ff3c9 | 181 | struct my_error_mgr jerr; |
e9c4b1a2 JS |
182 | JSAMPARRAY tempbuf; |
183 | unsigned char *ptr; | |
184 | unsigned stride; | |
995612e2 | 185 | |
e9c4b1a2 | 186 | image->Destroy(); |
b59ff3c9 VS |
187 | cinfo.err = jpeg_std_error( &jerr.pub ); |
188 | jerr.pub.error_exit = my_error_exit; | |
189 | ||
deb2fec0 SB |
190 | if (!verbose) cinfo.err->output_message=NULL; |
191 | ||
b59ff3c9 VS |
192 | /* Establish the setjmp return context for my_error_exit to use. */ |
193 | if (setjmp(jerr.setjmp_buffer)) { | |
194 | /* If we get here, the JPEG code has signaled an error. | |
195 | * We need to clean up the JPEG object, close the input file, and return. | |
196 | */ | |
58c837a4 RR |
197 | if (verbose) |
198 | wxLogError(_("JPEG: Couldn't load - file is probably corrupted.")); | |
b59ff3c9 VS |
199 | jpeg_destroy_decompress(&cinfo); |
200 | if (image->Ok()) image->Destroy(); | |
201 | return FALSE; | |
202 | } | |
203 | ||
e9c4b1a2 JS |
204 | jpeg_create_decompress( &cinfo ); |
205 | jpeg_wxio_src( &cinfo, stream ); | |
206 | jpeg_read_header( &cinfo, TRUE ); | |
207 | cinfo.out_color_space = JCS_RGB; | |
208 | jpeg_start_decompress( &cinfo ); | |
995612e2 | 209 | |
e9c4b1a2 JS |
210 | image->Create( cinfo.image_width, cinfo.image_height ); |
211 | if (!image->Ok()) { | |
212 | jpeg_finish_decompress( &cinfo ); | |
213 | jpeg_destroy_decompress( &cinfo ); | |
214 | return FALSE; | |
215 | } | |
216 | image->SetMask( FALSE ); | |
217 | ptr = image->GetData(); | |
218 | stride = cinfo.output_width * 3; | |
219 | tempbuf = (*cinfo.mem->alloc_sarray) | |
220 | ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 ); | |
995612e2 | 221 | |
e9c4b1a2 JS |
222 | while ( cinfo.output_scanline < cinfo.output_height ) { |
223 | jpeg_read_scanlines( &cinfo, tempbuf, 1 ); | |
224 | memcpy( ptr, tempbuf[0], stride ); | |
225 | ptr += stride; | |
226 | } | |
227 | jpeg_finish_decompress( &cinfo ); | |
228 | jpeg_destroy_decompress( &cinfo ); | |
229 | return TRUE; | |
230 | } | |
231 | ||
232 | ||
233 | ||
234 | ||
235 | ||
236 | typedef struct { | |
237 | struct jpeg_destination_mgr pub; | |
995612e2 | 238 | |
e9c4b1a2 JS |
239 | wxOutputStream *stream; |
240 | JOCTET * buffer; | |
241 | } my_destination_mgr; | |
242 | ||
243 | typedef my_destination_mgr * my_dest_ptr; | |
244 | ||
995612e2 | 245 | #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ |
e9c4b1a2 JS |
246 | |
247 | METHODDEF(void) init_destination (j_compress_ptr cinfo) | |
248 | { | |
249 | my_dest_ptr dest = (my_dest_ptr) cinfo->dest; | |
995612e2 | 250 | |
e9c4b1a2 JS |
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; | |
995612e2 | 262 | |
e9c4b1a2 JS |
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; | |
995612e2 VZ |
281 | |
282 | if (cinfo->dest == NULL) { /* first time for this JPEG object? */ | |
e9c4b1a2 JS |
283 | cinfo->dest = (struct jpeg_destination_mgr *) |
284 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
285 | sizeof(my_destination_mgr)); | |
286 | } | |
995612e2 | 287 | |
e9c4b1a2 JS |
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 | ||
deb2fec0 | 295 | bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) |
e9c4b1a2 JS |
296 | { |
297 | struct jpeg_compress_struct cinfo; | |
b59ff3c9 | 298 | struct my_error_mgr jerr; |
995612e2 | 299 | JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ |
e9c4b1a2 | 300 | JSAMPLE *image_buffer; |
995612e2 VZ |
301 | int stride; /* physical row width in image buffer */ |
302 | ||
b59ff3c9 VS |
303 | cinfo.err = jpeg_std_error(&jerr.pub); |
304 | jerr.pub.error_exit = my_error_exit; | |
305 | ||
deb2fec0 SB |
306 | if (!verbose) cinfo.err->output_message=NULL; |
307 | ||
b59ff3c9 | 308 | /* Establish the setjmp return context for my_error_exit to use. */ |
58c837a4 RR |
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; | |
b59ff3c9 VS |
318 | } |
319 | ||
e9c4b1a2 JS |
320 | jpeg_create_compress(&cinfo); |
321 | jpeg_wxio_dest(&cinfo, stream); | |
995612e2 | 322 | |
e9c4b1a2 JS |
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); | |
5e5437e0 JS |
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 | ||
e9c4b1a2 | 338 | jpeg_start_compress(&cinfo, TRUE); |
995612e2 VZ |
339 | |
340 | stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */ | |
e9c4b1a2 JS |
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); | |
995612e2 | 348 | |
e9c4b1a2 JS |
349 | return TRUE; |
350 | } | |
e9c4b1a2 | 351 | |
0828c087 | 352 | |
995612e2 | 353 | bool wxJPEGHandler::DoCanRead( wxInputStream& stream ) |
0828c087 VS |
354 | { |
355 | unsigned char hdr[2]; | |
995612e2 | 356 | |
0828c087 VS |
357 | stream.Read(&hdr, 2); |
358 | stream.SeekI(-2, wxFromCurrent); | |
359 | return (hdr[0] == 0xFF && hdr[1] == 0xD8); | |
360 | } | |
361 | ||
9ab6ee85 GRG |
362 | #endif // wxUSE_STREAMS |
363 | ||
364 | #endif // wxUSE_LIBJPEG | |
b59ff3c9 VS |
365 | |
366 | ||
367 | ||
368 | ||
369 | ||
370 |