f9a392588642f43c03ae64945885016fb8be6d13
[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
35 /**
36  * Decompression handler.
37  */
38 typedef struct opj_decompression
39 {
40         /** Main header reading function handler*/
41         opj_bool (* opj_read_header) (  struct opj_stream_private * cio,
42                                                                         void * p_codec,
43                                                                         opj_file_info_t * file_info,
44                                                                         struct opj_event_mgr * p_manager);
45         /** FIXME DOC */
46         opj_image_t* (* opj_decode) (   void * p_codec,
47                                                                         struct opj_stream_private *p_cio,
48                                                                         struct opj_event_mgr * p_manager);
49         /** FIXME DOC */
50         opj_bool (*opj_read_tile_header)(       void * p_codec,
51                                                                                 OPJ_UINT32 * p_tile_index,
52                                                                                 OPJ_UINT32* p_data_size,
53                                                                                 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
54                                                                                 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
55                                                                                 OPJ_UINT32 * p_nb_comps,
56                                                                                 opj_bool * p_should_go_on,
57                                                                                 struct opj_stream_private *p_cio,
58                                                                                 struct opj_event_mgr * p_manager);
59         /** FIXME DOC */
60         opj_bool (*opj_decode_tile_data)(       void * p_codec,
61                                                                                 OPJ_UINT32 p_tile_index,
62                                                                                 OPJ_BYTE * p_data,
63                                                                                 OPJ_UINT32 p_data_size,
64                                                                                 struct opj_stream_private *p_cio,
65                                                                                 struct opj_event_mgr * p_manager);
66         /** FIXME DOC */
67         opj_bool (* opj_end_decompress) (       void *p_codec,
68                                                                                 struct opj_stream_private *cio,
69                                                                                 struct opj_event_mgr * p_manager);
70         /** Codec destroy function handler*/
71         void (* opj_destroy) (void * p_codec);
72         /** Setup decoder function handler */
73         void (*opj_setup_decoder) (void * p_codec, opj_dparameters_t * p_param);
74         /** Set decode area function handler */
75         opj_bool (*opj_set_decode_area) (       void * p_codec,
76                                                                                 OPJ_INT32 p_start_x, OPJ_INT32 p_end_x,
77                                                                                 OPJ_INT32 p_start_y, OPJ_INT32 p_end_y,
78                                                                                 struct opj_event_mgr * p_manager);
79 }opj_decompression_t;
80
81 /**
82  * Compression handler. FIXME DOC
83  */
84 typedef struct opj_compression
85 {
86         opj_bool (* opj_start_compress) (void *p_codec,struct opj_stream_private *cio,struct opj_image * p_image,       struct opj_event_mgr * p_manager);
87         opj_bool (* opj_encode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
88         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);
89         opj_bool (* opj_end_compress) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
90         void (* opj_destroy) (void * p_codec);
91         void (*opj_setup_encoder) (void * p_codec,opj_cparameters_t * p_param,struct opj_image * p_image, struct opj_event_mgr * p_manager);
92
93 }opj_compression_t;
94
95 /**
96  * Main codec handler used for compression or decompression.
97  */
98 typedef struct opj_codec_private
99 {
100         /** FIXME DOC */
101         union {
102                 opj_decompression_t m_decompression;
103                 opj_compression_t m_compression;
104     } m_codec_data;
105     /** FIXME DOC*/
106         void * m_codec;
107         /** Event handler */
108         opj_event_mgr_t* m_event_mgr;
109         /** Flag to indicate if the codec is used to decode or encode*/
110         opj_bool is_decompressor;
111 }
112 opj_codec_private_t;
113
114
115
116 /* ---------------------------------------------------------------------- */
117
118
119 OPJ_UINT32 opj_read_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
120 {
121         OPJ_UINT32 l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
122         return l_nb_read ? l_nb_read : -1;
123 }
124
125 OPJ_UINT32 opj_write_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
126 {
127         return fwrite(p_buffer,1,p_nb_bytes,p_file);
128 }
129
130 OPJ_SIZE_T opj_skip_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
131 {
132         if (fseek(p_user_data,p_nb_bytes,SEEK_CUR)) {
133                 return -1;
134         }
135
136         return p_nb_bytes;
137 }
138
139 opj_bool opj_seek_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
140 {
141         if (fseek(p_user_data,p_nb_bytes,SEEK_SET)) {
142                 return EXIT_FAILURE;
143         }
144
145         return EXIT_SUCCESS;
146 }
147
148 /* ---------------------------------------------------------------------- */
149 #ifdef _WIN32
150 #ifndef OPJ_STATIC
151 BOOL APIENTRY
152 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
153
154         OPJ_ARG_NOT_USED(lpReserved);
155         OPJ_ARG_NOT_USED(hModule);
156
157         switch (ul_reason_for_call) {
158                 case DLL_PROCESS_ATTACH :
159                         break;
160                 case DLL_PROCESS_DETACH :
161                         break;
162                 case DLL_THREAD_ATTACH :
163                 case DLL_THREAD_DETACH :
164                         break;
165     }
166
167     return TRUE;
168 }
169 #endif /* OPJ_STATIC */
170 #endif /* _WIN32 */
171
172 /* ---------------------------------------------------------------------- */
173
174
175 const char* OPJ_CALLCONV opj_version(void) {
176     return PACKAGE_VERSION;
177 }
178
179
180
181 opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
182         opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
183         if(!dinfo) return NULL;
184         dinfo->is_decompressor = OPJ_TRUE;
185         switch(format) {
186                 case CODEC_J2K:
187                 case CODEC_JPT:
188                         /* get a J2K decoder handle */
189                         dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
190                         if(!dinfo->j2k_handle) {
191                                 opj_free(dinfo);
192                                 return NULL;
193                         }
194                         break;
195                 case CODEC_JP2:
196                         /* get a JP2 decoder handle */
197                         dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
198                         if(!dinfo->jp2_handle) {
199                                 opj_free(dinfo);
200                                 return NULL;
201                         }
202                         break;
203                 case CODEC_UNKNOWN:
204                 default:
205                         opj_free(dinfo);
206                         return NULL;
207         }
208
209         dinfo->codec_format = format;
210
211         return dinfo;
212 }
213
214 opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT p_format)
215 {
216         opj_codec_private_t *l_info = 00;
217
218         l_info = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
219         if (!l_info){
220                 return 00;
221         }
222         memset(l_info, 0, sizeof(opj_codec_private_t));
223
224         l_info->is_decompressor = 1;
225
226         switch (p_format) {
227                 case CODEC_J2K:
228                         l_info->m_codec_data.m_decompression.opj_decode =
229                                         (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))j2k_decode; // TODO MSD
230
231                         l_info->m_codec_data.m_decompression.opj_end_decompress =
232                                         (opj_bool (*) (void *, struct opj_stream_private *, struct opj_event_mgr *))j2k_end_decompress;
233
234                         l_info->m_codec_data.m_decompression.opj_read_header =
235                                         (opj_bool (*) ( struct opj_stream_private *,
236                                                                         void *,
237                                                                         opj_file_info_t *,
238                                                                         struct opj_event_mgr * )) j2k_read_header;
239
240                         l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))j2k_destroy;
241
242                         l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * , opj_dparameters_t * )) j2k_setup_decoder_v2;
243
244                         l_info->m_codec_data.m_decompression.opj_read_tile_header =
245                                         (opj_bool (*) ( void *,
246                                                                         OPJ_UINT32*,
247                                                                         OPJ_UINT32*,
248                                                                         OPJ_INT32*, OPJ_INT32*,
249                                                                         OPJ_INT32*, OPJ_INT32*,
250                                                                         OPJ_UINT32*,
251                                                                         opj_bool*,
252                                                                         struct opj_stream_private *,
253                                                                         struct opj_event_mgr * )) j2k_read_tile_header;
254
255                         l_info->m_codec_data.m_decompression.opj_decode_tile_data =
256                                         (opj_bool (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, struct opj_stream_private *, struct opj_event_mgr *)) j2k_decode_tile;
257
258                         l_info->m_codec_data.m_decompression.opj_set_decode_area =
259                                         (opj_bool (*) (void *, OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, struct opj_event_mgr *)) j2k_set_decode_area;
260
261                         l_info->m_codec = j2k_create_decompress_v2();
262
263                         if (! l_info->m_codec) {
264                                 opj_free(l_info);
265                                 return NULL;
266                         }
267
268                         break;
269
270                 case CODEC_JP2:
271                         /* get a JP2 decoder handle */
272                         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
273
274                         l_info->m_codec_data.m_decompression.opj_end_decompress =  (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
275
276                         l_info->m_codec_data.m_decompression.opj_read_header =  (opj_bool (*) (
277                                         struct opj_stream_private *,
278                                         void *,
279                                         opj_file_info_t *,
280                                         struct opj_event_mgr * )) jp2_read_header;
281
282                         l_info->m_codec_data.m_decompression.opj_read_tile_header = ( opj_bool (*) (
283                                         void *,
284                                         OPJ_UINT32*,
285                                         OPJ_UINT32*,
286                                         OPJ_INT32*,
287                                         OPJ_INT32*,
288                                         OPJ_INT32 * ,
289                                         OPJ_INT32 * ,
290                                         OPJ_UINT32 * ,
291                                         opj_bool *,
292                                         struct opj_stream_private *,
293                                         struct opj_event_mgr * )) jp2_read_tile_header;
294
295                         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;
296
297                         l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
298
299                         l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder_v2;
300
301                         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;
302
303                         l_info->m_codec = jp2_create(OPJ_TRUE);
304
305                         if (! l_info->m_codec) {
306                                 opj_free(l_info);
307                                 return 00;
308                         }
309
310                         break;
311                 case CODEC_UNKNOWN:
312                 case CODEC_JPT:
313                 default:
314                         opj_free(l_info);
315                         return 00;
316         }
317
318         // FIXME set_default_event_handler(&(l_info->m_event_mgr));
319         return (opj_codec_t*) l_info;
320 }
321
322
323 void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
324         if(dinfo) {
325                 /* destroy the codec */
326                 switch(dinfo->codec_format) {
327                         case CODEC_J2K:
328                         case CODEC_JPT:
329                                 j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
330                                 break;
331                         case CODEC_JP2:
332                                 jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
333                                 break;
334                         case CODEC_UNKNOWN:
335                         default:
336                                 break;
337                 }
338                 /* destroy the decompressor */
339                 opj_free(dinfo);
340         }
341 }
342
343 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
344         if(parameters) {
345                 memset(parameters, 0, sizeof(opj_dparameters_t));
346                 /* default decoding parameters */
347                 parameters->cp_layer = 0;
348                 parameters->cp_reduce = 0;
349                 parameters->cp_limit_decoding = NO_LIMITATION;
350
351                 parameters->decod_format = -1;
352                 parameters->cod_format = -1;
353 /* UniPG>> */
354 #ifdef USE_JPWL
355                 parameters->jpwl_correct = OPJ_FALSE;
356                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
357                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
358 #endif /* USE_JPWL */
359 /* <<UniPG */
360         }
361 }
362
363 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
364         if(dinfo && parameters) {
365                 switch(dinfo->codec_format) {
366                         case CODEC_J2K:
367                         case CODEC_JPT:
368                                 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
369                                 break;
370                         case CODEC_JP2:
371                                 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
372                                 break;
373                         case CODEC_UNKNOWN:
374                         default:
375                                 break;
376                 }
377         }
378 }
379
380 opj_bool OPJ_CALLCONV opj_setup_decoder_v2(opj_codec_t *p_info, opj_dparameters_t *parameters, opj_event_mgr_t* event_mgr)
381 {
382         opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
383
384         if ( !p_info || !parameters || !event_mgr ){
385                 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
386                 return OPJ_FALSE;
387         }
388
389         if ( !event_mgr->error_handler || !event_mgr->warning_handler || !event_mgr->error_handler){
390                 fprintf(stderr, "[ERROR] Event handler provided to the setup_decoder function is not valid.\n");
391                 return OPJ_FALSE;
392         }
393
394         if (! l_info->is_decompressor) {
395                 opj_event_msg_v2(event_mgr, EVT_ERROR, "Codec provided to the setup_decoder function is not a decompressor handler.\n");
396                 return OPJ_FALSE;
397         }
398
399         l_info->m_codec_data.m_decompression.opj_setup_decoder(l_info->m_codec, parameters);
400
401         l_info->m_event_mgr = event_mgr;
402
403         return OPJ_TRUE;
404 }
405
406 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
407         return opj_decode_with_info(dinfo, cio, NULL);
408 }
409
410 opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
411         if(dinfo && cio) {
412                 switch(dinfo->codec_format) {
413                         case CODEC_J2K:
414                                 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
415                         case CODEC_JPT:
416                                 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
417                         case CODEC_JP2:
418                                 return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
419                         case CODEC_UNKNOWN:
420                         default:
421                                 break;
422                 }
423         }
424         return NULL;
425 }
426
427 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
428         opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
429         if(!cinfo) return NULL;
430         cinfo->is_decompressor = OPJ_FALSE;
431         switch(format) {
432                 case CODEC_J2K:
433                         /* get a J2K coder handle */
434                         cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
435                         if(!cinfo->j2k_handle) {
436                                 opj_free(cinfo);
437                                 return NULL;
438                         }
439                         break;
440                 case CODEC_JP2:
441                         /* get a JP2 coder handle */
442                         cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
443                         if(!cinfo->jp2_handle) {
444                                 opj_free(cinfo);
445                                 return NULL;
446                         }
447                         break;
448                 case CODEC_JPT:
449                 case CODEC_UNKNOWN:
450                 default:
451                         opj_free(cinfo);
452                         return NULL;
453         }
454
455         cinfo->codec_format = format;
456
457         return cinfo;
458 }
459
460 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
461         if(cinfo) {
462                 /* destroy the codec */
463                 switch(cinfo->codec_format) {
464                         case CODEC_J2K:
465                                 j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
466                                 break;
467                         case CODEC_JP2:
468                                 jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
469                                 break;
470                         case CODEC_JPT:
471                         case CODEC_UNKNOWN:
472                         default:
473                                 break;
474                 }
475                 /* destroy the decompressor */
476                 opj_free(cinfo);
477         }
478 }
479
480 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
481         if(parameters) {
482                 memset(parameters, 0, sizeof(opj_cparameters_t));
483                 /* default coding parameters */
484                 parameters->cp_cinema = OFF; 
485                 parameters->max_comp_size = 0;
486                 parameters->numresolution = 6;
487                 parameters->cp_rsiz = STD_RSIZ;
488                 parameters->cblockw_init = 64;
489                 parameters->cblockh_init = 64;
490                 parameters->prog_order = LRCP;
491                 parameters->roi_compno = -1;            /* no ROI */
492                 parameters->subsampling_dx = 1;
493                 parameters->subsampling_dy = 1;
494                 parameters->tp_on = 0;
495                 parameters->decod_format = -1;
496                 parameters->cod_format = -1;
497                 parameters->tcp_rates[0] = 0;   
498                 parameters->tcp_numlayers = 0;
499                 parameters->cp_disto_alloc = 0;
500                 parameters->cp_fixed_alloc = 0;
501                 parameters->cp_fixed_quality = 0;
502                 parameters->jpip_on = OPJ_FALSE;
503 /* UniPG>> */
504 #ifdef USE_JPWL
505                 parameters->jpwl_epc_on = OPJ_FALSE;
506                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
507                 {
508                         int i;
509                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
510                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
511                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
512                         }
513                 };
514                 {
515                         int i;
516                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
517                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
518                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
519                                 parameters->jpwl_pprot[i] = 0; /* absent */
520                         }
521                 };
522                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
523                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
524                 parameters->jpwl_sens_range = 0; /* 0 means packet */
525                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
526                 {
527                         int i;
528                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
529                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
530                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
531                         }
532                 };
533 #endif /* USE_JPWL */
534 /* <<UniPG */
535         }
536 }
537
538 /**
539  * Helper function.
540  * Sets the stream to be a file stream. The FILE must have been open previously.
541  * @param               p_stream        the stream to modify
542  * @param               p_file          handler to an already open file.
543 */
544 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
545 {
546         return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
547 }
548
549 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file, OPJ_UINT32 p_size, opj_bool p_is_read_stream)
550 {
551         opj_stream_t* l_stream = 00;
552
553         if (! p_file) {
554                 return NULL;
555         }
556
557         l_stream = opj_stream_create(p_size,p_is_read_stream);
558         if (! l_stream) {
559                 return NULL;
560         }
561
562         opj_stream_set_user_data(l_stream, p_file);
563         opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
564         opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
565         opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
566         opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
567
568         return l_stream;
569 }
570
571 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
572         if(cinfo && parameters && image) {
573                 switch(cinfo->codec_format) {
574                         case CODEC_J2K:
575                                 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
576                                 break;
577                         case CODEC_JP2:
578                                 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
579                                 break;
580                         case CODEC_JPT:
581                         case CODEC_UNKNOWN:
582                         default:
583                                 break;
584                 }
585         }
586 }
587
588 opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
589         if (index != NULL)
590                 opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
591                 "To extract the index, use the opj_encode_with_info() function.\n"
592                 "No index will be generated during this encoding\n");
593         return opj_encode_with_info(cinfo, cio, image, NULL);
594 }
595
596 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) {
597         if(cinfo && cio && image) {
598                 switch(cinfo->codec_format) {
599                         case CODEC_J2K:
600                                 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
601                         case CODEC_JP2:
602                                 return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);        
603                         case CODEC_JPT:
604                         case CODEC_UNKNOWN:
605                         default:
606                                 break;
607                 }
608         }
609         return OPJ_FALSE;
610 }
611
612 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
613         if (cstr_info) {
614                 int tileno;
615                 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
616                         opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
617                         opj_free(tile_info->thresh);
618                         opj_free(tile_info->packet);
619                         opj_free(tile_info->tp);
620                         opj_free(tile_info->marker);
621                 }
622                 opj_free(cstr_info->tile);
623                 opj_free(cstr_info->marker);
624                 opj_free(cstr_info->numdecompos);
625         }
626 }
627
628
629
630 #ifdef OLD_WAY_MS
631 opj_bool OPJ_CALLCONV opj_read_header (
632                                                                    opj_codec_t *p_codec,
633                                                                    opj_image_t ** p_image,
634                                                                    OPJ_INT32 * p_tile_x0,
635                                                                    OPJ_INT32 * p_tile_y0,
636                                                                    OPJ_UINT32 * p_tile_width,
637                                                                    OPJ_UINT32 * p_tile_height,
638                                                                    OPJ_UINT32 * p_nb_tiles_x,
639                                                                    OPJ_UINT32 * p_nb_tiles_y,
640                                                                    opj_stream_t *p_cio)
641 {
642         if (p_codec && p_cio) {
643                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
644                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
645
646                 if(! l_info->is_decompressor) {
647                         return OPJ_FALSE;
648                 }
649
650                 return l_info->m_codec_data.m_decompression.opj_read_header(
651                         l_info->m_codec,
652                         p_image,
653                         p_tile_x0,
654                         p_tile_y0,
655                         p_tile_width,
656                         p_tile_height,
657                         p_nb_tiles_x,
658                         p_nb_tiles_y,
659                         l_cio,
660                         l_info->m_event_mgr); //&(l_info->m_event_mgr));
661         }
662         return OPJ_FALSE;
663 }
664 #endif
665
666 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_cio,
667                                                                                 opj_codec_t *p_codec,
668                                                                                 opj_file_info_t* p_file_info,
669                                                                                 OPJ_INT32 file_info_flag)
670
671 {
672         /* Initialize the output structure */
673         opj_initialise_file_info(p_file_info, file_info_flag, CODEC_J2K);
674
675         if (p_codec && p_cio) {
676                 opj_codec_private_t* l_info = (opj_codec_private_t*) p_codec;
677                 opj_stream_private_t* l_cio = (opj_stream_private_t*) p_cio;
678
679                 if(! l_info->is_decompressor) {
680                         opj_event_msg_v2(l_info->m_event_mgr, EVT_ERROR, "Codec provided to the read_header function is not a decompressor handler.\n");
681                         return OPJ_FALSE;
682                 }
683
684                 return l_info->m_codec_data.m_decompression.opj_read_header(
685                                         l_cio,
686                                         l_info->m_codec,
687                                         p_file_info,
688                                         l_info->m_event_mgr);
689         }
690
691         fprintf(stderr, "[ERROR] Input parameters of the read_header function are incorrect.\n");
692         return OPJ_FALSE;
693 }
694
695
696 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info)
697 {
698         if (p_info) {
699                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
700
701                 if (l_info->is_decompressor) {
702                         l_info->m_codec_data.m_decompression.opj_destroy(l_info->m_codec);
703                 }
704                 else {
705                         l_info->m_codec_data.m_compression.opj_destroy(l_info->m_codec);
706                 }
707
708                 l_info->m_codec = 00;
709                 opj_free(l_info);
710         }
711 }
712
713 /**
714  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
715  *
716  * @param       p_codec                 the jpeg2000 codec.
717  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
718  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
719  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
720  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
721  *
722  * @return      true                    if the area could be set.
723  */
724 opj_bool OPJ_CALLCONV opj_set_decode_area(      opj_codec_t *p_codec,
725                                                                                         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
726                                                                                         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
727                                                                                         )
728 {
729         if (p_codec) {
730                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
731                 if (! l_info->is_decompressor) {
732                         return OPJ_FALSE;
733                 }
734
735                 return  l_info->m_codec_data.m_decompression.opj_set_decode_area(
736                                 l_info->m_codec,
737                                 p_start_x,
738                                 p_start_y,
739                                 p_end_x,
740                                 p_end_y,
741                                 l_info->m_event_mgr);
742
743         }
744         return OPJ_FALSE;
745 }
746
747 /**
748  * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
749  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
750  *
751  * @param       p_codec                 the jpeg2000 codec.
752  * @param       p_tile_index    pointer to a value that will hold the index of the tile being decoded, in case of success.
753  * @param       p_data_size             pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
754  *                                                      of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
755  *                                                      as depicted in opj_write_tile.
756  * @param       p_tile_x0               pointer to a value that will hold the x0 pos of the tile (in the image).
757  * @param       p_tile_y0               pointer to a value that will hold the y0 pos of the tile (in the image).
758  * @param       p_tile_x1               pointer to a value that will hold the x1 pos of the tile (in the image).
759  * @param       p_tile_y1               pointer to a value that will hold the y1 pos of the tile (in the image).
760  * @param       p_nb_comps              pointer to a value that will hold the number of components in the tile.
761  * @param       p_should_go_on  pointer to a boolean that will hold the fact that the decoding should go on. In case the
762  *                                                      codestream is over at the time of the call, the value will be set to false. The user should then stop
763  *                                                      the decoding.
764  * @param       p_stream                the stream to decode.
765  * @return      true                    if the tile header could be decoded. In case the decoding should end, the returned value is still true.
766  *                                                      returning false may be the result of a shortage of memory or an internal error.
767  */
768 opj_bool OPJ_CALLCONV opj_read_tile_header(
769                                         opj_codec_t *p_codec,
770                                         opj_stream_t * p_stream,
771                                         OPJ_UINT32 * p_tile_index,
772                                         OPJ_UINT32 * p_data_size,
773                                         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
774                                         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
775                                         OPJ_UINT32 * p_nb_comps,
776                                         opj_bool * p_should_go_on)
777 {
778         if (p_codec && p_stream && p_data_size && p_tile_index) {
779                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
780                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
781
782                 if (! l_info->is_decompressor) {
783                         return OPJ_FALSE;
784                 }
785
786                 return l_info->m_codec_data.m_decompression.opj_read_tile_header(
787                         l_info->m_codec,
788                         p_tile_index,
789                         p_data_size,
790                         p_tile_x0, p_tile_y0,
791                         p_tile_x1, p_tile_y1,
792                         p_nb_comps,
793                         p_should_go_on,
794                         l_cio,
795                         l_info->m_event_mgr);
796         }
797         return OPJ_FALSE;
798 }
799
800 /**
801  * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
802  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
803  *
804  * @param       p_codec                 the jpeg2000 codec.
805  * @param       p_tile_index    the index of the tile being decoded, this should be the value set by opj_read_tile_header.
806  * @param       p_data                  pointer to a memory block that will hold the decoded data.
807  * @param       p_data_size             size of p_data. p_data_size should be bigger or equal to the value set by opj_read_tile_header.
808  * @param       p_stream                the stream to decode.
809  *
810  * @return      true                    if the data could be decoded.
811  */
812 opj_bool OPJ_CALLCONV opj_decode_tile_data(
813                                         opj_codec_t *p_codec,
814                                         OPJ_UINT32 p_tile_index,
815                                         OPJ_BYTE * p_data,
816                                         OPJ_UINT32 p_data_size,
817                                         opj_stream_t *p_stream
818                                         )
819 {
820         if (p_codec && p_data && p_stream) {
821                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
822                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
823
824                 if (! l_info->is_decompressor) {
825                         return OPJ_FALSE;
826                 }
827
828                 return l_info->m_codec_data.m_decompression.opj_decode_tile_data(       l_info->m_codec,
829                                                                                                                                                         p_tile_index,
830                                                                                                                                                         p_data,
831                                                                                                                                                         p_data_size,
832                                                                                                                                                         l_cio,
833                                                                                                                                                         l_info->m_event_mgr);
834         }
835         return OPJ_FALSE;
836 }