• 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(HINSTANCE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)135 DllMain(HINSTANCE 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 
173 	l_codec->is_decompressor = 1;
174 
175 	switch (p_format) {
176 		case OPJ_CODEC_J2K:
177 			l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
178 
179 			l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
180 
181 			l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
182 
183 			l_codec->m_codec_data.m_decompression.opj_decode =
184 					(OPJ_BOOL (*) (	void *,
185 									struct opj_stream_private *,
186 									opj_image_t*, struct opj_event_mgr * )) opj_j2k_decode;
187 
188 			l_codec->m_codec_data.m_decompression.opj_end_decompress =
189 					(OPJ_BOOL (*) (	void *,
190 									struct opj_stream_private *,
191 									struct opj_event_mgr *)) opj_j2k_end_decompress;
192 
193 			l_codec->m_codec_data.m_decompression.opj_read_header =
194 					(OPJ_BOOL (*) (	struct opj_stream_private *,
195 									void *,
196 									opj_image_t **,
197 									struct opj_event_mgr * )) opj_j2k_read_header;
198 
199 			l_codec->m_codec_data.m_decompression.opj_destroy =
200 					(void (*) (void *))opj_j2k_destroy;
201 
202 			l_codec->m_codec_data.m_decompression.opj_setup_decoder =
203 					(void (*) (void * , opj_dparameters_t * )) opj_j2k_setup_decoder;
204 
205 			l_codec->m_codec_data.m_decompression.opj_read_tile_header =
206 					(OPJ_BOOL (*) (	void *,
207 									OPJ_UINT32*,
208 									OPJ_UINT32*,
209 									OPJ_INT32*, OPJ_INT32*,
210 									OPJ_INT32*, OPJ_INT32*,
211 									OPJ_UINT32*,
212 									OPJ_BOOL*,
213 									struct opj_stream_private *,
214 									struct opj_event_mgr * )) opj_j2k_read_tile_header;
215 
216 			l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
217 					(OPJ_BOOL (*) ( void *,
218                                     OPJ_UINT32,
219                                     OPJ_BYTE*,
220                                     OPJ_UINT32,
221                                     struct opj_stream_private *,
222                                     struct opj_event_mgr *)) opj_j2k_decode_tile;
223 
224 			l_codec->m_codec_data.m_decompression.opj_set_decode_area =
225 					(OPJ_BOOL (*) ( void *,
226                                     opj_image_t*,
227                                     OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32,
228                                     struct opj_event_mgr *)) opj_j2k_set_decode_area;
229 
230 			l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
231                     (OPJ_BOOL (*) ( void *p_codec,
232 								    opj_stream_private_t *p_cio,
233 								    opj_image_t *p_image,
234 								    struct opj_event_mgr * p_manager,
235 								    OPJ_UINT32 tile_index)) opj_j2k_get_tile;
236 
237 			l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
238                     (OPJ_BOOL (*) ( void * p_codec,
239 									OPJ_UINT32 res_factor,
240 									struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_resolution_factor;
241 
242 			l_codec->m_codec = opj_j2k_create_decompress();
243 
244 			if (! l_codec->m_codec) {
245 				opj_free(l_codec);
246 				return NULL;
247 			}
248 
249 			break;
250 
251 		case OPJ_CODEC_JP2:
252 			/* get a JP2 decoder handle */
253 			l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
254 
255 			l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
256 
257 			l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
258 
259 			l_codec->m_codec_data.m_decompression.opj_decode =
260 					(OPJ_BOOL (*) (	void *,
261 									struct opj_stream_private *,
262 									opj_image_t*,
263 									struct opj_event_mgr * )) opj_jp2_decode;
264 
265 			l_codec->m_codec_data.m_decompression.opj_end_decompress =
266                     (OPJ_BOOL (*) ( void *,
267                                     struct opj_stream_private *,
268                                     struct opj_event_mgr *)) opj_jp2_end_decompress;
269 
270 			l_codec->m_codec_data.m_decompression.opj_read_header =
271                     (OPJ_BOOL (*) ( struct opj_stream_private *,
272 					                void *,
273 					                opj_image_t **,
274 					                struct opj_event_mgr * )) opj_jp2_read_header;
275 
276 			l_codec->m_codec_data.m_decompression.opj_read_tile_header =
277                     (OPJ_BOOL (*) ( void *,
278 					                OPJ_UINT32*,
279 					                OPJ_UINT32*,
280 					                OPJ_INT32*,
281 					                OPJ_INT32*,
282 					                OPJ_INT32 * ,
283 					                OPJ_INT32 * ,
284 					                OPJ_UINT32 * ,
285 					                OPJ_BOOL *,
286 					                struct opj_stream_private *,
287 					                struct opj_event_mgr * )) opj_jp2_read_tile_header;
288 
289 			l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
290                     (OPJ_BOOL (*) ( void *,
291                                     OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,
292                                     struct opj_stream_private *,
293                                     struct opj_event_mgr * )) opj_jp2_decode_tile;
294 
295 			l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))opj_jp2_destroy;
296 
297 			l_codec->m_codec_data.m_decompression.opj_setup_decoder =
298                     (void (*) (void * ,opj_dparameters_t * )) opj_jp2_setup_decoder;
299 
300 			l_codec->m_codec_data.m_decompression.opj_set_decode_area =
301                     (OPJ_BOOL (*) ( void *,
302                                     opj_image_t*,
303                                     OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32,
304                                     struct opj_event_mgr * )) opj_jp2_set_decode_area;
305 
306 			l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
307                     (OPJ_BOOL (*) ( void *p_codec,
308 									opj_stream_private_t *p_cio,
309 									opj_image_t *p_image,
310 									struct opj_event_mgr * p_manager,
311 									OPJ_UINT32 tile_index)) opj_jp2_get_tile;
312 
313 			l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
314                     (OPJ_BOOL (*) ( void * p_codec,
315 						    		OPJ_UINT32 res_factor,
316 							    	opj_event_mgr_t * p_manager)) opj_jp2_set_decoded_resolution_factor;
317 
318 			l_codec->m_codec = opj_jp2_create(OPJ_TRUE);
319 
320 			if (! l_codec->m_codec) {
321 				opj_free(l_codec);
322 				return 00;
323 			}
324 
325 			break;
326 		case OPJ_CODEC_UNKNOWN:
327 		case OPJ_CODEC_JPT:
328 		default:
329 			opj_free(l_codec);
330 			return 00;
331 	}
332 
333 	opj_set_default_event_handler(&(l_codec->m_event_mgr));
334 	return (opj_codec_t*) l_codec;
335 }
336 
opj_set_default_decoder_parameters(opj_dparameters_t * parameters)337 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
338 	if(parameters) {
339 		memset(parameters, 0, sizeof(opj_dparameters_t));
340 		/* default decoding parameters */
341 		parameters->cp_layer = 0;
342 		parameters->cp_reduce = 0;
343 
344 		parameters->decod_format = -1;
345 		parameters->cod_format = -1;
346 		parameters->flags = 0;
347 /* UniPG>> */
348 #ifdef USE_JPWL
349 		parameters->jpwl_correct = OPJ_FALSE;
350 		parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
351 		parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
352 #endif /* USE_JPWL */
353 /* <<UniPG */
354 	}
355 }
356 
opj_setup_decoder(opj_codec_t * p_codec,opj_dparameters_t * parameters)357 OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
358                                         opj_dparameters_t *parameters
359 										)
360 {
361 	if (p_codec && parameters) {
362 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
363 
364 		if (! l_codec->is_decompressor) {
365 			opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
366                 "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
367 			return OPJ_FALSE;
368 		}
369 
370 		l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
371 																parameters);
372 		return OPJ_TRUE;
373 	}
374 	return OPJ_FALSE;
375 }
376 
opj_read_header(opj_stream_t * p_stream,opj_codec_t * p_codec,opj_image_t ** p_image)377 OPJ_BOOL OPJ_CALLCONV opj_read_header (	opj_stream_t *p_stream,
378 										opj_codec_t *p_codec,
379 										opj_image_t **p_image )
380 {
381 	if (p_codec && p_stream) {
382 		opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
383 		opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
384 
385 		if(! l_codec->is_decompressor) {
386 			opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
387                 "Codec provided to the opj_read_header function is not a decompressor handler.\n");
388 			return OPJ_FALSE;
389 		}
390 
391 		return l_codec->m_codec_data.m_decompression.opj_read_header(	l_stream,
392 																		l_codec->m_codec,
393 																		p_image,
394 																		&(l_codec->m_event_mgr) );
395 	}
396 
397 	return OPJ_FALSE;
398 }
399 
opj_decode(opj_codec_t * p_codec,opj_stream_t * p_stream,opj_image_t * p_image)400 OPJ_BOOL OPJ_CALLCONV opj_decode(   opj_codec_t *p_codec,
401                                     opj_stream_t *p_stream,
402                                     opj_image_t* p_image)
403 {
404 	if (p_codec && p_stream) {
405 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
406 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
407 
408 		if (! l_codec->is_decompressor) {
409 			return OPJ_FALSE;
410 		}
411 
412 		return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
413 																l_stream,
414 																p_image,
415 																&(l_codec->m_event_mgr) );
416 	}
417 
418 	return OPJ_FALSE;
419 }
420 
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)421 OPJ_BOOL OPJ_CALLCONV opj_set_decode_area(	opj_codec_t *p_codec,
422 											opj_image_t* p_image,
423 											OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
424 											OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
425 											)
426 {
427 	if (p_codec) {
428 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
429 
430 		if (! l_codec->is_decompressor) {
431 			return OPJ_FALSE;
432 		}
433 
434 		return  l_codec->m_codec_data.m_decompression.opj_set_decode_area(	l_codec->m_codec,
435 																			p_image,
436 																			p_start_x, p_start_y,
437 																			p_end_x, p_end_y,
438 																			&(l_codec->m_event_mgr) );
439 	}
440 	return OPJ_FALSE;
441 }
442 
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)443 OPJ_BOOL OPJ_CALLCONV opj_read_tile_header(	opj_codec_t *p_codec,
444 											opj_stream_t * p_stream,
445 											OPJ_UINT32 * p_tile_index,
446 											OPJ_UINT32 * p_data_size,
447 											OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
448 											OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
449 											OPJ_UINT32 * p_nb_comps,
450 											OPJ_BOOL * p_should_go_on)
451 {
452 	if (p_codec && p_stream && p_data_size && p_tile_index) {
453 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
454 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
455 
456 		if (! l_codec->is_decompressor) {
457 			return OPJ_FALSE;
458 		}
459 
460 		return l_codec->m_codec_data.m_decompression.opj_read_tile_header(	l_codec->m_codec,
461 																			p_tile_index,
462 																			p_data_size,
463 																			p_tile_x0, p_tile_y0,
464 																			p_tile_x1, p_tile_y1,
465 																			p_nb_comps,
466 																			p_should_go_on,
467 																			l_stream,
468 																			&(l_codec->m_event_mgr));
469 	}
470 	return OPJ_FALSE;
471 }
472 
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)473 OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data(	opj_codec_t *p_codec,
474 											OPJ_UINT32 p_tile_index,
475 											OPJ_BYTE * p_data,
476 											OPJ_UINT32 p_data_size,
477 											opj_stream_t *p_stream
478 											)
479 {
480 	if (p_codec && p_data && p_stream) {
481 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
482 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
483 
484 		if (! l_codec->is_decompressor) {
485 			return OPJ_FALSE;
486 		}
487 
488 		return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(	l_codec->m_codec,
489 																			p_tile_index,
490 																			p_data,
491 																			p_data_size,
492 																			l_stream,
493 																			&(l_codec->m_event_mgr) );
494 	}
495 	return OPJ_FALSE;
496 }
497 
opj_get_decoded_tile(opj_codec_t * p_codec,opj_stream_t * p_stream,opj_image_t * p_image,OPJ_UINT32 tile_index)498 OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile(	opj_codec_t *p_codec,
499 											opj_stream_t *p_stream,
500 											opj_image_t *p_image,
501 											OPJ_UINT32 tile_index)
502 {
503 	if (p_codec && p_stream) {
504 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
505 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
506 
507 		if (! l_codec->is_decompressor) {
508 			return OPJ_FALSE;
509 		}
510 
511 		return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(	l_codec->m_codec,
512 																			l_stream,
513 																			p_image,
514 																			&(l_codec->m_event_mgr),
515 																			tile_index);
516 	}
517 
518 	return OPJ_FALSE;
519 }
520 
opj_set_decoded_resolution_factor(opj_codec_t * p_codec,OPJ_UINT32 res_factor)521 OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec,
522 														OPJ_UINT32 res_factor )
523 {
524 	opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
525 
526 	if ( !l_codec ){
527 		return OPJ_FALSE;
528 	}
529 
530 	return l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec,
531 																			res_factor,
532 																			&(l_codec->m_event_mgr) );
533 }
534 
535 /* ---------------------------------------------------------------------- */
536 /* COMPRESSION FUNCTIONS*/
537 
opj_create_compress(OPJ_CODEC_FORMAT p_format)538 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
539 {
540 	opj_codec_private_t *l_codec = 00;
541 
542 	l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
543 	if (!l_codec) {
544 		return 00;
545 	}
546 
547 	l_codec->is_decompressor = 0;
548 
549 	switch(p_format) {
550 		case OPJ_CODEC_J2K:
551 			l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *,
552 																			struct opj_stream_private *,
553 																			struct opj_event_mgr * )) opj_j2k_encode;
554 
555 			l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) (	void *,
556 																					struct opj_stream_private *,
557 																					struct opj_event_mgr *)) opj_j2k_end_compress;
558 
559 			l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *,
560 																					struct opj_stream_private *,
561 																					struct opj_image * ,
562 																					struct opj_event_mgr *)) opj_j2k_start_compress;
563 
564 			l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *,
565 																				OPJ_UINT32,
566 																				OPJ_BYTE*,
567 																				OPJ_UINT32,
568 																				struct opj_stream_private *,
569 																				struct opj_event_mgr *) ) opj_j2k_write_tile;
570 
571 			l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_j2k_destroy;
572 
573 			l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL (*) (	void *,
574 																				opj_cparameters_t *,
575 																				struct opj_image *,
576 																				struct opj_event_mgr * )) opj_j2k_setup_encoder;
577 
578 			l_codec->m_codec = opj_j2k_create_compress();
579 			if (! l_codec->m_codec) {
580 				opj_free(l_codec);
581 				return 00;
582 			}
583 
584 			break;
585 
586 		case OPJ_CODEC_JP2:
587 			/* get a JP2 decoder handle */
588 			l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *,
589 																			struct opj_stream_private *,
590 																			struct opj_event_mgr * )) opj_jp2_encode;
591 
592 			l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) (	void *,
593 																					struct opj_stream_private *,
594 																					struct opj_event_mgr *)) opj_jp2_end_compress;
595 
596 			l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *,
597 																					struct opj_stream_private *,
598 																					struct opj_image * ,
599 																					struct opj_event_mgr *))  opj_jp2_start_compress;
600 
601 			l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *,
602 																				OPJ_UINT32,
603 																				OPJ_BYTE*,
604 																				OPJ_UINT32,
605 																				struct opj_stream_private *,
606 																				struct opj_event_mgr *)) opj_jp2_write_tile;
607 
608 			l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_jp2_destroy;
609 
610 			l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL (*) (	void *,
611 																				opj_cparameters_t *,
612 																				struct opj_image *,
613 																				struct opj_event_mgr * )) opj_jp2_setup_encoder;
614 
615 			l_codec->m_codec = opj_jp2_create(OPJ_FALSE);
616 			if (! l_codec->m_codec) {
617 				opj_free(l_codec);
618 				return 00;
619 			}
620 
621 			break;
622 
623 		case OPJ_CODEC_UNKNOWN:
624 		case OPJ_CODEC_JPT:
625 		default:
626 			opj_free(l_codec);
627 			return 00;
628 	}
629 
630 	opj_set_default_event_handler(&(l_codec->m_event_mgr));
631 	return (opj_codec_t*) l_codec;
632 }
633 
opj_set_default_encoder_parameters(opj_cparameters_t * parameters)634 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
635 	if(parameters) {
636 		memset(parameters, 0, sizeof(opj_cparameters_t));
637 		/* default coding parameters */
638         parameters->cp_cinema = OPJ_OFF; /* DEPRECATED */
639         parameters->rsiz = OPJ_PROFILE_NONE;
640 		parameters->max_comp_size = 0;
641 		parameters->numresolution = 6;
642         parameters->cp_rsiz = OPJ_STD_RSIZ; /* DEPRECATED */
643 		parameters->cblockw_init = 64;
644 		parameters->cblockh_init = 64;
645 		parameters->prog_order = OPJ_LRCP;
646 		parameters->roi_compno = -1;		/* no ROI */
647 		parameters->subsampling_dx = 1;
648 		parameters->subsampling_dy = 1;
649 		parameters->tp_on = 0;
650 		parameters->decod_format = -1;
651 		parameters->cod_format = -1;
652 		parameters->tcp_rates[0] = 0;
653 		parameters->tcp_numlayers = 0;
654 		parameters->cp_disto_alloc = 0;
655 		parameters->cp_fixed_alloc = 0;
656 		parameters->cp_fixed_quality = 0;
657 		parameters->jpip_on = OPJ_FALSE;
658 /* UniPG>> */
659 #ifdef USE_JPWL
660 		parameters->jpwl_epc_on = OPJ_FALSE;
661 		parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
662 		{
663 			int i;
664 			for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
665 				parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
666 				parameters->jpwl_hprot_TPH[i] = 0; /* absent */
667 			}
668 		};
669 		{
670 			int i;
671 			for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
672 				parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
673 				parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
674 				parameters->jpwl_pprot[i] = 0; /* absent */
675 			}
676 		};
677 		parameters->jpwl_sens_size = 0; /* 0 means no ESD */
678 		parameters->jpwl_sens_addr = 0; /* 0 means auto */
679 		parameters->jpwl_sens_range = 0; /* 0 means packet */
680 		parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
681 		{
682 			int i;
683 			for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
684 				parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
685 				parameters->jpwl_sens_TPH[i] = -1; /* absent */
686 			}
687 		};
688 #endif /* USE_JPWL */
689 /* <<UniPG */
690 	}
691 }
692 
opj_setup_encoder(opj_codec_t * p_codec,opj_cparameters_t * parameters,opj_image_t * p_image)693 OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec,
694 										opj_cparameters_t *parameters,
695 										opj_image_t *p_image)
696 {
697 	if (p_codec && parameters && p_image) {
698 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
699 
700 		if (! l_codec->is_decompressor) {
701 			return l_codec->m_codec_data.m_compression.opj_setup_encoder(	l_codec->m_codec,
702 																	parameters,
703 																	p_image,
704 																	&(l_codec->m_event_mgr) );
705 		}
706 	}
707 
708 	return OPJ_FALSE;
709 }
710 
opj_start_compress(opj_codec_t * p_codec,opj_image_t * p_image,opj_stream_t * p_stream)711 OPJ_BOOL OPJ_CALLCONV opj_start_compress (	opj_codec_t *p_codec,
712 											opj_image_t * p_image,
713 											opj_stream_t *p_stream)
714 {
715 	if (p_codec && p_stream) {
716 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
717 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
718 
719 		if (! l_codec->is_decompressor) {
720 			return l_codec->m_codec_data.m_compression.opj_start_compress(	l_codec->m_codec,
721 																			l_stream,
722 																			p_image,
723 																			&(l_codec->m_event_mgr));
724 		}
725 	}
726 
727 	return OPJ_FALSE;
728 }
729 
opj_encode(opj_codec_t * p_info,opj_stream_t * p_stream)730 OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
731 {
732 	if (p_info && p_stream) {
733 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
734 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
735 
736 		if (! l_codec->is_decompressor) {
737 			return l_codec->m_codec_data.m_compression.opj_encode(	l_codec->m_codec,
738 															l_stream,
739 															&(l_codec->m_event_mgr));
740 		}
741 	}
742 
743 	return OPJ_FALSE;
744 
745 }
746 
opj_end_compress(opj_codec_t * p_codec,opj_stream_t * p_stream)747 OPJ_BOOL OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
748 										opj_stream_t *p_stream)
749 {
750 	if (p_codec && p_stream) {
751 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
752 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
753 
754 		if (! l_codec->is_decompressor) {
755 			return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
756 																		l_stream,
757 																		&(l_codec->m_event_mgr));
758 		}
759 	}
760 	return OPJ_FALSE;
761 
762 }
763 
opj_end_decompress(opj_codec_t * p_codec,opj_stream_t * p_stream)764 OPJ_BOOL OPJ_CALLCONV opj_end_decompress (	opj_codec_t *p_codec,
765 											opj_stream_t *p_stream)
766 {
767 	if (p_codec && p_stream) {
768 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
769 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
770 
771 		if (! l_codec->is_decompressor) {
772 			return OPJ_FALSE;
773 		}
774 
775 		return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
776 																		l_stream,
777 																		&(l_codec->m_event_mgr) );
778 	}
779 
780 	return OPJ_FALSE;
781 }
782 
opj_set_MCT(opj_cparameters_t * parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)783 OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,
784                                   OPJ_FLOAT32 * pEncodingMatrix,
785                                   OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
786 {
787 	OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
788 	OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32);
789 	OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
790 
791 	/* add MCT capability */
792     if (OPJ_IS_PART2(parameters->rsiz)) {
793         parameters->rsiz |= OPJ_EXTENSION_MCT;
794     } else {
795         parameters->rsiz = ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT));
796     }
797 	parameters->irreversible = 1;
798 
799 	/* use array based MCT */
800 	parameters->tcp_mct = 2;
801 	parameters->mct_data = opj_malloc(l_mct_total_size);
802 	if (! parameters->mct_data) {
803 		return OPJ_FALSE;
804 	}
805 
806 	memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
807 	memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size,p_dc_shift,l_dc_shift_size);
808 
809 	return OPJ_TRUE;
810 }
811 
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)812 OPJ_BOOL OPJ_CALLCONV opj_write_tile (	opj_codec_t *p_codec,
813 										OPJ_UINT32 p_tile_index,
814 										OPJ_BYTE * p_data,
815 										OPJ_UINT32 p_data_size,
816 										opj_stream_t *p_stream )
817 {
818 	if (p_codec && p_stream && p_data) {
819 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
820 		opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
821 
822 		if (l_codec->is_decompressor) {
823 			return OPJ_FALSE;
824 		}
825 
826 		return l_codec->m_codec_data.m_compression.opj_write_tile(	l_codec->m_codec,
827 																	p_tile_index,
828 																	p_data,
829 																	p_data_size,
830 																	l_stream,
831 																	&(l_codec->m_event_mgr) );
832 	}
833 
834 	return OPJ_FALSE;
835 }
836 
837 /* ---------------------------------------------------------------------- */
838 
opj_destroy_codec(opj_codec_t * p_codec)839 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
840 {
841 	if (p_codec) {
842 		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
843 
844 		if (l_codec->is_decompressor) {
845 			l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
846 		}
847 		else {
848 			l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
849 		}
850 
851 		l_codec->m_codec = 00;
852 		opj_free(l_codec);
853 	}
854 }
855 
856 /* ---------------------------------------------------------------------- */
857 
opj_dump_codec(opj_codec_t * p_codec,OPJ_INT32 info_flag,FILE * output_stream)858 void OPJ_CALLCONV opj_dump_codec(	opj_codec_t *p_codec,
859 									OPJ_INT32 info_flag,
860 									FILE* output_stream)
861 {
862 	if (p_codec) {
863 		opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
864 
865 		l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
866 		return;
867 	}
868 
869 	/* TODO return error */
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(const char * fname,OPJ_BOOL p_is_read_stream)920 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (const char *fname, OPJ_BOOL p_is_read_stream)
921 {
922     return opj_stream_create_file_stream(fname, OPJ_J2K_STREAM_CHUNK_SIZE, p_is_read_stream);
923 }
924 
opj_stream_create_file_stream(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 (
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(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