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