[trunk] move functions in openjpeg.c in two parts compression vs decompression
[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_image_t **p_image,
44                                                                         struct opj_event_mgr * p_manager);
45         /** Decoding function */
46         opj_bool (*opj_decode) (        void * p_codec,
47                                                                 struct opj_stream_private *p_cio,
48                                                                 opj_image_t *p_image,
49                                                                 struct opj_event_mgr * p_manager);
50         /** FIXME DOC */
51         opj_bool (*opj_read_tile_header)(       void * p_codec,
52                                                                                 OPJ_UINT32 * p_tile_index,
53                                                                                 OPJ_UINT32* p_data_size,
54                                                                                 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
55                                                                                 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
56                                                                                 OPJ_UINT32 * p_nb_comps,
57                                                                                 opj_bool * p_should_go_on,
58                                                                                 struct opj_stream_private *p_cio,
59                                                                                 struct opj_event_mgr * p_manager);
60         /** FIXME DOC */
61         opj_bool (*opj_decode_tile_data)(       void * p_codec,
62                                                                                 OPJ_UINT32 p_tile_index,
63                                                                                 OPJ_BYTE * p_data,
64                                                                                 OPJ_UINT32 p_data_size,
65                                                                                 struct opj_stream_private *p_cio,
66                                                                                 struct opj_event_mgr * p_manager);
67         /** Reading function used after codestream if necessary */
68         opj_bool (* opj_end_decompress) (       void *p_codec,
69                                                                                 struct opj_stream_private *cio,
70                                                                                 struct opj_event_mgr * p_manager);
71         /** Codec destroy function handler*/
72         void (*opj_destroy) (void * p_codec);
73         /** Setup decoder function handler */
74         void (*opj_setup_decoder) (void * p_codec, opj_dparameters_t * p_param);
75         /** Set decode area function handler */
76         opj_bool (*opj_set_decode_area) (       void * p_codec,
77                                                                                 opj_image_t* p_image,
78                                                                                 OPJ_INT32 p_start_x, OPJ_INT32 p_end_x,
79                                                                                 OPJ_INT32 p_start_y, OPJ_INT32 p_end_y,
80                                                                                 struct opj_event_mgr * p_manager);
81
82         /** Get tile function */
83         opj_bool (*opj_get_decoded_tile) (      void *p_codec,
84                                                                                 opj_stream_private_t *p_cio,
85                                                                                 opj_image_t *p_image,
86                                                                                 struct opj_event_mgr * p_manager,
87                                                                                 OPJ_UINT32 tile_index);
88
89         /** Set the decoded resolution factor */
90         opj_bool (*opj_set_decoded_resolution_factor) (void * p_codec, OPJ_UINT32 res_factor, struct opj_event_mgr * p_manager);
91
92 }opj_decompression_t;
93
94 /**
95  * Compression handler. FIXME DOC
96  */
97 typedef struct opj_compression
98 {
99         opj_bool (* opj_start_compress) (       void *p_codec,
100                                                                                 struct opj_stream_private *cio,
101                                                                                 struct opj_image * p_image,
102                                                                                 struct opj_event_mgr * p_manager);
103
104         opj_bool (* opj_encode) (       void * p_codec,
105                                                                 struct opj_stream_private *p_cio,
106                                                                 struct opj_event_mgr * p_manager);
107
108         opj_bool (* opj_write_tile) (   void * p_codec,
109                                                                         OPJ_UINT32 p_tile_index,
110                                                                         OPJ_BYTE * p_data,
111                                                                         OPJ_UINT32 p_data_size,
112                                                                         struct opj_stream_private * p_cio,
113                                                                         struct opj_event_mgr * p_manager);
114
115         opj_bool (* opj_end_compress) ( void * p_codec,
116                                                                         struct opj_stream_private *p_cio,
117                                                                         struct opj_event_mgr * p_manager);
118
119         void (* opj_destroy) (void * p_codec);
120
121         void (*opj_setup_encoder) (     void * p_codec,
122                                                                 opj_cparameters_t * p_param,
123                                                                 struct opj_image * p_image,
124                                                                 struct opj_event_mgr * p_manager);
125
126 }opj_compression_t;
127
128 /**
129  * Main codec handler used for compression or decompression.
130  */
131 typedef struct opj_codec_private
132 {
133         /** FIXME DOC */
134         union 
135     {
136         opj_decompression_t m_decompression;
137         opj_compression_t m_compression;
138     } m_codec_data;
139     /** FIXME DOC*/
140         void * m_codec;
141         /** Event handler */
142         opj_event_mgr_t m_event_mgr;
143         /** Flag to indicate if the codec is used to decode or encode*/
144         opj_bool is_decompressor;
145         void (*opj_dump_codec) (void * p_codec, OPJ_INT32 info_flag, FILE* output_stream);
146         opj_codestream_info_v2_t* (*opj_get_codec_info)(void* p_codec);
147         opj_codestream_index_t* (*opj_get_codec_index)(void* p_codec);
148 }
149 opj_codec_private_t;
150
151 /* ---------------------------------------------------------------------- */
152 /**
153  * Default callback function.
154  * Do nothing.
155  */
156 void opj_default_callback (const char *msg, void *client_data)
157 {
158 }
159
160 void set_default_event_handler(opj_event_mgr_t * p_manager)
161 {
162         p_manager->m_error_data = 00;
163         p_manager->m_warning_data = 00;
164         p_manager->m_info_data = 00;
165         p_manager->error_handler = opj_default_callback;
166         p_manager->info_handler = opj_default_callback;
167         p_manager->warning_handler = opj_default_callback;
168 }
169
170 opj_bool OPJ_CALLCONV opj_set_info_handler(     opj_codec_t * p_codec, 
171                                                                                         opj_msg_callback p_callback,
172                                                                                         void * p_user_data)
173 {
174         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
175         if(! l_codec){
176                 return OPJ_FALSE;
177         }
178         
179         l_codec->m_event_mgr.info_handler = p_callback;
180         l_codec->m_event_mgr.m_info_data = p_user_data;
181         
182         return OPJ_TRUE;
183 }
184
185 opj_bool OPJ_CALLCONV opj_set_warning_handler(  opj_codec_t * p_codec, 
186                                                                                                 opj_msg_callback p_callback,
187                                                                                                 void * p_user_data)
188 {
189         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
190         if (! l_codec) {
191                 return OPJ_FALSE;
192         }
193         
194         l_codec->m_event_mgr.warning_handler = p_callback;
195         l_codec->m_event_mgr.m_warning_data = p_user_data;
196         
197         return OPJ_TRUE;
198 }
199
200 opj_bool OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, 
201                                                                                         opj_msg_callback p_callback,
202                                                                                         void * p_user_data)
203 {
204         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
205         if (! l_codec) {
206                 return OPJ_FALSE;
207         }
208         
209         l_codec->m_event_mgr.error_handler = p_callback;
210         l_codec->m_event_mgr.m_error_data = p_user_data;
211         
212         return OPJ_TRUE;
213 }
214
215 /* ---------------------------------------------------------------------- */
216
217
218 OPJ_SIZE_T opj_read_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
219 {
220         OPJ_SIZE_T l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
221         return l_nb_read ? l_nb_read : -1;
222 }
223
224 OPJ_UINT64 opj_get_data_length_from_file (FILE * p_file)
225 {
226         OPJ_OFF_T file_length = 0;
227
228         OPJ_FSEEK(p_file, 0, SEEK_END);
229         file_length = (OPJ_UINT64)OPJ_FTELL(p_file);
230         OPJ_FSEEK(p_file, 0, SEEK_SET);
231
232         return file_length;
233 }
234
235 OPJ_SIZE_T opj_write_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
236 {
237         return fwrite(p_buffer,1,p_nb_bytes,p_file);
238 }
239
240 OPJ_OFF_T opj_skip_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
241 {
242         if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_CUR)) {
243                 return -1;
244         }
245
246         return p_nb_bytes;
247 }
248
249 opj_bool opj_seek_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
250 {
251         if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_SET)) {
252                 return OPJ_FALSE;
253         }
254
255         return OPJ_TRUE;
256 }
257
258 /* ---------------------------------------------------------------------- */
259 #ifdef _WIN32
260 #ifndef OPJ_STATIC
261 BOOL APIENTRY
262 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
263
264         OPJ_ARG_NOT_USED(lpReserved);
265         OPJ_ARG_NOT_USED(hModule);
266
267         switch (ul_reason_for_call) {
268                 case DLL_PROCESS_ATTACH :
269                         break;
270                 case DLL_PROCESS_DETACH :
271                         break;
272                 case DLL_THREAD_ATTACH :
273                 case DLL_THREAD_DETACH :
274                         break;
275     }
276
277     return TRUE;
278 }
279 #endif /* OPJ_STATIC */
280 #endif /* _WIN32 */
281
282 /* ---------------------------------------------------------------------- */
283
284
285 const char* OPJ_CALLCONV opj_version(void) {
286     return PACKAGE_VERSION;
287 }
288
289 /* ---------------------------------------------------------------------- */
290 /* DECOMPRESSION FUNCTIONS*/
291
292 opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format)
293 {
294         opj_codec_private_t *l_codec = 00;
295
296         l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
297         if (!l_codec){
298                 return 00;
299         }
300         memset(l_codec, 0, sizeof(opj_codec_private_t));
301
302         l_codec->is_decompressor = 1;
303
304         switch (p_format) {
305                 case CODEC_J2K:
306                         l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
307
308                         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
309
310                         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
311
312                         l_codec->m_codec_data.m_decompression.opj_decode =
313                                         (opj_bool (*) ( void *,
314                                                                         struct opj_stream_private *,
315                                                                         opj_image_t*, struct opj_event_mgr * )) j2k_decode_v2;
316
317                         l_codec->m_codec_data.m_decompression.opj_end_decompress =
318                                         (opj_bool (*) ( void *,
319                                                                         struct opj_stream_private *,
320                                                                         struct opj_event_mgr *)) j2k_end_decompress;
321
322                         l_codec->m_codec_data.m_decompression.opj_read_header =
323                                         (opj_bool (*) ( struct opj_stream_private *,
324                                                                         void *,
325                                                                         opj_image_t **,
326                                                                         struct opj_event_mgr * )) j2k_read_header;
327
328                         l_codec->m_codec_data.m_decompression.opj_destroy =
329                                         (void (*) (void *))j2k_destroy;
330
331                         l_codec->m_codec_data.m_decompression.opj_setup_decoder =
332                                         (void (*) (void * , opj_dparameters_t * )) j2k_setup_decoder_v2;
333
334                         l_codec->m_codec_data.m_decompression.opj_read_tile_header =
335                                         (opj_bool (*) ( void *,
336                                                                         OPJ_UINT32*,
337                                                                         OPJ_UINT32*,
338                                                                         OPJ_INT32*, OPJ_INT32*,
339                                                                         OPJ_INT32*, OPJ_INT32*,
340                                                                         OPJ_UINT32*,
341                                                                         opj_bool*,
342                                                                         struct opj_stream_private *,
343                                                                         struct opj_event_mgr * )) j2k_read_tile_header;
344
345                         l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
346                                         (opj_bool (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, struct opj_stream_private *, struct opj_event_mgr *)) j2k_decode_tile;
347
348                         l_codec->m_codec_data.m_decompression.opj_set_decode_area =
349                                         (opj_bool (*) (void *, opj_image_t*, OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, struct opj_event_mgr *)) j2k_set_decode_area;
350
351                         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (void *p_codec,
352                                                                                                                                                                                 opj_stream_private_t *p_cio,
353                                                                                                                                                                                 opj_image_t *p_image,
354                                                                                                                                                                                 struct opj_event_mgr * p_manager,
355                                                                                                                                                                                 OPJ_UINT32 tile_index)) j2k_get_tile;
356
357                         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
358                                                                                                                                                                                                         OPJ_UINT32 res_factor,
359                                                                                                                                                                                                         struct opj_event_mgr * p_manager)) j2k_set_decoded_resolution_factor;
360
361                         l_codec->m_codec = j2k_create_decompress_v2();
362
363                         if (! l_codec->m_codec) {
364                                 opj_free(l_codec);
365                                 return NULL;
366                         }
367
368                         break;
369
370                 case CODEC_JP2:
371                         /* get a JP2 decoder handle */
372                         l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
373
374                         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
375
376                         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
377
378                         l_codec->m_codec_data.m_decompression.opj_decode =
379                                         (opj_bool (*) ( void *,
380                                                                         struct opj_stream_private *,
381                                                                         opj_image_t*,
382                                                                         struct opj_event_mgr * )) jp2_decode_v2;
383
384                         l_codec->m_codec_data.m_decompression.opj_end_decompress =  (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
385
386                         l_codec->m_codec_data.m_decompression.opj_read_header =  (opj_bool (*) (
387                                         struct opj_stream_private *,
388                                         void *,
389                                         opj_image_t **,
390                                         struct opj_event_mgr * )) jp2_read_header;
391
392                         l_codec->m_codec_data.m_decompression.opj_read_tile_header = ( opj_bool (*) (
393                                         void *,
394                                         OPJ_UINT32*,
395                                         OPJ_UINT32*,
396                                         OPJ_INT32*,
397                                         OPJ_INT32*,
398                                         OPJ_INT32 * ,
399                                         OPJ_INT32 * ,
400                                         OPJ_UINT32 * ,
401                                         opj_bool *,
402                                         struct opj_stream_private *,
403                                         struct opj_event_mgr * )) jp2_read_tile_header;
404
405                         l_codec->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 * )) jp2_decode_tile;
406
407                         l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
408
409                         l_codec->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder_v2;
410
411                         l_codec->m_codec_data.m_decompression.opj_set_decode_area = (opj_bool (*) (void *,opj_image_t*, OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) jp2_set_decode_area;
412
413                         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (void *p_codec,
414                                                                                                                                                                                 opj_stream_private_t *p_cio,
415                                                                                                                                                                                 opj_image_t *p_image,
416                                                                                                                                                                                 struct opj_event_mgr * p_manager,
417                                                                                                                                                                                 OPJ_UINT32 tile_index)) jp2_get_tile;
418
419                         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
420                                                                                                                                                                                                         OPJ_UINT32 res_factor,
421                                                                                                                                                                                                         opj_event_mgr_t * p_manager)) jp2_set_decoded_resolution_factor;
422
423                         l_codec->m_codec = jp2_create(OPJ_TRUE);
424
425                         if (! l_codec->m_codec) {
426                                 opj_free(l_codec);
427                                 return 00;
428                         }
429
430                         break;
431                 case CODEC_UNKNOWN:
432                 case CODEC_JPT:
433                 default:
434                         opj_free(l_codec);
435                         return 00;
436         }
437
438         set_default_event_handler(&(l_codec->m_event_mgr));
439         return (opj_codec_t*) l_codec;
440 }
441
442 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
443         if(parameters) {
444                 memset(parameters, 0, sizeof(opj_dparameters_t));
445                 /* default decoding parameters */
446                 parameters->cp_layer = 0;
447                 parameters->cp_reduce = 0;
448                 parameters->cp_limit_decoding = NO_LIMITATION;
449
450                 parameters->decod_format = -1;
451                 parameters->cod_format = -1;
452                 parameters->flags = 0;          
453 /* UniPG>> */
454 #ifdef USE_JPWL
455                 parameters->jpwl_correct = OPJ_FALSE;
456                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
457                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
458 #endif /* USE_JPWL */
459 /* <<UniPG */
460         }
461 }
462
463 opj_bool OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
464                                         opj_dparameters_t *parameters 
465                                                                                 )
466 {
467         if (p_codec && parameters) { 
468                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
469
470                 if (! l_codec->is_decompressor) {
471                         opj_event_msg_v2(&(l_codec->m_event_mgr), EVT_ERROR, "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
472                         return OPJ_FALSE;
473                 }
474
475                 l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
476                                                                                                                                 parameters);
477                 return OPJ_TRUE;
478         }
479         return OPJ_FALSE;
480 }
481
482 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
483                                                                                 opj_codec_t *p_codec,
484                                                                                 opj_image_t **p_image )
485 {
486         if (p_codec && p_stream) {
487                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
488                 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
489
490                 if(! l_codec->is_decompressor) {
491                         opj_event_msg_v2(&(l_codec->m_event_mgr), EVT_ERROR, "Codec provided to the opj_read_header function is not a decompressor handler.\n");
492                         return OPJ_FALSE;
493                 }
494
495                 return l_codec->m_codec_data.m_decompression.opj_read_header(   l_stream,
496                                                                                                                                                 l_codec->m_codec,
497                                                                                                                                                 p_image,
498                                                                                                                                                 &(l_codec->m_event_mgr) );
499         }
500
501         return OPJ_FALSE;
502 }
503
504 /*
505  *
506  *
507  */
508 opj_bool OPJ_CALLCONV opj_decode(   opj_codec_t *p_codec,
509                                     opj_stream_t *p_stream,
510                                     opj_image_t* p_image)
511 {
512         if (p_codec && p_stream) {
513                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
514                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
515
516                 if (! l_codec->is_decompressor) {
517                         return OPJ_FALSE;
518                 }
519
520                 return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
521                                                                                                                                 l_stream,
522                                                                                                                                 p_image,
523                                                                                                                                 &(l_codec->m_event_mgr) );
524         }
525
526         return OPJ_FALSE;
527 }
528
529
530 /**
531  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
532  *
533  * @param       p_codec                 the jpeg2000 codec.
534  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
535  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
536  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
537  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
538  *
539  * @return      true                    if the area could be set.
540  */
541 opj_bool OPJ_CALLCONV opj_set_decode_area(      opj_codec_t *p_codec,
542                                                                                         opj_image_t* p_image,
543                                                                                         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
544                                                                                         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
545                                                                                         )
546 {
547         if (p_codec) {
548                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
549                 
550                 if (! l_codec->is_decompressor) {
551                         return OPJ_FALSE;
552                 }
553
554                 return  l_codec->m_codec_data.m_decompression.opj_set_decode_area(      l_codec->m_codec,
555                                                                                                                                                         p_image,
556                                                                                                                                                         p_start_x, p_start_y,
557                                                                                                                                                         p_end_x, p_end_y,
558                                                                                                                                                         &(l_codec->m_event_mgr) );
559         }
560         return OPJ_FALSE;
561 }
562
563 /**
564  * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
565  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
566  *
567  * @param       p_codec                 the jpeg2000 codec.
568  * @param       p_tile_index    pointer to a value that will hold the index of the tile being decoded, in case of success.
569  * @param       p_data_size             pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
570  *                                                      of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
571  *                                                      as depicted in opj_write_tile.
572  * @param       p_tile_x0               pointer to a value that will hold the x0 pos of the tile (in the image).
573  * @param       p_tile_y0               pointer to a value that will hold the y0 pos of the tile (in the image).
574  * @param       p_tile_x1               pointer to a value that will hold the x1 pos of the tile (in the image).
575  * @param       p_tile_y1               pointer to a value that will hold the y1 pos of the tile (in the image).
576  * @param       p_nb_comps              pointer to a value that will hold the number of components in the tile.
577  * @param       p_should_go_on  pointer to a boolean that will hold the fact that the decoding should go on. In case the
578  *                                                      codestream is over at the time of the call, the value will be set to false. The user should then stop
579  *                                                      the decoding.
580  * @param       p_stream                the stream to decode.
581  * @return      true                    if the tile header could be decoded. In case the decoding should end, the returned value is still true.
582  *                                                      returning false may be the result of a shortage of memory or an internal error.
583  */
584 opj_bool OPJ_CALLCONV opj_read_tile_header(     opj_codec_t *p_codec,
585                                                                                         opj_stream_t * p_stream,
586                                                                                         OPJ_UINT32 * p_tile_index,
587                                                                                         OPJ_UINT32 * p_data_size,
588                                                                                         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
589                                                                                         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
590                                                                                         OPJ_UINT32 * p_nb_comps,
591                                                                                         opj_bool * p_should_go_on)
592 {
593         if (p_codec && p_stream && p_data_size && p_tile_index) {
594                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
595                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
596
597                 if (! l_codec->is_decompressor) {
598                         return OPJ_FALSE;
599                 }
600
601                 return l_codec->m_codec_data.m_decompression.opj_read_tile_header(      l_codec->m_codec,
602                                                                                                                                                         p_tile_index,
603                                                                                                                                                         p_data_size,
604                                                                                                                                                         p_tile_x0, p_tile_y0,
605                                                                                                                                                         p_tile_x1, p_tile_y1,
606                                                                                                                                                         p_nb_comps,
607                                                                                                                                                         p_should_go_on,
608                                                                                                                                                         l_stream,
609                                                                                                                                                         &(l_codec->m_event_mgr));
610         }
611         return OPJ_FALSE;
612 }
613
614 /**
615  * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
616  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
617  *
618  * @param       p_codec                 the jpeg2000 codec.
619  * @param       p_tile_index    the index of the tile being decoded, this should be the value set by opj_read_tile_header.
620  * @param       p_data                  pointer to a memory block that will hold the decoded data.
621  * @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.
622  * @param       p_stream                the stream to decode.
623  *
624  * @return      true                    if the data could be decoded.
625  */
626 opj_bool OPJ_CALLCONV opj_decode_tile_data(     opj_codec_t *p_codec,
627                                                                                         OPJ_UINT32 p_tile_index,
628                                                                                         OPJ_BYTE * p_data,
629                                                                                         OPJ_UINT32 p_data_size,
630                                                                                         opj_stream_t *p_stream
631                                                                                         )
632 {
633         if (p_codec && p_data && p_stream) {
634                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
635                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
636
637                 if (! l_codec->is_decompressor) {
638                         return OPJ_FALSE;
639                 }
640
641                 return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(      l_codec->m_codec,
642                                                                                                                                                         p_tile_index,
643                                                                                                                                                         p_data,
644                                                                                                                                                         p_data_size,
645                                                                                                                                                         l_stream,
646                                                                                                                                                         &(l_codec->m_event_mgr) );
647         }
648         return OPJ_FALSE;
649 }
650
651 /*
652  *
653  *
654  */
655 opj_bool OPJ_CALLCONV opj_get_decoded_tile(     opj_codec_t *p_codec,
656                                                                                         opj_stream_t *p_stream,
657                                                                                         opj_image_t *p_image,
658                                                                                         OPJ_UINT32 tile_index)
659 {
660         if (p_codec && p_stream) {
661                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
662                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
663
664                 if (! l_codec->is_decompressor) {
665                         return OPJ_FALSE;
666                 }
667                 
668                 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(      l_codec->m_codec,
669                                                                                                                                                         l_stream,
670                                                                                                                                                         p_image,
671                                                                                                                                                         &(l_codec->m_event_mgr),
672                                                                                                                                                         tile_index);
673         }
674
675         return OPJ_FALSE;
676 }
677
678 /*
679  *
680  *
681  */
682 opj_bool OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, 
683                                                                                                                 OPJ_UINT32 res_factor )
684 {
685         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
686
687         if ( !l_codec ){
688                 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
689                 return OPJ_FALSE;
690         }
691
692         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec, 
693                                                                                                                                                         res_factor,
694                                                                                                                                                         &(l_codec->m_event_mgr) );
695         return OPJ_TRUE;
696 }
697
698 /* ---------------------------------------------------------------------- */
699 /* COMPRESSION FUNCTIONS*/
700
701 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
702 {
703         opj_codec_private_t *l_codec = 00;
704
705         l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
706         if (!l_codec) {
707                 return 00;
708         }
709         memset(l_codec, 0, sizeof(opj_codec_private_t));
710         
711         l_codec->is_decompressor = 0;
712
713         switch(p_format) {
714                 case CODEC_J2K:
715                         l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
716                                                                                                                                                         struct opj_stream_private *,
717                                                                                                                                                         struct opj_event_mgr * )) j2k_encode_v2;
718
719                         l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) (  void *,
720                                                                                                                                                                         struct opj_stream_private *,
721                                                                                                                                                                         struct opj_event_mgr *)) j2k_end_compress;
722
723                         l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
724                                                                                                                                                                         struct opj_stream_private *,
725                                                                                                                                                                         struct opj_image * ,
726                                                                                                                                                                         struct opj_event_mgr *)) j2k_start_compress;
727
728                         l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
729                                                                                                                                                                 OPJ_UINT32,
730                                                                                                                                                                 OPJ_BYTE*,
731                                                                                                                                                                 OPJ_UINT32,
732                                                                                                                                                                 struct opj_stream_private *,
733                                                                                                                                                                 struct opj_event_mgr *) ) j2k_write_tile;
734
735                         l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) j2k_destroy;
736
737                         l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) (     void *,
738                                                                                                                                                                 opj_cparameters_t *,
739                                                                                                                                                                 struct opj_image *,
740                                                                                                                                                                 struct opj_event_mgr * )) j2k_setup_encoder_v2;
741
742                         l_codec->m_codec = j2k_create_compress_v2();
743                         if (! l_codec->m_codec) {
744                                 opj_free(l_codec);
745                                 return 00;
746                         }
747
748                         break;
749
750                 case CODEC_JP2:
751                         /* get a JP2 decoder handle */
752                         l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
753                                                                                                                                                         struct opj_stream_private *,
754                                                                                                                                                         struct opj_event_mgr * )) opj_jp2_encode_v2;
755
756                         l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) (  void *,
757                                                                                                                                                                         struct opj_stream_private *,
758                                                                                                                                                                         struct opj_event_mgr *)) jp2_end_compress;
759
760                         l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
761                                                                                                                                                                         struct opj_stream_private *,
762                                                                                                                                                                         struct opj_image * ,
763                                                                                                                                                                         struct opj_event_mgr *))  jp2_start_compress;
764
765                         l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
766                                                                                                                                                                 OPJ_UINT32,
767                                                                                                                                                                 OPJ_BYTE*,
768                                                                                                                                                                 OPJ_UINT32,
769                                                                                                                                                                 struct opj_stream_private *,
770                                                                                                                                                                 struct opj_event_mgr *)) jp2_write_tile;
771
772                         l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) jp2_destroy;
773
774                         l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) (     void *,
775                                                                                                                                                                 opj_cparameters_t *,
776                                                                                                                                                                 struct opj_image *,
777                                                                                                                                                                 struct opj_event_mgr * )) jp2_setup_encoder;
778
779                         l_codec->m_codec = jp2_create(OPJ_FALSE);
780                         if (! l_codec->m_codec) {
781                                 opj_free(l_codec);
782                                 return 00;
783                         }
784
785                         break;
786
787                 case CODEC_UNKNOWN:
788                 case CODEC_JPT:
789                 default:
790                         opj_free(l_codec);
791                         return 00;
792         }
793
794         set_default_event_handler(&(l_codec->m_event_mgr));
795         return (opj_codec_t*) l_codec;
796 }
797
798 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
799         if(parameters) {
800                 memset(parameters, 0, sizeof(opj_cparameters_t));
801                 /* default coding parameters */
802                 parameters->cp_cinema = OFF; 
803                 parameters->max_comp_size = 0;
804                 parameters->numresolution = 6;
805                 parameters->cp_rsiz = STD_RSIZ;
806                 parameters->cblockw_init = 64;
807                 parameters->cblockh_init = 64;
808                 parameters->prog_order = LRCP;
809                 parameters->roi_compno = -1;            /* no ROI */
810                 parameters->subsampling_dx = 1;
811                 parameters->subsampling_dy = 1;
812                 parameters->tp_on = 0;
813                 parameters->decod_format = -1;
814                 parameters->cod_format = -1;
815                 parameters->tcp_rates[0] = 0;   
816                 parameters->tcp_numlayers = 0;
817                 parameters->cp_disto_alloc = 0;
818                 parameters->cp_fixed_alloc = 0;
819                 parameters->cp_fixed_quality = 0;
820                 parameters->jpip_on = OPJ_FALSE;
821 /* UniPG>> */
822 #ifdef USE_JPWL
823                 parameters->jpwl_epc_on = OPJ_FALSE;
824                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
825                 {
826                         int i;
827                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
828                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
829                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
830                         }
831                 };
832                 {
833                         int i;
834                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
835                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
836                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
837                                 parameters->jpwl_pprot[i] = 0; /* absent */
838                         }
839                 };
840                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
841                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
842                 parameters->jpwl_sens_range = 0; /* 0 means packet */
843                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
844                 {
845                         int i;
846                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
847                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
848                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
849                         }
850                 };
851 #endif /* USE_JPWL */
852 /* <<UniPG */
853         }
854 }
855
856 opj_bool OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec, 
857                                                                                 opj_cparameters_t *parameters, 
858                                                                                 opj_image_t *p_image)
859 {
860         if (p_codec && parameters && p_image) {
861                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
862
863                 if (! l_codec->is_decompressor) {
864                         l_codec->m_codec_data.m_compression.opj_setup_encoder(  l_codec->m_codec,
865                                                                                                                                         parameters,
866                                                                                                                                         p_image,
867                                                                                                                                         &(l_codec->m_event_mgr) );
868                         return OPJ_TRUE;
869                 }
870         }
871
872         return OPJ_FALSE;
873 }
874
875 opj_bool OPJ_CALLCONV opj_start_compress (      opj_codec_t *p_codec,
876                                                                                         opj_image_t * p_image,
877                                                                                         opj_stream_t *p_stream)
878 {
879         if (p_codec && p_stream) {
880                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
881                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
882
883                 if (! l_codec->is_decompressor) {
884                         return l_codec->m_codec_data.m_compression.opj_start_compress(  l_codec->m_codec,
885                                                                                                                                                         l_stream,
886                                                                                                                                                         p_image,
887                                                                                                                                                         &(l_codec->m_event_mgr));
888                 }
889         }
890
891         return OPJ_FALSE;
892 }
893
894 opj_bool OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
895 {
896         if (p_info && p_stream) {
897                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
898                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
899
900                 if (! l_codec->is_decompressor) {
901                         l_codec->m_codec_data.m_compression.opj_encode( l_codec->m_codec,
902                                                                                                                         l_stream,
903                                                                                                                         &(l_codec->m_event_mgr));
904                         return OPJ_TRUE;
905                 }
906         }
907
908         return OPJ_FALSE;
909
910 }
911
912 opj_bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
913                                                                                 opj_stream_t *p_stream)
914 {
915         if (p_codec && p_stream) {
916                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
917                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
918
919                 if (! l_codec->is_decompressor) {
920                         return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
921                                                                                                                                                 l_stream,
922                                                                                                                                                 &(l_codec->m_event_mgr));
923                 }
924         }
925         return OPJ_FALSE;
926
927 }
928
929 /*
930  *
931  *
932  */
933 opj_bool OPJ_CALLCONV opj_end_decompress (      opj_codec_t *p_codec,
934                                                                                         opj_stream_t *p_stream)
935 {
936         if (p_codec && p_stream) {
937                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
938                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
939
940                 if (! l_codec->is_decompressor) {
941                         return OPJ_FALSE;
942                 }
943                 
944                 return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
945                                                                                                                                                 l_stream,
946                                                                                                                                                 &(l_codec->m_event_mgr) );
947         }
948
949         return OPJ_FALSE;
950 }
951
952
953 opj_bool OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
954 {
955         OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * sizeof(OPJ_FLOAT32);
956         OPJ_UINT32 l_dc_shift_size = pNbComp * sizeof(OPJ_INT32);
957         OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
958
959         /* add MCT capability */
960         int rsiz = (int)parameters->cp_rsiz | (int)MCT;
961         parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
962         parameters->irreversible = 1;
963
964         /* use array based MCT */
965         parameters->tcp_mct = 2;
966         parameters->mct_data = opj_malloc(l_mct_total_size);
967         if (! parameters->mct_data) {
968                 return OPJ_FALSE;
969         }
970
971         memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
972         memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size,p_dc_shift,l_dc_shift_size);
973
974         return OPJ_TRUE;
975 }
976
977 /**
978  * Writes a tile with the given data.
979  *
980  * @param       p_compressor            the jpeg2000 codec.
981  * @param       p_tile_index            the index of the tile to write. At the moment, the tiles must be written from 0 to n-1 in sequence.
982  * @param       p_data                          pointer to the data to write. Data is arranged in sequence, data_comp0, then data_comp1, then ... NO INTERLEAVING should be set.
983  * @param       p_data_size                     this value os used to make sure the data being written is correct. The size must be equal to the sum for each component of tile_width * tile_height * component_size. component_size can be 1,2 or 4 bytes,
984  *                                                              depending on the precision of the given component.
985  * @param       p_stream                        the stream to write data to.
986  */
987 opj_bool OPJ_CALLCONV opj_write_tile (  opj_codec_t *p_codec,
988                                                                                 OPJ_UINT32 p_tile_index,
989                                                                                 OPJ_BYTE * p_data,
990                                                                                 OPJ_UINT32 p_data_size,
991                                                                                 opj_stream_t *p_stream )
992 {
993         if (p_codec && p_stream && p_data) {
994                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
995                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
996
997                 if (l_codec->is_decompressor) {
998                         return OPJ_FALSE;
999                 }
1000
1001                 return l_codec->m_codec_data.m_compression.opj_write_tile(      l_codec->m_codec,
1002                                                                                                                                         p_tile_index,
1003                                                                                                                                         p_data,
1004                                                                                                                                         p_data_size,
1005                                                                                                                                         l_stream,
1006                                                                                                                                         &(l_codec->m_event_mgr) );
1007         }
1008
1009         return OPJ_FALSE;
1010 }
1011
1012
1013 /* DEPRECATED */
1014 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
1015         if (cstr_info) {
1016                 int tileno;
1017                 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
1018                         opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
1019                         opj_free(tile_info->thresh);
1020                         opj_free(tile_info->packet);
1021                         opj_free(tile_info->tp);
1022                         opj_free(tile_info->marker);
1023                 }
1024                 opj_free(cstr_info->tile);
1025                 opj_free(cstr_info->marker);
1026                 opj_free(cstr_info->numdecompos);
1027         }
1028 }
1029
1030
1031
1032
1033
1034 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
1035 {
1036         if (p_codec) {
1037                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1038
1039                 if (l_codec->is_decompressor) {
1040                         l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
1041                 }
1042                 else {
1043                         l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
1044                 }
1045
1046                 l_codec->m_codec = 00;
1047                 opj_free(l_codec);
1048         }
1049 }
1050
1051
1052
1053 /*
1054  *
1055  *
1056  */
1057 void OPJ_CALLCONV opj_dump_codec(       opj_codec_t *p_codec,
1058                                                                         OPJ_INT32 info_flag,
1059                                                                         FILE* output_stream)
1060 {
1061         if (p_codec) {
1062                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1063
1064                 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
1065                 return;
1066         }
1067
1068         fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
1069         return;
1070 }
1071
1072 /*
1073  *
1074  *
1075  */
1076 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
1077 {
1078         if (p_codec) {
1079                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1080
1081                 return l_codec->opj_get_codec_info(l_codec->m_codec);
1082         }
1083
1084         return NULL;
1085 }
1086
1087 /*
1088  *
1089  *
1090  */
1091 void OPJ_CALLCONV opj_destroy_cstr_info_v2(opj_codestream_info_v2_t **cstr_info) {
1092         if (cstr_info) {
1093
1094                 if ((*cstr_info)->m_default_tile_info.tccp_info){
1095                         opj_free((*cstr_info)->m_default_tile_info.tccp_info);
1096                 }
1097
1098                 if ((*cstr_info)->tile_info){
1099                         /* FIXME not used for the moment*/
1100                 }
1101
1102                 opj_free((*cstr_info));
1103                 (*cstr_info) = NULL;
1104         }
1105 }
1106
1107 /*
1108  *
1109  *
1110  */
1111 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
1112 {
1113         if (p_codec) {
1114                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1115
1116                 return l_codec->opj_get_codec_index(l_codec->m_codec);
1117         }
1118
1119         return NULL;
1120 }
1121
1122 /*
1123  *
1124  *
1125  */
1126 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
1127 {
1128         if (*p_cstr_index){
1129                 j2k_destroy_cstr_index(*p_cstr_index);
1130                 (*p_cstr_index) = NULL;
1131         }
1132 }
1133
1134
1135
1136
1137
1138 /* ---------------------------------------------------------------------- */
1139 /**
1140  * Helper function.
1141  * Sets the stream to be a file stream. The FILE must have been open previously.
1142  * @param               p_stream        the stream to modify
1143  * @param               p_file          handler to an already open file.
1144 */
1145 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
1146 {
1147         return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
1148 }
1149
1150 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (      FILE * p_file, 
1151                                                                                                                         OPJ_SIZE_T p_size, 
1152                                                                                                                         opj_bool p_is_read_stream)
1153 {
1154         opj_stream_t* l_stream = 00;
1155
1156         if (! p_file) {
1157                 return NULL;
1158         }
1159
1160         l_stream = opj_stream_create(p_size,p_is_read_stream);
1161         if (! l_stream) {
1162                 return NULL;
1163         }
1164
1165         opj_stream_set_user_data(l_stream, p_file);
1166         opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
1167         opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
1168         opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
1169         opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
1170         opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
1171
1172         return l_stream;
1173 }