]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
small changes here and there
[wxWidgets.git] / src / common / imagjpeg.cpp
CommitLineData
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
b9b32d5c 24#if wxUSE_STREAMS && 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"
ce4169a4
RR
31extern "C" {
32#include "jpeglib.h"
e9c4b1a2 33}
e9c4b1a2
JS
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>
b59ff3c9
VS
41// For JPEG library error handling
42#include <setjmp.h>
e9c4b1a2
JS
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
e9c4b1a2
JS
58#if !USE_SHARED_LIBRARIES
59IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
60#endif
61
e9c4b1a2
JS
62//------------- JPEG Data Source Manager
63
64typedef struct {
65 struct jpeg_source_mgr pub; /* public fields */
66
67 JOCTET* buffer; /* start of buffer */
68} my_source_mgr;
69
70typedef my_source_mgr * my_src_ptr;
71
74e3313b 72METHODDEF(void) my_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
e9c4b1a2
JS
73{
74}
75
74e3313b 76METHODDEF(boolean) my_fill_input_buffer ( j_decompress_ptr WXUNUSED(cinfo) )
e9c4b1a2
JS
77{
78 return TRUE;
79}
80
81METHODDEF(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
89METHODDEF(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
96void 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;
cd25b18c
RR
107 src->pub.bytes_in_buffer = infile.GetSize(); /* forces fill_input_buffer on first read */
108 src->buffer = (JOCTET *) malloc (infile.GetSize());
e9c4b1a2 109 src->pub.next_input_byte = src->buffer; /* until buffer loaded */
cd25b18c 110 infile.Read(src->buffer, infile.GetSize());
e9c4b1a2
JS
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
b59ff3c9
VS
120// JPEG error manager:
121
122struct my_error_mgr {
123 struct jpeg_error_mgr pub; /* "public" fields */
124
125 jmp_buf setjmp_buffer; /* for return to caller */
126};
127
128typedef struct my_error_mgr * my_error_ptr;
129
130/*
131 * Here's the routine that will replace the standard error_exit method:
132 */
133
134METHODDEF(void)
135my_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. */
deb2fec0 142 if (cinfo->err->output_message) (*cinfo->err->output_message) (cinfo);
b59ff3c9
VS
143
144 /* Return control to the setjmp point */
145 longjmp(myerr->setjmp_buffer, 1);
146}
147
148
e9c4b1a2 149
deb2fec0 150bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose )
e9c4b1a2
JS
151{
152 struct jpeg_decompress_struct cinfo;
b59ff3c9 153 struct my_error_mgr jerr;
e9c4b1a2
JS
154 JSAMPARRAY tempbuf;
155 unsigned char *ptr;
156 unsigned stride;
157
158 image->Destroy();
b59ff3c9
VS
159 cinfo.err = jpeg_std_error( &jerr.pub );
160 jerr.pub.error_exit = my_error_exit;
161
deb2fec0
SB
162 if (!verbose) cinfo.err->output_message=NULL;
163
b59ff3c9
VS
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 */
deb2fec0 169 if (verbose) wxLogError(_("Couldn't load a JPEG image - probably file is corrupted."));
b59ff3c9
VS
170 jpeg_destroy_decompress(&cinfo);
171 if (image->Ok()) image->Destroy();
172 return FALSE;
173 }
174
e9c4b1a2
JS
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
207typedef struct {
208 struct jpeg_destination_mgr pub;
209
210 wxOutputStream *stream;
211 JOCTET * buffer;
212} my_destination_mgr;
213
214typedef my_destination_mgr * my_dest_ptr;
215
216#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
217
218METHODDEF(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
230METHODDEF(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
240METHODDEF(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
249GLOBAL(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
deb2fec0 266bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2
JS
267{
268 struct jpeg_compress_struct cinfo;
b59ff3c9 269 struct my_error_mgr jerr;
e9c4b1a2
JS
270 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
271 JSAMPLE *image_buffer;
272 int stride; /* physical row width in image buffer */
273
b59ff3c9
VS
274 cinfo.err = jpeg_std_error(&jerr.pub);
275 jerr.pub.error_exit = my_error_exit;
276
deb2fec0
SB
277 if (!verbose) cinfo.err->output_message=NULL;
278
b59ff3c9
VS
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 */
deb2fec0 284 if (verbose) wxLogError(_("Couldn't save a JPEG image - probably file is corrupted."));
b59ff3c9
VS
285 jpeg_destroy_compress(&cinfo);
286 return FALSE;
287 }
288
e9c4b1a2
JS
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}
e9c4b1a2 310
0828c087
VS
311
312bool 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
b9b32d5c 321#endif // wxUSE_STREAMS && wxUSE_LIBJPEG
b59ff3c9
VS
322
323
324
325
326
327