[trunk] remove deprecated v1 style function opj_encode and opj_encode_with_info
[openjpeg.git] / libopenjpeg / openjpeg.c
1 /*
2  * Copyright (c) 2005, Herve Drolon, FreeImage Team
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifdef _WIN32
28 #include <windows.h>
29 #endif /* _WIN32 */
30
31 #include "opj_config.h"
32 #include "opj_includes.h"
33
34
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 /* ---------------------------------------------------------------------- */
483 /* COMPRESSION FUNCTIONS*/
484
485 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
486 {
487         opj_codec_private_t *l_codec = 00;
488
489         l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
490         if (!l_codec) {
491                 return 00;
492         }
493         memset(l_codec, 0, sizeof(opj_codec_private_t));
494         
495         l_codec->is_decompressor = 0;
496
497         switch(p_format) {
498                 case CODEC_J2K:
499                         l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
500                                                                                                                                                         struct opj_stream_private *,
501                                                                                                                                                         struct opj_event_mgr * )) j2k_encode_v2;
502
503                         l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) (  void *,
504                                                                                                                                                                         struct opj_stream_private *,
505                                                                                                                                                                         struct opj_event_mgr *)) j2k_end_compress;
506
507                         l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
508                                                                                                                                                                         struct opj_stream_private *,
509                                                                                                                                                                         struct opj_image * ,
510                                                                                                                                                                         struct opj_event_mgr *)) j2k_start_compress;
511
512                         l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
513                                                                                                                                                                 OPJ_UINT32,
514                                                                                                                                                                 OPJ_BYTE*,
515                                                                                                                                                                 OPJ_UINT32,
516                                                                                                                                                                 struct opj_stream_private *,
517                                                                                                                                                                 struct opj_event_mgr *) ) j2k_write_tile;
518
519                         l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) j2k_destroy;
520
521                         l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) (     void *,
522                                                                                                                                                                 opj_cparameters_t *,
523                                                                                                                                                                 struct opj_image *,
524                                                                                                                                                                 struct opj_event_mgr * )) j2k_setup_encoder_v2;
525
526                         l_codec->m_codec = j2k_create_compress_v2();
527                         if (! l_codec->m_codec) {
528                                 opj_free(l_codec);
529                                 return 00;
530                         }
531
532                         break;
533
534                 case CODEC_JP2:
535                         /* get a JP2 decoder handle */
536                         l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
537                                                                                                                                                         struct opj_stream_private *,
538                                                                                                                                                         struct opj_event_mgr * )) opj_jp2_encode_v2;
539
540                         l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) (  void *,
541                                                                                                                                                                         struct opj_stream_private *,
542                                                                                                                                                                         struct opj_event_mgr *)) jp2_end_compress;
543
544                         l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
545                                                                                                                                                                         struct opj_stream_private *,
546                                                                                                                                                                         struct opj_image * ,
547                                                                                                                                                                         struct opj_event_mgr *))  jp2_start_compress;
548
549                         l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
550                                                                                                                                                                 OPJ_UINT32,
551                                                                                                                                                                 OPJ_BYTE*,
552                                                                                                                                                                 OPJ_UINT32,
553                                                                                                                                                                 struct opj_stream_private *,
554                                                                                                                                                                 struct opj_event_mgr *)) jp2_write_tile;
555
556                         l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) jp2_destroy;
557
558                         l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) (     void *,
559                                                                                                                                                                 opj_cparameters_t *,
560                                                                                                                                                                 struct opj_image *,
561                                                                                                                                                                 struct opj_event_mgr * )) jp2_setup_encoder;
562
563                         l_codec->m_codec = jp2_create(OPJ_FALSE);
564                         if (! l_codec->m_codec) {
565                                 opj_free(l_codec);
566                                 return 00;
567                         }
568
569                         break;
570
571                 case CODEC_UNKNOWN:
572                 case CODEC_JPT:
573                 default:
574                         opj_free(l_codec);
575                         return 00;
576         }
577
578         set_default_event_handler(&(l_codec->m_event_mgr));
579         return (opj_codec_t*) l_codec;
580 }
581
582 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
583         if(parameters) {
584                 memset(parameters, 0, sizeof(opj_cparameters_t));
585                 /* default coding parameters */
586                 parameters->cp_cinema = OFF; 
587                 parameters->max_comp_size = 0;
588                 parameters->numresolution = 6;
589                 parameters->cp_rsiz = STD_RSIZ;
590                 parameters->cblockw_init = 64;
591                 parameters->cblockh_init = 64;
592                 parameters->prog_order = LRCP;
593                 parameters->roi_compno = -1;            /* no ROI */
594                 parameters->subsampling_dx = 1;
595                 parameters->subsampling_dy = 1;
596                 parameters->tp_on = 0;
597                 parameters->decod_format = -1;
598                 parameters->cod_format = -1;
599                 parameters->tcp_rates[0] = 0;   
600                 parameters->tcp_numlayers = 0;
601                 parameters->cp_disto_alloc = 0;
602                 parameters->cp_fixed_alloc = 0;
603                 parameters->cp_fixed_quality = 0;
604                 parameters->jpip_on = OPJ_FALSE;
605 /* UniPG>> */
606 #ifdef USE_JPWL
607                 parameters->jpwl_epc_on = OPJ_FALSE;
608                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
609                 {
610                         int i;
611                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
612                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
613                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
614                         }
615                 };
616                 {
617                         int i;
618                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
619                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
620                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
621                                 parameters->jpwl_pprot[i] = 0; /* absent */
622                         }
623                 };
624                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
625                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
626                 parameters->jpwl_sens_range = 0; /* 0 means packet */
627                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
628                 {
629                         int i;
630                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
631                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
632                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
633                         }
634                 };
635 #endif /* USE_JPWL */
636 /* <<UniPG */
637         }
638 }
639
640 opj_bool OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec, 
641                                                                                 opj_cparameters_t *parameters, 
642                                                                                 opj_image_t *p_image)
643 {
644         if (p_codec && parameters && p_image) {
645                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
646
647                 if (! l_codec->is_decompressor) {
648                         l_codec->m_codec_data.m_compression.opj_setup_encoder(  l_codec->m_codec,
649                                                                                                                                         parameters,
650                                                                                                                                         p_image,
651                                                                                                                                         &(l_codec->m_event_mgr) );
652                         return OPJ_TRUE;
653                 }
654         }
655
656         return OPJ_FALSE;
657 }
658
659 opj_bool OPJ_CALLCONV opj_start_compress (      opj_codec_t *p_codec,
660                                                                                         opj_image_t * p_image,
661                                                                                         opj_stream_t *p_stream)
662 {
663         if (p_codec && p_stream) {
664                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
665                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
666
667                 if (! l_codec->is_decompressor) {
668                         return l_codec->m_codec_data.m_compression.opj_start_compress(  l_codec->m_codec,
669                                                                                                                                                         l_stream,
670                                                                                                                                                         p_image,
671                                                                                                                                                         &(l_codec->m_event_mgr));
672                 }
673         }
674
675         return OPJ_FALSE;
676 }
677
678 opj_bool OPJ_CALLCONV opj_encode_v2(opj_codec_t *p_info, opj_stream_t *p_stream)
679 {
680         if (p_info && p_stream) {
681                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
682                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
683
684                 if (! l_codec->is_decompressor) {
685                         l_codec->m_codec_data.m_compression.opj_encode( l_codec->m_codec,
686                                                                                                                         l_stream,
687                                                                                                                         &(l_codec->m_event_mgr));
688                         return OPJ_TRUE;
689                 }
690         }
691
692         return OPJ_FALSE;
693
694 }
695
696 opj_bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
697                                                                                 opj_stream_t *p_stream)
698 {
699         if (p_codec && p_stream) {
700                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
701                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
702
703                 if (! l_codec->is_decompressor) {
704                         return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
705                                                                                                                                                 l_stream,
706                                                                                                                                                 &(l_codec->m_event_mgr));
707                 }
708         }
709         return OPJ_FALSE;
710
711 }
712
713
714
715 /* DEPRECATED */
716 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
717         if (cstr_info) {
718                 int tileno;
719                 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
720                         opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
721                         opj_free(tile_info->thresh);
722                         opj_free(tile_info->packet);
723                         opj_free(tile_info->tp);
724                         opj_free(tile_info->marker);
725                 }
726                 opj_free(cstr_info->tile);
727                 opj_free(cstr_info->marker);
728                 opj_free(cstr_info->numdecompos);
729         }
730 }
731
732
733 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
734                                                                                 opj_codec_t *p_codec,
735                                                                                 opj_image_t **p_image )
736 {
737         if (p_codec && p_stream) {
738                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
739                 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
740
741                 if(! l_codec->is_decompressor) {
742                         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");
743                         return OPJ_FALSE;
744                 }
745
746                 return l_codec->m_codec_data.m_decompression.opj_read_header(   l_stream,
747                                                                                                                                                 l_codec->m_codec,
748                                                                                                                                                 p_image,
749                                                                                                                                                 &(l_codec->m_event_mgr) );
750         }
751
752         return OPJ_FALSE;
753 }
754
755
756 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
757 {
758         if (p_codec) {
759                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
760
761                 if (l_codec->is_decompressor) {
762                         l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
763                 }
764                 else {
765                         l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
766                 }
767
768                 l_codec->m_codec = 00;
769                 opj_free(l_codec);
770         }
771 }
772
773 /**
774  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
775  *
776  * @param       p_codec                 the jpeg2000 codec.
777  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
778  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
779  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
780  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
781  *
782  * @return      true                    if the area could be set.
783  */
784 opj_bool OPJ_CALLCONV opj_set_decode_area(      opj_codec_t *p_codec,
785                                                                                         opj_image_t* p_image,
786                                                                                         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
787                                                                                         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
788                                                                                         )
789 {
790         if (p_codec) {
791                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
792                 
793                 if (! l_codec->is_decompressor) {
794                         return OPJ_FALSE;
795                 }
796
797                 return  l_codec->m_codec_data.m_decompression.opj_set_decode_area(      l_codec->m_codec,
798                                                                                                                                                         p_image,
799                                                                                                                                                         p_start_x, p_start_y,
800                                                                                                                                                         p_end_x, p_end_y,
801                                                                                                                                                         &(l_codec->m_event_mgr) );
802         }
803         return OPJ_FALSE;
804 }
805
806 /**
807  * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
808  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
809  *
810  * @param       p_codec                 the jpeg2000 codec.
811  * @param       p_tile_index    pointer to a value that will hold the index of the tile being decoded, in case of success.
812  * @param       p_data_size             pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
813  *                                                      of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
814  *                                                      as depicted in opj_write_tile.
815  * @param       p_tile_x0               pointer to a value that will hold the x0 pos of the tile (in the image).
816  * @param       p_tile_y0               pointer to a value that will hold the y0 pos of the tile (in the image).
817  * @param       p_tile_x1               pointer to a value that will hold the x1 pos of the tile (in the image).
818  * @param       p_tile_y1               pointer to a value that will hold the y1 pos of the tile (in the image).
819  * @param       p_nb_comps              pointer to a value that will hold the number of components in the tile.
820  * @param       p_should_go_on  pointer to a boolean that will hold the fact that the decoding should go on. In case the
821  *                                                      codestream is over at the time of the call, the value will be set to false. The user should then stop
822  *                                                      the decoding.
823  * @param       p_stream                the stream to decode.
824  * @return      true                    if the tile header could be decoded. In case the decoding should end, the returned value is still true.
825  *                                                      returning false may be the result of a shortage of memory or an internal error.
826  */
827 opj_bool OPJ_CALLCONV opj_read_tile_header(     opj_codec_t *p_codec,
828                                                                                         opj_stream_t * p_stream,
829                                                                                         OPJ_UINT32 * p_tile_index,
830                                                                                         OPJ_UINT32 * p_data_size,
831                                                                                         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
832                                                                                         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
833                                                                                         OPJ_UINT32 * p_nb_comps,
834                                                                                         opj_bool * p_should_go_on)
835 {
836         if (p_codec && p_stream && p_data_size && p_tile_index) {
837                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
838                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
839
840                 if (! l_codec->is_decompressor) {
841                         return OPJ_FALSE;
842                 }
843
844                 return l_codec->m_codec_data.m_decompression.opj_read_tile_header(      l_codec->m_codec,
845                                                                                                                                                         p_tile_index,
846                                                                                                                                                         p_data_size,
847                                                                                                                                                         p_tile_x0, p_tile_y0,
848                                                                                                                                                         p_tile_x1, p_tile_y1,
849                                                                                                                                                         p_nb_comps,
850                                                                                                                                                         p_should_go_on,
851                                                                                                                                                         l_stream,
852                                                                                                                                                         &(l_codec->m_event_mgr));
853         }
854         return OPJ_FALSE;
855 }
856
857 /**
858  * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
859  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
860  *
861  * @param       p_codec                 the jpeg2000 codec.
862  * @param       p_tile_index    the index of the tile being decoded, this should be the value set by opj_read_tile_header.
863  * @param       p_data                  pointer to a memory block that will hold the decoded data.
864  * @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.
865  * @param       p_stream                the stream to decode.
866  *
867  * @return      true                    if the data could be decoded.
868  */
869 opj_bool OPJ_CALLCONV opj_decode_tile_data(     opj_codec_t *p_codec,
870                                                                                         OPJ_UINT32 p_tile_index,
871                                                                                         OPJ_BYTE * p_data,
872                                                                                         OPJ_UINT32 p_data_size,
873                                                                                         opj_stream_t *p_stream
874                                                                                         )
875 {
876         if (p_codec && p_data && p_stream) {
877                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
878                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
879
880                 if (! l_codec->is_decompressor) {
881                         return OPJ_FALSE;
882                 }
883
884                 return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(      l_codec->m_codec,
885                                                                                                                                                         p_tile_index,
886                                                                                                                                                         p_data,
887                                                                                                                                                         p_data_size,
888                                                                                                                                                         l_stream,
889                                                                                                                                                         &(l_codec->m_event_mgr) );
890         }
891         return OPJ_FALSE;
892 }
893
894 /*
895  *
896  *
897  */
898 void OPJ_CALLCONV opj_dump_codec(       opj_codec_t *p_codec,
899                                                                         OPJ_INT32 info_flag,
900                                                                         FILE* output_stream)
901 {
902         if (p_codec) {
903                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
904
905                 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
906                 return;
907         }
908
909         fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
910         return;
911 }
912
913 /*
914  *
915  *
916  */
917 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
918 {
919         if (p_codec) {
920                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
921
922                 return l_codec->opj_get_codec_info(l_codec->m_codec);
923         }
924
925         return NULL;
926 }
927
928 /*
929  *
930  *
931  */
932 void OPJ_CALLCONV opj_destroy_cstr_info_v2(opj_codestream_info_v2_t **cstr_info) {
933         if (cstr_info) {
934
935                 if ((*cstr_info)->m_default_tile_info.tccp_info){
936                         opj_free((*cstr_info)->m_default_tile_info.tccp_info);
937                 }
938
939                 if ((*cstr_info)->tile_info){
940                         /* FIXME not used for the moment*/
941                 }
942
943                 opj_free((*cstr_info));
944                 (*cstr_info) = NULL;
945         }
946 }
947
948 /*
949  *
950  *
951  */
952 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
953 {
954         if (p_codec) {
955                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
956
957                 return l_codec->opj_get_codec_index(l_codec->m_codec);
958         }
959
960         return NULL;
961 }
962
963 /*
964  *
965  *
966  */
967 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
968 {
969         if (*p_cstr_index){
970                 j2k_destroy_cstr_index(*p_cstr_index);
971                 (*p_cstr_index) = NULL;
972         }
973 }
974
975 /*
976  *
977  *
978  */
979 opj_bool OPJ_CALLCONV opj_decode(   opj_codec_t *p_codec,
980                                     opj_stream_t *p_stream,
981                                     opj_image_t* p_image)
982 {
983         if (p_codec && p_stream) {
984                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
985                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
986
987                 if (! l_codec->is_decompressor) {
988                         return OPJ_FALSE;
989                 }
990
991                 return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
992                                                                                                                                 l_stream,
993                                                                                                                                 p_image,
994                                                                                                                                 &(l_codec->m_event_mgr) );
995         }
996
997         return OPJ_FALSE;
998 }
999
1000 /*
1001  *
1002  *
1003  */
1004 opj_bool OPJ_CALLCONV opj_end_decompress (      opj_codec_t *p_codec,
1005                                                                                         opj_stream_t *p_stream)
1006 {
1007         if (p_codec && p_stream) {
1008                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1009                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1010
1011                 if (! l_codec->is_decompressor) {
1012                         return OPJ_FALSE;
1013                 }
1014                 
1015                 return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
1016                                                                                                                                                 l_stream,
1017                                                                                                                                                 &(l_codec->m_event_mgr) );
1018         }
1019
1020         return OPJ_FALSE;
1021 }
1022
1023 /*
1024  *
1025  *
1026  */
1027 opj_bool OPJ_CALLCONV opj_get_decoded_tile(     opj_codec_t *p_codec,
1028                                                                                         opj_stream_t *p_stream,
1029                                                                                         opj_image_t *p_image,
1030                                                                                         OPJ_UINT32 tile_index)
1031 {
1032         if (p_codec && p_stream) {
1033                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1034                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1035
1036                 if (! l_codec->is_decompressor) {
1037                         return OPJ_FALSE;
1038                 }
1039                 
1040                 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(      l_codec->m_codec,
1041                                                                                                                                                         l_stream,
1042                                                                                                                                                         p_image,
1043                                                                                                                                                         &(l_codec->m_event_mgr),
1044                                                                                                                                                         tile_index);
1045         }
1046
1047         return OPJ_FALSE;
1048 }
1049
1050 /*
1051  *
1052  *
1053  */
1054 opj_bool OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, 
1055                                                                                                                 OPJ_UINT32 res_factor )
1056 {
1057         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1058
1059         if ( !l_codec ){
1060                 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
1061                 return OPJ_FALSE;
1062         }
1063
1064         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec, 
1065                                                                                                                                                         res_factor,
1066                                                                                                                                                         &(l_codec->m_event_mgr) );
1067         return OPJ_TRUE;
1068 }
1069
1070
1071 opj_bool OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
1072 {
1073         OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * sizeof(OPJ_FLOAT32);
1074         OPJ_UINT32 l_dc_shift_size = pNbComp * sizeof(OPJ_INT32);
1075         OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
1076
1077         /* add MCT capability */
1078         int rsiz = (int)parameters->cp_rsiz | (int)MCT;
1079         parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
1080         parameters->irreversible = 1;
1081
1082         /* use array based MCT */
1083         parameters->tcp_mct = 2;
1084         parameters->mct_data = opj_malloc(l_mct_total_size);
1085         if (! parameters->mct_data) {
1086                 return OPJ_FALSE;
1087         }
1088
1089         memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
1090         memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size,p_dc_shift,l_dc_shift_size);
1091
1092         return OPJ_TRUE;
1093 }
1094
1095 /**
1096  * Writes a tile with the given data.
1097  *
1098  * @param       p_compressor            the jpeg2000 codec.
1099  * @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.
1100  * @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.
1101  * @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,
1102  *                                                              depending on the precision of the given component.
1103  * @param       p_stream                        the stream to write data to.
1104  */
1105 opj_bool OPJ_CALLCONV opj_write_tile (  opj_codec_t *p_codec,
1106                                                                                 OPJ_UINT32 p_tile_index,
1107                                                                                 OPJ_BYTE * p_data,
1108                                                                                 OPJ_UINT32 p_data_size,
1109                                                                                 opj_stream_t *p_stream )
1110 {
1111         if (p_codec && p_stream && p_data) {
1112                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1113                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1114
1115                 if (l_codec->is_decompressor) {
1116                         return OPJ_FALSE;
1117                 }
1118
1119                 return l_codec->m_codec_data.m_compression.opj_write_tile(      l_codec->m_codec,
1120                                                                                                                                         p_tile_index,
1121                                                                                                                                         p_data,
1122                                                                                                                                         p_data_size,
1123                                                                                                                                         l_stream,
1124                                                                                                                                         &(l_codec->m_event_mgr) );
1125         }
1126
1127         return OPJ_FALSE;
1128 }
1129
1130 /* ---------------------------------------------------------------------- */
1131 /**
1132  * Helper function.
1133  * Sets the stream to be a file stream. The FILE must have been open previously.
1134  * @param               p_stream        the stream to modify
1135  * @param               p_file          handler to an already open file.
1136 */
1137 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
1138 {
1139         return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
1140 }
1141
1142 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (      FILE * p_file, 
1143                                                                                                                         OPJ_SIZE_T p_size, 
1144                                                                                                                         opj_bool p_is_read_stream)
1145 {
1146         opj_stream_t* l_stream = 00;
1147
1148         if (! p_file) {
1149                 return NULL;
1150         }
1151
1152         l_stream = opj_stream_create(p_size,p_is_read_stream);
1153         if (! l_stream) {
1154                 return NULL;
1155         }
1156
1157         opj_stream_set_user_data(l_stream, p_file);
1158         opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
1159         opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
1160         opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
1161         opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
1162         opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
1163
1164         return l_stream;
1165 }