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