]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagjpeg.cpp
ensure minimal mime support in wxHTML even without wxMimeTypesMananger
[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
1aaaa712
VS
10#ifdef __GNUG__
11#pragma implementation "imagjpeg.h"
12#endif
e9c4b1a2
JS
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
ce4169a4
RR
21#include "wx/defs.h"
22
c96ea657 23#if wxUSE_IMAGE && wxUSE_LIBJPEG
ce4169a4 24
1aaaa712 25#include "wx/imagjpeg.h"
e9c4b1a2
JS
26#include "wx/bitmap.h"
27#include "wx/debug.h"
28#include "wx/log.h"
29#include "wx/app.h"
cb69afe0
VS
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.
e7ca6139
VS
35#if defined(__WXMSW__) && (defined(__MWERKS__) || defined(__WATCOMC__))
36 #define HAVE_BOOLEAN
cb69afe0 37 #include <windows.h>
e7ca6139 38#endif
cb69afe0 39
995612e2
VZ
40extern "C"
41{
42 #include "jpeglib.h"
e9c4b1a2 43}
cb69afe0 44
e9c4b1a2
JS
45#include "wx/filefn.h"
46#include "wx/wfstream.h"
47#include "wx/intl.h"
48#include "wx/module.h"
49
50// For memcpy
51#include <string.h>
b59ff3c9
VS
52// For JPEG library error handling
53#include <setjmp.h>
e9c4b1a2
JS
54
55#ifdef __SALFORDC__
56#ifdef FAR
57#undef FAR
58#endif
59#endif
60
90350682
VZ
61// we can't use METHODDEF here as it includes static yet the functions must be
62// extern "C" and these can't be used together!
63#undef METHODDEF
8af27d1d 64#define METHODDEF(type) extern "C" type wxC_CALLING_CONV
90350682 65
e9c4b1a2
JS
66//-----------------------------------------------------------------------------
67// wxJPEGHandler
68//-----------------------------------------------------------------------------
69
e9c4b1a2 70IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
e9c4b1a2 71
9ab6ee85
GRG
72#if wxUSE_STREAMS
73
e9c4b1a2
JS
74//------------- JPEG Data Source Manager
75
1aaaa712
VS
76#define JPEG_IO_BUFFER_SIZE 2048
77
e9c4b1a2
JS
78typedef struct {
79 struct jpeg_source_mgr pub; /* public fields */
995612e2 80
e9c4b1a2 81 JOCTET* buffer; /* start of buffer */
1aaaa712 82 wxInputStream *stream;
e9c4b1a2
JS
83} my_source_mgr;
84
85typedef my_source_mgr * my_src_ptr;
86
74e3313b 87METHODDEF(void) my_init_source ( j_decompress_ptr WXUNUSED(cinfo) )
e9c4b1a2
JS
88{
89}
90
1aaaa712 91METHODDEF(boolean) my_fill_input_buffer ( j_decompress_ptr cinfo )
e9c4b1a2 92{
1aaaa712
VS
93 my_src_ptr src = (my_src_ptr) cinfo->src;
94
95 src->pub.next_input_byte = src->buffer;
96 src->pub.bytes_in_buffer = src->stream->Read(src->buffer, JPEG_IO_BUFFER_SIZE).LastRead();
97
98 if (src->pub.bytes_in_buffer == 0) // check for end-of-stream
99 {
100 // Insert a fake EOI marker
101 src->buffer[0] = 0xFF;
102 src->buffer[1] = JPEG_EOI;
103 src->pub.bytes_in_buffer = 2;
104 }
e9c4b1a2
JS
105 return TRUE;
106}
107
108METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
109{
1aaaa712
VS
110 if (num_bytes > 0)
111 {
112 my_src_ptr src = (my_src_ptr) cinfo->src;
113
33ac7e6f 114 while (num_bytes > (long)src->pub.bytes_in_buffer)
1aaaa712
VS
115 {
116 num_bytes -= (long) src->pub.bytes_in_buffer;
117 src->pub.fill_input_buffer(cinfo);
118 }
119 src->pub.next_input_byte += (size_t) num_bytes;
120 src->pub.bytes_in_buffer -= (size_t) num_bytes;
121 }
e9c4b1a2
JS
122}
123
124METHODDEF(void) my_term_source ( j_decompress_ptr cinfo )
125{
126 my_src_ptr src = (my_src_ptr) cinfo->src;
995612e2 127
1aaaa712 128 if (src->pub.bytes_in_buffer > 0)
3ca6a5f0 129 src->stream->SeekI(-(long)src->pub.bytes_in_buffer, wxFromCurrent);
1aaaa712 130 delete[] src->buffer;
e9c4b1a2
JS
131}
132
e9c4b1a2 133
b59ff3c9
VS
134// JPEG error manager:
135
136struct my_error_mgr {
995612e2 137 struct jpeg_error_mgr pub; /* "public" fields */
b59ff3c9 138
995612e2 139 jmp_buf setjmp_buffer; /* for return to caller */
b59ff3c9
VS
140};
141
142typedef struct my_error_mgr * my_error_ptr;
143
144/*
145 * Here's the routine that will replace the standard error_exit method:
146 */
147
90350682 148METHODDEF(void) my_error_exit (j_common_ptr cinfo)
b59ff3c9
VS
149{
150 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
151 my_error_ptr myerr = (my_error_ptr) cinfo->err;
152
153 /* Always display the message. */
154 /* We could postpone this until after returning, if we chose. */
deb2fec0 155 if (cinfo->err->output_message) (*cinfo->err->output_message) (cinfo);
b59ff3c9
VS
156
157 /* Return control to the setjmp point */
158 longjmp(myerr->setjmp_buffer, 1);
159}
160
90350682
VZ
161void jpeg_wxio_src( j_decompress_ptr cinfo, wxInputStream& infile )
162{
163 my_src_ptr src;
164
165 if (cinfo->src == NULL) { /* first time for this JPEG object? */
166 cinfo->src = (struct jpeg_source_mgr *)
167 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
168 sizeof(my_source_mgr));
169 src = (my_src_ptr) cinfo->src;
170 }
171 src = (my_src_ptr) cinfo->src;
172 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
173 src->buffer = new JOCTET[JPEG_IO_BUFFER_SIZE];
174 src->pub.next_input_byte = NULL; /* until buffer loaded */
175 src->stream = &infile;
176
177 src->pub.init_source = my_init_source;
178 src->pub.fill_input_buffer = my_fill_input_buffer;
179 src->pub.skip_input_data = my_skip_input_data;
180 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
181 src->pub.term_source = my_term_source;
182}
183
184
3ca6a5f0
BP
185// temporarily disable the warning C4611 (interaction between '_setjmp' and
186// C++ object destruction is non-portable) - I don't see any dtors here
187#ifdef __VISUALC__
188 #pragma warning(disable:4611)
189#endif /* VC++ */
e9c4b1a2 190
700ec454 191bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
e9c4b1a2
JS
192{
193 struct jpeg_decompress_struct cinfo;
b59ff3c9 194 struct my_error_mgr jerr;
e9c4b1a2
JS
195 JSAMPARRAY tempbuf;
196 unsigned char *ptr;
197 unsigned stride;
995612e2 198
e9c4b1a2 199 image->Destroy();
b59ff3c9
VS
200 cinfo.err = jpeg_std_error( &jerr.pub );
201 jerr.pub.error_exit = my_error_exit;
202
deb2fec0
SB
203 if (!verbose) cinfo.err->output_message=NULL;
204
b59ff3c9
VS
205 /* Establish the setjmp return context for my_error_exit to use. */
206 if (setjmp(jerr.setjmp_buffer)) {
207 /* If we get here, the JPEG code has signaled an error.
208 * We need to clean up the JPEG object, close the input file, and return.
209 */
33ac7e6f 210 if (verbose)
58c837a4 211 wxLogError(_("JPEG: Couldn't load - file is probably corrupted."));
192644bf 212 (cinfo.src->term_source)(&cinfo);
b59ff3c9
VS
213 jpeg_destroy_decompress(&cinfo);
214 if (image->Ok()) image->Destroy();
215 return FALSE;
216 }
217
e9c4b1a2
JS
218 jpeg_create_decompress( &cinfo );
219 jpeg_wxio_src( &cinfo, stream );
220 jpeg_read_header( &cinfo, TRUE );
221 cinfo.out_color_space = JCS_RGB;
222 jpeg_start_decompress( &cinfo );
995612e2 223
e9c4b1a2
JS
224 image->Create( cinfo.image_width, cinfo.image_height );
225 if (!image->Ok()) {
226 jpeg_finish_decompress( &cinfo );
227 jpeg_destroy_decompress( &cinfo );
228 return FALSE;
229 }
230 image->SetMask( FALSE );
231 ptr = image->GetData();
232 stride = cinfo.output_width * 3;
233 tempbuf = (*cinfo.mem->alloc_sarray)
234 ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 );
995612e2 235
e9c4b1a2
JS
236 while ( cinfo.output_scanline < cinfo.output_height ) {
237 jpeg_read_scanlines( &cinfo, tempbuf, 1 );
238 memcpy( ptr, tempbuf[0], stride );
239 ptr += stride;
240 }
241 jpeg_finish_decompress( &cinfo );
242 jpeg_destroy_decompress( &cinfo );
243 return TRUE;
244}
245
e9c4b1a2
JS
246typedef struct {
247 struct jpeg_destination_mgr pub;
995612e2 248
e9c4b1a2
JS
249 wxOutputStream *stream;
250 JOCTET * buffer;
251} my_destination_mgr;
252
253typedef my_destination_mgr * my_dest_ptr;
254
995612e2 255#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
e9c4b1a2
JS
256
257METHODDEF(void) init_destination (j_compress_ptr cinfo)
258{
259 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
995612e2 260
e9c4b1a2
JS
261 /* Allocate the output buffer --- it will be released when done with image */
262 dest->buffer = (JOCTET *)
263 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
264 OUTPUT_BUF_SIZE * sizeof(JOCTET));
265 dest->pub.next_output_byte = dest->buffer;
266 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
267}
268
269METHODDEF(boolean) empty_output_buffer (j_compress_ptr cinfo)
270{
271 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
995612e2 272
e9c4b1a2
JS
273 dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE);
274 dest->pub.next_output_byte = dest->buffer;
275 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
276 return TRUE;
277}
278
279METHODDEF(void) term_destination (j_compress_ptr cinfo)
280{
281 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
282 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
283 /* Write any data remaining in the buffer */
284 if (datacount > 0)
285 dest->stream->Write(dest->buffer, datacount);
286}
287
288GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
289{
290 my_dest_ptr dest;
995612e2
VZ
291
292 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
e9c4b1a2
JS
293 cinfo->dest = (struct jpeg_destination_mgr *)
294 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
295 sizeof(my_destination_mgr));
296 }
995612e2 297
e9c4b1a2
JS
298 dest = (my_dest_ptr) cinfo->dest;
299 dest->pub.init_destination = init_destination;
300 dest->pub.empty_output_buffer = empty_output_buffer;
301 dest->pub.term_destination = term_destination;
302 dest->stream = &outfile;
303}
304
deb2fec0 305bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2
JS
306{
307 struct jpeg_compress_struct cinfo;
b59ff3c9 308 struct my_error_mgr jerr;
995612e2 309 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
e9c4b1a2 310 JSAMPLE *image_buffer;
995612e2
VZ
311 int stride; /* physical row width in image buffer */
312
b59ff3c9
VS
313 cinfo.err = jpeg_std_error(&jerr.pub);
314 jerr.pub.error_exit = my_error_exit;
315
deb2fec0
SB
316 if (!verbose) cinfo.err->output_message=NULL;
317
b59ff3c9 318 /* Establish the setjmp return context for my_error_exit to use. */
33ac7e6f 319 if (setjmp(jerr.setjmp_buffer))
58c837a4
RR
320 {
321 /* If we get here, the JPEG code has signaled an error.
322 * We need to clean up the JPEG object, close the input file, and return.
323 */
33ac7e6f 324 if (verbose)
58c837a4
RR
325 wxLogError(_("JPEG: Couldn't save image."));
326 jpeg_destroy_compress(&cinfo);
327 return FALSE;
b59ff3c9
VS
328 }
329
e9c4b1a2
JS
330 jpeg_create_compress(&cinfo);
331 jpeg_wxio_dest(&cinfo, stream);
995612e2 332
e9c4b1a2
JS
333 cinfo.image_width = image->GetWidth();
334 cinfo.image_height = image->GetHeight();
335 cinfo.input_components = 3;
336 cinfo.in_color_space = JCS_RGB;
337 jpeg_set_defaults(&cinfo);
5e5437e0
JS
338
339 // TODO: 3rd parameter is force_baseline, what value should this be?
340 // Code says: "If force_baseline is TRUE, the computed quantization table entries
341 // are limited to 1..255 for JPEG baseline compatibility."
342 // 'Quality' is a number between 0 (terrible) and 100 (very good).
343 // The default (in jcparam.c, jpeg_set_defaults) is 75,
344 // and force_baseline is TRUE.
345 if (image->HasOption(wxT("quality")))
346 jpeg_set_quality(&cinfo, image->GetOptionInt(wxT("quality")), TRUE);
347
e9c4b1a2 348 jpeg_start_compress(&cinfo, TRUE);
995612e2
VZ
349
350 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
e9c4b1a2
JS
351 image_buffer = image->GetData();
352 while (cinfo.next_scanline < cinfo.image_height) {
353 row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
354 jpeg_write_scanlines( &cinfo, row_pointer, 1 );
355 }
356 jpeg_finish_compress(&cinfo);
357 jpeg_destroy_compress(&cinfo);
995612e2 358
e9c4b1a2
JS
359 return TRUE;
360}
e9c4b1a2 361
3ca6a5f0
BP
362#ifdef __VISUALC__
363 #pragma warning(default:4611)
364#endif /* VC++ */
0828c087 365
995612e2 366bool wxJPEGHandler::DoCanRead( wxInputStream& stream )
0828c087
VS
367{
368 unsigned char hdr[2];
995612e2 369
33ac7e6f 370 stream.Read(hdr, 2);
0828c087
VS
371 stream.SeekI(-2, wxFromCurrent);
372 return (hdr[0] == 0xFF && hdr[1] == 0xD8);
373}
374
9ab6ee85
GRG
375#endif // wxUSE_STREAMS
376
377#endif // wxUSE_LIBJPEG
b59ff3c9
VS
378
379
380
381
382
383