WIP: manage the case of event_mgr is not provided to setup_decoder function
[openjpeg.git] / libopenjpeg / openjpeg.c
1 /*
2  * Copyright (c) 2005, Herve Drolon, FreeImage Team
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifdef _WIN32
28 #include <windows.h>
29 #endif /* _WIN32 */
30
31 #include "opj_config.h"
32 #include "opj_includes.h"
33
34 typedef struct opj_decompression
35 {
36         opj_bool (* opj_read_header) (
37                 void *p_codec,
38                 opj_image_t **,
39                 OPJ_INT32 * p_tile_x0,
40                 OPJ_INT32 * p_tile_y0,
41                 OPJ_UINT32 * p_tile_width,
42                 OPJ_UINT32 * p_tile_height,
43                 OPJ_UINT32 * p_nb_tiles_x,
44                 OPJ_UINT32 * p_nb_tiles_y,
45                 struct opj_stream_private *cio,
46                 struct opj_event_mgr * p_manager);
47         opj_image_t* (* opj_decode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
48         opj_bool (*opj_read_tile_header)(
49                 void * p_codec,
50                 OPJ_UINT32 * p_tile_index,
51                 OPJ_UINT32* p_data_size,
52                 OPJ_INT32 * p_tile_x0,
53                 OPJ_INT32 * p_tile_y0,
54                 OPJ_INT32 * p_tile_x1,
55                 OPJ_INT32 * p_tile_y1,
56                 OPJ_UINT32 * p_nb_comps,
57                 opj_bool * p_should_go_on,
58                 struct opj_stream_private *p_cio,
59                 struct opj_event_mgr * p_manager);
60                 opj_bool (*opj_decode_tile_data)(void * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,struct opj_stream_private *p_cio,struct opj_event_mgr * p_manager);
61         opj_bool (* opj_end_decompress) (void *p_codec,struct opj_stream_private *cio,struct opj_event_mgr * p_manager);
62         void (* opj_destroy) (void * p_codec);
63         void (*opj_setup_decoder) (void * p_codec,opj_dparameters_t * p_param);
64         opj_bool (*opj_set_decode_area) (void * p_codec,OPJ_INT32 p_start_x,OPJ_INT32 p_end_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_y,struct opj_event_mgr * p_manager);
65
66
67 }opj_decompression_t;
68
69 typedef struct opj_compression
70 {
71         opj_bool (* opj_start_compress) (void *p_codec,struct opj_stream_private *cio,struct opj_image * p_image,       struct opj_event_mgr * p_manager);
72         opj_bool (* opj_encode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
73         opj_bool (* opj_write_tile) (void * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,struct opj_stream_private * p_cio,struct opj_event_mgr * p_manager);
74         opj_bool (* opj_end_compress) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
75         void (* opj_destroy) (void * p_codec);
76         void (*opj_setup_encoder) (void * p_codec,opj_cparameters_t * p_param,struct opj_image * p_image, struct opj_event_mgr * p_manager);
77
78 }opj_compression_t;
79
80 typedef struct opj_codec_private
81 {
82         union
83         {               /* code-blocks informations */
84           opj_decompression_t m_decompression;
85           opj_compression_t m_compression;
86     } m_codec_data;
87         void * m_codec;
88         opj_event_mgr_t* m_event_mgr;
89         unsigned is_decompressor : 1;
90 }
91 opj_codec_private_t;
92
93 /**
94  * Default callback function.
95  * Do nothing.
96  */
97 void opj_default_callback (const char *msg, void *client_data)
98 {
99         //FIXME V2 -> V1 cf below
100 }
101
102 void set_default_event_handler(opj_event_mgr_t * p_manager)
103 {
104         //FIXME V2 -> V1
105         //p_manager->m_error_data = 00;
106         //p_manager->m_warning_data = 00;
107         //p_manager->m_info_data = 00;
108         p_manager->error_handler = opj_default_callback;
109         p_manager->info_handler = opj_default_callback;
110         p_manager->warning_handler = opj_default_callback;
111 }
112
113 OPJ_UINT32 opj_read_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
114 {
115         OPJ_UINT32 l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
116         return l_nb_read ? l_nb_read : -1;
117 }
118
119 OPJ_UINT32 opj_write_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
120 {
121         return fwrite(p_buffer,1,p_nb_bytes,p_file);
122 }
123
124 OPJ_SIZE_T opj_skip_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
125 {
126         if
127                 (fseek(p_user_data,p_nb_bytes,SEEK_CUR))
128         {
129                 return -1;
130         }
131         return p_nb_bytes;
132 }
133
134 opj_bool opj_seek_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
135 {
136         if
137                 (fseek(p_user_data,p_nb_bytes,SEEK_SET))
138         {
139                 return EXIT_FAILURE;
140         }
141         return EXIT_SUCCESS;
142 }
143
144 /* ---------------------------------------------------------------------- */
145 #ifdef _WIN32
146 #ifndef OPJ_STATIC
147 BOOL APIENTRY
148 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
149
150         OPJ_ARG_NOT_USED(lpReserved);
151         OPJ_ARG_NOT_USED(hModule);
152
153         switch (ul_reason_for_call) {
154                 case DLL_PROCESS_ATTACH :
155                         break;
156                 case DLL_PROCESS_DETACH :
157                         break;
158                 case DLL_THREAD_ATTACH :
159                 case DLL_THREAD_DETACH :
160                         break;
161     }
162
163     return TRUE;
164 }
165 #endif /* OPJ_STATIC */
166 #endif /* _WIN32 */
167
168 /* ---------------------------------------------------------------------- */
169
170
171 const char* OPJ_CALLCONV opj_version(void) {
172     return PACKAGE_VERSION;
173 }
174
175 opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
176         opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
177         if(!dinfo) return NULL;
178         dinfo->is_decompressor = OPJ_TRUE;
179         switch(format) {
180                 case CODEC_J2K:
181                 case CODEC_JPT:
182                         /* get a J2K decoder handle */
183                         dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
184                         if(!dinfo->j2k_handle) {
185                                 opj_free(dinfo);
186                                 return NULL;
187                         }
188                         break;
189                 case CODEC_JP2:
190                         /* get a JP2 decoder handle */
191                         dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
192                         if(!dinfo->jp2_handle) {
193                                 opj_free(dinfo);
194                                 return NULL;
195                         }
196                         break;
197                 case CODEC_UNKNOWN:
198                 default:
199                         opj_free(dinfo);
200                         return NULL;
201         }
202
203         dinfo->codec_format = format;
204
205         return dinfo;
206 }
207
208 opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT p_format)
209 {
210         opj_codec_private_t *l_info = 00;
211
212         l_info = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
213         if (!l_info){
214                 return 00;
215         }
216         memset(l_info, 0, sizeof(opj_codec_private_t));
217
218         l_info->is_decompressor = 1;
219
220         switch
221                 (p_format)
222         {
223                 case CODEC_J2K:
224                         l_info->m_codec_data.m_decompression.opj_decode = (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))j2k_decode;
225                         l_info->m_codec_data.m_decompression.opj_end_decompress =  (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *))j2k_end_decompress;
226                         l_info->m_codec_data.m_decompression.opj_read_header =  (opj_bool (*) (
227                                 void *,
228                                 opj_image_t **,
229                                 OPJ_INT32 * ,
230                                 OPJ_INT32 * ,
231                                 OPJ_UINT32 * ,
232                                 OPJ_UINT32 * ,
233                                 OPJ_UINT32 * ,
234                                 OPJ_UINT32 * ,
235                                 struct opj_stream_private *,
236                                 struct opj_event_mgr * )) j2k_read_header;
237                         l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))j2k_destroy;
238                         l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) j2k_setup_decoder;
239                         l_info->m_codec_data.m_decompression.opj_read_tile_header = (opj_bool (*) (
240                                 void *,
241                                 OPJ_UINT32*,
242                                 OPJ_UINT32*,
243                                 OPJ_INT32 * ,
244                                 OPJ_INT32 * ,
245                                 OPJ_INT32 * ,
246                                 OPJ_INT32 * ,
247                                 OPJ_UINT32 * ,
248                                 opj_bool *,
249                                 struct opj_stream_private *,
250                                 struct opj_event_mgr * )) j2k_read_tile_header;
251                                 l_info->m_codec_data.m_decompression.opj_decode_tile_data = (opj_bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *,  struct opj_event_mgr * )) j2k_decode_tile;
252                         l_info->m_codec_data.m_decompression.opj_set_decode_area = (opj_bool (*) (void *,OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) j2k_set_decode_area;
253                         l_info->m_codec = j2k_create_decompress_v2();
254                         if
255                                 (! l_info->m_codec)
256                         {
257                                 opj_free(l_info);
258                                 return 00;
259                         }
260                         break;
261
262                 case CODEC_JP2:
263                         /* get a JP2 decoder handle */
264 #ifdef TODO_MSD
265                         l_info->m_codec_data.m_decompression.opj_decode = (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))opj_jp2_decode;
266                         l_info->m_codec_data.m_decompression.opj_end_decompress =  (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
267                         l_info->m_codec_data.m_decompression.opj_read_header =  (opj_bool (*) (
268                                 void *,
269                                 opj_image_t **,
270
271                                 OPJ_INT32 * ,
272                                 OPJ_INT32 * ,
273                                 OPJ_UINT32 * ,
274                                 OPJ_UINT32 * ,
275                                 OPJ_UINT32 * ,
276                                 OPJ_UINT32 * ,
277                                 struct opj_stream_private *,
278                                 struct opj_event_mgr * )) jp2_read_header;
279
280                         l_info->m_codec_data.m_decompression.opj_read_tile_header = (
281                                 opj_bool (*) (
282                                         void *,
283                                         OPJ_UINT32*,
284                                         OPJ_UINT32*,
285                                         OPJ_INT32*,
286                                         OPJ_INT32*,
287                                         OPJ_INT32 * ,
288                                         OPJ_INT32 * ,
289                                         OPJ_UINT32 * ,
290                                         opj_bool *,
291                                         struct opj_stream_private *,
292                                         struct opj_event_mgr * )) jp2_read_tile_header;
293
294                         l_info->m_codec_data.m_decompression.opj_decode_tile_data = (opj_bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *,  struct opj_event_mgr * )) opj_jp2_decode_tile;
295
296                         l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
297                         l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder;
298                         l_info->m_codec_data.m_decompression.opj_set_decode_area = (opj_bool (*) (void *,OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) jp2_set_decode_area;
299
300
301                         l_info->m_codec = jp2_create(OPJ_TRUE);
302                         if
303                                 (! l_info->m_codec)
304                         {
305                                 opj_free(l_info);
306                                 return 00;
307                         }
308 #endif
309                         break;
310                 case CODEC_UNKNOWN:
311                 case CODEC_JPT:
312                 default:
313                         opj_free(l_info);
314                         return 00;
315         }
316
317         // FIXME set_default_event_handler(&(l_info->m_event_mgr));
318         return (opj_codec_t*) l_info;
319 }
320
321
322 void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
323         if(dinfo) {
324                 /* destroy the codec */
325                 switch(dinfo->codec_format) {
326                         case CODEC_J2K:
327                         case CODEC_JPT:
328                                 j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
329                                 break;
330                         case CODEC_JP2:
331                                 jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
332                                 break;
333                         case CODEC_UNKNOWN:
334                         default:
335                                 break;
336                 }
337                 /* destroy the decompressor */
338                 opj_free(dinfo);
339         }
340 }
341
342 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
343         if(parameters) {
344                 memset(parameters, 0, sizeof(opj_dparameters_t));
345                 /* default decoding parameters */
346                 parameters->cp_layer = 0;
347                 parameters->cp_reduce = 0;
348                 parameters->cp_limit_decoding = NO_LIMITATION;
349
350                 parameters->decod_format = -1;
351                 parameters->cod_format = -1;
352 /* UniPG>> */
353 #ifdef USE_JPWL
354                 parameters->jpwl_correct = OPJ_FALSE;
355                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
356                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
357 #endif /* USE_JPWL */
358 /* <<UniPG */
359         }
360 }
361
362 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
363         if(dinfo && parameters) {
364                 switch(dinfo->codec_format) {
365                         case CODEC_J2K:
366                         case CODEC_JPT:
367                                 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
368                                 break;
369                         case CODEC_JP2:
370                                 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
371                                 break;
372                         case CODEC_UNKNOWN:
373                         default:
374                                 break;
375                 }
376         }
377 }
378
379 opj_bool OPJ_CALLCONV opj_setup_decoder_v2(opj_codec_t *p_info, opj_dparameters_t *parameters, opj_event_mgr_t* event_mgr)
380 {
381         if (p_info && parameters) {
382                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
383
384                 if (! l_info->is_decompressor) {
385                         return OPJ_FALSE;
386                 }
387
388                 l_info->m_codec_data.m_decompression.opj_setup_decoder(l_info->m_codec,parameters);
389
390                 if (event_mgr == NULL)
391                 {
392                         l_info->m_event_mgr->error_handler = opj_default_callback ;
393                         l_info->m_event_mgr->warning_handler = opj_default_callback ;
394                         l_info->m_event_mgr->info_handler = opj_default_callback ;
395                         l_info->m_event_mgr->client_data = stderr;
396                 }
397                 else
398                         l_info->m_event_mgr = event_mgr;
399                 return OPJ_TRUE;
400         }
401         return OPJ_FALSE;
402 }
403
404 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
405         return opj_decode_with_info(dinfo, cio, NULL);
406 }
407
408 opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
409         if(dinfo && cio) {
410                 switch(dinfo->codec_format) {
411                         case CODEC_J2K:
412                                 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
413                         case CODEC_JPT:
414                                 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
415                         case CODEC_JP2:
416                                 return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
417                         case CODEC_UNKNOWN:
418                         default:
419                                 break;
420                 }
421         }
422         return NULL;
423 }
424
425 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
426         opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
427         if(!cinfo) return NULL;
428         cinfo->is_decompressor = OPJ_FALSE;
429         switch(format) {
430                 case CODEC_J2K:
431                         /* get a J2K coder handle */
432                         cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
433                         if(!cinfo->j2k_handle) {
434                                 opj_free(cinfo);
435                                 return NULL;
436                         }
437                         break;
438                 case CODEC_JP2:
439                         /* get a JP2 coder handle */
440                         cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
441                         if(!cinfo->jp2_handle) {
442                                 opj_free(cinfo);
443                                 return NULL;
444                         }
445                         break;
446                 case CODEC_JPT:
447                 case CODEC_UNKNOWN:
448                 default:
449                         opj_free(cinfo);
450                         return NULL;
451         }
452
453         cinfo->codec_format = format;
454
455         return cinfo;
456 }
457
458 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
459         if(cinfo) {
460                 /* destroy the codec */
461                 switch(cinfo->codec_format) {
462                         case CODEC_J2K:
463                                 j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
464                                 break;
465                         case CODEC_JP2:
466                                 jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
467                                 break;
468                         case CODEC_JPT:
469                         case CODEC_UNKNOWN:
470                         default:
471                                 break;
472                 }
473                 /* destroy the decompressor */
474                 opj_free(cinfo);
475         }
476 }
477
478 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
479         if(parameters) {
480                 memset(parameters, 0, sizeof(opj_cparameters_t));
481                 /* default coding parameters */
482                 parameters->cp_cinema = OFF; 
483                 parameters->max_comp_size = 0;
484                 parameters->numresolution = 6;
485                 parameters->cp_rsiz = STD_RSIZ;
486                 parameters->cblockw_init = 64;
487                 parameters->cblockh_init = 64;
488                 parameters->prog_order = LRCP;
489                 parameters->roi_compno = -1;            /* no ROI */
490                 parameters->subsampling_dx = 1;
491                 parameters->subsampling_dy = 1;
492                 parameters->tp_on = 0;
493                 parameters->decod_format = -1;
494                 parameters->cod_format = -1;
495                 parameters->tcp_rates[0] = 0;   
496                 parameters->tcp_numlayers = 0;
497     parameters->cp_disto_alloc = 0;
498                 parameters->cp_fixed_alloc = 0;
499                 parameters->cp_fixed_quality = 0;
500                 parameters->jpip_on = OPJ_FALSE;
501 /* UniPG>> */
502 #ifdef USE_JPWL
503                 parameters->jpwl_epc_on = OPJ_FALSE;
504                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
505                 {
506                         int i;
507                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
508                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
509                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
510                         }
511                 };
512                 {
513                         int i;
514                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
515                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
516                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
517                                 parameters->jpwl_pprot[i] = 0; /* absent */
518                         }
519                 };
520                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
521                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
522                 parameters->jpwl_sens_range = 0; /* 0 means packet */
523                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
524                 {
525                         int i;
526                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
527                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
528                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
529                         }
530                 };
531 #endif /* USE_JPWL */
532 /* <<UniPG */
533         }
534 }
535
536 /**
537  * Helper function.
538  * Sets the stream to be a file stream. The FILE must have been open previously.
539  * @param               p_stream        the stream to modify
540  * @param               p_file          handler to an already open file.
541 */
542 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
543 {
544         return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
545 }
546
547 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file, OPJ_UINT32 p_size, opj_bool p_is_read_stream)
548 {
549         opj_stream_t* l_stream = 00;
550         if
551                 (! p_file)
552         {
553                 return 00;
554         }
555         l_stream = opj_stream_create(p_size,p_is_read_stream);
556         if
557                 (! l_stream)
558         {
559                 return 00;
560         }
561         opj_stream_set_user_data(l_stream,p_file);
562         opj_stream_set_read_function(l_stream,(opj_stream_read_fn) opj_read_from_file);
563         opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
564         opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
565         opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
566         return l_stream;
567 }
568
569 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
570         if(cinfo && parameters && image) {
571                 switch(cinfo->codec_format) {
572                         case CODEC_J2K:
573                                 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
574                                 break;
575                         case CODEC_JP2:
576                                 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
577                                 break;
578                         case CODEC_JPT:
579                         case CODEC_UNKNOWN:
580                         default:
581                                 break;
582                 }
583         }
584 }
585
586 opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
587         if (index != NULL)
588                 opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
589                 "To extract the index, use the opj_encode_with_info() function.\n"
590                 "No index will be generated during this encoding\n");
591         return opj_encode_with_info(cinfo, cio, image, NULL);
592 }
593
594 opj_bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
595         if(cinfo && cio && image) {
596                 switch(cinfo->codec_format) {
597                         case CODEC_J2K:
598                                 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
599                         case CODEC_JP2:
600                                 return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);        
601                         case CODEC_JPT:
602                         case CODEC_UNKNOWN:
603                         default:
604                                 break;
605                 }
606         }
607         return OPJ_FALSE;
608 }
609
610 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
611         if (cstr_info) {
612                 int tileno;
613                 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
614                         opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
615                         opj_free(tile_info->thresh);
616                         opj_free(tile_info->packet);
617                         opj_free(tile_info->tp);
618                         opj_free(tile_info->marker);
619                 }
620                 opj_free(cstr_info->tile);
621                 opj_free(cstr_info->marker);
622                 opj_free(cstr_info->numdecompos);
623         }
624 }
625
626
627 opj_bool OPJ_CALLCONV opj_read_header (
628                                                                    opj_codec_t *p_codec,
629                                                                    opj_image_t ** p_image,
630                                                                    OPJ_INT32 * p_tile_x0,
631                                                                    OPJ_INT32 * p_tile_y0,
632                                                                    OPJ_UINT32 * p_tile_width,
633                                                                    OPJ_UINT32 * p_tile_height,
634                                                                    OPJ_UINT32 * p_nb_tiles_x,
635                                                                    OPJ_UINT32 * p_nb_tiles_y,
636                                                                    opj_stream_t *p_cio)
637 {
638         if
639                 (p_codec && p_cio)
640         {
641                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
642                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
643                 if
644                         (! l_info->is_decompressor)
645                 {
646                         return OPJ_FALSE;
647                 }
648                 return l_info->m_codec_data.m_decompression.opj_read_header(
649                         l_info->m_codec,
650                         p_image,
651                         p_tile_x0,
652                         p_tile_y0,
653                         p_tile_width,
654                         p_tile_height,
655                         p_nb_tiles_x,
656                         p_nb_tiles_y,
657                         l_cio,
658                         //&(l_info->m_event_mgr));
659                         l_info->m_event_mgr);
660         }
661         return OPJ_FALSE;
662 }
663
664 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info)
665 {
666         if
667                 (p_info)
668         {
669                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
670                 if
671                         (l_info->is_decompressor)
672                 {
673                         l_info->m_codec_data.m_decompression.opj_destroy(l_info->m_codec);
674                 }
675                 else
676                 {
677                         l_info->m_codec_data.m_compression.opj_destroy(l_info->m_codec);
678                 }
679                 l_info->m_codec = 00;
680                 opj_free(l_info);
681         }
682 }