compilation fix for Watcom
[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
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
44 extern "C"
45 {
46 #include "jpeglib.h"
47 }
48
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>
56 // For JPEG library error handling
57 #include <setjmp.h>
58
59 #ifdef __SALFORDC__
60 #ifdef FAR
61 #undef FAR
62 #endif
63 #endif
64
65 //-----------------------------------------------------------------------------
66 // wxJPEGHandler
67 //-----------------------------------------------------------------------------
68
69 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
70
71 #if wxUSE_STREAMS
72
73 //------------- JPEG Data Source Manager
74
75 #define JPEG_IO_BUFFER_SIZE 2048
76
77 typedef struct {
78 struct jpeg_source_mgr pub; /* public fields */
79
80 JOCTET* buffer; /* start of buffer */
81 wxInputStream *stream;
82 } my_source_mgr;
83
84 typedef my_source_mgr * my_src_ptr;
85
86 METHODDEF(void) my_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
87 {
88 }
89
90 METHODDEF(boolean) my_fill_input_buffer ( j_decompress_ptr cinfo )
91 {
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 }
104 return TRUE;
105 }
106
107 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
108 {
109 if (num_bytes > 0)
110 {
111 my_src_ptr src = (my_src_ptr) cinfo->src;
112
113 while (num_bytes > (long)src->pub.bytes_in_buffer)
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 }
121 }
122
123 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo )
124 {
125 my_src_ptr src = (my_src_ptr) cinfo->src;
126
127 if (src->pub.bytes_in_buffer > 0)
128 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
129 delete[] src->buffer;
130 }
131
132 void jpeg_wxio_src( j_decompress_ptr cinfo, wxInputStream& infile )
133 {
134 my_src_ptr src;
135
136 if (cinfo->src == NULL) { /* first time for this JPEG object? */
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;
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;
147
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
156 // JPEG error manager:
157
158 struct my_error_mgr {
159 struct jpeg_error_mgr pub; /* "public" fields */
160
161 jmp_buf setjmp_buffer; /* for return to caller */
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. */
178 if (cinfo->err->output_message) (*cinfo->err->output_message) (cinfo);
179
180 /* Return control to the setjmp point */
181 longjmp(myerr->setjmp_buffer, 1);
182 }
183
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++ */
189
190 bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
191 {
192 struct jpeg_decompress_struct cinfo;
193 struct my_error_mgr jerr;
194 JSAMPARRAY tempbuf;
195 unsigned char *ptr;
196 unsigned stride;
197
198 image->Destroy();
199 cinfo.err = jpeg_std_error( &jerr.pub );
200 jerr.pub.error_exit = my_error_exit;
201
202 if (!verbose) cinfo.err->output_message=NULL;
203
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 */
209 if (verbose)
210 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
211 (cinfo.src->term_source)(&cinfo);
212 jpeg_destroy_decompress(&cinfo);
213 if (image->Ok()) image->Destroy();
214 return FALSE;
215 }
216
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 );
222
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 );
234
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
245 typedef struct {
246 struct jpeg_destination_mgr pub;
247
248 wxOutputStream *stream;
249 JOCTET * buffer;
250 } my_destination_mgr;
251
252 typedef my_destination_mgr * my_dest_ptr;
253
254 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
255
256 METHODDEF(void) init_destination (j_compress_ptr cinfo)
257 {
258 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
259
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;
271
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;
290
291 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
292 cinfo->dest = (struct jpeg_destination_mgr *)
293 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
294 sizeof(my_destination_mgr));
295 }
296
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
304 bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
305 {
306 struct jpeg_compress_struct cinfo;
307 struct my_error_mgr jerr;
308 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
309 JSAMPLE *image_buffer;
310 int stride; /* physical row width in image buffer */
311
312 cinfo.err = jpeg_std_error(&jerr.pub);
313 jerr.pub.error_exit = my_error_exit;
314
315 if (!verbose) cinfo.err->output_message=NULL;
316
317 /* Establish the setjmp return context for my_error_exit to use. */
318 if (setjmp(jerr.setjmp_buffer))
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 */
323 if (verbose)
324 wxLogError(_("JPEG: Couldn't save image."));
325 jpeg_destroy_compress(&cinfo);
326 return FALSE;
327 }
328
329 jpeg_create_compress(&cinfo);
330 jpeg_wxio_dest(&cinfo, stream);
331
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);
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
347 jpeg_start_compress(&cinfo, TRUE);
348
349 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
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);
357
358 return TRUE;
359 }
360
361 #ifdef __VISUALC__
362 #pragma warning(default:4611)
363 #endif /* VC++ */
364
365 bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
366 {
367 unsigned char hdr[2];
368
369 stream.Read(hdr, 2);
370 stream.SeekI(-2, wxFromCurrent);
371 return (hdr[0] == 0xFF && hdr[1] == 0xD8);
372 }
373
374 #endif // wxUSE_STREAMS
375
376 #endif // wxUSE_LIBJPEG
377
378
379
380
381
382