]>
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 | ||
c972e125 VZ |
10 | /* |
11 | We don't put pragma implement in this file because it is already present in | |
12 | src/common/image.cpp | |
c972e125 | 13 | */ |
e9c4b1a2 JS |
14 | |
15 | // For compilers that support precompilation, includes "wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #ifdef __BORLANDC__ | |
19 | #pragma hdrstop | |
20 | #endif | |
21 | ||
ce4169a4 RR |
22 | #include "wx/defs.h" |
23 | ||
9ab6ee85 | 24 | #if wxUSE_LIBJPEG |
ce4169a4 | 25 | |
e9c4b1a2 JS |
26 | #include "wx/image.h" |
27 | #include "wx/bitmap.h" | |
28 | #include "wx/debug.h" | |
29 | #include "wx/log.h" | |
30 | #include "wx/app.h" | |
995612e2 VZ |
31 | extern "C" |
32 | { | |
33 | #include "jpeglib.h" | |
e9c4b1a2 | 34 | } |
e9c4b1a2 JS |
35 | #include "wx/filefn.h" |
36 | #include "wx/wfstream.h" | |
37 | #include "wx/intl.h" | |
38 | #include "wx/module.h" | |
39 | ||
40 | // For memcpy | |
41 | #include <string.h> | |
b59ff3c9 VS |
42 | // For JPEG library error handling |
43 | #include <setjmp.h> | |
e9c4b1a2 JS |
44 | |
45 | #ifdef __SALFORDC__ | |
46 | #ifdef FAR | |
47 | #undef FAR | |
48 | #endif | |
49 | #endif | |
50 | ||
51 | #ifdef __WXMSW__ | |
52 | #include <windows.h> | |
53 | #endif | |
54 | ||
55 | //----------------------------------------------------------------------------- | |
56 | // wxJPEGHandler | |
57 | //----------------------------------------------------------------------------- | |
58 | ||
e9c4b1a2 JS |
59 | #if !USE_SHARED_LIBRARIES |
60 | IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler) | |
61 | #endif | |
62 | ||
9ab6ee85 GRG |
63 | #if wxUSE_STREAMS |
64 | ||
e9c4b1a2 JS |
65 | //------------- JPEG Data Source Manager |
66 | ||
67 | typedef struct { | |
68 | struct jpeg_source_mgr pub; /* public fields */ | |
995612e2 | 69 | |
e9c4b1a2 JS |
70 | JOCTET* buffer; /* start of buffer */ |
71 | } my_source_mgr; | |
72 | ||
73 | typedef my_source_mgr * my_src_ptr; | |
74 | ||
74e3313b | 75 | METHODDEF(void) my_init_source ( j_decompress_ptr WXUNUSED(cinfo) ) |
e9c4b1a2 JS |
76 | { |
77 | } | |
78 | ||
74e3313b | 79 | METHODDEF(boolean) my_fill_input_buffer ( j_decompress_ptr WXUNUSED(cinfo) ) |
e9c4b1a2 JS |
80 | { |
81 | return TRUE; | |
82 | } | |
83 | ||
84 | METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo, long num_bytes ) | |
85 | { | |
86 | my_src_ptr src = (my_src_ptr) cinfo->src; | |
995612e2 | 87 | |
e9c4b1a2 JS |
88 | src->pub.next_input_byte += (size_t) num_bytes; |
89 | src->pub.bytes_in_buffer -= (size_t) num_bytes; | |
90 | } | |
91 | ||
92 | METHODDEF(void) my_term_source ( j_decompress_ptr cinfo ) | |
93 | { | |
94 | my_src_ptr src = (my_src_ptr) cinfo->src; | |
995612e2 | 95 | |
e9c4b1a2 JS |
96 | free (src->buffer); |
97 | } | |
98 | ||
99 | void jpeg_wxio_src( j_decompress_ptr cinfo, wxInputStream& infile ) | |
100 | { | |
101 | my_src_ptr src; | |
995612e2 VZ |
102 | |
103 | if (cinfo->src == NULL) { /* first time for this JPEG object? */ | |
e9c4b1a2 JS |
104 | cinfo->src = (struct jpeg_source_mgr *) |
105 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
106 | sizeof(my_source_mgr)); | |
107 | src = (my_src_ptr) cinfo->src; | |
108 | } | |
109 | src = (my_src_ptr) cinfo->src; | |
cd25b18c RR |
110 | src->pub.bytes_in_buffer = infile.GetSize(); /* forces fill_input_buffer on first read */ |
111 | src->buffer = (JOCTET *) malloc (infile.GetSize()); | |
e9c4b1a2 | 112 | src->pub.next_input_byte = src->buffer; /* until buffer loaded */ |
cd25b18c | 113 | infile.Read(src->buffer, infile.GetSize()); |
995612e2 | 114 | |
e9c4b1a2 JS |
115 | src->pub.init_source = my_init_source; |
116 | src->pub.fill_input_buffer = my_fill_input_buffer; | |
117 | src->pub.skip_input_data = my_skip_input_data; | |
118 | src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ | |
119 | src->pub.term_source = my_term_source; | |
120 | } | |
121 | ||
122 | ||
b59ff3c9 VS |
123 | // JPEG error manager: |
124 | ||
125 | struct my_error_mgr { | |
995612e2 | 126 | struct jpeg_error_mgr pub; /* "public" fields */ |
b59ff3c9 | 127 | |
995612e2 | 128 | jmp_buf setjmp_buffer; /* for return to caller */ |
b59ff3c9 VS |
129 | }; |
130 | ||
131 | typedef struct my_error_mgr * my_error_ptr; | |
132 | ||
133 | /* | |
134 | * Here's the routine that will replace the standard error_exit method: | |
135 | */ | |
136 | ||
137 | METHODDEF(void) | |
138 | my_error_exit (j_common_ptr cinfo) | |
139 | { | |
140 | /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */ | |
141 | my_error_ptr myerr = (my_error_ptr) cinfo->err; | |
142 | ||
143 | /* Always display the message. */ | |
144 | /* We could postpone this until after returning, if we chose. */ | |
deb2fec0 | 145 | if (cinfo->err->output_message) (*cinfo->err->output_message) (cinfo); |
b59ff3c9 VS |
146 | |
147 | /* Return control to the setjmp point */ | |
148 | longjmp(myerr->setjmp_buffer, 1); | |
149 | } | |
150 | ||
151 | ||
e9c4b1a2 | 152 | |
700ec454 | 153 | bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) ) |
e9c4b1a2 JS |
154 | { |
155 | struct jpeg_decompress_struct cinfo; | |
b59ff3c9 | 156 | struct my_error_mgr jerr; |
e9c4b1a2 JS |
157 | JSAMPARRAY tempbuf; |
158 | unsigned char *ptr; | |
159 | unsigned stride; | |
995612e2 | 160 | |
e9c4b1a2 | 161 | image->Destroy(); |
b59ff3c9 VS |
162 | cinfo.err = jpeg_std_error( &jerr.pub ); |
163 | jerr.pub.error_exit = my_error_exit; | |
164 | ||
deb2fec0 SB |
165 | if (!verbose) cinfo.err->output_message=NULL; |
166 | ||
b59ff3c9 VS |
167 | /* Establish the setjmp return context for my_error_exit to use. */ |
168 | if (setjmp(jerr.setjmp_buffer)) { | |
169 | /* If we get here, the JPEG code has signaled an error. | |
170 | * We need to clean up the JPEG object, close the input file, and return. | |
171 | */ | |
58c837a4 RR |
172 | if (verbose) |
173 | wxLogError(_("JPEG: Couldn't load - file is probably corrupted.")); | |
b59ff3c9 VS |
174 | jpeg_destroy_decompress(&cinfo); |
175 | if (image->Ok()) image->Destroy(); | |
176 | return FALSE; | |
177 | } | |
178 | ||
e9c4b1a2 JS |
179 | jpeg_create_decompress( &cinfo ); |
180 | jpeg_wxio_src( &cinfo, stream ); | |
181 | jpeg_read_header( &cinfo, TRUE ); | |
182 | cinfo.out_color_space = JCS_RGB; | |
183 | jpeg_start_decompress( &cinfo ); | |
995612e2 | 184 | |
e9c4b1a2 JS |
185 | image->Create( cinfo.image_width, cinfo.image_height ); |
186 | if (!image->Ok()) { | |
187 | jpeg_finish_decompress( &cinfo ); | |
188 | jpeg_destroy_decompress( &cinfo ); | |
189 | return FALSE; | |
190 | } | |
191 | image->SetMask( FALSE ); | |
192 | ptr = image->GetData(); | |
193 | stride = cinfo.output_width * 3; | |
194 | tempbuf = (*cinfo.mem->alloc_sarray) | |
195 | ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 ); | |
995612e2 | 196 | |
e9c4b1a2 JS |
197 | while ( cinfo.output_scanline < cinfo.output_height ) { |
198 | jpeg_read_scanlines( &cinfo, tempbuf, 1 ); | |
199 | memcpy( ptr, tempbuf[0], stride ); | |
200 | ptr += stride; | |
201 | } | |
202 | jpeg_finish_decompress( &cinfo ); | |
203 | jpeg_destroy_decompress( &cinfo ); | |
204 | return TRUE; | |
205 | } | |
206 | ||
207 | ||
208 | ||
209 | ||
210 | ||
211 | typedef struct { | |
212 | struct jpeg_destination_mgr pub; | |
995612e2 | 213 | |
e9c4b1a2 JS |
214 | wxOutputStream *stream; |
215 | JOCTET * buffer; | |
216 | } my_destination_mgr; | |
217 | ||
218 | typedef my_destination_mgr * my_dest_ptr; | |
219 | ||
995612e2 | 220 | #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ |
e9c4b1a2 JS |
221 | |
222 | METHODDEF(void) init_destination (j_compress_ptr cinfo) | |
223 | { | |
224 | my_dest_ptr dest = (my_dest_ptr) cinfo->dest; | |
995612e2 | 225 | |
e9c4b1a2 JS |
226 | /* Allocate the output buffer --- it will be released when done with image */ |
227 | dest->buffer = (JOCTET *) | |
228 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | |
229 | OUTPUT_BUF_SIZE * sizeof(JOCTET)); | |
230 | dest->pub.next_output_byte = dest->buffer; | |
231 | dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; | |
232 | } | |
233 | ||
234 | METHODDEF(boolean) empty_output_buffer (j_compress_ptr cinfo) | |
235 | { | |
236 | my_dest_ptr dest = (my_dest_ptr) cinfo->dest; | |
995612e2 | 237 | |
e9c4b1a2 JS |
238 | dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE); |
239 | dest->pub.next_output_byte = dest->buffer; | |
240 | dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; | |
241 | return TRUE; | |
242 | } | |
243 | ||
244 | METHODDEF(void) term_destination (j_compress_ptr cinfo) | |
245 | { | |
246 | my_dest_ptr dest = (my_dest_ptr) cinfo->dest; | |
247 | size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer; | |
248 | /* Write any data remaining in the buffer */ | |
249 | if (datacount > 0) | |
250 | dest->stream->Write(dest->buffer, datacount); | |
251 | } | |
252 | ||
253 | GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo, wxOutputStream& outfile) | |
254 | { | |
255 | my_dest_ptr dest; | |
995612e2 VZ |
256 | |
257 | if (cinfo->dest == NULL) { /* first time for this JPEG object? */ | |
e9c4b1a2 JS |
258 | cinfo->dest = (struct jpeg_destination_mgr *) |
259 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
260 | sizeof(my_destination_mgr)); | |
261 | } | |
995612e2 | 262 | |
e9c4b1a2 JS |
263 | dest = (my_dest_ptr) cinfo->dest; |
264 | dest->pub.init_destination = init_destination; | |
265 | dest->pub.empty_output_buffer = empty_output_buffer; | |
266 | dest->pub.term_destination = term_destination; | |
267 | dest->stream = &outfile; | |
268 | } | |
269 | ||
deb2fec0 | 270 | bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) |
e9c4b1a2 JS |
271 | { |
272 | struct jpeg_compress_struct cinfo; | |
b59ff3c9 | 273 | struct my_error_mgr jerr; |
995612e2 | 274 | JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ |
e9c4b1a2 | 275 | JSAMPLE *image_buffer; |
995612e2 VZ |
276 | int stride; /* physical row width in image buffer */ |
277 | ||
b59ff3c9 VS |
278 | cinfo.err = jpeg_std_error(&jerr.pub); |
279 | jerr.pub.error_exit = my_error_exit; | |
280 | ||
deb2fec0 SB |
281 | if (!verbose) cinfo.err->output_message=NULL; |
282 | ||
b59ff3c9 | 283 | /* Establish the setjmp return context for my_error_exit to use. */ |
58c837a4 RR |
284 | if (setjmp(jerr.setjmp_buffer)) |
285 | { | |
286 | /* If we get here, the JPEG code has signaled an error. | |
287 | * We need to clean up the JPEG object, close the input file, and return. | |
288 | */ | |
289 | if (verbose) | |
290 | wxLogError(_("JPEG: Couldn't save image.")); | |
291 | jpeg_destroy_compress(&cinfo); | |
292 | return FALSE; | |
b59ff3c9 VS |
293 | } |
294 | ||
e9c4b1a2 JS |
295 | jpeg_create_compress(&cinfo); |
296 | jpeg_wxio_dest(&cinfo, stream); | |
995612e2 | 297 | |
e9c4b1a2 JS |
298 | cinfo.image_width = image->GetWidth(); |
299 | cinfo.image_height = image->GetHeight(); | |
300 | cinfo.input_components = 3; | |
301 | cinfo.in_color_space = JCS_RGB; | |
302 | jpeg_set_defaults(&cinfo); | |
303 | jpeg_start_compress(&cinfo, TRUE); | |
995612e2 VZ |
304 | |
305 | stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */ | |
e9c4b1a2 JS |
306 | image_buffer = image->GetData(); |
307 | while (cinfo.next_scanline < cinfo.image_height) { | |
308 | row_pointer[0] = &image_buffer[cinfo.next_scanline * stride]; | |
309 | jpeg_write_scanlines( &cinfo, row_pointer, 1 ); | |
310 | } | |
311 | jpeg_finish_compress(&cinfo); | |
312 | jpeg_destroy_compress(&cinfo); | |
995612e2 | 313 | |
e9c4b1a2 JS |
314 | return TRUE; |
315 | } | |
e9c4b1a2 | 316 | |
0828c087 | 317 | |
995612e2 | 318 | bool wxJPEGHandler::DoCanRead( wxInputStream& stream ) |
0828c087 VS |
319 | { |
320 | unsigned char hdr[2]; | |
995612e2 | 321 | |
0828c087 VS |
322 | stream.Read(&hdr, 2); |
323 | stream.SeekI(-2, wxFromCurrent); | |
324 | return (hdr[0] == 0xFF && hdr[1] == 0xD8); | |
325 | } | |
326 | ||
9ab6ee85 GRG |
327 | #endif // wxUSE_STREAMS |
328 | ||
329 | #endif // wxUSE_LIBJPEG | |
b59ff3c9 VS |
330 | |
331 | ||
332 | ||
333 | ||
334 | ||
335 |