xref: /aosp_15_r20/external/pdfium/third_party/libopenjpeg/openjpeg.c (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 = j2k_dump;
193 
194         l_codec->opj_get_codec_info = j2k_get_cstr_info;
195 
196         l_codec->opj_get_codec_index = j2k_get_cstr_index;
197 
198         l_codec->m_codec_data.m_decompression.opj_decode = opj_j2k_decode;
199 
200         l_codec->m_codec_data.m_decompression.opj_end_decompress =
201             opj_j2k_end_decompress;
202 
203         l_codec->m_codec_data.m_decompression.opj_read_header =
204             opj_j2k_read_header;
205 
206         l_codec->m_codec_data.m_decompression.opj_destroy = opj_j2k_destroy;
207 
208         l_codec->m_codec_data.m_decompression.opj_setup_decoder =
209             opj_j2k_setup_decoder;
210 
211         l_codec->m_codec_data.m_decompression.opj_decoder_set_strict_mode =
212             opj_j2k_decoder_set_strict_mode;
213 
214 
215         l_codec->m_codec_data.m_decompression.opj_read_tile_header =
216             opj_j2k_read_tile_header;
217 
218         l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
219             opj_j2k_decode_tile;
220 
221         l_codec->m_codec_data.m_decompression.opj_set_decode_area =
222             opj_j2k_set_decode_area;
223 
224         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
225             opj_j2k_get_tile;
226 
227         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
228             opj_j2k_set_decoded_resolution_factor;
229 
230         l_codec->m_codec_data.m_decompression.opj_set_decoded_components =
231             opj_j2k_set_decoded_components;
232 
233         l_codec->opj_set_threads = opj_j2k_set_threads;
234 
235         l_codec->m_codec = opj_j2k_create_decompress();
236 
237         if (! l_codec->m_codec) {
238             opj_free(l_codec);
239             return NULL;
240         }
241 
242         break;
243 
244     case OPJ_CODEC_JP2:
245         /* get a JP2 decoder handle */
246         l_codec->opj_dump_codec = jp2_dump;
247 
248         l_codec->opj_get_codec_info = jp2_get_cstr_info;
249 
250         l_codec->opj_get_codec_index = jp2_get_cstr_index;
251 
252         l_codec->m_codec_data.m_decompression.opj_decode = opj_jp2_decode;
253 
254         l_codec->m_codec_data.m_decompression.opj_end_decompress =
255             opj_jp2_end_decompress;
256 
257         l_codec->m_codec_data.m_decompression.opj_read_header =
258             opj_jp2_read_header;
259 
260         l_codec->m_codec_data.m_decompression.opj_read_tile_header =
261             opj_jp2_read_tile_header;
262 
263         l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
264             opj_jp2_decode_tile;
265 
266         l_codec->m_codec_data.m_decompression.opj_destroy = opj_jp2_destroy;
267 
268         l_codec->m_codec_data.m_decompression.opj_setup_decoder =
269              opj_jp2_setup_decoder;
270 
271         l_codec->m_codec_data.m_decompression.opj_decoder_set_strict_mode =
272             opj_jp2_decoder_set_strict_mode;
273 
274         l_codec->m_codec_data.m_decompression.opj_set_decode_area =
275             opj_jp2_set_decode_area;
276 
277         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
278             opj_jp2_get_tile;
279 
280         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
281             opj_jp2_set_decoded_resolution_factor;
282 
283         l_codec->m_codec_data.m_decompression.opj_set_decoded_components =
284             opj_jp2_set_decoded_components;
285 
286         l_codec->opj_set_threads = opj_jp2_set_threads;
287 
288         l_codec->m_codec = opj_jp2_create(OPJ_TRUE);
289 
290         if (! l_codec->m_codec) {
291             opj_free(l_codec);
292             return 00;
293         }
294 
295         break;
296     case OPJ_CODEC_UNKNOWN:
297     case OPJ_CODEC_JPT:
298     default:
299         opj_free(l_codec);
300         return 00;
301     }
302 
303     opj_set_default_event_handler(&(l_codec->m_event_mgr));
304     return (opj_codec_t*) l_codec;
305 }
306 
opj_set_default_decoder_parameters(opj_dparameters_t * parameters)307 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t
308         *parameters)
309 {
310     if (parameters) {
311         memset(parameters, 0, sizeof(opj_dparameters_t));
312         /* default decoding parameters */
313         parameters->cp_layer = 0;
314         parameters->cp_reduce = 0;
315 
316         parameters->decod_format = -1;
317         parameters->cod_format = -1;
318         parameters->flags = 0;
319         /* UniPG>> */
320 #ifdef USE_JPWL
321         parameters->jpwl_correct = OPJ_FALSE;
322         parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
323         parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
324 #endif /* USE_JPWL */
325         /* <<UniPG */
326     }
327 }
328 
329 
opj_codec_set_threads(opj_codec_t * p_codec,int num_threads)330 OPJ_BOOL OPJ_CALLCONV opj_codec_set_threads(opj_codec_t *p_codec,
331         int num_threads)
332 {
333     if (p_codec && (num_threads >= 0)) {
334         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
335 
336         return l_codec->opj_set_threads(l_codec->m_codec, (OPJ_UINT32)num_threads);
337     }
338     return OPJ_FALSE;
339 }
340 
opj_setup_decoder(opj_codec_t * p_codec,opj_dparameters_t * parameters)341 OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
342                                         opj_dparameters_t *parameters
343                                        )
344 {
345     if (p_codec && parameters) {
346         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
347 
348         if (! l_codec->is_decompressor) {
349             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
350                           "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
351             return OPJ_FALSE;
352         }
353 
354         l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
355                 parameters);
356         return OPJ_TRUE;
357     }
358     return OPJ_FALSE;
359 }
360 
opj_decoder_set_strict_mode(opj_codec_t * p_codec,OPJ_BOOL strict)361 OPJ_API OPJ_BOOL OPJ_CALLCONV opj_decoder_set_strict_mode(opj_codec_t *p_codec,
362         OPJ_BOOL strict)
363 {
364     if (p_codec) {
365         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
366 
367         if (! l_codec->is_decompressor) {
368             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
369                           "Codec provided to the opj_decoder_set_strict_mode function is not a decompressor handler.\n");
370             return OPJ_FALSE;
371         }
372 
373         l_codec->m_codec_data.m_decompression.opj_decoder_set_strict_mode(
374             l_codec->m_codec,
375             strict);
376         return OPJ_TRUE;
377     }
378     return OPJ_FALSE;
379 }
380 
opj_read_header(opj_stream_t * p_stream,opj_codec_t * p_codec,opj_image_t ** p_image)381 OPJ_BOOL OPJ_CALLCONV opj_read_header(opj_stream_t *p_stream,
382                                       opj_codec_t *p_codec,
383                                       opj_image_t **p_image)
384 {
385     if (p_codec && p_stream) {
386         opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
387         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
388 
389         if (! l_codec->is_decompressor) {
390             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
391                           "Codec provided to the opj_read_header function is not a decompressor handler.\n");
392             return OPJ_FALSE;
393         }
394 
395         return l_codec->m_codec_data.m_decompression.opj_read_header(l_stream,
396                 l_codec->m_codec,
397                 p_image,
398                 &(l_codec->m_event_mgr));
399     }
400 
401     return OPJ_FALSE;
402 }
403 
404 
opj_set_decoded_components(opj_codec_t * p_codec,OPJ_UINT32 numcomps,const OPJ_UINT32 * comps_indices,OPJ_BOOL apply_color_transforms)405 OPJ_BOOL OPJ_CALLCONV opj_set_decoded_components(opj_codec_t *p_codec,
406         OPJ_UINT32 numcomps,
407         const OPJ_UINT32* comps_indices,
408         OPJ_BOOL apply_color_transforms)
409 {
410     if (p_codec) {
411         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
412 
413         if (! l_codec->is_decompressor) {
414             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
415                           "Codec provided to the opj_set_decoded_components function is not a decompressor handler.\n");
416             return OPJ_FALSE;
417         }
418 
419         if (apply_color_transforms) {
420             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
421                           "apply_color_transforms = OPJ_TRUE is not supported.\n");
422             return OPJ_FALSE;
423         }
424 
425         return  l_codec->m_codec_data.m_decompression.opj_set_decoded_components(
426                     l_codec->m_codec,
427                     numcomps,
428                     comps_indices,
429                     &(l_codec->m_event_mgr));
430     }
431     return OPJ_FALSE;
432 }
433 
opj_decode(opj_codec_t * p_codec,opj_stream_t * p_stream,opj_image_t * p_image)434 OPJ_BOOL OPJ_CALLCONV opj_decode(opj_codec_t *p_codec,
435                                  opj_stream_t *p_stream,
436                                  opj_image_t* p_image)
437 {
438     if (p_codec && p_stream) {
439         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
440         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
441 
442         if (! l_codec->is_decompressor) {
443             return OPJ_FALSE;
444         }
445 
446         return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
447                 l_stream,
448                 p_image,
449                 &(l_codec->m_event_mgr));
450     }
451 
452     return OPJ_FALSE;
453 }
454 
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)455 OPJ_BOOL OPJ_CALLCONV opj_set_decode_area(opj_codec_t *p_codec,
456         opj_image_t* p_image,
457         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
458         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
459                                          )
460 {
461     if (p_codec) {
462         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
463 
464         if (! l_codec->is_decompressor) {
465             return OPJ_FALSE;
466         }
467 
468         return  l_codec->m_codec_data.m_decompression.opj_set_decode_area(
469                     l_codec->m_codec,
470                     p_image,
471                     p_start_x, p_start_y,
472                     p_end_x, p_end_y,
473                     &(l_codec->m_event_mgr));
474     }
475     return OPJ_FALSE;
476 }
477 
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)478 OPJ_BOOL OPJ_CALLCONV opj_read_tile_header(opj_codec_t *p_codec,
479         opj_stream_t * p_stream,
480         OPJ_UINT32 * p_tile_index,
481         OPJ_UINT32 * p_data_size,
482         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
483         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
484         OPJ_UINT32 * p_nb_comps,
485         OPJ_BOOL * p_should_go_on)
486 {
487     if (p_codec && p_stream && p_data_size && p_tile_index) {
488         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
489         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
490 
491         if (! l_codec->is_decompressor) {
492             return OPJ_FALSE;
493         }
494 
495         return l_codec->m_codec_data.m_decompression.opj_read_tile_header(
496                    l_codec->m_codec,
497                    p_tile_index,
498                    p_data_size,
499                    p_tile_x0, p_tile_y0,
500                    p_tile_x1, p_tile_y1,
501                    p_nb_comps,
502                    p_should_go_on,
503                    l_stream,
504                    &(l_codec->m_event_mgr));
505     }
506     return OPJ_FALSE;
507 }
508 
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)509 OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data(opj_codec_t *p_codec,
510         OPJ_UINT32 p_tile_index,
511         OPJ_BYTE * p_data,
512         OPJ_UINT32 p_data_size,
513         opj_stream_t *p_stream
514                                           )
515 {
516     if (p_codec && p_data && p_stream) {
517         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
518         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
519 
520         if (! l_codec->is_decompressor) {
521             return OPJ_FALSE;
522         }
523 
524         return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(
525                    l_codec->m_codec,
526                    p_tile_index,
527                    p_data,
528                    p_data_size,
529                    l_stream,
530                    &(l_codec->m_event_mgr));
531     }
532     return OPJ_FALSE;
533 }
534 
opj_get_decoded_tile(opj_codec_t * p_codec,opj_stream_t * p_stream,opj_image_t * p_image,OPJ_UINT32 tile_index)535 OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile(opj_codec_t *p_codec,
536         opj_stream_t *p_stream,
537         opj_image_t *p_image,
538         OPJ_UINT32 tile_index)
539 {
540     if (p_codec && p_stream) {
541         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
542         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
543 
544         if (! l_codec->is_decompressor) {
545             return OPJ_FALSE;
546         }
547 
548         return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(
549                    l_codec->m_codec,
550                    l_stream,
551                    p_image,
552                    &(l_codec->m_event_mgr),
553                    tile_index);
554     }
555 
556     return OPJ_FALSE;
557 }
558 
opj_set_decoded_resolution_factor(opj_codec_t * p_codec,OPJ_UINT32 res_factor)559 OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec,
560         OPJ_UINT32 res_factor)
561 {
562     opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
563 
564     if (!l_codec) {
565         return OPJ_FALSE;
566     }
567 
568     return l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(
569                l_codec->m_codec,
570                res_factor,
571                &(l_codec->m_event_mgr));
572 }
573 
574 /* ---------------------------------------------------------------------- */
575 /* COMPRESSION FUNCTIONS*/
576 
opj_create_compress(OPJ_CODEC_FORMAT p_format)577 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
578 {
579     opj_codec_private_t *l_codec = 00;
580 
581     l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
582     if (!l_codec) {
583         return 00;
584     }
585 
586     l_codec->is_decompressor = 0;
587 
588     switch (p_format) {
589     case OPJ_CODEC_J2K:
590         l_codec->m_codec_data.m_compression.opj_encode = opj_j2k_encode;
591 
592         l_codec->m_codec_data.m_compression.opj_end_compress =
593             opj_j2k_end_compress;
594 
595         l_codec->m_codec_data.m_compression.opj_start_compress =
596             opj_j2k_start_compress;
597 
598         l_codec->m_codec_data.m_compression.opj_write_tile = opj_j2k_write_tile;
599 
600         l_codec->m_codec_data.m_compression.opj_destroy = opj_j2k_destroy;
601 
602         l_codec->m_codec_data.m_compression.opj_setup_encoder =
603             opj_j2k_setup_encoder;
604 
605         l_codec->m_codec_data.m_compression.opj_encoder_set_extra_options =
606             opj_j2k_encoder_set_extra_options;
607 
608         l_codec->opj_set_threads = opj_j2k_set_threads;
609 
610         l_codec->m_codec = opj_j2k_create_compress();
611         if (! l_codec->m_codec) {
612             opj_free(l_codec);
613             return 00;
614         }
615 
616         break;
617 
618     case OPJ_CODEC_JP2:
619         /* get a JP2 decoder handle */
620         l_codec->m_codec_data.m_compression.opj_encode = opj_jp2_encode;
621 
622         l_codec->m_codec_data.m_compression.opj_end_compress =
623             opj_jp2_end_compress;
624 
625         l_codec->m_codec_data.m_compression.opj_start_compress =
626             opj_jp2_start_compress;
627 
628         l_codec->m_codec_data.m_compression.opj_write_tile = opj_jp2_write_tile;
629 
630         l_codec->m_codec_data.m_compression.opj_destroy = opj_jp2_destroy;
631 
632         l_codec->m_codec_data.m_compression.opj_setup_encoder =
633             opj_jp2_setup_encoder;
634 
635         l_codec->m_codec_data.m_compression.opj_encoder_set_extra_options =
636             opj_jp2_encoder_set_extra_options;
637 
638         l_codec->opj_set_threads = opj_jp2_set_threads;
639 
640         l_codec->m_codec = opj_jp2_create(OPJ_FALSE);
641         if (! l_codec->m_codec) {
642             opj_free(l_codec);
643             return 00;
644         }
645 
646         break;
647 
648     case OPJ_CODEC_UNKNOWN:
649     case OPJ_CODEC_JPT:
650     default:
651         opj_free(l_codec);
652         return 00;
653     }
654 
655     opj_set_default_event_handler(&(l_codec->m_event_mgr));
656     return (opj_codec_t*) l_codec;
657 }
658 
opj_set_default_encoder_parameters(opj_cparameters_t * parameters)659 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t
660         *parameters)
661 {
662     if (parameters) {
663         memset(parameters, 0, sizeof(opj_cparameters_t));
664         /* default coding parameters */
665         parameters->cp_cinema = OPJ_OFF; /* DEPRECATED */
666         parameters->rsiz = OPJ_PROFILE_NONE;
667         parameters->max_comp_size = 0;
668         parameters->numresolution = OPJ_COMP_PARAM_DEFAULT_NUMRESOLUTION;
669         parameters->cp_rsiz = OPJ_STD_RSIZ; /* DEPRECATED */
670         parameters->cblockw_init = OPJ_COMP_PARAM_DEFAULT_CBLOCKW;
671         parameters->cblockh_init = OPJ_COMP_PARAM_DEFAULT_CBLOCKH;
672         parameters->prog_order = OPJ_COMP_PARAM_DEFAULT_PROG_ORDER;
673         parameters->roi_compno = -1;        /* no ROI */
674         parameters->subsampling_dx = 1;
675         parameters->subsampling_dy = 1;
676         parameters->tp_on = 0;
677         parameters->decod_format = -1;
678         parameters->cod_format = -1;
679         parameters->tcp_rates[0] = 0;
680         parameters->tcp_numlayers = 0;
681         parameters->cp_disto_alloc = 0;
682         parameters->cp_fixed_alloc = 0;
683         parameters->cp_fixed_quality = 0;
684         parameters->jpip_on = OPJ_FALSE;
685         /* UniPG>> */
686 #ifdef USE_JPWL
687         parameters->jpwl_epc_on = OPJ_FALSE;
688         parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
689         {
690             int i;
691             for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
692                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
693                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
694             }
695         };
696         {
697             int i;
698             for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
699                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
700                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
701                 parameters->jpwl_pprot[i] = 0; /* absent */
702             }
703         };
704         parameters->jpwl_sens_size = 0; /* 0 means no ESD */
705         parameters->jpwl_sens_addr = 0; /* 0 means auto */
706         parameters->jpwl_sens_range = 0; /* 0 means packet */
707         parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
708         {
709             int i;
710             for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
711                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
712                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
713             }
714         };
715 #endif /* USE_JPWL */
716         /* <<UniPG */
717     }
718 }
719 
opj_setup_encoder(opj_codec_t * p_codec,opj_cparameters_t * parameters,opj_image_t * p_image)720 OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec,
721                                         opj_cparameters_t *parameters,
722                                         opj_image_t *p_image)
723 {
724     if (p_codec && parameters && p_image) {
725         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
726 
727         if (! l_codec->is_decompressor) {
728             return l_codec->m_codec_data.m_compression.opj_setup_encoder(l_codec->m_codec,
729                     parameters,
730                     p_image,
731                     &(l_codec->m_event_mgr));
732         }
733     }
734 
735     return OPJ_FALSE;
736 }
737 
738 /* ----------------------------------------------------------------------- */
739 
opj_encoder_set_extra_options(opj_codec_t * p_codec,const char * const * options)740 OPJ_BOOL OPJ_CALLCONV opj_encoder_set_extra_options(opj_codec_t *p_codec,
741         const char* const* options)
742 {
743     if (p_codec) {
744         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
745 
746         if (! l_codec->is_decompressor) {
747             return l_codec->m_codec_data.m_compression.opj_encoder_set_extra_options(
748                        l_codec->m_codec,
749                        options,
750                        &(l_codec->m_event_mgr));
751         }
752     }
753 
754     return OPJ_FALSE;
755 }
756 
757 /* ----------------------------------------------------------------------- */
758 
opj_start_compress(opj_codec_t * p_codec,opj_image_t * p_image,opj_stream_t * p_stream)759 OPJ_BOOL OPJ_CALLCONV opj_start_compress(opj_codec_t *p_codec,
760         opj_image_t * p_image,
761         opj_stream_t *p_stream)
762 {
763     if (p_codec && p_stream) {
764         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
765         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
766 
767         if (! l_codec->is_decompressor) {
768             return l_codec->m_codec_data.m_compression.opj_start_compress(l_codec->m_codec,
769                     l_stream,
770                     p_image,
771                     &(l_codec->m_event_mgr));
772         }
773     }
774 
775     return OPJ_FALSE;
776 }
777 
opj_encode(opj_codec_t * p_info,opj_stream_t * p_stream)778 OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
779 {
780     if (p_info && p_stream) {
781         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
782         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
783 
784         if (! l_codec->is_decompressor) {
785             return l_codec->m_codec_data.m_compression.opj_encode(l_codec->m_codec,
786                     l_stream,
787                     &(l_codec->m_event_mgr));
788         }
789     }
790 
791     return OPJ_FALSE;
792 
793 }
794 
opj_end_compress(opj_codec_t * p_codec,opj_stream_t * p_stream)795 OPJ_BOOL OPJ_CALLCONV opj_end_compress(opj_codec_t *p_codec,
796                                        opj_stream_t *p_stream)
797 {
798     if (p_codec && p_stream) {
799         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
800         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
801 
802         if (! l_codec->is_decompressor) {
803             return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
804                     l_stream,
805                     &(l_codec->m_event_mgr));
806         }
807     }
808     return OPJ_FALSE;
809 
810 }
811 
opj_end_decompress(opj_codec_t * p_codec,opj_stream_t * p_stream)812 OPJ_BOOL OPJ_CALLCONV opj_end_decompress(opj_codec_t *p_codec,
813         opj_stream_t *p_stream)
814 {
815     if (p_codec && p_stream) {
816         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
817         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
818 
819         if (! l_codec->is_decompressor) {
820             return OPJ_FALSE;
821         }
822 
823         return l_codec->m_codec_data.m_decompression.opj_end_decompress(
824                    l_codec->m_codec,
825                    l_stream,
826                    &(l_codec->m_event_mgr));
827     }
828 
829     return OPJ_FALSE;
830 }
831 
opj_set_MCT(opj_cparameters_t * parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)832 OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,
833                                   OPJ_FLOAT32 * pEncodingMatrix,
834                                   OPJ_INT32 * p_dc_shift, OPJ_UINT32 pNbComp)
835 {
836     OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
837     OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32);
838     OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
839 
840     /* add MCT capability */
841     if (OPJ_IS_PART2(parameters->rsiz)) {
842         parameters->rsiz |= OPJ_EXTENSION_MCT;
843     } else {
844         parameters->rsiz = ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT));
845     }
846     parameters->irreversible = 1;
847 
848     /* use array based MCT */
849     parameters->tcp_mct = 2;
850     parameters->mct_data = opj_malloc(l_mct_total_size);
851     if (! parameters->mct_data) {
852         return OPJ_FALSE;
853     }
854 
855     memcpy(parameters->mct_data, pEncodingMatrix, l_matrix_size);
856     memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size, p_dc_shift,
857            l_dc_shift_size);
858 
859     return OPJ_TRUE;
860 }
861 
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)862 OPJ_BOOL OPJ_CALLCONV opj_write_tile(opj_codec_t *p_codec,
863                                      OPJ_UINT32 p_tile_index,
864                                      OPJ_BYTE * p_data,
865                                      OPJ_UINT32 p_data_size,
866                                      opj_stream_t *p_stream)
867 {
868     if (p_codec && p_stream && p_data) {
869         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
870         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
871 
872         if (l_codec->is_decompressor) {
873             return OPJ_FALSE;
874         }
875 
876         return l_codec->m_codec_data.m_compression.opj_write_tile(l_codec->m_codec,
877                 p_tile_index,
878                 p_data,
879                 p_data_size,
880                 l_stream,
881                 &(l_codec->m_event_mgr));
882     }
883 
884     return OPJ_FALSE;
885 }
886 
887 /* ---------------------------------------------------------------------- */
888 
opj_destroy_codec(opj_codec_t * p_codec)889 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
890 {
891     if (p_codec) {
892         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
893 
894         if (l_codec->is_decompressor) {
895             l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
896         } else {
897             l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
898         }
899 
900         l_codec->m_codec = 00;
901         opj_free(l_codec);
902     }
903 }
904 
905 /* ---------------------------------------------------------------------- */
906 
opj_dump_codec(opj_codec_t * p_codec,OPJ_INT32 info_flag,FILE * output_stream)907 void OPJ_CALLCONV opj_dump_codec(opj_codec_t *p_codec,
908                                  OPJ_INT32 info_flag,
909                                  FILE* output_stream)
910 {
911     if (p_codec) {
912         opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
913 
914         l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
915         return;
916     }
917 
918     /* TODO return error */
919     /* fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n"); */
920     return;
921 }
922 
opj_get_cstr_info(opj_codec_t * p_codec)923 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
924 {
925     if (p_codec) {
926         opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
927 
928         return l_codec->opj_get_codec_info(l_codec->m_codec);
929     }
930 
931     return NULL;
932 }
933 
opj_destroy_cstr_info(opj_codestream_info_v2_t ** cstr_info)934 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info)
935 {
936     if (cstr_info) {
937 
938         if ((*cstr_info)->m_default_tile_info.tccp_info) {
939             opj_free((*cstr_info)->m_default_tile_info.tccp_info);
940         }
941 
942         if ((*cstr_info)->tile_info) {
943             /* FIXME not used for the moment*/
944         }
945 
946         opj_free((*cstr_info));
947         (*cstr_info) = NULL;
948     }
949 }
950 
opj_get_cstr_index(opj_codec_t * p_codec)951 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
952 {
953     if (p_codec) {
954         opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
955 
956         return l_codec->opj_get_codec_index(l_codec->m_codec);
957     }
958 
959     return NULL;
960 }
961 
opj_destroy_cstr_index(opj_codestream_index_t ** p_cstr_index)962 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
963 {
964     if (*p_cstr_index) {
965         j2k_destroy_cstr_index(*p_cstr_index);
966         (*p_cstr_index) = NULL;
967     }
968 }
969 
opj_stream_create_default_file_stream(const char * fname,OPJ_BOOL p_is_read_stream)970 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream(
971     const char *fname, OPJ_BOOL p_is_read_stream)
972 {
973     return opj_stream_create_file_stream(fname, OPJ_J2K_STREAM_CHUNK_SIZE,
974                                          p_is_read_stream);
975 }
976 
opj_stream_create_file_stream(const char * fname,OPJ_SIZE_T p_size,OPJ_BOOL p_is_read_stream)977 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream(
978     const char *fname,
979     OPJ_SIZE_T p_size,
980     OPJ_BOOL p_is_read_stream)
981 {
982     opj_stream_t* l_stream = 00;
983     FILE *p_file;
984     const char *mode;
985 
986     if (! fname) {
987         return NULL;
988     }
989 
990     if (p_is_read_stream) {
991         mode = "rb";
992     } else {
993         mode = "wb";
994     }
995 
996     p_file = fopen(fname, mode);
997 
998     if (! p_file) {
999         return NULL;
1000     }
1001 
1002     l_stream = opj_stream_create(p_size, p_is_read_stream);
1003     if (! l_stream) {
1004         fclose(p_file);
1005         return NULL;
1006     }
1007 
1008     opj_stream_set_user_data(l_stream, p_file, opj_close_from_file);
1009     opj_stream_set_user_data_length(l_stream,
1010                                     opj_get_data_length_from_file(p_file));
1011     opj_stream_set_read_function(l_stream, opj_read_from_file);
1012     opj_stream_set_write_function(l_stream,
1013                                   (opj_stream_write_fn) opj_write_from_file);
1014     opj_stream_set_skip_function(l_stream, opj_skip_from_file);
1015     opj_stream_set_seek_function(l_stream, opj_seek_from_file);
1016 
1017     return l_stream;
1018 }
1019 
1020 
opj_image_data_alloc(OPJ_SIZE_T size)1021 void* OPJ_CALLCONV opj_image_data_alloc(OPJ_SIZE_T size)
1022 {
1023     void* ret = opj_aligned_malloc(size);
1024     /* printf("opj_image_data_alloc %p\n", ret); */
1025     return ret;
1026 }
1027 
opj_image_data_free(void * ptr)1028 void OPJ_CALLCONV opj_image_data_free(void* ptr)
1029 {
1030     /* printf("opj_image_data_free %p\n", ptr); */
1031     opj_aligned_free(ptr);
1032 }
1033