• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * The copyright in this software is being made available under the 2-clauses
3  * BSD License, included below. This software may be subject to other third
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
9  * Copyright (c) 2012, CS Systemes d'Information, France
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifdef _WIN32
35 #include <windows.h>
36 #endif /* _WIN32 */
37 
38 #include "opj_includes.h"
39 
40 
41 /* ---------------------------------------------------------------------- */
42 /* Functions to set the message handlers */
43 
opj_set_info_handler(opj_codec_t * p_codec,opj_msg_callback p_callback,void * p_user_data)44 OPJ_BOOL OPJ_CALLCONV opj_set_info_handler(opj_codec_t * p_codec,
45         opj_msg_callback p_callback,
46         void * p_user_data)
47 {
48     opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
49     if (! l_codec) {
50         return OPJ_FALSE;
51     }
52 
53     l_codec->m_event_mgr.info_handler = p_callback;
54     l_codec->m_event_mgr.m_info_data = p_user_data;
55 
56     return OPJ_TRUE;
57 }
58 
opj_set_warning_handler(opj_codec_t * p_codec,opj_msg_callback p_callback,void * p_user_data)59 OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler(opj_codec_t * p_codec,
60         opj_msg_callback p_callback,
61         void * p_user_data)
62 {
63     opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
64     if (! l_codec) {
65         return OPJ_FALSE;
66     }
67 
68     l_codec->m_event_mgr.warning_handler = p_callback;
69     l_codec->m_event_mgr.m_warning_data = p_user_data;
70 
71     return OPJ_TRUE;
72 }
73 
opj_set_error_handler(opj_codec_t * p_codec,opj_msg_callback p_callback,void * p_user_data)74 OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec,
75         opj_msg_callback p_callback,
76         void * p_user_data)
77 {
78     opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
79     if (! l_codec) {
80         return OPJ_FALSE;
81     }
82 
83     l_codec->m_event_mgr.error_handler = p_callback;
84     l_codec->m_event_mgr.m_error_data = p_user_data;
85 
86     return OPJ_TRUE;
87 }
88 
89 /* ---------------------------------------------------------------------- */
90 
opj_read_from_file(void * p_buffer,OPJ_SIZE_T p_nb_bytes,void * p_user_data)91 static OPJ_SIZE_T opj_read_from_file(void * p_buffer, OPJ_SIZE_T p_nb_bytes,
92                                      void * p_user_data)
93 {
94     FILE* p_file = (FILE*)p_user_data;
95     OPJ_SIZE_T l_nb_read = fread(p_buffer, 1, p_nb_bytes, (FILE*)p_file);
96     return l_nb_read ? l_nb_read : (OPJ_SIZE_T) - 1;
97 }
98 
opj_get_data_length_from_file(void * p_user_data)99 static OPJ_UINT64 opj_get_data_length_from_file(void * p_user_data)
100 {
101     FILE* p_file = (FILE*)p_user_data;
102     OPJ_OFF_T file_length = 0;
103 
104     OPJ_FSEEK(p_file, 0, SEEK_END);
105     file_length = (OPJ_OFF_T)OPJ_FTELL(p_file);
106     OPJ_FSEEK(p_file, 0, SEEK_SET);
107 
108     return (OPJ_UINT64)file_length;
109 }
110 
opj_write_from_file(void * p_buffer,OPJ_SIZE_T p_nb_bytes,void * p_user_data)111 static OPJ_SIZE_T opj_write_from_file(void * p_buffer, OPJ_SIZE_T p_nb_bytes,
112                                       void * p_user_data)
113 {
114     FILE* p_file = (FILE*)p_user_data;
115     return fwrite(p_buffer, 1, p_nb_bytes, p_file);
116 }
117 
opj_skip_from_file(OPJ_OFF_T p_nb_bytes,void * p_user_data)118 static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, void * p_user_data)
119 {
120     FILE* p_file = (FILE*)p_user_data;
121     if (OPJ_FSEEK(p_file, p_nb_bytes, SEEK_CUR)) {
122         return -1;
123     }
124 
125     return p_nb_bytes;
126 }
127 
opj_seek_from_file(OPJ_OFF_T p_nb_bytes,void * p_user_data)128 static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, void * p_user_data)
129 {
130     FILE* p_file = (FILE*)p_user_data;
131     if (OPJ_FSEEK(p_file, p_nb_bytes, SEEK_SET)) {
132         return OPJ_FALSE;
133     }
134 
135     return OPJ_TRUE;
136 }
137 
opj_close_from_file(void * p_user_data)138 static void opj_close_from_file(void* p_user_data)
139 {
140     FILE* p_file = (FILE*)p_user_data;
141     fclose(p_file);
142 }
143 
144 /* ---------------------------------------------------------------------- */
145 #ifdef _WIN32
146 #ifndef OPJ_STATIC
147 BOOL APIENTRY
DllMain(HINSTANCE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)148 DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
149 {
150 
151     OPJ_ARG_NOT_USED(lpReserved);
152     OPJ_ARG_NOT_USED(hModule);
153 
154     switch (ul_reason_for_call) {
155     case DLL_PROCESS_ATTACH :
156         break;
157     case DLL_PROCESS_DETACH :
158         break;
159     case DLL_THREAD_ATTACH :
160     case DLL_THREAD_DETACH :
161         break;
162     }
163 
164     return TRUE;
165 }
166 #endif /* OPJ_STATIC */
167 #endif /* _WIN32 */
168 
169 /* ---------------------------------------------------------------------- */
170 
opj_version(void)171 const char* OPJ_CALLCONV opj_version(void)
172 {
173     return OPJ_PACKAGE_VERSION;
174 }
175 
176 /* ---------------------------------------------------------------------- */
177 /* DECOMPRESSION FUNCTIONS*/
178 
opj_create_decompress(OPJ_CODEC_FORMAT p_format)179 opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format)
180 {
181     opj_codec_private_t *l_codec = 00;
182 
183     l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
184     if (!l_codec) {
185         return 00;
186     }
187 
188     l_codec->is_decompressor = 1;
189 
190     switch (p_format) {
191     case OPJ_CODEC_J2K:
192         l_codec->opj_dump_codec = (void (*)(void*, OPJ_INT32, FILE*)) j2k_dump;
193 
194         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*)(
195                                            void*)) j2k_get_cstr_info;
196 
197         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*)(
198                                             void*)) j2k_get_cstr_index;
199 
200         l_codec->m_codec_data.m_decompression.opj_decode =
201             (OPJ_BOOL(*)(void *,
202                          struct opj_stream_private *,
203                          opj_image_t*, struct opj_event_mgr *)) opj_j2k_decode;
204 
205         l_codec->m_codec_data.m_decompression.opj_end_decompress =
206             (OPJ_BOOL(*)(void *,
207                          struct opj_stream_private *,
208                          struct opj_event_mgr *)) opj_j2k_end_decompress;
209 
210         l_codec->m_codec_data.m_decompression.opj_read_header =
211             (OPJ_BOOL(*)(struct opj_stream_private *,
212                          void *,
213                          opj_image_t **,
214                          struct opj_event_mgr *)) opj_j2k_read_header;
215 
216         l_codec->m_codec_data.m_decompression.opj_destroy =
217             (void (*)(void *))opj_j2k_destroy;
218 
219         l_codec->m_codec_data.m_decompression.opj_setup_decoder =
220             (void (*)(void *, opj_dparameters_t *)) opj_j2k_setup_decoder;
221 
222         l_codec->m_codec_data.m_decompression.opj_decoder_set_strict_mode =
223             (void (*)(void *, OPJ_BOOL)) opj_j2k_decoder_set_strict_mode;
224 
225 
226         l_codec->m_codec_data.m_decompression.opj_read_tile_header =
227             (OPJ_BOOL(*)(void *,
228                          OPJ_UINT32*,
229                          OPJ_UINT32*,
230                          OPJ_INT32*, OPJ_INT32*,
231                          OPJ_INT32*, OPJ_INT32*,
232                          OPJ_UINT32*,
233                          OPJ_BOOL*,
234                          struct opj_stream_private *,
235                          struct opj_event_mgr *)) opj_j2k_read_tile_header;
236 
237         l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
238             (OPJ_BOOL(*)(void *,
239                          OPJ_UINT32,
240                          OPJ_BYTE*,
241                          OPJ_UINT32,
242                          struct opj_stream_private *,
243                          struct opj_event_mgr *)) opj_j2k_decode_tile;
244 
245         l_codec->m_codec_data.m_decompression.opj_set_decode_area =
246             (OPJ_BOOL(*)(void *,
247                          opj_image_t*,
248                          OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32,
249                          struct opj_event_mgr *)) opj_j2k_set_decode_area;
250 
251         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
252             (OPJ_BOOL(*)(void *p_codec,
253                          opj_stream_private_t *p_cio,
254                          opj_image_t *p_image,
255                          struct opj_event_mgr * p_manager,
256                          OPJ_UINT32 tile_index)) opj_j2k_get_tile;
257 
258         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
259             (OPJ_BOOL(*)(void * p_codec,
260                          OPJ_UINT32 res_factor,
261                          struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_resolution_factor;
262 
263         l_codec->m_codec_data.m_decompression.opj_set_decoded_components =
264             (OPJ_BOOL(*)(void * p_codec,
265                          OPJ_UINT32 numcomps,
266                          const OPJ_UINT32 * comps_indices,
267                          struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_components;
268 
269         l_codec->opj_set_threads =
270             (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_j2k_set_threads;
271 
272         l_codec->m_codec = opj_j2k_create_decompress();
273 
274         if (! l_codec->m_codec) {
275             opj_free(l_codec);
276             return NULL;
277         }
278 
279         break;
280 
281     case OPJ_CODEC_JP2:
282         /* get a JP2 decoder handle */
283         l_codec->opj_dump_codec = (void (*)(void*, OPJ_INT32, FILE*)) jp2_dump;
284 
285         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*)(
286                                            void*)) jp2_get_cstr_info;
287 
288         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*)(
289                                             void*)) jp2_get_cstr_index;
290 
291         l_codec->m_codec_data.m_decompression.opj_decode =
292             (OPJ_BOOL(*)(void *,
293                          struct opj_stream_private *,
294                          opj_image_t*,
295                          struct opj_event_mgr *)) opj_jp2_decode;
296 
297         l_codec->m_codec_data.m_decompression.opj_end_decompress =
298             (OPJ_BOOL(*)(void *,
299                          struct opj_stream_private *,
300                          struct opj_event_mgr *)) opj_jp2_end_decompress;
301 
302         l_codec->m_codec_data.m_decompression.opj_read_header =
303             (OPJ_BOOL(*)(struct opj_stream_private *,
304                          void *,
305                          opj_image_t **,
306                          struct opj_event_mgr *)) opj_jp2_read_header;
307 
308         l_codec->m_codec_data.m_decompression.opj_read_tile_header =
309             (OPJ_BOOL(*)(void *,
310                          OPJ_UINT32*,
311                          OPJ_UINT32*,
312                          OPJ_INT32*,
313                          OPJ_INT32*,
314                          OPJ_INT32 *,
315                          OPJ_INT32 *,
316                          OPJ_UINT32 *,
317                          OPJ_BOOL *,
318                          struct opj_stream_private *,
319                          struct opj_event_mgr *)) opj_jp2_read_tile_header;
320 
321         l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
322             (OPJ_BOOL(*)(void *,
323                          OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32,
324                          struct opj_stream_private *,
325                          struct opj_event_mgr *)) opj_jp2_decode_tile;
326 
327         l_codec->m_codec_data.m_decompression.opj_destroy = (void (*)(
328                     void *))opj_jp2_destroy;
329 
330         l_codec->m_codec_data.m_decompression.opj_setup_decoder =
331             (void (*)(void *, opj_dparameters_t *)) opj_jp2_setup_decoder;
332 
333         l_codec->m_codec_data.m_decompression.opj_decoder_set_strict_mode =
334             (void (*)(void *, OPJ_BOOL)) opj_jp2_decoder_set_strict_mode;
335 
336         l_codec->m_codec_data.m_decompression.opj_set_decode_area =
337             (OPJ_BOOL(*)(void *,
338                          opj_image_t*,
339                          OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32,
340                          struct opj_event_mgr *)) opj_jp2_set_decode_area;
341 
342         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
343             (OPJ_BOOL(*)(void *p_codec,
344                          opj_stream_private_t *p_cio,
345                          opj_image_t *p_image,
346                          struct opj_event_mgr * p_manager,
347                          OPJ_UINT32 tile_index)) opj_jp2_get_tile;
348 
349         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
350             (OPJ_BOOL(*)(void * p_codec,
351                          OPJ_UINT32 res_factor,
352                          opj_event_mgr_t * p_manager)) opj_jp2_set_decoded_resolution_factor;
353 
354         l_codec->m_codec_data.m_decompression.opj_set_decoded_components =
355             (OPJ_BOOL(*)(void * p_codec,
356                          OPJ_UINT32 numcomps,
357                          const OPJ_UINT32 * comps_indices,
358                          struct opj_event_mgr * p_manager)) opj_jp2_set_decoded_components;
359 
360         l_codec->opj_set_threads =
361             (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_jp2_set_threads;
362 
363         l_codec->m_codec = opj_jp2_create(OPJ_TRUE);
364 
365         if (! l_codec->m_codec) {
366             opj_free(l_codec);
367             return 00;
368         }
369 
370         break;
371     case OPJ_CODEC_UNKNOWN:
372     case OPJ_CODEC_JPT:
373     default:
374         opj_free(l_codec);
375         return 00;
376     }
377 
378     opj_set_default_event_handler(&(l_codec->m_event_mgr));
379     return (opj_codec_t*) l_codec;
380 }
381 
opj_set_default_decoder_parameters(opj_dparameters_t * parameters)382 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t
383         *parameters)
384 {
385     if (parameters) {
386         memset(parameters, 0, sizeof(opj_dparameters_t));
387         /* default decoding parameters */
388         parameters->cp_layer = 0;
389         parameters->cp_reduce = 0;
390 
391         parameters->decod_format = -1;
392         parameters->cod_format = -1;
393         parameters->flags = 0;
394         /* UniPG>> */
395 #ifdef USE_JPWL
396         parameters->jpwl_correct = OPJ_FALSE;
397         parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
398         parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
399 #endif /* USE_JPWL */
400         /* <<UniPG */
401     }
402 }
403 
404 
opj_codec_set_threads(opj_codec_t * p_codec,int num_threads)405 OPJ_BOOL OPJ_CALLCONV opj_codec_set_threads(opj_codec_t *p_codec,
406         int num_threads)
407 {
408     if (p_codec && (num_threads >= 0)) {
409         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
410 
411         return l_codec->opj_set_threads(l_codec->m_codec, (OPJ_UINT32)num_threads);
412     }
413     return OPJ_FALSE;
414 }
415 
opj_setup_decoder(opj_codec_t * p_codec,opj_dparameters_t * parameters)416 OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
417                                         opj_dparameters_t *parameters
418                                        )
419 {
420     if (p_codec && parameters) {
421         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
422 
423         if (! l_codec->is_decompressor) {
424             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
425                           "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
426             return OPJ_FALSE;
427         }
428 
429         l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
430                 parameters);
431         return OPJ_TRUE;
432     }
433     return OPJ_FALSE;
434 }
435 
opj_decoder_set_strict_mode(opj_codec_t * p_codec,OPJ_BOOL strict)436 OPJ_API OPJ_BOOL OPJ_CALLCONV opj_decoder_set_strict_mode(opj_codec_t *p_codec,
437         OPJ_BOOL strict)
438 {
439     if (p_codec) {
440         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
441 
442         if (! l_codec->is_decompressor) {
443             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
444                           "Codec provided to the opj_decoder_set_strict_mode function is not a decompressor handler.\n");
445             return OPJ_FALSE;
446         }
447 
448         l_codec->m_codec_data.m_decompression.opj_decoder_set_strict_mode(
449             l_codec->m_codec,
450             strict);
451         return OPJ_TRUE;
452     }
453     return OPJ_FALSE;
454 }
455 
opj_read_header(opj_stream_t * p_stream,opj_codec_t * p_codec,opj_image_t ** p_image)456 OPJ_BOOL OPJ_CALLCONV opj_read_header(opj_stream_t *p_stream,
457                                       opj_codec_t *p_codec,
458                                       opj_image_t **p_image)
459 {
460     if (p_codec && p_stream) {
461         opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
462         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
463 
464         if (! l_codec->is_decompressor) {
465             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
466                           "Codec provided to the opj_read_header function is not a decompressor handler.\n");
467             return OPJ_FALSE;
468         }
469 
470         return l_codec->m_codec_data.m_decompression.opj_read_header(l_stream,
471                 l_codec->m_codec,
472                 p_image,
473                 &(l_codec->m_event_mgr));
474     }
475 
476     return OPJ_FALSE;
477 }
478 
479 
opj_set_decoded_components(opj_codec_t * p_codec,OPJ_UINT32 numcomps,const OPJ_UINT32 * comps_indices,OPJ_BOOL apply_color_transforms)480 OPJ_BOOL OPJ_CALLCONV opj_set_decoded_components(opj_codec_t *p_codec,
481         OPJ_UINT32 numcomps,
482         const OPJ_UINT32* comps_indices,
483         OPJ_BOOL apply_color_transforms)
484 {
485     if (p_codec) {
486         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
487 
488         if (! l_codec->is_decompressor) {
489             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
490                           "Codec provided to the opj_set_decoded_components function is not a decompressor handler.\n");
491             return OPJ_FALSE;
492         }
493 
494         if (apply_color_transforms) {
495             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
496                           "apply_color_transforms = OPJ_TRUE is not supported.\n");
497             return OPJ_FALSE;
498         }
499 
500         return  l_codec->m_codec_data.m_decompression.opj_set_decoded_components(
501                     l_codec->m_codec,
502                     numcomps,
503                     comps_indices,
504                     &(l_codec->m_event_mgr));
505     }
506     return OPJ_FALSE;
507 }
508 
opj_decode(opj_codec_t * p_codec,opj_stream_t * p_stream,opj_image_t * p_image)509 OPJ_BOOL OPJ_CALLCONV opj_decode(opj_codec_t *p_codec,
510                                  opj_stream_t *p_stream,
511                                  opj_image_t* p_image)
512 {
513     if (p_codec && p_stream) {
514         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
515         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
516 
517         if (! l_codec->is_decompressor) {
518             return OPJ_FALSE;
519         }
520 
521         return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
522                 l_stream,
523                 p_image,
524                 &(l_codec->m_event_mgr));
525     }
526 
527     return OPJ_FALSE;
528 }
529 
opj_set_decode_area(opj_codec_t * p_codec,opj_image_t * p_image,OPJ_INT32 p_start_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_x,OPJ_INT32 p_end_y)530 OPJ_BOOL OPJ_CALLCONV opj_set_decode_area(opj_codec_t *p_codec,
531         opj_image_t* p_image,
532         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
533         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
534                                          )
535 {
536     if (p_codec) {
537         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
538 
539         if (! l_codec->is_decompressor) {
540             return OPJ_FALSE;
541         }
542 
543         return  l_codec->m_codec_data.m_decompression.opj_set_decode_area(
544                     l_codec->m_codec,
545                     p_image,
546                     p_start_x, p_start_y,
547                     p_end_x, p_end_y,
548                     &(l_codec->m_event_mgr));
549     }
550     return OPJ_FALSE;
551 }
552 
opj_read_tile_header(opj_codec_t * p_codec,opj_stream_t * p_stream,OPJ_UINT32 * p_tile_index,OPJ_UINT32 * p_data_size,OPJ_INT32 * p_tile_x0,OPJ_INT32 * p_tile_y0,OPJ_INT32 * p_tile_x1,OPJ_INT32 * p_tile_y1,OPJ_UINT32 * p_nb_comps,OPJ_BOOL * p_should_go_on)553 OPJ_BOOL OPJ_CALLCONV opj_read_tile_header(opj_codec_t *p_codec,
554         opj_stream_t * p_stream,
555         OPJ_UINT32 * p_tile_index,
556         OPJ_UINT32 * p_data_size,
557         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
558         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
559         OPJ_UINT32 * p_nb_comps,
560         OPJ_BOOL * p_should_go_on)
561 {
562     if (p_codec && p_stream && p_data_size && p_tile_index) {
563         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
564         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
565 
566         if (! l_codec->is_decompressor) {
567             return OPJ_FALSE;
568         }
569 
570         return l_codec->m_codec_data.m_decompression.opj_read_tile_header(
571                    l_codec->m_codec,
572                    p_tile_index,
573                    p_data_size,
574                    p_tile_x0, p_tile_y0,
575                    p_tile_x1, p_tile_y1,
576                    p_nb_comps,
577                    p_should_go_on,
578                    l_stream,
579                    &(l_codec->m_event_mgr));
580     }
581     return OPJ_FALSE;
582 }
583 
opj_decode_tile_data(opj_codec_t * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,opj_stream_t * p_stream)584 OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data(opj_codec_t *p_codec,
585         OPJ_UINT32 p_tile_index,
586         OPJ_BYTE * p_data,
587         OPJ_UINT32 p_data_size,
588         opj_stream_t *p_stream
589                                           )
590 {
591     if (p_codec && p_data && p_stream) {
592         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
593         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
594 
595         if (! l_codec->is_decompressor) {
596             return OPJ_FALSE;
597         }
598 
599         return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(
600                    l_codec->m_codec,
601                    p_tile_index,
602                    p_data,
603                    p_data_size,
604                    l_stream,
605                    &(l_codec->m_event_mgr));
606     }
607     return OPJ_FALSE;
608 }
609 
opj_get_decoded_tile(opj_codec_t * p_codec,opj_stream_t * p_stream,opj_image_t * p_image,OPJ_UINT32 tile_index)610 OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile(opj_codec_t *p_codec,
611         opj_stream_t *p_stream,
612         opj_image_t *p_image,
613         OPJ_UINT32 tile_index)
614 {
615     if (p_codec && p_stream) {
616         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
617         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
618 
619         if (! l_codec->is_decompressor) {
620             return OPJ_FALSE;
621         }
622 
623         return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(
624                    l_codec->m_codec,
625                    l_stream,
626                    p_image,
627                    &(l_codec->m_event_mgr),
628                    tile_index);
629     }
630 
631     return OPJ_FALSE;
632 }
633 
opj_set_decoded_resolution_factor(opj_codec_t * p_codec,OPJ_UINT32 res_factor)634 OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec,
635         OPJ_UINT32 res_factor)
636 {
637     opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
638 
639     if (!l_codec) {
640         return OPJ_FALSE;
641     }
642 
643     return l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(
644                l_codec->m_codec,
645                res_factor,
646                &(l_codec->m_event_mgr));
647 }
648 
649 /* ---------------------------------------------------------------------- */
650 /* COMPRESSION FUNCTIONS*/
651 
opj_create_compress(OPJ_CODEC_FORMAT p_format)652 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
653 {
654     opj_codec_private_t *l_codec = 00;
655 
656     l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
657     if (!l_codec) {
658         return 00;
659     }
660 
661     l_codec->is_decompressor = 0;
662 
663     switch (p_format) {
664     case OPJ_CODEC_J2K:
665         l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL(*)(void *,
666                 struct opj_stream_private *,
667                 struct opj_event_mgr *)) opj_j2k_encode;
668 
669         l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL(*)(void *,
670                 struct opj_stream_private *,
671                 struct opj_event_mgr *)) opj_j2k_end_compress;
672 
673         l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL(*)(void *,
674                 struct opj_stream_private *,
675                 struct opj_image *,
676                 struct opj_event_mgr *)) opj_j2k_start_compress;
677 
678         l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL(*)(void *,
679                 OPJ_UINT32,
680                 OPJ_BYTE*,
681                 OPJ_UINT32,
682                 struct opj_stream_private *,
683                 struct opj_event_mgr *)) opj_j2k_write_tile;
684 
685         l_codec->m_codec_data.m_compression.opj_destroy = (void (*)(
686                     void *)) opj_j2k_destroy;
687 
688         l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL(*)(void *,
689                 opj_cparameters_t *,
690                 struct opj_image *,
691                 struct opj_event_mgr *)) opj_j2k_setup_encoder;
692 
693         l_codec->m_codec_data.m_compression.opj_encoder_set_extra_options = (OPJ_BOOL(
694                     *)(void *,
695                        const char* const*,
696                        struct opj_event_mgr *)) opj_j2k_encoder_set_extra_options;
697 
698         l_codec->opj_set_threads =
699             (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_j2k_set_threads;
700 
701         l_codec->m_codec = opj_j2k_create_compress();
702         if (! l_codec->m_codec) {
703             opj_free(l_codec);
704             return 00;
705         }
706 
707         break;
708 
709     case OPJ_CODEC_JP2:
710         /* get a JP2 decoder handle */
711         l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL(*)(void *,
712                 struct opj_stream_private *,
713                 struct opj_event_mgr *)) opj_jp2_encode;
714 
715         l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL(*)(void *,
716                 struct opj_stream_private *,
717                 struct opj_event_mgr *)) opj_jp2_end_compress;
718 
719         l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL(*)(void *,
720                 struct opj_stream_private *,
721                 struct opj_image *,
722                 struct opj_event_mgr *))  opj_jp2_start_compress;
723 
724         l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL(*)(void *,
725                 OPJ_UINT32,
726                 OPJ_BYTE*,
727                 OPJ_UINT32,
728                 struct opj_stream_private *,
729                 struct opj_event_mgr *)) opj_jp2_write_tile;
730 
731         l_codec->m_codec_data.m_compression.opj_destroy = (void (*)(
732                     void *)) opj_jp2_destroy;
733 
734         l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL(*)(void *,
735                 opj_cparameters_t *,
736                 struct opj_image *,
737                 struct opj_event_mgr *)) opj_jp2_setup_encoder;
738 
739         l_codec->m_codec_data.m_compression.opj_encoder_set_extra_options = (OPJ_BOOL(
740                     *)(void *,
741                        const char* const*,
742                        struct opj_event_mgr *)) opj_jp2_encoder_set_extra_options;
743 
744         l_codec->opj_set_threads =
745             (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_jp2_set_threads;
746 
747         l_codec->m_codec = opj_jp2_create(OPJ_FALSE);
748         if (! l_codec->m_codec) {
749             opj_free(l_codec);
750             return 00;
751         }
752 
753         break;
754 
755     case OPJ_CODEC_UNKNOWN:
756     case OPJ_CODEC_JPT:
757     default:
758         opj_free(l_codec);
759         return 00;
760     }
761 
762     opj_set_default_event_handler(&(l_codec->m_event_mgr));
763     return (opj_codec_t*) l_codec;
764 }
765 
opj_set_default_encoder_parameters(opj_cparameters_t * parameters)766 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t
767         *parameters)
768 {
769     if (parameters) {
770         memset(parameters, 0, sizeof(opj_cparameters_t));
771         /* default coding parameters */
772         parameters->cp_cinema = OPJ_OFF; /* DEPRECATED */
773         parameters->rsiz = OPJ_PROFILE_NONE;
774         parameters->max_comp_size = 0;
775         parameters->numresolution = OPJ_COMP_PARAM_DEFAULT_NUMRESOLUTION;
776         parameters->cp_rsiz = OPJ_STD_RSIZ; /* DEPRECATED */
777         parameters->cblockw_init = OPJ_COMP_PARAM_DEFAULT_CBLOCKW;
778         parameters->cblockh_init = OPJ_COMP_PARAM_DEFAULT_CBLOCKH;
779         parameters->prog_order = OPJ_COMP_PARAM_DEFAULT_PROG_ORDER;
780         parameters->roi_compno = -1;        /* no ROI */
781         parameters->subsampling_dx = 1;
782         parameters->subsampling_dy = 1;
783         parameters->tp_on = 0;
784         parameters->decod_format = -1;
785         parameters->cod_format = -1;
786         parameters->tcp_rates[0] = 0;
787         parameters->tcp_numlayers = 0;
788         parameters->cp_disto_alloc = 0;
789         parameters->cp_fixed_alloc = 0;
790         parameters->cp_fixed_quality = 0;
791         parameters->jpip_on = OPJ_FALSE;
792         /* UniPG>> */
793 #ifdef USE_JPWL
794         parameters->jpwl_epc_on = OPJ_FALSE;
795         parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
796         {
797             int i;
798             for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
799                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
800                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
801             }
802         };
803         {
804             int i;
805             for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
806                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
807                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
808                 parameters->jpwl_pprot[i] = 0; /* absent */
809             }
810         };
811         parameters->jpwl_sens_size = 0; /* 0 means no ESD */
812         parameters->jpwl_sens_addr = 0; /* 0 means auto */
813         parameters->jpwl_sens_range = 0; /* 0 means packet */
814         parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
815         {
816             int i;
817             for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
818                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
819                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
820             }
821         };
822 #endif /* USE_JPWL */
823         /* <<UniPG */
824     }
825 }
826 
opj_setup_encoder(opj_codec_t * p_codec,opj_cparameters_t * parameters,opj_image_t * p_image)827 OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec,
828                                         opj_cparameters_t *parameters,
829                                         opj_image_t *p_image)
830 {
831     if (p_codec && parameters && p_image) {
832         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
833 
834         if (! l_codec->is_decompressor) {
835             return l_codec->m_codec_data.m_compression.opj_setup_encoder(l_codec->m_codec,
836                     parameters,
837                     p_image,
838                     &(l_codec->m_event_mgr));
839         }
840     }
841 
842     return OPJ_FALSE;
843 }
844 
845 /* ----------------------------------------------------------------------- */
846 
opj_encoder_set_extra_options(opj_codec_t * p_codec,const char * const * options)847 OPJ_BOOL OPJ_CALLCONV opj_encoder_set_extra_options(opj_codec_t *p_codec,
848         const char* const* options)
849 {
850     if (p_codec) {
851         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
852 
853         if (! l_codec->is_decompressor) {
854             return l_codec->m_codec_data.m_compression.opj_encoder_set_extra_options(
855                        l_codec->m_codec,
856                        options,
857                        &(l_codec->m_event_mgr));
858         }
859     }
860 
861     return OPJ_FALSE;
862 }
863 
864 /* ----------------------------------------------------------------------- */
865 
opj_start_compress(opj_codec_t * p_codec,opj_image_t * p_image,opj_stream_t * p_stream)866 OPJ_BOOL OPJ_CALLCONV opj_start_compress(opj_codec_t *p_codec,
867         opj_image_t * p_image,
868         opj_stream_t *p_stream)
869 {
870     if (p_codec && p_stream) {
871         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
872         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
873 
874         if (! l_codec->is_decompressor) {
875             return l_codec->m_codec_data.m_compression.opj_start_compress(l_codec->m_codec,
876                     l_stream,
877                     p_image,
878                     &(l_codec->m_event_mgr));
879         }
880     }
881 
882     return OPJ_FALSE;
883 }
884 
opj_encode(opj_codec_t * p_info,opj_stream_t * p_stream)885 OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
886 {
887     if (p_info && p_stream) {
888         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
889         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
890 
891         if (! l_codec->is_decompressor) {
892             return l_codec->m_codec_data.m_compression.opj_encode(l_codec->m_codec,
893                     l_stream,
894                     &(l_codec->m_event_mgr));
895         }
896     }
897 
898     return OPJ_FALSE;
899 
900 }
901 
opj_end_compress(opj_codec_t * p_codec,opj_stream_t * p_stream)902 OPJ_BOOL OPJ_CALLCONV opj_end_compress(opj_codec_t *p_codec,
903                                        opj_stream_t *p_stream)
904 {
905     if (p_codec && p_stream) {
906         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
907         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
908 
909         if (! l_codec->is_decompressor) {
910             return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
911                     l_stream,
912                     &(l_codec->m_event_mgr));
913         }
914     }
915     return OPJ_FALSE;
916 
917 }
918 
opj_end_decompress(opj_codec_t * p_codec,opj_stream_t * p_stream)919 OPJ_BOOL OPJ_CALLCONV opj_end_decompress(opj_codec_t *p_codec,
920         opj_stream_t *p_stream)
921 {
922     if (p_codec && p_stream) {
923         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
924         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
925 
926         if (! l_codec->is_decompressor) {
927             return OPJ_FALSE;
928         }
929 
930         return l_codec->m_codec_data.m_decompression.opj_end_decompress(
931                    l_codec->m_codec,
932                    l_stream,
933                    &(l_codec->m_event_mgr));
934     }
935 
936     return OPJ_FALSE;
937 }
938 
opj_set_MCT(opj_cparameters_t * parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)939 OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,
940                                   OPJ_FLOAT32 * pEncodingMatrix,
941                                   OPJ_INT32 * p_dc_shift, OPJ_UINT32 pNbComp)
942 {
943     OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
944     OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32);
945     OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
946 
947     /* add MCT capability */
948     if (OPJ_IS_PART2(parameters->rsiz)) {
949         parameters->rsiz |= OPJ_EXTENSION_MCT;
950     } else {
951         parameters->rsiz = ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT));
952     }
953     parameters->irreversible = 1;
954 
955     /* use array based MCT */
956     parameters->tcp_mct = 2;
957     parameters->mct_data = opj_malloc(l_mct_total_size);
958     if (! parameters->mct_data) {
959         return OPJ_FALSE;
960     }
961 
962     memcpy(parameters->mct_data, pEncodingMatrix, l_matrix_size);
963     memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size, p_dc_shift,
964            l_dc_shift_size);
965 
966     return OPJ_TRUE;
967 }
968 
opj_write_tile(opj_codec_t * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,opj_stream_t * p_stream)969 OPJ_BOOL OPJ_CALLCONV opj_write_tile(opj_codec_t *p_codec,
970                                      OPJ_UINT32 p_tile_index,
971                                      OPJ_BYTE * p_data,
972                                      OPJ_UINT32 p_data_size,
973                                      opj_stream_t *p_stream)
974 {
975     if (p_codec && p_stream && p_data) {
976         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
977         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
978 
979         if (l_codec->is_decompressor) {
980             return OPJ_FALSE;
981         }
982 
983         return l_codec->m_codec_data.m_compression.opj_write_tile(l_codec->m_codec,
984                 p_tile_index,
985                 p_data,
986                 p_data_size,
987                 l_stream,
988                 &(l_codec->m_event_mgr));
989     }
990 
991     return OPJ_FALSE;
992 }
993 
994 /* ---------------------------------------------------------------------- */
995 
opj_destroy_codec(opj_codec_t * p_codec)996 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
997 {
998     if (p_codec) {
999         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1000 
1001         if (l_codec->is_decompressor) {
1002             l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
1003         } else {
1004             l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
1005         }
1006 
1007         l_codec->m_codec = 00;
1008         opj_free(l_codec);
1009     }
1010 }
1011 
1012 /* ---------------------------------------------------------------------- */
1013 
opj_dump_codec(opj_codec_t * p_codec,OPJ_INT32 info_flag,FILE * output_stream)1014 void OPJ_CALLCONV opj_dump_codec(opj_codec_t *p_codec,
1015                                  OPJ_INT32 info_flag,
1016                                  FILE* output_stream)
1017 {
1018     if (p_codec) {
1019         opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1020 
1021         l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
1022         return;
1023     }
1024 
1025     /* TODO return error */
1026     /* fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n"); */
1027     return;
1028 }
1029 
opj_get_cstr_info(opj_codec_t * p_codec)1030 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
1031 {
1032     if (p_codec) {
1033         opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1034 
1035         return l_codec->opj_get_codec_info(l_codec->m_codec);
1036     }
1037 
1038     return NULL;
1039 }
1040 
opj_destroy_cstr_info(opj_codestream_info_v2_t ** cstr_info)1041 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info)
1042 {
1043     if (cstr_info) {
1044 
1045         if ((*cstr_info)->m_default_tile_info.tccp_info) {
1046             opj_free((*cstr_info)->m_default_tile_info.tccp_info);
1047         }
1048 
1049         if ((*cstr_info)->tile_info) {
1050             /* FIXME not used for the moment*/
1051         }
1052 
1053         opj_free((*cstr_info));
1054         (*cstr_info) = NULL;
1055     }
1056 }
1057 
opj_get_cstr_index(opj_codec_t * p_codec)1058 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
1059 {
1060     if (p_codec) {
1061         opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1062 
1063         return l_codec->opj_get_codec_index(l_codec->m_codec);
1064     }
1065 
1066     return NULL;
1067 }
1068 
opj_destroy_cstr_index(opj_codestream_index_t ** p_cstr_index)1069 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
1070 {
1071     if (*p_cstr_index) {
1072         j2k_destroy_cstr_index(*p_cstr_index);
1073         (*p_cstr_index) = NULL;
1074     }
1075 }
1076 
opj_stream_create_default_file_stream(const char * fname,OPJ_BOOL p_is_read_stream)1077 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream(
1078     const char *fname, OPJ_BOOL p_is_read_stream)
1079 {
1080     return opj_stream_create_file_stream(fname, OPJ_J2K_STREAM_CHUNK_SIZE,
1081                                          p_is_read_stream);
1082 }
1083 
opj_stream_create_file_stream(const char * fname,OPJ_SIZE_T p_size,OPJ_BOOL p_is_read_stream)1084 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream(
1085     const char *fname,
1086     OPJ_SIZE_T p_size,
1087     OPJ_BOOL p_is_read_stream)
1088 {
1089     opj_stream_t* l_stream = 00;
1090     FILE *p_file;
1091     const char *mode;
1092 
1093     if (! fname) {
1094         return NULL;
1095     }
1096 
1097     if (p_is_read_stream) {
1098         mode = "rb";
1099     } else {
1100         mode = "wb";
1101     }
1102 
1103     p_file = fopen(fname, mode);
1104 
1105     if (! p_file) {
1106         return NULL;
1107     }
1108 
1109     l_stream = opj_stream_create(p_size, p_is_read_stream);
1110     if (! l_stream) {
1111         fclose(p_file);
1112         return NULL;
1113     }
1114 
1115     opj_stream_set_user_data(l_stream, p_file, opj_close_from_file);
1116     opj_stream_set_user_data_length(l_stream,
1117                                     opj_get_data_length_from_file(p_file));
1118     opj_stream_set_read_function(l_stream, opj_read_from_file);
1119     opj_stream_set_write_function(l_stream,
1120                                   (opj_stream_write_fn) opj_write_from_file);
1121     opj_stream_set_skip_function(l_stream, opj_skip_from_file);
1122     opj_stream_set_seek_function(l_stream, opj_seek_from_file);
1123 
1124     return l_stream;
1125 }
1126 
1127 
opj_image_data_alloc(OPJ_SIZE_T size)1128 void* OPJ_CALLCONV opj_image_data_alloc(OPJ_SIZE_T size)
1129 {
1130     void* ret = opj_aligned_malloc(size);
1131     /* printf("opj_image_data_alloc %p\n", ret); */
1132     return ret;
1133 }
1134 
opj_image_data_free(void * ptr)1135 void OPJ_CALLCONV opj_image_data_free(void* ptr)
1136 {
1137     /* printf("opj_image_data_free %p\n", ptr); */
1138     opj_aligned_free(ptr);
1139 }
1140