1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage JPEG handler
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 We don't put pragma implement in this file because it is already present in
15 #pragma implementation "image.h"
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
27 #include "wx/bitmap.h"
36 #include "wx/filefn.h"
37 #include "wx/wfstream.h"
39 #include "wx/module.h"
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
60 #if !USE_SHARED_LIBRARIES
61 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
67 //------------- JPEG Data Source Manager
70 struct jpeg_source_mgr pub
; /* public fields */
72 JOCTET
* buffer
; /* start of buffer */
75 typedef my_source_mgr
* my_src_ptr
;
77 METHODDEF(void) my_init_source ( j_decompress_ptr cinfo
)
81 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr cinfo
)
86 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
88 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
90 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
91 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
94 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
96 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
101 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
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
;
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());
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
;
126 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
128 struct jpeg_decompress_struct cinfo
;
129 struct jpeg_error_mgr jerr
;
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
);
142 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
144 jpeg_finish_decompress( &cinfo
);
145 jpeg_destroy_decompress( &cinfo
);
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 );
154 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
155 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
156 memcpy( ptr
, tempbuf
[0], stride
);
159 jpeg_finish_decompress( &cinfo
);
160 jpeg_destroy_decompress( &cinfo
);
169 struct jpeg_destination_mgr pub
;
171 wxOutputStream
*stream
;
173 } my_destination_mgr
;
175 typedef my_destination_mgr
* my_dest_ptr
;
177 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
179 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
181 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
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
;
191 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
193 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
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
;
201 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
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 */
207 dest
->stream
->Write(dest
->buffer
, datacount
);
210 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
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
));
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
;
227 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
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 */
235 cinfo
.err
= jpeg_std_error(&jerr
);
236 jpeg_create_compress(&cinfo
);
237 jpeg_wxio_dest(&cinfo
, stream
);
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
);
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 );
252 jpeg_finish_compress(&cinfo
);
253 jpeg_destroy_compress(&cinfo
);
257 #endif // wxUSE_STREAMS