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