5b99d4d1b99dfae6ec7c939fef3ca8a2350b0928
[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) (  struct opj_stream_private * cio,
37                                                                         void * p_codec,
38                                                                         opj_file_info_t * file_info,
39                                                                         struct opj_event_mgr * p_manager);
40
41         opj_image_t* (* opj_decode) (   void * p_codec,
42                                                                         struct opj_stream_private *p_cio,
43                                                                         struct opj_event_mgr * p_manager);
44
45         opj_bool (*opj_read_tile_header)(       void * p_codec,
46                                                                                 OPJ_UINT32 * p_tile_index,
47                                                                                 OPJ_UINT32* p_data_size,
48                                                                                 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
49                                                                                 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
50                                                                                 OPJ_UINT32 * p_nb_comps,
51                                                                                 opj_bool * p_should_go_on,
52                                                                                 struct opj_stream_private *p_cio,
53                                                                                 struct opj_event_mgr * p_manager);
54
55         opj_bool (*opj_decode_tile_data)(       void * p_codec,
56                                                                                 OPJ_UINT32 p_tile_index,
57                                                                                 OPJ_BYTE * p_data,
58                                                                                 OPJ_UINT32 p_data_size,
59                                                                                 struct opj_stream_private *p_cio,
60                                                                                 struct opj_event_mgr * p_manager);
61
62         opj_bool (* opj_end_decompress) (       void *p_codec,
63                                                                                 struct opj_stream_private *cio,
64                                                                                 struct opj_event_mgr * p_manager);
65
66         void (* opj_destroy) (void * p_codec);
67
68         void (*opj_setup_decoder) (void * p_codec, opj_dparameters_t * p_param);
69
70         opj_bool (*opj_set_decode_area) (       void * p_codec,
71                                                                                 OPJ_INT32 p_start_x, OPJ_INT32 p_end_x,
72                                                                                 OPJ_INT32 p_start_y, OPJ_INT32 p_end_y,
73                                                                                 struct opj_event_mgr * p_manager);
74 }opj_decompression_t;
75
76 typedef struct opj_compression
77 {
78         opj_bool (* opj_start_compress) (void *p_codec,struct opj_stream_private *cio,struct opj_image * p_image,       struct opj_event_mgr * p_manager);
79         opj_bool (* opj_encode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
80         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);
81         opj_bool (* opj_end_compress) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
82         void (* opj_destroy) (void * p_codec);
83         void (*opj_setup_encoder) (void * p_codec,opj_cparameters_t * p_param,struct opj_image * p_image, struct opj_event_mgr * p_manager);
84
85 }opj_compression_t;
86
87 typedef struct opj_codec_private
88 {
89         /* code-blocks informations */
90         union {
91                 opj_decompression_t m_decompression;
92                 opj_compression_t m_compression;
93     } m_codec_data;
94         void * m_codec;
95         opj_event_mgr_t* m_event_mgr;
96         unsigned is_decompressor : 1;
97 }
98 opj_codec_private_t;
99
100 /**
101  * Default callback function.
102  * Do nothing.
103  */
104 void opj_default_callback (const char *msg, void *client_data)
105 {
106         //FIXME V2 -> V1 cf below
107 }
108
109 void set_default_event_handler(opj_event_mgr_t * p_manager)
110 {
111         //FIXME V2 -> V1
112         //p_manager->m_error_data = 00;
113         //p_manager->m_warning_data = 00;
114         //p_manager->m_info_data = 00;
115         p_manager->error_handler = opj_default_callback;
116         p_manager->info_handler = opj_default_callback;
117         p_manager->warning_handler = opj_default_callback;
118 }
119
120 OPJ_UINT32 opj_read_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
121 {
122         OPJ_UINT32 l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
123         return l_nb_read ? l_nb_read : -1;
124 }
125
126 OPJ_UINT32 opj_write_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
127 {
128         return fwrite(p_buffer,1,p_nb_bytes,p_file);
129 }
130
131 OPJ_SIZE_T opj_skip_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
132 {
133         if (fseek(p_user_data,p_nb_bytes,SEEK_CUR)) {
134                 return -1;
135         }
136
137         return p_nb_bytes;
138 }
139
140 opj_bool opj_seek_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
141 {
142         if (fseek(p_user_data,p_nb_bytes,SEEK_SET)) {
143                 return EXIT_FAILURE;
144         }
145
146         return EXIT_SUCCESS;
147 }
148
149 /* ---------------------------------------------------------------------- */
150 #ifdef _WIN32
151 #ifndef OPJ_STATIC
152 BOOL APIENTRY
153 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
154
155         OPJ_ARG_NOT_USED(lpReserved);
156         OPJ_ARG_NOT_USED(hModule);
157
158         switch (ul_reason_for_call) {
159                 case DLL_PROCESS_ATTACH :
160                         break;
161                 case DLL_PROCESS_DETACH :
162                         break;
163                 case DLL_THREAD_ATTACH :
164                 case DLL_THREAD_DETACH :
165                         break;
166     }
167
168     return TRUE;
169 }
170 #endif /* OPJ_STATIC */
171 #endif /* _WIN32 */
172
173 /* ---------------------------------------------------------------------- */
174
175
176 const char* OPJ_CALLCONV opj_version(void) {
177     return PACKAGE_VERSION;
178 }
179
180 opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
181         opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
182         if(!dinfo) return NULL;
183         dinfo->is_decompressor = OPJ_TRUE;
184         switch(format) {
185                 case CODEC_J2K:
186                 case CODEC_JPT:
187                         /* get a J2K decoder handle */
188                         dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
189                         if(!dinfo->j2k_handle) {
190                                 opj_free(dinfo);
191                                 return NULL;
192                         }
193                         break;
194                 case CODEC_JP2:
195                         /* get a JP2 decoder handle */
196                         dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
197                         if(!dinfo->jp2_handle) {
198                                 opj_free(dinfo);
199                                 return NULL;
200                         }
201                         break;
202                 case CODEC_UNKNOWN:
203                 default:
204                         opj_free(dinfo);
205                         return NULL;
206         }
207
208         dinfo->codec_format = format;
209
210         return dinfo;
211 }
212
213 opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT p_format)
214 {
215         opj_codec_private_t *l_info = 00;
216
217         l_info = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
218         if (!l_info){
219                 return 00;
220         }
221         memset(l_info, 0, sizeof(opj_codec_private_t));
222
223         l_info->is_decompressor = 1;
224
225         switch (p_format) {
226                 case CODEC_J2K:
227                         l_info->m_codec_data.m_decompression.opj_decode =
228                                         (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))j2k_decode; // TODO MSD
229
230                         l_info->m_codec_data.m_decompression.opj_end_decompress =
231                                         (opj_bool (*) (void *, struct opj_stream_private *, struct opj_event_mgr *))j2k_end_decompress;
232
233                         l_info->m_codec_data.m_decompression.opj_read_header =
234                                         (opj_bool (*) ( struct opj_stream_private *,
235                                                                         void *,
236                                                                         opj_file_info_t *,
237                                                                         struct opj_event_mgr * )) j2k_read_header;
238
239                         l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))j2k_destroy;
240
241                         l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * , opj_dparameters_t * )) j2k_setup_decoder_v2;
242
243                         l_info->m_codec_data.m_decompression.opj_read_tile_header =
244                                         (opj_bool (*) ( void *,
245                                                                         OPJ_UINT32*,
246                                                                         OPJ_UINT32*,
247                                                                         OPJ_INT32*, OPJ_INT32*,
248                                                                         OPJ_INT32*, OPJ_INT32*,
249                                                                         OPJ_UINT32*,
250                                                                         opj_bool*,
251                                                                         struct opj_stream_private *,
252                                                                         struct opj_event_mgr * )) j2k_read_tile_header;
253
254                         l_info->m_codec_data.m_decompression.opj_decode_tile_data =
255                                         (opj_bool (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, struct opj_stream_private *, struct opj_event_mgr *)) j2k_decode_tile;
256
257                         l_info->m_codec_data.m_decompression.opj_set_decode_area =
258                                         (opj_bool (*) (void *, OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, struct opj_event_mgr *)) j2k_set_decode_area;
259
260                         l_info->m_codec = j2k_create_decompress_v2();
261
262                         if (! l_info->m_codec) {
263                                 opj_free(l_info);
264                                 return NULL;
265                         }
266
267                         break;
268
269                 case CODEC_JP2:
270                         /* get a JP2 decoder handle */
271                         l_info->m_codec_data.m_decompression.opj_decode = (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))opj_jp2_decode; // TODO MSD
272
273                         l_info->m_codec_data.m_decompression.opj_end_decompress =  (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
274
275                         l_info->m_codec_data.m_decompression.opj_read_header =  (opj_bool (*) (
276                                         struct opj_stream_private *,
277                                         void *,
278                                         opj_file_info_t *,
279                                         struct opj_event_mgr * )) jp2_read_header;
280
281                         l_info->m_codec_data.m_decompression.opj_read_tile_header = ( 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
298                         l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder_v2;
299
300                         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;
301
302                         l_info->m_codec = jp2_create(OPJ_TRUE);
303
304                         if (! l_info->m_codec) {
305                                 opj_free(l_info);
306                                 return 00;
307                         }
308
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
551         if (! p_file) {
552                 return NULL;
553         }
554
555         l_stream = opj_stream_create(p_size,p_is_read_stream);
556         if (! l_stream) {
557                 return NULL;
558         }
559
560         opj_stream_set_user_data(l_stream, p_file);
561         opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
562         opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
563         opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
564         opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
565
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
628 #ifdef OLD_WAY_MS
629 opj_bool OPJ_CALLCONV opj_read_header (
630                                                                    opj_codec_t *p_codec,
631                                                                    opj_image_t ** p_image,
632                                                                    OPJ_INT32 * p_tile_x0,
633                                                                    OPJ_INT32 * p_tile_y0,
634                                                                    OPJ_UINT32 * p_tile_width,
635                                                                    OPJ_UINT32 * p_tile_height,
636                                                                    OPJ_UINT32 * p_nb_tiles_x,
637                                                                    OPJ_UINT32 * p_nb_tiles_y,
638                                                                    opj_stream_t *p_cio)
639 {
640         if (p_codec && p_cio) {
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
644                 if(! l_info->is_decompressor) {
645                         return OPJ_FALSE;
646                 }
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); //&(l_info->m_event_mgr));
659         }
660         return OPJ_FALSE;
661 }
662 #endif
663
664 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_cio,
665                                                                                 opj_codec_t *p_codec,
666                                                                                 opj_file_info_t* p_file_info,
667                                                                                 OPJ_INT32 file_info_flag)
668
669 {
670         /* Initialize the output structure */
671         opj_initialise_file_info(p_file_info, file_info_flag, CODEC_J2K);
672
673         if (p_codec && p_cio) {
674                 opj_codec_private_t* l_info = (opj_codec_private_t*) p_codec;
675                 opj_stream_private_t* l_cio = (opj_stream_private_t*) p_cio;
676
677                 if(! l_info->is_decompressor) {
678                         return OPJ_FALSE;
679                 }
680
681                 return l_info->m_codec_data.m_decompression.opj_read_header(
682                                         l_cio,
683                                         l_info->m_codec,
684                                         p_file_info,
685                                         l_info->m_event_mgr);
686         }
687         return OPJ_FALSE;
688 }
689
690
691 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info)
692 {
693         if
694                 (p_info)
695         {
696                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
697                 if
698                         (l_info->is_decompressor)
699                 {
700                         l_info->m_codec_data.m_decompression.opj_destroy(l_info->m_codec);
701                 }
702                 else
703                 {
704                         l_info->m_codec_data.m_compression.opj_destroy(l_info->m_codec);
705                 }
706                 l_info->m_codec = 00;
707                 opj_free(l_info);
708         }
709 }
710
711 /**
712  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
713  *
714  * @param       p_codec                 the jpeg2000 codec.
715  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
716  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
717  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
718  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
719  *
720  * @return      true                    if the area could be set.
721  */
722 opj_bool OPJ_CALLCONV opj_set_decode_area(      opj_codec_t *p_codec,
723                                                                                         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
724                                                                                         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
725                                                                                         )
726 {
727         if (p_codec) {
728                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
729                 if (! l_info->is_decompressor) {
730                         return OPJ_FALSE;
731                 }
732
733                 return  l_info->m_codec_data.m_decompression.opj_set_decode_area(
734                                 l_info->m_codec,
735                                 p_start_x,
736                                 p_start_y,
737                                 p_end_x,
738                                 p_end_y,
739                                 &(l_info->m_event_mgr));
740
741         }
742         return OPJ_FALSE;
743 }