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