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