• 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,FILE * p_file)91 static OPJ_SIZE_T opj_read_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
92 {
93 	OPJ_SIZE_T l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
94 	return l_nb_read ? l_nb_read : (OPJ_SIZE_T)-1;
95 }
96 
opj_get_data_length_from_file(FILE * p_file)97 static OPJ_UINT64 opj_get_data_length_from_file (FILE * p_file)
98 {
99 	OPJ_OFF_T file_length = 0;
100 
101 	OPJ_FSEEK(p_file, 0, SEEK_END);
102 	file_length = (OPJ_OFF_T)OPJ_FTELL(p_file);
103 	OPJ_FSEEK(p_file, 0, SEEK_SET);
104 
105 	return (OPJ_UINT64)file_length;
106 }
107 
opj_write_from_file(void * p_buffer,OPJ_SIZE_T p_nb_bytes,FILE * p_file)108 static OPJ_SIZE_T opj_write_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
109 {
110 	return fwrite(p_buffer,1,p_nb_bytes,p_file);
111 }
112 
opj_skip_from_file(OPJ_OFF_T p_nb_bytes,FILE * p_user_data)113 static OPJ_OFF_T opj_skip_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
114 {
115 	if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_CUR)) {
116 		return -1;
117 	}
118 
119 	return p_nb_bytes;
120 }
121 
opj_seek_from_file(OPJ_OFF_T p_nb_bytes,FILE * p_user_data)122 static OPJ_BOOL opj_seek_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
123 {
124 	if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_SET)) {
125 		return OPJ_FALSE;
126 	}
127 
128 	return OPJ_TRUE;
129 }
130 
131 /* ---------------------------------------------------------------------- */
132 #ifdef _WIN32
133 #ifndef OPJ_STATIC
134 BOOL APIENTRY
DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)135 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
136 
137 	OPJ_ARG_NOT_USED(lpReserved);
138 	OPJ_ARG_NOT_USED(hModule);
139 
140 	switch (ul_reason_for_call) {
141 		case DLL_PROCESS_ATTACH :
142 			break;
143 		case DLL_PROCESS_DETACH :
144 			break;
145 		case DLL_THREAD_ATTACH :
146 		case DLL_THREAD_DETACH :
147 			break;
148     }
149 
150     return TRUE;
151 }
152 #endif /* OPJ_STATIC */
153 #endif /* _WIN32 */
154 
155 /* ---------------------------------------------------------------------- */
156 
opj_version(void)157 const char* OPJ_CALLCONV opj_version(void) {
158     return OPJ_PACKAGE_VERSION;
159 }
160 
161 /* ---------------------------------------------------------------------- */
162 /* DECOMPRESSION FUNCTIONS*/
163 
opj_create_decompress(OPJ_CODEC_FORMAT p_format)164 opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format)
165 {
166 	opj_codec_private_t *l_codec = 00;
167 
168 	l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
169 	if (!l_codec){
170 		return 00;
171 	}
172 	memset(l_codec, 0, sizeof(opj_codec_private_t));
173 
174 	l_codec->is_decompressor = 1;
175 
176 	switch (p_format) {
177 		case OPJ_CODEC_J2K:
178 			l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
179 
180 			l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
181 
182 			l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
183 
184 			l_codec->m_codec_data.m_decompression.opj_decode =
185 					(OPJ_BOOL (*) (	void *,
186 									struct opj_stream_private *,
187 									opj_image_t*, struct opj_event_mgr * )) opj_j2k_decode;
188 
189 			l_codec->m_codec_data.m_decompression.opj_end_decompress =
190 					(OPJ_BOOL (*) (	void *,
191 									struct opj_stream_private *,
192 									struct opj_event_mgr *)) opj_j2k_end_decompress;
193 
194 			l_codec->m_codec_data.m_decompression.opj_read_header =
195 					(OPJ_BOOL (*) (	struct opj_stream_private *,
196 									void *,
197 									opj_image_t **,
198 									struct opj_event_mgr * )) opj_j2k_read_header;
199 
200 			l_codec->m_codec_data.m_decompression.opj_destroy =
201 					(void (*) (void *))opj_j2k_destroy;
202 
203 			l_codec->m_codec_data.m_decompression.opj_setup_decoder =
204 					(void (*) (void * , opj_dparameters_t * )) opj_j2k_setup_decoder;
205 
206 			l_codec->m_codec_data.m_decompression.opj_read_tile_header =
207 					(OPJ_BOOL (*) (	void *,
208 									OPJ_UINT32*,
209 									OPJ_UINT32*,
210 									OPJ_INT32*, OPJ_INT32*,
211 									OPJ_INT32*, OPJ_INT32*,
212 									OPJ_UINT32*,
213 									OPJ_BOOL*,
214 									struct opj_stream_private *,
215 									struct opj_event_mgr * )) opj_j2k_read_tile_header;
216 
217 			l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
218 					(OPJ_BOOL (*) ( void *,
219                                     OPJ_UINT32,
220                                     OPJ_BYTE*,
221                                     OPJ_UINT32,
222                                     struct opj_stream_private *,
223                                     struct opj_event_mgr *)) opj_j2k_decode_tile;
224 
225 			l_codec->m_codec_data.m_decompression.opj_set_decode_area =
226 					(OPJ_BOOL (*) ( void *,
227                                     opj_image_t*,
228                                     OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32,
229                                     struct opj_event_mgr *)) opj_j2k_set_decode_area;
230 
231 			l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
232                     (OPJ_BOOL (*) ( void *p_codec,
233 								    opj_stream_private_t *p_cio,
234 								    opj_image_t *p_image,
235 								    struct opj_event_mgr * p_manager,
236 								    OPJ_UINT32 tile_index)) opj_j2k_get_tile;
237 
238 			l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
239                     (OPJ_BOOL (*) ( void * p_codec,
240 									OPJ_UINT32 res_factor,
241 									struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_resolution_factor;
242 
243 			l_codec->m_codec = opj_j2k_create_decompress();
244 
245 			if (! l_codec->m_codec) {
246 				opj_free(l_codec);
247 				return NULL;
248 			}
249 
250 			break;
251 
252 		case OPJ_CODEC_JP2:
253 			/* get a JP2 decoder handle */
254 			l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
255 
256 			l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
257 
258 			l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
259 
260 			l_codec->m_codec_data.m_decompression.opj_decode =
261 					(OPJ_BOOL (*) (	void *,
262 									struct opj_stream_private *,
263 									opj_image_t*,
264 									struct opj_event_mgr * )) opj_jp2_decode;
265 
266 			l_codec->m_codec_data.m_decompression.opj_end_decompress =
267                     (OPJ_BOOL (*) ( void *,
268                                     struct opj_stream_private *,
269                                     struct opj_event_mgr *)) opj_jp2_end_decompress;
270 
271 			l_codec->m_codec_data.m_decompression.opj_read_header =
272                     (OPJ_BOOL (*) ( struct opj_stream_private *,
273 					                void *,
274 					                opj_image_t **,
275 					                struct opj_event_mgr * )) opj_jp2_read_header;
276 
277 			l_codec->m_codec_data.m_decompression.opj_read_tile_header =
278                     (OPJ_BOOL (*) ( void *,
279 					                OPJ_UINT32*,
280 					                OPJ_UINT32*,
281 					                OPJ_INT32*,
282 					                OPJ_INT32*,
283 					                OPJ_INT32 * ,
284 					                OPJ_INT32 * ,
285 					                OPJ_UINT32 * ,
286 					                OPJ_BOOL *,
287 					                struct opj_stream_private *,
288 					                struct opj_event_mgr * )) opj_jp2_read_tile_header;
289 
290 			l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
291                     (OPJ_BOOL (*) ( void *,
292                                     OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,
293                                     struct opj_stream_private *,
294                                     struct opj_event_mgr * )) opj_jp2_decode_tile;
295 
296 			l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))opj_jp2_destroy;
297 
298 			l_codec->m_codec_data.m_decompression.opj_setup_decoder =
299                     (void (*) (void * ,opj_dparameters_t * )) opj_jp2_setup_decoder;
300 
301 			l_codec->m_codec_data.m_decompression.opj_set_decode_area =
302                     (OPJ_BOOL (*) ( void *,
303                                     opj_image_t*,
304                                     OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32,
305                                     struct opj_event_mgr * )) opj_jp2_set_decode_area;
306 
307 			l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
308                     (OPJ_BOOL (*) ( void *p_codec,
309 									opj_stream_private_t *p_cio,
310 									opj_image_t *p_image,
311 									struct opj_event_mgr * p_manager,
312 									OPJ_UINT32 tile_index)) opj_jp2_get_tile;
313 
314 			l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
315                     (OPJ_BOOL (*) ( void * p_codec,
316 						    		OPJ_UINT32 res_factor,
317 							    	opj_event_mgr_t * p_manager)) opj_jp2_set_decoded_resolution_factor;
318 
319 			l_codec->m_codec = opj_jp2_create(OPJ_TRUE);
320 
321 			if (! l_codec->m_codec) {
322 				opj_free(l_codec);
323 				return 00;
324 			}
325 
326 			break;
327 		case OPJ_CODEC_UNKNOWN:
328 		case OPJ_CODEC_JPT:
329 		default:
330 			opj_free(l_codec);
331 			return 00;
332 	}
333 
334 	opj_set_default_event_handler(&(l_codec->m_event_mgr));
335 	return (opj_codec_t*) l_codec;
336 }
337 
opj_set_default_decoder_parameters(opj_dparameters_t * parameters)338 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
339 	if(parameters) {
340 		memset(parameters, 0, sizeof(opj_dparameters_t));
341 		/* default decoding parameters */
342 		parameters->cp_layer = 0;
343 		parameters->cp_reduce = 0;
344 
345 		parameters->decod_format = -1;
346 		parameters->cod_format = -1;
347 		parameters->flags = 0;
348 /* UniPG>> */
349 #ifdef USE_JPWL
350 		parameters->jpwl_correct = OPJ_FALSE;
351 		parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
352 		parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
353 #endif /* USE_JPWL */
354 /* <<UniPG */
355 	}
356 }
357 
opj_setup_decoder(opj_codec_t * p_codec,opj_dparameters_t * parameters)358 OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
359                                         opj_dparameters_t *parameters
360 										)
361 {
362 	if (p_codec && parameters) {
363 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
364 
365 		if (! l_codec->is_decompressor) {
366 			opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
367                 "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
368 			return OPJ_FALSE;
369 		}
370 
371 		l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
372 																parameters);
373 		return OPJ_TRUE;
374 	}
375 	return OPJ_FALSE;
376 }
377 
opj_read_header(opj_stream_t * p_stream,opj_codec_t * p_codec,opj_image_t ** p_image)378 OPJ_BOOL OPJ_CALLCONV opj_read_header (	opj_stream_t *p_stream,
379 										opj_codec_t *p_codec,
380 										opj_image_t **p_image )
381 {
382 	if (p_codec && p_stream) {
383 		opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
384 		opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
385 
386 		if(! l_codec->is_decompressor) {
387 			opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
388                 "Codec provided to the opj_read_header function is not a decompressor handler.\n");
389 			return OPJ_FALSE;
390 		}
391 
392 		return l_codec->m_codec_data.m_decompression.opj_read_header(	l_stream,
393 																		l_codec->m_codec,
394 																		p_image,
395 																		&(l_codec->m_event_mgr) );
396 	}
397 
398 	return OPJ_FALSE;
399 }
400 
opj_decode(opj_codec_t * p_codec,opj_stream_t * p_stream,opj_image_t * p_image)401 OPJ_BOOL OPJ_CALLCONV opj_decode(   opj_codec_t *p_codec,
402                                     opj_stream_t *p_stream,
403                                     opj_image_t* p_image)
404 {
405 	if (p_codec && p_stream) {
406 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
407 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
408 
409 		if (! l_codec->is_decompressor) {
410 			return OPJ_FALSE;
411 		}
412 
413 		return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
414 																l_stream,
415 																p_image,
416 																&(l_codec->m_event_mgr) );
417 	}
418 
419 	return OPJ_FALSE;
420 }
421 
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)422 OPJ_BOOL OPJ_CALLCONV opj_set_decode_area(	opj_codec_t *p_codec,
423 											opj_image_t* p_image,
424 											OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
425 											OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
426 											)
427 {
428 	if (p_codec) {
429 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
430 
431 		if (! l_codec->is_decompressor) {
432 			return OPJ_FALSE;
433 		}
434 
435 		return  l_codec->m_codec_data.m_decompression.opj_set_decode_area(	l_codec->m_codec,
436 																			p_image,
437 																			p_start_x, p_start_y,
438 																			p_end_x, p_end_y,
439 																			&(l_codec->m_event_mgr) );
440 	}
441 	return OPJ_FALSE;
442 }
443 
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)444 OPJ_BOOL OPJ_CALLCONV opj_read_tile_header(	opj_codec_t *p_codec,
445 											opj_stream_t * p_stream,
446 											OPJ_UINT32 * p_tile_index,
447 											OPJ_UINT32 * p_data_size,
448 											OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
449 											OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
450 											OPJ_UINT32 * p_nb_comps,
451 											OPJ_BOOL * p_should_go_on)
452 {
453 	if (p_codec && p_stream && p_data_size && p_tile_index) {
454 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
455 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
456 
457 		if (! l_codec->is_decompressor) {
458 			return OPJ_FALSE;
459 		}
460 
461 		return l_codec->m_codec_data.m_decompression.opj_read_tile_header(	l_codec->m_codec,
462 																			p_tile_index,
463 																			p_data_size,
464 																			p_tile_x0, p_tile_y0,
465 																			p_tile_x1, p_tile_y1,
466 																			p_nb_comps,
467 																			p_should_go_on,
468 																			l_stream,
469 																			&(l_codec->m_event_mgr));
470 	}
471 	return OPJ_FALSE;
472 }
473 
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)474 OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data(	opj_codec_t *p_codec,
475 											OPJ_UINT32 p_tile_index,
476 											OPJ_BYTE * p_data,
477 											OPJ_UINT32 p_data_size,
478 											opj_stream_t *p_stream
479 											)
480 {
481 	if (p_codec && p_data && p_stream) {
482 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
483 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
484 
485 		if (! l_codec->is_decompressor) {
486 			return OPJ_FALSE;
487 		}
488 
489 		return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(	l_codec->m_codec,
490 																			p_tile_index,
491 																			p_data,
492 																			p_data_size,
493 																			l_stream,
494 																			&(l_codec->m_event_mgr) );
495 	}
496 	return OPJ_FALSE;
497 }
498 
opj_get_decoded_tile(opj_codec_t * p_codec,opj_stream_t * p_stream,opj_image_t * p_image,OPJ_UINT32 tile_index)499 OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile(	opj_codec_t *p_codec,
500 											opj_stream_t *p_stream,
501 											opj_image_t *p_image,
502 											OPJ_UINT32 tile_index)
503 {
504 	if (p_codec && p_stream) {
505 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
506 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
507 
508 		if (! l_codec->is_decompressor) {
509 			return OPJ_FALSE;
510 		}
511 
512 		return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(	l_codec->m_codec,
513 																			l_stream,
514 																			p_image,
515 																			&(l_codec->m_event_mgr),
516 																			tile_index);
517 	}
518 
519 	return OPJ_FALSE;
520 }
521 
opj_set_decoded_resolution_factor(opj_codec_t * p_codec,OPJ_UINT32 res_factor)522 OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec,
523 														OPJ_UINT32 res_factor )
524 {
525 	opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
526 
527 	if ( !l_codec ){
528 		fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
529 		return OPJ_FALSE;
530 	}
531 
532 	l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec,
533 																			res_factor,
534 																			&(l_codec->m_event_mgr) );
535 	return OPJ_TRUE;
536 }
537 
538 /* ---------------------------------------------------------------------- */
539 /* COMPRESSION FUNCTIONS*/
540 
opj_create_compress(OPJ_CODEC_FORMAT p_format)541 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
542 {
543 	opj_codec_private_t *l_codec = 00;
544 
545 	l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
546 	if (!l_codec) {
547 		return 00;
548 	}
549 	memset(l_codec, 0, sizeof(opj_codec_private_t));
550 
551 	l_codec->is_decompressor = 0;
552 
553 	switch(p_format) {
554 		case OPJ_CODEC_J2K:
555 			l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *,
556 																			struct opj_stream_private *,
557 																			struct opj_event_mgr * )) opj_j2k_encode;
558 
559 			l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) (	void *,
560 																					struct opj_stream_private *,
561 																					struct opj_event_mgr *)) opj_j2k_end_compress;
562 
563 			l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *,
564 																					struct opj_stream_private *,
565 																					struct opj_image * ,
566 																					struct opj_event_mgr *)) opj_j2k_start_compress;
567 
568 			l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *,
569 																				OPJ_UINT32,
570 																				OPJ_BYTE*,
571 																				OPJ_UINT32,
572 																				struct opj_stream_private *,
573 																				struct opj_event_mgr *) ) opj_j2k_write_tile;
574 
575 			l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_j2k_destroy;
576 
577 			l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) (	void *,
578 																				opj_cparameters_t *,
579 																				struct opj_image *,
580 																				struct opj_event_mgr * )) opj_j2k_setup_encoder;
581 
582 			l_codec->m_codec = opj_j2k_create_compress();
583 			if (! l_codec->m_codec) {
584 				opj_free(l_codec);
585 				return 00;
586 			}
587 
588 			break;
589 
590 		case OPJ_CODEC_JP2:
591 			/* get a JP2 decoder handle */
592 			l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *,
593 																			struct opj_stream_private *,
594 																			struct opj_event_mgr * )) opj_jp2_encode;
595 
596 			l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) (	void *,
597 																					struct opj_stream_private *,
598 																					struct opj_event_mgr *)) opj_jp2_end_compress;
599 
600 			l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *,
601 																					struct opj_stream_private *,
602 																					struct opj_image * ,
603 																					struct opj_event_mgr *))  opj_jp2_start_compress;
604 
605 			l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *,
606 																				OPJ_UINT32,
607 																				OPJ_BYTE*,
608 																				OPJ_UINT32,
609 																				struct opj_stream_private *,
610 																				struct opj_event_mgr *)) opj_jp2_write_tile;
611 
612 			l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_jp2_destroy;
613 
614 			l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) (	void *,
615 																				opj_cparameters_t *,
616 																				struct opj_image *,
617 																				struct opj_event_mgr * )) opj_jp2_setup_encoder;
618 
619 			l_codec->m_codec = opj_jp2_create(OPJ_FALSE);
620 			if (! l_codec->m_codec) {
621 				opj_free(l_codec);
622 				return 00;
623 			}
624 
625 			break;
626 
627 		case OPJ_CODEC_UNKNOWN:
628 		case OPJ_CODEC_JPT:
629 		default:
630 			opj_free(l_codec);
631 			return 00;
632 	}
633 
634 	opj_set_default_event_handler(&(l_codec->m_event_mgr));
635 	return (opj_codec_t*) l_codec;
636 }
637 
opj_set_default_encoder_parameters(opj_cparameters_t * parameters)638 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
639 	if(parameters) {
640 		memset(parameters, 0, sizeof(opj_cparameters_t));
641 		/* default coding parameters */
642 		parameters->cp_cinema = OPJ_OFF;
643 		parameters->max_comp_size = 0;
644 		parameters->numresolution = 6;
645 		parameters->cp_rsiz = OPJ_STD_RSIZ;
646 		parameters->cblockw_init = 64;
647 		parameters->cblockh_init = 64;
648 		parameters->prog_order = OPJ_LRCP;
649 		parameters->roi_compno = -1;		/* no ROI */
650 		parameters->subsampling_dx = 1;
651 		parameters->subsampling_dy = 1;
652 		parameters->tp_on = 0;
653 		parameters->decod_format = -1;
654 		parameters->cod_format = -1;
655 		parameters->tcp_rates[0] = 0;
656 		parameters->tcp_numlayers = 0;
657 		parameters->cp_disto_alloc = 0;
658 		parameters->cp_fixed_alloc = 0;
659 		parameters->cp_fixed_quality = 0;
660 		parameters->jpip_on = OPJ_FALSE;
661 /* UniPG>> */
662 #ifdef USE_JPWL
663 		parameters->jpwl_epc_on = OPJ_FALSE;
664 		parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
665 		{
666 			int i;
667 			for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
668 				parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
669 				parameters->jpwl_hprot_TPH[i] = 0; /* absent */
670 			}
671 		};
672 		{
673 			int i;
674 			for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
675 				parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
676 				parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
677 				parameters->jpwl_pprot[i] = 0; /* absent */
678 			}
679 		};
680 		parameters->jpwl_sens_size = 0; /* 0 means no ESD */
681 		parameters->jpwl_sens_addr = 0; /* 0 means auto */
682 		parameters->jpwl_sens_range = 0; /* 0 means packet */
683 		parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
684 		{
685 			int i;
686 			for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
687 				parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
688 				parameters->jpwl_sens_TPH[i] = -1; /* absent */
689 			}
690 		};
691 #endif /* USE_JPWL */
692 /* <<UniPG */
693 	}
694 }
695 
opj_setup_encoder(opj_codec_t * p_codec,opj_cparameters_t * parameters,opj_image_t * p_image)696 OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec,
697 										opj_cparameters_t *parameters,
698 										opj_image_t *p_image)
699 {
700 	if (p_codec && parameters && p_image) {
701 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
702 
703 		if (! l_codec->is_decompressor) {
704 			l_codec->m_codec_data.m_compression.opj_setup_encoder(	l_codec->m_codec,
705 																	parameters,
706 																	p_image,
707 																	&(l_codec->m_event_mgr) );
708 			return OPJ_TRUE;
709 		}
710 	}
711 
712 	return OPJ_FALSE;
713 }
714 
opj_start_compress(opj_codec_t * p_codec,opj_image_t * p_image,opj_stream_t * p_stream)715 OPJ_BOOL OPJ_CALLCONV opj_start_compress (	opj_codec_t *p_codec,
716 											opj_image_t * p_image,
717 											opj_stream_t *p_stream)
718 {
719 	if (p_codec && p_stream) {
720 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
721 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
722 
723 		if (! l_codec->is_decompressor) {
724 			return l_codec->m_codec_data.m_compression.opj_start_compress(	l_codec->m_codec,
725 																			l_stream,
726 																			p_image,
727 																			&(l_codec->m_event_mgr));
728 		}
729 	}
730 
731 	return OPJ_FALSE;
732 }
733 
opj_encode(opj_codec_t * p_info,opj_stream_t * p_stream)734 OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
735 {
736 	if (p_info && p_stream) {
737 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
738 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
739 
740 		if (! l_codec->is_decompressor) {
741 			return l_codec->m_codec_data.m_compression.opj_encode(	l_codec->m_codec,
742 															l_stream,
743 															&(l_codec->m_event_mgr));
744 		}
745 	}
746 
747 	return OPJ_FALSE;
748 
749 }
750 
opj_end_compress(opj_codec_t * p_codec,opj_stream_t * p_stream)751 OPJ_BOOL OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
752 										opj_stream_t *p_stream)
753 {
754 	if (p_codec && p_stream) {
755 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
756 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
757 
758 		if (! l_codec->is_decompressor) {
759 			return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
760 																		l_stream,
761 																		&(l_codec->m_event_mgr));
762 		}
763 	}
764 	return OPJ_FALSE;
765 
766 }
767 
opj_end_decompress(opj_codec_t * p_codec,opj_stream_t * p_stream)768 OPJ_BOOL OPJ_CALLCONV opj_end_decompress (	opj_codec_t *p_codec,
769 											opj_stream_t *p_stream)
770 {
771 	if (p_codec && p_stream) {
772 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
773 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
774 
775 		if (! l_codec->is_decompressor) {
776 			return OPJ_FALSE;
777 		}
778 
779 		return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
780 																		l_stream,
781 																		&(l_codec->m_event_mgr) );
782 	}
783 
784 	return OPJ_FALSE;
785 }
786 
opj_set_MCT(opj_cparameters_t * parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)787 OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,
788                                   OPJ_FLOAT32 * pEncodingMatrix,
789                                   OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
790 {
791 	OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
792 	OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32);
793 	OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
794 
795 	/* add MCT capability */
796 	OPJ_INT32 rsiz = (OPJ_INT32)parameters->cp_rsiz | (OPJ_INT32)OPJ_MCT;
797 	parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
798 	parameters->irreversible = 1;
799 
800 	/* use array based MCT */
801 	parameters->tcp_mct = 2;
802 	parameters->mct_data = opj_malloc(l_mct_total_size);
803 	if (! parameters->mct_data) {
804 		return OPJ_FALSE;
805 	}
806 
807 	memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
808 	memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size,p_dc_shift,l_dc_shift_size);
809 
810 	return OPJ_TRUE;
811 }
812 
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)813 OPJ_BOOL OPJ_CALLCONV opj_write_tile (	opj_codec_t *p_codec,
814 										OPJ_UINT32 p_tile_index,
815 										OPJ_BYTE * p_data,
816 										OPJ_UINT32 p_data_size,
817 										opj_stream_t *p_stream )
818 {
819 	if (p_codec && p_stream && p_data) {
820 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
821 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
822 
823 		if (l_codec->is_decompressor) {
824 			return OPJ_FALSE;
825 		}
826 
827 		return l_codec->m_codec_data.m_compression.opj_write_tile(	l_codec->m_codec,
828 																	p_tile_index,
829 																	p_data,
830 																	p_data_size,
831 																	l_stream,
832 																	&(l_codec->m_event_mgr) );
833 	}
834 
835 	return OPJ_FALSE;
836 }
837 
838 /* ---------------------------------------------------------------------- */
839 
opj_destroy_codec(opj_codec_t * p_codec)840 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
841 {
842 	if (p_codec) {
843 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
844 
845 		if (l_codec->is_decompressor) {
846 			l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
847 		}
848 		else {
849 			l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
850 		}
851 
852 		l_codec->m_codec = 00;
853 		opj_free(l_codec);
854 	}
855 }
856 
857 /* ---------------------------------------------------------------------- */
858 
opj_dump_codec(opj_codec_t * p_codec,OPJ_INT32 info_flag,FILE * output_stream)859 void OPJ_CALLCONV opj_dump_codec(	opj_codec_t *p_codec,
860 									OPJ_INT32 info_flag,
861 									FILE* output_stream)
862 {
863 	if (p_codec) {
864 		opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
865 
866 		l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
867 		return;
868 	}
869 
870 	fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
871 	return;
872 }
873 
opj_get_cstr_info(opj_codec_t * p_codec)874 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
875 {
876 	if (p_codec) {
877 		opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
878 
879 		return l_codec->opj_get_codec_info(l_codec->m_codec);
880 	}
881 
882 	return NULL;
883 }
884 
opj_destroy_cstr_info(opj_codestream_info_v2_t ** cstr_info)885 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) {
886 	if (cstr_info) {
887 
888 		if ((*cstr_info)->m_default_tile_info.tccp_info){
889 			opj_free((*cstr_info)->m_default_tile_info.tccp_info);
890 		}
891 
892 		if ((*cstr_info)->tile_info){
893 			/* FIXME not used for the moment*/
894 		}
895 
896 		opj_free((*cstr_info));
897 		(*cstr_info) = NULL;
898 	}
899 }
900 
opj_get_cstr_index(opj_codec_t * p_codec)901 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
902 {
903 	if (p_codec) {
904 		opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
905 
906 		return l_codec->opj_get_codec_index(l_codec->m_codec);
907 	}
908 
909 	return NULL;
910 }
911 
opj_destroy_cstr_index(opj_codestream_index_t ** p_cstr_index)912 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
913 {
914 	if (*p_cstr_index){
915 		j2k_destroy_cstr_index(*p_cstr_index);
916 		(*p_cstr_index) = NULL;
917 	}
918 }
919 
opj_stream_create_default_file_stream_v3(const char * fname,OPJ_BOOL p_is_read_stream)920 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream_v3 (const char *fname, OPJ_BOOL p_is_read_stream)
921 {
922     return opj_stream_create_file_stream_v3(fname, OPJ_J2K_STREAM_CHUNK_SIZE, p_is_read_stream);
923 }
924 
opj_stream_create_file_stream_v3(const char * fname,OPJ_SIZE_T p_size,OPJ_BOOL p_is_read_stream)925 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream_v3 (
926         const char *fname,
927 		OPJ_SIZE_T p_size,
928         OPJ_BOOL p_is_read_stream)
929 {
930     opj_stream_t* l_stream = 00;
931     FILE *p_file;
932     const char *mode;
933 
934     if (! fname) {
935         return NULL;
936     }
937 
938     if(p_is_read_stream) mode = "rb"; else mode = "wb";
939 
940     p_file = fopen(fname, mode);
941 
942     if (! p_file) {
943 	    return NULL;
944     }
945 
946     l_stream = opj_stream_create(p_size,p_is_read_stream);
947     if (! l_stream) {
948         fclose(p_file);
949         return NULL;
950     }
951 
952     opj_stream_set_user_data_v3(l_stream, p_file, (opj_stream_free_user_data_fn) fclose);
953     opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
954     opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
955     opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
956     opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
957     opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
958 
959     return l_stream;
960 }
961