1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage JPEG handler
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "image.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
22 #include "wx/bitmap.h"
31 #include "wx/filefn.h"
32 #include "wx/wfstream.h"
34 #include "wx/module.h"
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
55 #if !USE_SHARED_LIBRARIES
56 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
62 //------------- JPEG Data Source Manager
65 struct jpeg_source_mgr pub
; /* public fields */
67 JOCTET
* buffer
; /* start of buffer */
70 typedef my_source_mgr
* my_src_ptr
;
72 METHODDEF(void) my_init_source ( j_decompress_ptr cinfo
)
76 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr cinfo
)
81 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
83 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
85 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
86 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
89 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
91 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
96 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
100 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
101 cinfo
->src
= (struct jpeg_source_mgr
*)
102 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
103 sizeof(my_source_mgr
));
104 src
= (my_src_ptr
) cinfo
->src
;
106 src
= (my_src_ptr
) cinfo
->src
;
107 src
->pub
.bytes_in_buffer
= infile
.StreamSize(); /* forces fill_input_buffer on first read */
108 src
->buffer
= (JOCTET
*) malloc (infile
.StreamSize());
109 src
->pub
.next_input_byte
= src
->buffer
; /* until buffer loaded */
110 infile
.Read(src
->buffer
, infile
.StreamSize());
112 src
->pub
.init_source
= my_init_source
;
113 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
114 src
->pub
.skip_input_data
= my_skip_input_data
;
115 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
116 src
->pub
.term_source
= my_term_source
;
121 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
123 struct jpeg_decompress_struct cinfo
;
124 struct jpeg_error_mgr jerr
;
130 cinfo
.err
= jpeg_std_error( &jerr
);
131 jpeg_create_decompress( &cinfo
);
132 jpeg_wxio_src( &cinfo
, stream
);
133 jpeg_read_header( &cinfo
, TRUE
);
134 cinfo
.out_color_space
= JCS_RGB
;
135 jpeg_start_decompress( &cinfo
);
137 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
139 jpeg_finish_decompress( &cinfo
);
140 jpeg_destroy_decompress( &cinfo
);
143 image
->SetMask( FALSE
);
144 ptr
= image
->GetData();
145 stride
= cinfo
.output_width
* 3;
146 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
147 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
149 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
150 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
151 memcpy( ptr
, tempbuf
[0], stride
);
154 jpeg_finish_decompress( &cinfo
);
155 jpeg_destroy_decompress( &cinfo
);
164 struct jpeg_destination_mgr pub
;
166 wxOutputStream
*stream
;
168 } my_destination_mgr
;
170 typedef my_destination_mgr
* my_dest_ptr
;
172 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
174 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
176 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
178 /* Allocate the output buffer --- it will be released when done with image */
179 dest
->buffer
= (JOCTET
*)
180 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
181 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
182 dest
->pub
.next_output_byte
= dest
->buffer
;
183 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
186 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
188 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
190 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
191 dest
->pub
.next_output_byte
= dest
->buffer
;
192 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
196 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
198 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
199 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
200 /* Write any data remaining in the buffer */
202 dest
->stream
->Write(dest
->buffer
, datacount
);
205 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
209 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
210 cinfo
->dest
= (struct jpeg_destination_mgr
*)
211 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
212 sizeof(my_destination_mgr
));
215 dest
= (my_dest_ptr
) cinfo
->dest
;
216 dest
->pub
.init_destination
= init_destination
;
217 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
218 dest
->pub
.term_destination
= term_destination
;
219 dest
->stream
= &outfile
;
222 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
224 struct jpeg_compress_struct cinfo
;
225 struct jpeg_error_mgr jerr
;
226 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
227 JSAMPLE
*image_buffer
;
228 int stride
; /* physical row width in image buffer */
230 cinfo
.err
= jpeg_std_error(&jerr
);
231 jpeg_create_compress(&cinfo
);
232 jpeg_wxio_dest(&cinfo
, stream
);
234 cinfo
.image_width
= image
->GetWidth();
235 cinfo
.image_height
= image
->GetHeight();
236 cinfo
.input_components
= 3;
237 cinfo
.in_color_space
= JCS_RGB
;
238 jpeg_set_defaults(&cinfo
);
239 jpeg_start_compress(&cinfo
, TRUE
);
241 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
242 image_buffer
= image
->GetData();
243 while (cinfo
.next_scanline
< cinfo
.image_height
) {
244 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
245 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
247 jpeg_finish_compress(&cinfo
);
248 jpeg_destroy_compress(&cinfo
);
252 #endif // wxUSE_STREAMS