/[projet1]/public/pc/shared_libraries/freeimage/v3.12.0/Source/FreeImage/PluginJ2K.cpp
Defence Force logotype

Contents of /public/pc/shared_libraries/freeimage/v3.12.0/Source/FreeImage/PluginJ2K.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 126 - (show annotations)
Mon Jul 13 12:20:10 2009 UTC (10 years, 7 months ago) by dbug
File size: 9712 byte(s)
Added some shared libraries (UnitTest++, and FreeImage) to avoid having every single project brings its own libraries.
Ideally people should add them in a way we can upgrade versions without breaking things:
-> public/pc/shared_libraries/library_name/library_version/actuall_content
1 // ==========================================================
2 // JPEG2000 J2K codestream Loader and Writer
3 //
4 // Design and implementation by
5 // - Hervé Drolon (drolon@infonie.fr)
6 //
7 // This file is part of FreeImage 3
8 //
9 // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
10 // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
11 // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
12 // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
13 // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
14 // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
15 // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
16 // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
17 // THIS DISCLAIMER.
18 //
19 // Use at your own risk!
20 // ==========================================================
21
22 #include "FreeImage.h"
23 #include "Utilities.h"
24 #include "../LibOpenJPEG/openjpeg.h"
25
26 // ==========================================================
27 // Plugin Interface
28 // ==========================================================
29
30 static int s_format_id;
31
32 // ==========================================================
33 // Helper functions (see J2KHelper.cpp)
34 // ==========================================================
35
36 FIBITMAP* J2KImageToFIBITMAP(int format_id, const opj_image_t *image);
37 opj_image_t* FIBITMAPToJ2KImage(int format_id, FIBITMAP *dib, const opj_cparameters_t *parameters);
38
39 // ==========================================================
40 // Internal functions
41 // ==========================================================
42
43 /**
44 OpenJPEG Error callback
45 */
46 static void j2k_error_callback(const char *msg, void *client_data) {
47 FreeImage_OutputMessageProc(s_format_id, "Error: %s", msg);
48 }
49 /**
50 OpenJPEG Warning callback
51 */
52 static void j2k_warning_callback(const char *msg, void *client_data) {
53 FreeImage_OutputMessageProc(s_format_id, "Warning: %s", msg);
54 }
55
56 // ==========================================================
57 // Plugin Implementation
58 // ==========================================================
59
60 static const char * DLL_CALLCONV
61 Format() {
62 return "J2K";
63 }
64
65 static const char * DLL_CALLCONV
66 Description() {
67 return "JPEG-2000 codestream";
68 }
69
70 static const char * DLL_CALLCONV
71 Extension() {
72 return "j2k,j2c";
73 }
74
75 static const char * DLL_CALLCONV
76 RegExpr() {
77 return NULL;
78 }
79
80 static const char * DLL_CALLCONV
81 MimeType() {
82 return "image/j2k";
83 }
84
85 static BOOL DLL_CALLCONV
86 Validate(FreeImageIO *io, fi_handle handle) {
87 BYTE jpc_signature[] = { 0xFF, 0x4F };
88 BYTE signature[2] = { 0, 0 };
89
90 long tell = io->tell_proc(handle);
91 io->read_proc(signature, 1, sizeof(jpc_signature), handle);
92 io->seek_proc(handle, tell, SEEK_SET);
93
94 return (memcmp(jpc_signature, signature, sizeof(jpc_signature)) == 0);
95 }
96
97 static BOOL DLL_CALLCONV
98 SupportsExportDepth(int depth) {
99 return (
100 (depth == 8) ||
101 (depth == 24) ||
102 (depth == 32)
103 );
104 }
105
106 static BOOL DLL_CALLCONV
107 SupportsExportType(FREE_IMAGE_TYPE type) {
108 return (
109 (type == FIT_BITMAP) ||
110 (type == FIT_UINT16) ||
111 (type == FIT_RGB16) ||
112 (type == FIT_RGBA16)
113 );
114 }
115
116 // ----------------------------------------------------------
117
118 static void * DLL_CALLCONV
119 Open(FreeImageIO *io, fi_handle handle, BOOL read) {
120 return NULL;
121 }
122
123 static void DLL_CALLCONV
124 Close(FreeImageIO *io, fi_handle handle, void *data) {
125 }
126
127 // ----------------------------------------------------------
128
129 static FIBITMAP * DLL_CALLCONV
130 Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
131 if (handle) {
132 opj_dparameters_t parameters; // decompression parameters
133 opj_event_mgr_t event_mgr; // event manager
134 opj_image_t *image = NULL; // decoded image
135
136 BYTE *src = NULL;
137 long file_length;
138
139 opj_dinfo_t* dinfo = NULL; // handle to a decompressor
140 opj_cio_t *cio = NULL;
141
142 FIBITMAP *dib = NULL;
143
144 // check the file format
145 if(!Validate(io, handle)) {
146 return NULL;
147 }
148
149 // configure the event callbacks
150 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
151 event_mgr.error_handler = j2k_error_callback;
152 event_mgr.warning_handler = j2k_warning_callback;
153 event_mgr.info_handler = NULL;
154
155 // set decoding parameters to default values
156 opj_set_default_decoder_parameters(&parameters);
157
158 try {
159 // read the input file and put it in memory
160
161 long start_pos = io->tell_proc(handle);
162 io->seek_proc(handle, 0, SEEK_END);
163 file_length = io->tell_proc(handle) - start_pos;
164 io->seek_proc(handle, start_pos, SEEK_SET);
165 src = (BYTE*)malloc(file_length * sizeof(BYTE));
166 if(!src) {
167 throw "Memory allocation failed";
168 }
169 if(io->read_proc(src, 1, file_length, handle) < 1) {
170 throw "Error while reading input stream";
171 }
172
173 // decode the JPEG-2000 codestream
174
175 // get a decoder handle
176 dinfo = opj_create_decompress(CODEC_J2K);
177
178 // catch events using our callbacks
179 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, NULL);
180
181 // setup the decoder decoding parameters using user parameters
182 opj_setup_decoder(dinfo, &parameters);
183
184 // open a byte stream
185 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
186
187 // decode the stream and fill the image structure
188 image = opj_decode(dinfo, cio);
189 if(!image) {
190 throw "Failed to decode image!\n";
191 }
192
193 // close the byte stream
194 opj_cio_close(cio);
195 cio = NULL;
196
197 // free the memory containing the code-stream
198 free(src);
199 src = NULL;
200
201 // free the codec context
202 opj_destroy_decompress(dinfo);
203
204 // create output image
205 dib = J2KImageToFIBITMAP(s_format_id, image);
206 if(!dib) throw "Failed to import JPEG2000 image";
207
208 // free image data structure
209 opj_image_destroy(image);
210
211 return dib;
212
213 } catch (const char *text) {
214 if(src) free(src);
215 if(dib) FreeImage_Unload(dib);
216 // free remaining structures
217 opj_destroy_decompress(dinfo);
218 opj_image_destroy(image);
219 // close the byte stream
220 if(cio) opj_cio_close(cio);
221
222 FreeImage_OutputMessageProc(s_format_id, text);
223
224 return NULL;
225 }
226 }
227
228 return NULL;
229 }
230
231 static BOOL DLL_CALLCONV
232 Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data) {
233 if ((dib) && (handle)) {
234 BOOL bSuccess;
235 opj_cparameters_t parameters; // compression parameters
236 opj_event_mgr_t event_mgr; // event manager
237 opj_image_t *image = NULL; // image to encode
238 opj_cinfo_t* cinfo = NULL; // codec context
239 opj_cio_t *cio = NULL; // memory byte stream
240
241 // configure the event callbacks
242 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
243 event_mgr.error_handler = j2k_error_callback;
244 event_mgr.warning_handler = j2k_warning_callback;
245 event_mgr.info_handler = NULL;
246
247 // set encoding parameters to default values
248 opj_set_default_encoder_parameters(&parameters);
249
250 // if no rate entered, apply a 16:1 rate by default
251 if(flags == J2K_DEFAULT) {
252 parameters.tcp_rates[0] = (float)16;
253 } else {
254 // for now, the flags parameter is only used to specify the rate
255 parameters.tcp_rates[0] = (float)flags;
256 }
257 parameters.tcp_numlayers++;
258 parameters.cp_disto_alloc = 1;
259
260 try {
261 // convert the dib to a OpenJPEG image
262 image = FIBITMAPToJ2KImage(s_format_id, dib, &parameters);
263 if(!image) return FALSE;
264
265 // encode the destination image
266
267 // get a J2K compressor handle
268 cinfo = opj_create_compress(CODEC_J2K);
269
270 // catch events using our callbacks
271 opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, NULL);
272
273 // setup the encoder parameters using the current image and using user parameters
274 opj_setup_encoder(cinfo, &parameters, image);
275
276 // open a byte stream for writing, allocate memory for all tiles
277 cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
278
279 // encode the image
280 bSuccess = opj_encode(cinfo, cio, image, NULL/*parameters.index*/);
281 if (!bSuccess) {
282 throw "Failed to encode image";
283 }
284 int codestream_length = cio_tell(cio);
285
286 // write the buffer to user's IO handle
287 io->write_proc(cio->buffer, 1, codestream_length, handle);
288
289 // close and free the byte stream
290 opj_cio_close(cio);
291
292 // free remaining compression structures
293 opj_destroy_compress(cinfo);
294
295 // free image data
296 opj_image_destroy(image);
297
298 return TRUE;
299
300 } catch (const char *text) {
301 if(cio) opj_cio_close(cio);
302 if(cinfo) opj_destroy_compress(cinfo);
303 if(image) opj_image_destroy(image);
304 FreeImage_OutputMessageProc(s_format_id, text);
305 return FALSE;
306 }
307 }
308
309 return FALSE;
310 }
311
312 // ==========================================================
313 // Init
314 // ==========================================================
315
316 void DLL_CALLCONV
317 InitJ2K(Plugin *plugin, int format_id) {
318 s_format_id = format_id;
319
320 plugin->format_proc = Format;
321 plugin->description_proc = Description;
322 plugin->extension_proc = Extension;
323 plugin->regexpr_proc = RegExpr;
324 plugin->open_proc = Open;
325 plugin->close_proc = Close;
326 plugin->pagecount_proc = NULL;
327 plugin->pagecapability_proc = NULL;
328 plugin->load_proc = Load;
329 plugin->save_proc = Save;
330 plugin->validate_proc = Validate;
331 plugin->mime_proc = MimeType;
332 plugin->supports_export_bpp_proc = SupportsExportDepth;
333 plugin->supports_export_type_proc = SupportsExportType;
334 plugin->supports_icc_profiles_proc = NULL;
335 }

  ViewVC Help
Powered by ViewVC 1.1.26