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