]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagjpeg.cpp
I seem to have fixed the DeleteSubGroups() bug - PLEASE CHECK!
[wxWidgets.git] / src / common / imagjpeg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: imagjpeg.cpp
3 // Purpose: wxImage JPEG handler
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 /*
11 We don't put pragma implement in this file because it is already present in
12 src/common/image.cpp
13
14 #ifdef __GNUG__
15 #pragma implementation "image.h"
16 #endif
17 */
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
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"
31 #if wxUSE_LIBJPEG
32 extern "C" {
33 #include <jpeglib.h>
34 }
35 #endif
36 #include "wx/filefn.h"
37 #include "wx/wfstream.h"
38 #include "wx/intl.h"
39 #include "wx/module.h"
40
41 // For memcpy
42 #include <string.h>
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
58 #if wxUSE_LIBJPEG
59
60 #if !USE_SHARED_LIBRARIES
61 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
62 #endif
63
64 #if wxUSE_STREAMS
65
66
67 //------------- JPEG Data Source Manager
68
69 typedef struct {
70 struct jpeg_source_mgr pub; /* public fields */
71
72 JOCTET* buffer; /* start of buffer */
73 } my_source_mgr;
74
75 typedef my_source_mgr * my_src_ptr;
76
77 METHODDEF(void) my_init_source ( j_decompress_ptr cinfo )
78 {
79 }
80
81 METHODDEF(boolean) my_fill_input_buffer ( j_decompress_ptr cinfo )
82 {
83 return TRUE;
84 }
85
86 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
87 {
88 my_src_ptr src = (my_src_ptr) cinfo->src;
89
90 src->pub.next_input_byte += (size_t) num_bytes;
91 src->pub.bytes_in_buffer -= (size_t) num_bytes;
92 }
93
94 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo )
95 {
96 my_src_ptr src = (my_src_ptr) cinfo->src;
97
98 free (src->buffer);
99 }
100
101 void jpeg_wxio_src( j_decompress_ptr cinfo, wxInputStream& infile )
102 {
103 my_src_ptr src;
104
105 if (cinfo->src == NULL) { /* first time for this JPEG object? */
106 cinfo->src = (struct jpeg_source_mgr *)
107 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
108 sizeof(my_source_mgr));
109 src = (my_src_ptr) cinfo->src;
110 }
111 src = (my_src_ptr) cinfo->src;
112 src->pub.bytes_in_buffer = infile.StreamSize(); /* forces fill_input_buffer on first read */
113 src->buffer = (JOCTET *) malloc (infile.StreamSize());
114 src->pub.next_input_byte = src->buffer; /* until buffer loaded */
115 infile.Read(src->buffer, infile.StreamSize());
116
117 src->pub.init_source = my_init_source;
118 src->pub.fill_input_buffer = my_fill_input_buffer;
119 src->pub.skip_input_data = my_skip_input_data;
120 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
121 src->pub.term_source = my_term_source;
122 }
123
124
125
126 bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream )
127 {
128 struct jpeg_decompress_struct cinfo;
129 struct jpeg_error_mgr jerr;
130 JSAMPARRAY tempbuf;
131 unsigned char *ptr;
132 unsigned stride;
133
134 image->Destroy();
135 cinfo.err = jpeg_std_error( &jerr );
136 jpeg_create_decompress( &cinfo );
137 jpeg_wxio_src( &cinfo, stream );
138 jpeg_read_header( &cinfo, TRUE );
139 cinfo.out_color_space = JCS_RGB;
140 jpeg_start_decompress( &cinfo );
141
142 image->Create( cinfo.image_width, cinfo.image_height );
143 if (!image->Ok()) {
144 jpeg_finish_decompress( &cinfo );
145 jpeg_destroy_decompress( &cinfo );
146 return FALSE;
147 }
148 image->SetMask( FALSE );
149 ptr = image->GetData();
150 stride = cinfo.output_width * 3;
151 tempbuf = (*cinfo.mem->alloc_sarray)
152 ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 );
153
154 while ( cinfo.output_scanline < cinfo.output_height ) {
155 jpeg_read_scanlines( &cinfo, tempbuf, 1 );
156 memcpy( ptr, tempbuf[0], stride );
157 ptr += stride;
158 }
159 jpeg_finish_decompress( &cinfo );
160 jpeg_destroy_decompress( &cinfo );
161 return TRUE;
162 }
163
164
165
166
167
168 typedef struct {
169 struct jpeg_destination_mgr pub;
170
171 wxOutputStream *stream;
172 JOCTET * buffer;
173 } my_destination_mgr;
174
175 typedef my_destination_mgr * my_dest_ptr;
176
177 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
178
179 METHODDEF(void) init_destination (j_compress_ptr cinfo)
180 {
181 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
182
183 /* Allocate the output buffer --- it will be released when done with image */
184 dest->buffer = (JOCTET *)
185 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
186 OUTPUT_BUF_SIZE * sizeof(JOCTET));
187 dest->pub.next_output_byte = dest->buffer;
188 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
189 }
190
191 METHODDEF(boolean) empty_output_buffer (j_compress_ptr cinfo)
192 {
193 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
194
195 dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE);
196 dest->pub.next_output_byte = dest->buffer;
197 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
198 return TRUE;
199 }
200
201 METHODDEF(void) term_destination (j_compress_ptr cinfo)
202 {
203 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
204 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
205 /* Write any data remaining in the buffer */
206 if (datacount > 0)
207 dest->stream->Write(dest->buffer, datacount);
208 }
209
210 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
211 {
212 my_dest_ptr dest;
213
214 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
215 cinfo->dest = (struct jpeg_destination_mgr *)
216 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
217 sizeof(my_destination_mgr));
218 }
219
220 dest = (my_dest_ptr) cinfo->dest;
221 dest->pub.init_destination = init_destination;
222 dest->pub.empty_output_buffer = empty_output_buffer;
223 dest->pub.term_destination = term_destination;
224 dest->stream = &outfile;
225 }
226
227 bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream )
228 {
229 struct jpeg_compress_struct cinfo;
230 struct jpeg_error_mgr jerr;
231 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
232 JSAMPLE *image_buffer;
233 int stride; /* physical row width in image buffer */
234
235 cinfo.err = jpeg_std_error(&jerr);
236 jpeg_create_compress(&cinfo);
237 jpeg_wxio_dest(&cinfo, stream);
238
239 cinfo.image_width = image->GetWidth();
240 cinfo.image_height = image->GetHeight();
241 cinfo.input_components = 3;
242 cinfo.in_color_space = JCS_RGB;
243 jpeg_set_defaults(&cinfo);
244 jpeg_start_compress(&cinfo, TRUE);
245
246 stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
247 image_buffer = image->GetData();
248 while (cinfo.next_scanline < cinfo.image_height) {
249 row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
250 jpeg_write_scanlines( &cinfo, row_pointer, 1 );
251 }
252 jpeg_finish_compress(&cinfo);
253 jpeg_destroy_compress(&cinfo);
254
255 return TRUE;
256 }
257 #endif // wxUSE_STREAMS
258
259 #endif
260
261 // wxUSE_LIBJPEG
262