]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
Unicode fix, it seems
[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
24#if wxUSE_LIBJPEG
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
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;
109 src->pub.bytes_in_buffer = infile.StreamSize(); /* forces fill_input_buffer on first read */
110 src->buffer = (JOCTET *) malloc (infile.StreamSize());
111 src->pub.next_input_byte = src->buffer; /* until buffer loaded */
112 infile.Read(src->buffer, infile.StreamSize());
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. */
144 (*cinfo->err->output_message) (cinfo);
145
146 /* Return control to the setjmp point */
147 longjmp(myerr->setjmp_buffer, 1);
148}
149
150
e9c4b1a2
JS
151
152bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream )
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
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 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
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
266bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream )
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
277 /* Establish the setjmp return context for my_error_exit to use. */
278 if (setjmp(jerr.setjmp_buffer)) {
279 /* If we get here, the JPEG code has signaled an error.
280 * We need to clean up the JPEG object, close the input file, and return.
281 */
282 wxLogError(_("Couldn't save a JPEG image - probably file is corrupted."));
283 jpeg_destroy_compress(&cinfo);
284 return FALSE;
285 }
286
e9c4b1a2
JS
287 jpeg_create_compress(&cinfo);
288 jpeg_wxio_dest(&cinfo, stream);
289
290 cinfo.image_width = image->GetWidth();
291 cinfo.image_height = image->GetHeight();
292 cinfo.input_components = 3;
293 cinfo.in_color_space = JCS_RGB;
294 jpeg_set_defaults(&cinfo);
295 jpeg_start_compress(&cinfo, TRUE);
296
297 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
298 image_buffer = image->GetData();
299 while (cinfo.next_scanline < cinfo.image_height) {
300 row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
301 jpeg_write_scanlines( &cinfo, row_pointer, 1 );
302 }
303 jpeg_finish_compress(&cinfo);
304 jpeg_destroy_compress(&cinfo);
305
306 return TRUE;
307}
e9c4b1a2 308
ce4169a4
RR
309#endif
310 // wxUSE_STREAMS
e9c4b1a2 311
ce4169a4
RR
312#endif
313 // wxUSE_LIBJPEG
b59ff3c9
VS
314
315
316
317
318
319