blob: 7b12303423b7ad79cf50b6f259a682dcdab25bb4 [file] [log] [blame]
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001/*
Nicolas Pena088ca032017-09-01 13:25:16 -04002 * 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
John Abd-El-Malek5110c472014-05-17 22:33:34 -07004 * 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
Nicolas Pena088ca032017-09-01 13:25:16 -04008 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
John Abd-El-Malek5110c472014-05-17 22:33:34 -07009 * 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
Nicolas Pena088ca032017-09-01 13:25:16 -040044OPJ_BOOL OPJ_CALLCONV opj_set_info_handler(opj_codec_t * p_codec,
45 opj_msg_callback p_callback,
46 void * p_user_data)
John Abd-El-Malek5110c472014-05-17 22:33:34 -070047{
Nicolas Pena088ca032017-09-01 13:25:16 -040048 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;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070057}
58
Nicolas Pena088ca032017-09-01 13:25:16 -040059OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler(opj_codec_t * p_codec,
60 opj_msg_callback p_callback,
61 void * p_user_data)
John Abd-El-Malek5110c472014-05-17 22:33:34 -070062{
Nicolas Pena088ca032017-09-01 13:25:16 -040063 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;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070072}
73
Nicolas Pena088ca032017-09-01 13:25:16 -040074OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec,
75 opj_msg_callback p_callback,
76 void * p_user_data)
John Abd-El-Malek5110c472014-05-17 22:33:34 -070077{
Nicolas Pena088ca032017-09-01 13:25:16 -040078 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;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070087}
88
89/* ---------------------------------------------------------------------- */
90
Nicolas Pena088ca032017-09-01 13:25:16 -040091static OPJ_SIZE_T opj_read_from_file(void * p_buffer, OPJ_SIZE_T p_nb_bytes,
92 FILE * p_file)
John Abd-El-Malek5110c472014-05-17 22:33:34 -070093{
Nicolas Pena088ca032017-09-01 13:25:16 -040094 OPJ_SIZE_T l_nb_read = fread(p_buffer, 1, p_nb_bytes, p_file);
95 return l_nb_read ? l_nb_read : (OPJ_SIZE_T) - 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070096}
97
Nicolas Pena088ca032017-09-01 13:25:16 -040098static OPJ_UINT64 opj_get_data_length_from_file(FILE * p_file)
John Abd-El-Malek5110c472014-05-17 22:33:34 -070099{
Nicolas Pena088ca032017-09-01 13:25:16 -0400100 OPJ_OFF_T file_length = 0;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700101
Nicolas Pena088ca032017-09-01 13:25:16 -0400102 OPJ_FSEEK(p_file, 0, SEEK_END);
103 file_length = (OPJ_OFF_T)OPJ_FTELL(p_file);
104 OPJ_FSEEK(p_file, 0, SEEK_SET);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700105
Nicolas Pena088ca032017-09-01 13:25:16 -0400106 return (OPJ_UINT64)file_length;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700107}
108
Nicolas Pena088ca032017-09-01 13:25:16 -0400109static OPJ_SIZE_T opj_write_from_file(void * p_buffer, OPJ_SIZE_T p_nb_bytes,
110 FILE * p_file)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700111{
Nicolas Pena088ca032017-09-01 13:25:16 -0400112 return fwrite(p_buffer, 1, p_nb_bytes, p_file);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700113}
114
Nicolas Pena088ca032017-09-01 13:25:16 -0400115static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700116{
Nicolas Pena088ca032017-09-01 13:25:16 -0400117 if (OPJ_FSEEK(p_user_data, p_nb_bytes, SEEK_CUR)) {
118 return -1;
119 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700120
Nicolas Pena088ca032017-09-01 13:25:16 -0400121 return p_nb_bytes;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700122}
123
Nicolas Pena088ca032017-09-01 13:25:16 -0400124static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700125{
Nicolas Pena088ca032017-09-01 13:25:16 -0400126 if (OPJ_FSEEK(p_user_data, p_nb_bytes, SEEK_SET)) {
127 return OPJ_FALSE;
128 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700129
Nicolas Pena088ca032017-09-01 13:25:16 -0400130 return OPJ_TRUE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700131}
132
133/* ---------------------------------------------------------------------- */
134#ifdef _WIN32
135#ifndef OPJ_STATIC
136BOOL APIENTRY
Nicolas Pena088ca032017-09-01 13:25:16 -0400137DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
138{
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700139
Nicolas Pena088ca032017-09-01 13:25:16 -0400140 OPJ_ARG_NOT_USED(lpReserved);
141 OPJ_ARG_NOT_USED(hModule);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700142
Nicolas Pena088ca032017-09-01 13:25:16 -0400143 switch (ul_reason_for_call) {
144 case DLL_PROCESS_ATTACH :
145 break;
146 case DLL_PROCESS_DETACH :
147 break;
148 case DLL_THREAD_ATTACH :
149 case DLL_THREAD_DETACH :
150 break;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700151 }
152
153 return TRUE;
154}
155#endif /* OPJ_STATIC */
156#endif /* _WIN32 */
157
158/* ---------------------------------------------------------------------- */
159
Nicolas Pena088ca032017-09-01 13:25:16 -0400160const char* OPJ_CALLCONV opj_version(void)
161{
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700162 return OPJ_PACKAGE_VERSION;
163}
164
165/* ---------------------------------------------------------------------- */
166/* DECOMPRESSION FUNCTIONS*/
167
168opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format)
169{
Nicolas Pena088ca032017-09-01 13:25:16 -0400170 opj_codec_private_t *l_codec = 00;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700171
Nicolas Pena088ca032017-09-01 13:25:16 -0400172 l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
173 if (!l_codec) {
174 return 00;
175 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700176
Nicolas Pena088ca032017-09-01 13:25:16 -0400177 l_codec->is_decompressor = 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700178
Nicolas Pena088ca032017-09-01 13:25:16 -0400179 switch (p_format) {
180 case OPJ_CODEC_J2K:
181 l_codec->opj_dump_codec = (void (*)(void*, OPJ_INT32, FILE*)) j2k_dump;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700182
Nicolas Pena088ca032017-09-01 13:25:16 -0400183 l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*)(
184 void*)) j2k_get_cstr_info;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700185
Nicolas Pena088ca032017-09-01 13:25:16 -0400186 l_codec->opj_get_codec_index = (opj_codestream_index_t* (*)(
187 void*)) j2k_get_cstr_index;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700188
Nicolas Pena088ca032017-09-01 13:25:16 -0400189 l_codec->m_codec_data.m_decompression.opj_decode =
190 (OPJ_BOOL(*)(void *,
191 struct opj_stream_private *,
192 opj_image_t*, struct opj_event_mgr *)) opj_j2k_decode;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700193
Nicolas Pena088ca032017-09-01 13:25:16 -0400194 l_codec->m_codec_data.m_decompression.opj_end_decompress =
195 (OPJ_BOOL(*)(void *,
196 struct opj_stream_private *,
197 struct opj_event_mgr *)) opj_j2k_end_decompress;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700198
Nicolas Pena088ca032017-09-01 13:25:16 -0400199 l_codec->m_codec_data.m_decompression.opj_read_header =
200 (OPJ_BOOL(*)(struct opj_stream_private *,
201 void *,
202 opj_image_t **,
203 struct opj_event_mgr *)) opj_j2k_read_header;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700204
Nicolas Pena088ca032017-09-01 13:25:16 -0400205 l_codec->m_codec_data.m_decompression.opj_destroy =
206 (void (*)(void *))opj_j2k_destroy;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700207
Nicolas Pena088ca032017-09-01 13:25:16 -0400208 l_codec->m_codec_data.m_decompression.opj_setup_decoder =
209 (void (*)(void *, opj_dparameters_t *)) opj_j2k_setup_decoder;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700210
Nicolas Pena088ca032017-09-01 13:25:16 -0400211 l_codec->m_codec_data.m_decompression.opj_read_tile_header =
212 (OPJ_BOOL(*)(void *,
213 OPJ_UINT32*,
214 OPJ_UINT32*,
215 OPJ_INT32*, OPJ_INT32*,
216 OPJ_INT32*, OPJ_INT32*,
217 OPJ_UINT32*,
218 OPJ_BOOL*,
219 struct opj_stream_private *,
220 struct opj_event_mgr *)) opj_j2k_read_tile_header;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700221
Nicolas Pena088ca032017-09-01 13:25:16 -0400222 l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
223 (OPJ_BOOL(*)(void *,
224 OPJ_UINT32,
225 OPJ_BYTE*,
226 OPJ_UINT32,
227 struct opj_stream_private *,
228 struct opj_event_mgr *)) opj_j2k_decode_tile;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700229
Nicolas Pena088ca032017-09-01 13:25:16 -0400230 l_codec->m_codec_data.m_decompression.opj_set_decode_area =
231 (OPJ_BOOL(*)(void *,
232 opj_image_t*,
233 OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32,
234 struct opj_event_mgr *)) opj_j2k_set_decode_area;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700235
Nicolas Pena088ca032017-09-01 13:25:16 -0400236 l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
237 (OPJ_BOOL(*)(void *p_codec,
238 opj_stream_private_t *p_cio,
239 opj_image_t *p_image,
240 struct opj_event_mgr * p_manager,
241 OPJ_UINT32 tile_index)) opj_j2k_get_tile;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700242
Nicolas Pena088ca032017-09-01 13:25:16 -0400243 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
244 (OPJ_BOOL(*)(void * p_codec,
245 OPJ_UINT32 res_factor,
246 struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_resolution_factor;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700247
Nicolas Pena826480c2017-10-23 10:30:46 -0400248 l_codec->m_codec_data.m_decompression.opj_set_decoded_components =
249 (OPJ_BOOL(*)(void * p_codec,
250 OPJ_UINT32 numcomps,
251 const OPJ_UINT32 * comps_indices,
252 struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_components;
253
Nicolas Pena088ca032017-09-01 13:25:16 -0400254 l_codec->opj_set_threads =
255 (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_j2k_set_threads;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700256
Nicolas Pena088ca032017-09-01 13:25:16 -0400257 l_codec->m_codec = opj_j2k_create_decompress();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700258
Nicolas Pena088ca032017-09-01 13:25:16 -0400259 if (! l_codec->m_codec) {
260 opj_free(l_codec);
261 return NULL;
262 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700263
Nicolas Pena088ca032017-09-01 13:25:16 -0400264 break;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700265
Nicolas Pena088ca032017-09-01 13:25:16 -0400266 case OPJ_CODEC_JP2:
267 /* get a JP2 decoder handle */
268 l_codec->opj_dump_codec = (void (*)(void*, OPJ_INT32, FILE*)) jp2_dump;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700269
Nicolas Pena088ca032017-09-01 13:25:16 -0400270 l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*)(
271 void*)) jp2_get_cstr_info;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700272
Nicolas Pena088ca032017-09-01 13:25:16 -0400273 l_codec->opj_get_codec_index = (opj_codestream_index_t* (*)(
274 void*)) jp2_get_cstr_index;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700275
Nicolas Pena088ca032017-09-01 13:25:16 -0400276 l_codec->m_codec_data.m_decompression.opj_decode =
277 (OPJ_BOOL(*)(void *,
278 struct opj_stream_private *,
279 opj_image_t*,
280 struct opj_event_mgr *)) opj_jp2_decode;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700281
Nicolas Pena088ca032017-09-01 13:25:16 -0400282 l_codec->m_codec_data.m_decompression.opj_end_decompress =
283 (OPJ_BOOL(*)(void *,
284 struct opj_stream_private *,
285 struct opj_event_mgr *)) opj_jp2_end_decompress;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700286
Nicolas Pena088ca032017-09-01 13:25:16 -0400287 l_codec->m_codec_data.m_decompression.opj_read_header =
288 (OPJ_BOOL(*)(struct opj_stream_private *,
289 void *,
290 opj_image_t **,
291 struct opj_event_mgr *)) opj_jp2_read_header;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700292
Nicolas Pena088ca032017-09-01 13:25:16 -0400293 l_codec->m_codec_data.m_decompression.opj_read_tile_header =
294 (OPJ_BOOL(*)(void *,
295 OPJ_UINT32*,
296 OPJ_UINT32*,
297 OPJ_INT32*,
298 OPJ_INT32*,
299 OPJ_INT32 *,
300 OPJ_INT32 *,
301 OPJ_UINT32 *,
302 OPJ_BOOL *,
303 struct opj_stream_private *,
304 struct opj_event_mgr *)) opj_jp2_read_tile_header;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700305
Nicolas Pena088ca032017-09-01 13:25:16 -0400306 l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
307 (OPJ_BOOL(*)(void *,
308 OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32,
309 struct opj_stream_private *,
310 struct opj_event_mgr *)) opj_jp2_decode_tile;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700311
Nicolas Pena088ca032017-09-01 13:25:16 -0400312 l_codec->m_codec_data.m_decompression.opj_destroy = (void (*)(
313 void *))opj_jp2_destroy;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700314
Nicolas Pena088ca032017-09-01 13:25:16 -0400315 l_codec->m_codec_data.m_decompression.opj_setup_decoder =
316 (void (*)(void *, opj_dparameters_t *)) opj_jp2_setup_decoder;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700317
Nicolas Pena088ca032017-09-01 13:25:16 -0400318 l_codec->m_codec_data.m_decompression.opj_set_decode_area =
319 (OPJ_BOOL(*)(void *,
320 opj_image_t*,
321 OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32,
322 struct opj_event_mgr *)) opj_jp2_set_decode_area;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700323
Nicolas Pena088ca032017-09-01 13:25:16 -0400324 l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
325 (OPJ_BOOL(*)(void *p_codec,
326 opj_stream_private_t *p_cio,
327 opj_image_t *p_image,
328 struct opj_event_mgr * p_manager,
329 OPJ_UINT32 tile_index)) opj_jp2_get_tile;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700330
Nicolas Pena088ca032017-09-01 13:25:16 -0400331 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
332 (OPJ_BOOL(*)(void * p_codec,
333 OPJ_UINT32 res_factor,
334 opj_event_mgr_t * p_manager)) opj_jp2_set_decoded_resolution_factor;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700335
Nicolas Pena826480c2017-10-23 10:30:46 -0400336 l_codec->m_codec_data.m_decompression.opj_set_decoded_components =
337 (OPJ_BOOL(*)(void * p_codec,
338 OPJ_UINT32 numcomps,
339 const OPJ_UINT32 * comps_indices,
340 struct opj_event_mgr * p_manager)) opj_jp2_set_decoded_components;
341
Nicolas Pena088ca032017-09-01 13:25:16 -0400342 l_codec->opj_set_threads =
343 (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_jp2_set_threads;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700344
Nicolas Pena088ca032017-09-01 13:25:16 -0400345 l_codec->m_codec = opj_jp2_create(OPJ_TRUE);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700346
Nicolas Pena088ca032017-09-01 13:25:16 -0400347 if (! l_codec->m_codec) {
348 opj_free(l_codec);
349 return 00;
350 }
351
352 break;
353 case OPJ_CODEC_UNKNOWN:
354 case OPJ_CODEC_JPT:
355 default:
356 opj_free(l_codec);
357 return 00;
358 }
359
360 opj_set_default_event_handler(&(l_codec->m_event_mgr));
361 return (opj_codec_t*) l_codec;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700362}
363
Nicolas Pena088ca032017-09-01 13:25:16 -0400364void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t
365 *parameters)
366{
367 if (parameters) {
368 memset(parameters, 0, sizeof(opj_dparameters_t));
369 /* default decoding parameters */
370 parameters->cp_layer = 0;
371 parameters->cp_reduce = 0;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700372
Nicolas Pena088ca032017-09-01 13:25:16 -0400373 parameters->decod_format = -1;
374 parameters->cod_format = -1;
375 parameters->flags = 0;
376 /* UniPG>> */
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700377#ifdef USE_JPWL
Nicolas Pena088ca032017-09-01 13:25:16 -0400378 parameters->jpwl_correct = OPJ_FALSE;
379 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
380 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700381#endif /* USE_JPWL */
Nicolas Pena088ca032017-09-01 13:25:16 -0400382 /* <<UniPG */
383 }
384}
385
386
387OPJ_BOOL OPJ_CALLCONV opj_codec_set_threads(opj_codec_t *p_codec,
388 int num_threads)
389{
390 if (p_codec && (num_threads >= 0)) {
391 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
392
393 return l_codec->opj_set_threads(l_codec->m_codec, (OPJ_UINT32)num_threads);
394 }
395 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700396}
397
398OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
Nicolas Pena088ca032017-09-01 13:25:16 -0400399 opj_dparameters_t *parameters
400 )
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700401{
Nicolas Pena088ca032017-09-01 13:25:16 -0400402 if (p_codec && parameters) {
403 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700404
Nicolas Pena088ca032017-09-01 13:25:16 -0400405 if (! l_codec->is_decompressor) {
406 opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
407 "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
408 return OPJ_FALSE;
409 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700410
Nicolas Pena088ca032017-09-01 13:25:16 -0400411 l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
412 parameters);
413 return OPJ_TRUE;
414 }
415 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700416}
417
Nicolas Pena088ca032017-09-01 13:25:16 -0400418OPJ_BOOL OPJ_CALLCONV opj_read_header(opj_stream_t *p_stream,
419 opj_codec_t *p_codec,
420 opj_image_t **p_image)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700421{
Nicolas Pena088ca032017-09-01 13:25:16 -0400422 if (p_codec && p_stream) {
423 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
424 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700425
Nicolas Pena088ca032017-09-01 13:25:16 -0400426 if (! l_codec->is_decompressor) {
427 opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
428 "Codec provided to the opj_read_header function is not a decompressor handler.\n");
429 return OPJ_FALSE;
430 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700431
Nicolas Pena088ca032017-09-01 13:25:16 -0400432 return l_codec->m_codec_data.m_decompression.opj_read_header(l_stream,
433 l_codec->m_codec,
434 p_image,
435 &(l_codec->m_event_mgr));
436 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700437
Nicolas Pena088ca032017-09-01 13:25:16 -0400438 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700439}
440
Nicolas Pena826480c2017-10-23 10:30:46 -0400441
442OPJ_BOOL OPJ_CALLCONV opj_set_decoded_components(opj_codec_t *p_codec,
443 OPJ_UINT32 numcomps,
444 const OPJ_UINT32* comps_indices,
445 OPJ_BOOL apply_color_transforms)
446{
447 if (p_codec) {
448 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
449
450 if (! l_codec->is_decompressor) {
451 opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
452 "Codec provided to the opj_set_decoded_components function is not a decompressor handler.\n");
453 return OPJ_FALSE;
454 }
455
456 if (apply_color_transforms) {
457 opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
458 "apply_color_transforms = OPJ_TRUE is not supported.\n");
459 return OPJ_FALSE;
460 }
461
462 return l_codec->m_codec_data.m_decompression.opj_set_decoded_components(
463 l_codec->m_codec,
464 numcomps,
465 comps_indices,
466 &(l_codec->m_event_mgr));
467 }
468 return OPJ_FALSE;
469}
470
Nicolas Pena088ca032017-09-01 13:25:16 -0400471OPJ_BOOL OPJ_CALLCONV opj_decode(opj_codec_t *p_codec,
472 opj_stream_t *p_stream,
473 opj_image_t* p_image)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700474{
Nicolas Pena088ca032017-09-01 13:25:16 -0400475 if (p_codec && p_stream) {
476 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
477 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700478
Nicolas Pena088ca032017-09-01 13:25:16 -0400479 if (! l_codec->is_decompressor) {
480 return OPJ_FALSE;
481 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700482
Nicolas Pena088ca032017-09-01 13:25:16 -0400483 return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
484 l_stream,
485 p_image,
486 &(l_codec->m_event_mgr));
487 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700488
Nicolas Pena088ca032017-09-01 13:25:16 -0400489 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700490}
491
Nicolas Pena088ca032017-09-01 13:25:16 -0400492OPJ_BOOL OPJ_CALLCONV opj_set_decode_area(opj_codec_t *p_codec,
493 opj_image_t* p_image,
494 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
495 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
496 )
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700497{
Nicolas Pena088ca032017-09-01 13:25:16 -0400498 if (p_codec) {
499 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700500
Nicolas Pena088ca032017-09-01 13:25:16 -0400501 if (! l_codec->is_decompressor) {
502 return OPJ_FALSE;
503 }
504
505 return l_codec->m_codec_data.m_decompression.opj_set_decode_area(
506 l_codec->m_codec,
507 p_image,
508 p_start_x, p_start_y,
509 p_end_x, p_end_y,
510 &(l_codec->m_event_mgr));
511 }
512 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700513}
514
Nicolas Pena088ca032017-09-01 13:25:16 -0400515OPJ_BOOL OPJ_CALLCONV opj_read_tile_header(opj_codec_t *p_codec,
516 opj_stream_t * p_stream,
517 OPJ_UINT32 * p_tile_index,
518 OPJ_UINT32 * p_data_size,
519 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
520 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
521 OPJ_UINT32 * p_nb_comps,
522 OPJ_BOOL * p_should_go_on)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700523{
Nicolas Pena088ca032017-09-01 13:25:16 -0400524 if (p_codec && p_stream && p_data_size && p_tile_index) {
525 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
526 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700527
Nicolas Pena088ca032017-09-01 13:25:16 -0400528 if (! l_codec->is_decompressor) {
529 return OPJ_FALSE;
530 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700531
Nicolas Pena088ca032017-09-01 13:25:16 -0400532 return l_codec->m_codec_data.m_decompression.opj_read_tile_header(
533 l_codec->m_codec,
534 p_tile_index,
535 p_data_size,
536 p_tile_x0, p_tile_y0,
537 p_tile_x1, p_tile_y1,
538 p_nb_comps,
539 p_should_go_on,
540 l_stream,
541 &(l_codec->m_event_mgr));
542 }
543 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700544}
545
Nicolas Pena088ca032017-09-01 13:25:16 -0400546OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data(opj_codec_t *p_codec,
547 OPJ_UINT32 p_tile_index,
548 OPJ_BYTE * p_data,
549 OPJ_UINT32 p_data_size,
550 opj_stream_t *p_stream
551 )
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700552{
Nicolas Pena088ca032017-09-01 13:25:16 -0400553 if (p_codec && p_data && p_stream) {
554 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
555 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700556
Nicolas Pena088ca032017-09-01 13:25:16 -0400557 if (! l_codec->is_decompressor) {
558 return OPJ_FALSE;
559 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700560
Nicolas Pena088ca032017-09-01 13:25:16 -0400561 return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(
562 l_codec->m_codec,
563 p_tile_index,
564 p_data,
565 p_data_size,
566 l_stream,
567 &(l_codec->m_event_mgr));
568 }
569 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700570}
571
Nicolas Pena088ca032017-09-01 13:25:16 -0400572OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile(opj_codec_t *p_codec,
573 opj_stream_t *p_stream,
574 opj_image_t *p_image,
575 OPJ_UINT32 tile_index)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700576{
Nicolas Pena088ca032017-09-01 13:25:16 -0400577 if (p_codec && p_stream) {
578 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
579 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700580
Nicolas Pena088ca032017-09-01 13:25:16 -0400581 if (! l_codec->is_decompressor) {
582 return OPJ_FALSE;
583 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700584
Nicolas Pena088ca032017-09-01 13:25:16 -0400585 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(
586 l_codec->m_codec,
587 l_stream,
588 p_image,
589 &(l_codec->m_event_mgr),
590 tile_index);
591 }
592
593 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700594}
595
Nicolas Pena088ca032017-09-01 13:25:16 -0400596OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec,
597 OPJ_UINT32 res_factor)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700598{
Nicolas Pena088ca032017-09-01 13:25:16 -0400599 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700600
Nicolas Pena088ca032017-09-01 13:25:16 -0400601 if (!l_codec) {
602 return OPJ_FALSE;
603 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700604
Nicolas Pena088ca032017-09-01 13:25:16 -0400605 return l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(
606 l_codec->m_codec,
607 res_factor,
608 &(l_codec->m_event_mgr));
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700609}
610
611/* ---------------------------------------------------------------------- */
612/* COMPRESSION FUNCTIONS*/
613
614opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
615{
Nicolas Pena088ca032017-09-01 13:25:16 -0400616 opj_codec_private_t *l_codec = 00;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700617
Nicolas Pena088ca032017-09-01 13:25:16 -0400618 l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
619 if (!l_codec) {
620 return 00;
621 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700622
Nicolas Pena088ca032017-09-01 13:25:16 -0400623 l_codec->is_decompressor = 0;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700624
Nicolas Pena088ca032017-09-01 13:25:16 -0400625 switch (p_format) {
626 case OPJ_CODEC_J2K:
627 l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL(*)(void *,
628 struct opj_stream_private *,
629 struct opj_event_mgr *)) opj_j2k_encode;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700630
Nicolas Pena088ca032017-09-01 13:25:16 -0400631 l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL(*)(void *,
632 struct opj_stream_private *,
633 struct opj_event_mgr *)) opj_j2k_end_compress;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700634
Nicolas Pena088ca032017-09-01 13:25:16 -0400635 l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL(*)(void *,
636 struct opj_stream_private *,
637 struct opj_image *,
638 struct opj_event_mgr *)) opj_j2k_start_compress;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700639
Nicolas Pena088ca032017-09-01 13:25:16 -0400640 l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL(*)(void *,
641 OPJ_UINT32,
642 OPJ_BYTE*,
643 OPJ_UINT32,
644 struct opj_stream_private *,
645 struct opj_event_mgr *)) opj_j2k_write_tile;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700646
Nicolas Pena088ca032017-09-01 13:25:16 -0400647 l_codec->m_codec_data.m_compression.opj_destroy = (void (*)(
648 void *)) opj_j2k_destroy;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700649
Nicolas Pena088ca032017-09-01 13:25:16 -0400650 l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL(*)(void *,
651 opj_cparameters_t *,
652 struct opj_image *,
653 struct opj_event_mgr *)) opj_j2k_setup_encoder;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700654
Nicolas Pena088ca032017-09-01 13:25:16 -0400655 l_codec->m_codec = opj_j2k_create_compress();
656 if (! l_codec->m_codec) {
657 opj_free(l_codec);
658 return 00;
659 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700660
Nicolas Pena088ca032017-09-01 13:25:16 -0400661 break;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700662
Nicolas Pena088ca032017-09-01 13:25:16 -0400663 case OPJ_CODEC_JP2:
664 /* get a JP2 decoder handle */
665 l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL(*)(void *,
666 struct opj_stream_private *,
667 struct opj_event_mgr *)) opj_jp2_encode;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700668
Nicolas Pena088ca032017-09-01 13:25:16 -0400669 l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL(*)(void *,
670 struct opj_stream_private *,
671 struct opj_event_mgr *)) opj_jp2_end_compress;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700672
Nicolas Pena088ca032017-09-01 13:25:16 -0400673 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_jp2_start_compress;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700677
Nicolas Pena088ca032017-09-01 13:25:16 -0400678 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_jp2_write_tile;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700684
Nicolas Pena088ca032017-09-01 13:25:16 -0400685 l_codec->m_codec_data.m_compression.opj_destroy = (void (*)(
686 void *)) opj_jp2_destroy;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700687
Nicolas Pena088ca032017-09-01 13:25:16 -0400688 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_jp2_setup_encoder;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700692
Nicolas Pena088ca032017-09-01 13:25:16 -0400693 l_codec->m_codec = opj_jp2_create(OPJ_FALSE);
694 if (! l_codec->m_codec) {
695 opj_free(l_codec);
696 return 00;
697 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700698
Nicolas Pena088ca032017-09-01 13:25:16 -0400699 break;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700700
Nicolas Pena088ca032017-09-01 13:25:16 -0400701 case OPJ_CODEC_UNKNOWN:
702 case OPJ_CODEC_JPT:
703 default:
704 opj_free(l_codec);
705 return 00;
706 }
707
708 opj_set_default_event_handler(&(l_codec->m_event_mgr));
709 return (opj_codec_t*) l_codec;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700710}
711
Nicolas Pena088ca032017-09-01 13:25:16 -0400712void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t
713 *parameters)
714{
715 if (parameters) {
716 memset(parameters, 0, sizeof(opj_cparameters_t));
717 /* default coding parameters */
Bo Xud53e6fd2014-09-30 11:12:05 -0700718 parameters->cp_cinema = OPJ_OFF; /* DEPRECATED */
719 parameters->rsiz = OPJ_PROFILE_NONE;
Nicolas Pena088ca032017-09-01 13:25:16 -0400720 parameters->max_comp_size = 0;
721 parameters->numresolution = 6;
Bo Xud53e6fd2014-09-30 11:12:05 -0700722 parameters->cp_rsiz = OPJ_STD_RSIZ; /* DEPRECATED */
Nicolas Pena088ca032017-09-01 13:25:16 -0400723 parameters->cblockw_init = 64;
724 parameters->cblockh_init = 64;
725 parameters->prog_order = OPJ_LRCP;
726 parameters->roi_compno = -1; /* no ROI */
727 parameters->subsampling_dx = 1;
728 parameters->subsampling_dy = 1;
729 parameters->tp_on = 0;
730 parameters->decod_format = -1;
731 parameters->cod_format = -1;
732 parameters->tcp_rates[0] = 0;
733 parameters->tcp_numlayers = 0;
734 parameters->cp_disto_alloc = 0;
735 parameters->cp_fixed_alloc = 0;
736 parameters->cp_fixed_quality = 0;
737 parameters->jpip_on = OPJ_FALSE;
738 /* UniPG>> */
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700739#ifdef USE_JPWL
Nicolas Pena088ca032017-09-01 13:25:16 -0400740 parameters->jpwl_epc_on = OPJ_FALSE;
741 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
742 {
743 int i;
744 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
745 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
746 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
747 }
748 };
749 {
750 int i;
751 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
752 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
753 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
754 parameters->jpwl_pprot[i] = 0; /* absent */
755 }
756 };
757 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
758 parameters->jpwl_sens_addr = 0; /* 0 means auto */
759 parameters->jpwl_sens_range = 0; /* 0 means packet */
760 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
761 {
762 int i;
763 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
764 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
765 parameters->jpwl_sens_TPH[i] = -1; /* absent */
766 }
767 };
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700768#endif /* USE_JPWL */
Nicolas Pena088ca032017-09-01 13:25:16 -0400769 /* <<UniPG */
770 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700771}
772
Nicolas Pena088ca032017-09-01 13:25:16 -0400773OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec,
774 opj_cparameters_t *parameters,
775 opj_image_t *p_image)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700776{
Nicolas Pena088ca032017-09-01 13:25:16 -0400777 if (p_codec && parameters && p_image) {
778 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700779
Nicolas Pena088ca032017-09-01 13:25:16 -0400780 if (! l_codec->is_decompressor) {
781 return l_codec->m_codec_data.m_compression.opj_setup_encoder(l_codec->m_codec,
782 parameters,
783 p_image,
784 &(l_codec->m_event_mgr));
785 }
786 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700787
Nicolas Pena088ca032017-09-01 13:25:16 -0400788 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700789}
790
Nicolas Pena088ca032017-09-01 13:25:16 -0400791OPJ_BOOL OPJ_CALLCONV opj_start_compress(opj_codec_t *p_codec,
792 opj_image_t * p_image,
793 opj_stream_t *p_stream)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700794{
Nicolas Pena088ca032017-09-01 13:25:16 -0400795 if (p_codec && p_stream) {
796 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
797 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700798
Nicolas Pena088ca032017-09-01 13:25:16 -0400799 if (! l_codec->is_decompressor) {
800 return l_codec->m_codec_data.m_compression.opj_start_compress(l_codec->m_codec,
801 l_stream,
802 p_image,
803 &(l_codec->m_event_mgr));
804 }
805 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700806
Nicolas Pena088ca032017-09-01 13:25:16 -0400807 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700808}
809
810OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
811{
Nicolas Pena088ca032017-09-01 13:25:16 -0400812 if (p_info && p_stream) {
813 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
814 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700815
Nicolas Pena088ca032017-09-01 13:25:16 -0400816 if (! l_codec->is_decompressor) {
817 return l_codec->m_codec_data.m_compression.opj_encode(l_codec->m_codec,
818 l_stream,
819 &(l_codec->m_event_mgr));
820 }
821 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700822
Nicolas Pena088ca032017-09-01 13:25:16 -0400823 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700824
825}
826
Nicolas Pena088ca032017-09-01 13:25:16 -0400827OPJ_BOOL OPJ_CALLCONV opj_end_compress(opj_codec_t *p_codec,
828 opj_stream_t *p_stream)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700829{
Nicolas Pena088ca032017-09-01 13:25:16 -0400830 if (p_codec && p_stream) {
831 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
832 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700833
Nicolas Pena088ca032017-09-01 13:25:16 -0400834 if (! l_codec->is_decompressor) {
835 return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
836 l_stream,
837 &(l_codec->m_event_mgr));
838 }
839 }
840 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700841
842}
843
Nicolas Pena088ca032017-09-01 13:25:16 -0400844OPJ_BOOL OPJ_CALLCONV opj_end_decompress(opj_codec_t *p_codec,
845 opj_stream_t *p_stream)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700846{
Nicolas Pena088ca032017-09-01 13:25:16 -0400847 if (p_codec && p_stream) {
848 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
849 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700850
Nicolas Pena088ca032017-09-01 13:25:16 -0400851 if (! l_codec->is_decompressor) {
852 return OPJ_FALSE;
853 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700854
Nicolas Pena088ca032017-09-01 13:25:16 -0400855 return l_codec->m_codec_data.m_decompression.opj_end_decompress(
856 l_codec->m_codec,
857 l_stream,
858 &(l_codec->m_event_mgr));
859 }
860
861 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700862}
863
864OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,
865 OPJ_FLOAT32 * pEncodingMatrix,
Nicolas Pena088ca032017-09-01 13:25:16 -0400866 OPJ_INT32 * p_dc_shift, OPJ_UINT32 pNbComp)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700867{
Nicolas Pena088ca032017-09-01 13:25:16 -0400868 OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
869 OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32);
870 OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700871
Nicolas Pena088ca032017-09-01 13:25:16 -0400872 /* add MCT capability */
Bo Xud53e6fd2014-09-30 11:12:05 -0700873 if (OPJ_IS_PART2(parameters->rsiz)) {
874 parameters->rsiz |= OPJ_EXTENSION_MCT;
875 } else {
876 parameters->rsiz = ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT));
877 }
Nicolas Pena088ca032017-09-01 13:25:16 -0400878 parameters->irreversible = 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700879
Nicolas Pena088ca032017-09-01 13:25:16 -0400880 /* use array based MCT */
881 parameters->tcp_mct = 2;
882 parameters->mct_data = opj_malloc(l_mct_total_size);
883 if (! parameters->mct_data) {
884 return OPJ_FALSE;
885 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700886
Nicolas Pena088ca032017-09-01 13:25:16 -0400887 memcpy(parameters->mct_data, pEncodingMatrix, l_matrix_size);
888 memcpy(((OPJ_BYTE *) parameters->mct_data) + l_matrix_size, p_dc_shift,
889 l_dc_shift_size);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700890
Nicolas Pena088ca032017-09-01 13:25:16 -0400891 return OPJ_TRUE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700892}
893
Nicolas Pena088ca032017-09-01 13:25:16 -0400894OPJ_BOOL OPJ_CALLCONV opj_write_tile(opj_codec_t *p_codec,
895 OPJ_UINT32 p_tile_index,
896 OPJ_BYTE * p_data,
897 OPJ_UINT32 p_data_size,
898 opj_stream_t *p_stream)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700899{
Nicolas Pena088ca032017-09-01 13:25:16 -0400900 if (p_codec && p_stream && p_data) {
901 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
902 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700903
Nicolas Pena088ca032017-09-01 13:25:16 -0400904 if (l_codec->is_decompressor) {
905 return OPJ_FALSE;
906 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700907
Nicolas Pena088ca032017-09-01 13:25:16 -0400908 return l_codec->m_codec_data.m_compression.opj_write_tile(l_codec->m_codec,
909 p_tile_index,
910 p_data,
911 p_data_size,
912 l_stream,
913 &(l_codec->m_event_mgr));
914 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700915
Nicolas Pena088ca032017-09-01 13:25:16 -0400916 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700917}
918
919/* ---------------------------------------------------------------------- */
920
921void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
922{
Nicolas Pena088ca032017-09-01 13:25:16 -0400923 if (p_codec) {
924 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700925
Nicolas Pena088ca032017-09-01 13:25:16 -0400926 if (l_codec->is_decompressor) {
927 l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
928 } else {
929 l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
930 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700931
Nicolas Pena088ca032017-09-01 13:25:16 -0400932 l_codec->m_codec = 00;
933 opj_free(l_codec);
934 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700935}
936
937/* ---------------------------------------------------------------------- */
938
Nicolas Pena088ca032017-09-01 13:25:16 -0400939void OPJ_CALLCONV opj_dump_codec(opj_codec_t *p_codec,
940 OPJ_INT32 info_flag,
941 FILE* output_stream)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700942{
Nicolas Pena088ca032017-09-01 13:25:16 -0400943 if (p_codec) {
944 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700945
Nicolas Pena088ca032017-09-01 13:25:16 -0400946 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
947 return;
948 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700949
Nicolas Pena088ca032017-09-01 13:25:16 -0400950 /* TODO return error */
951 /* fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n"); */
952 return;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700953}
954
955opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
956{
Nicolas Pena088ca032017-09-01 13:25:16 -0400957 if (p_codec) {
958 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700959
Nicolas Pena088ca032017-09-01 13:25:16 -0400960 return l_codec->opj_get_codec_info(l_codec->m_codec);
961 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700962
Nicolas Pena088ca032017-09-01 13:25:16 -0400963 return NULL;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700964}
965
Nicolas Pena088ca032017-09-01 13:25:16 -0400966void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info)
967{
968 if (cstr_info) {
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700969
Nicolas Pena088ca032017-09-01 13:25:16 -0400970 if ((*cstr_info)->m_default_tile_info.tccp_info) {
971 opj_free((*cstr_info)->m_default_tile_info.tccp_info);
972 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700973
Nicolas Pena088ca032017-09-01 13:25:16 -0400974 if ((*cstr_info)->tile_info) {
975 /* FIXME not used for the moment*/
976 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700977
Nicolas Pena088ca032017-09-01 13:25:16 -0400978 opj_free((*cstr_info));
979 (*cstr_info) = NULL;
980 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700981}
982
983opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
984{
Nicolas Pena088ca032017-09-01 13:25:16 -0400985 if (p_codec) {
986 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700987
Nicolas Pena088ca032017-09-01 13:25:16 -0400988 return l_codec->opj_get_codec_index(l_codec->m_codec);
989 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700990
Nicolas Pena088ca032017-09-01 13:25:16 -0400991 return NULL;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700992}
993
994void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
995{
Nicolas Pena088ca032017-09-01 13:25:16 -0400996 if (*p_cstr_index) {
997 j2k_destroy_cstr_index(*p_cstr_index);
998 (*p_cstr_index) = NULL;
999 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001000}
1001
Nicolas Pena088ca032017-09-01 13:25:16 -04001002opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream(
1003 const char *fname, OPJ_BOOL p_is_read_stream)
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001004{
Nicolas Pena088ca032017-09-01 13:25:16 -04001005 return opj_stream_create_file_stream(fname, OPJ_J2K_STREAM_CHUNK_SIZE,
1006 p_is_read_stream);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001007}
1008
Nicolas Pena088ca032017-09-01 13:25:16 -04001009opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream(
1010 const char *fname,
1011 OPJ_SIZE_T p_size,
1012 OPJ_BOOL p_is_read_stream)
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001013{
1014 opj_stream_t* l_stream = 00;
1015 FILE *p_file;
1016 const char *mode;
1017
1018 if (! fname) {
1019 return NULL;
1020 }
Nicolas Pena088ca032017-09-01 13:25:16 -04001021
1022 if (p_is_read_stream) {
1023 mode = "rb";
1024 } else {
1025 mode = "wb";
1026 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001027
1028 p_file = fopen(fname, mode);
1029
1030 if (! p_file) {
Nicolas Pena088ca032017-09-01 13:25:16 -04001031 return NULL;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001032 }
1033
Nicolas Pena088ca032017-09-01 13:25:16 -04001034 l_stream = opj_stream_create(p_size, p_is_read_stream);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001035 if (! l_stream) {
1036 fclose(p_file);
1037 return NULL;
1038 }
1039
Nicolas Pena088ca032017-09-01 13:25:16 -04001040 opj_stream_set_user_data(l_stream, p_file,
1041 (opj_stream_free_user_data_fn) fclose);
1042 opj_stream_set_user_data_length(l_stream,
1043 opj_get_data_length_from_file(p_file));
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001044 opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
Nicolas Pena088ca032017-09-01 13:25:16 -04001045 opj_stream_set_write_function(l_stream,
1046 (opj_stream_write_fn) opj_write_from_file);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001047 opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
1048 opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
1049
1050 return l_stream;
1051}
Nicolas Pena088ca032017-09-01 13:25:16 -04001052
1053
1054void* OPJ_CALLCONV opj_image_data_alloc(OPJ_SIZE_T size)
1055{
1056 void* ret = opj_aligned_malloc(size);
1057 /* printf("opj_image_data_alloc %p\n", ret); */
1058 return ret;
1059}
1060
1061void OPJ_CALLCONV opj_image_data_free(void* ptr)
1062{
1063 /* printf("opj_image_data_free %p\n", ptr); */
1064 opj_aligned_free(ptr);
1065}