2 * Copyright (c) 2005, Herve Drolon, FreeImage Team
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
31 #include "opj_config.h"
32 #include "opj_includes.h"
34 typedef struct opj_decompression
36 opj_bool (* opj_read_header) (
39 OPJ_INT32 * p_tile_x0,
40 OPJ_INT32 * p_tile_y0,
41 OPJ_UINT32 * p_tile_width,
42 OPJ_UINT32 * p_tile_height,
43 OPJ_UINT32 * p_nb_tiles_x,
44 OPJ_UINT32 * p_nb_tiles_y,
45 struct opj_stream_private *cio,
46 struct opj_event_mgr * p_manager);
47 opj_image_t* (* opj_decode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
48 opj_bool (*opj_read_tile_header)(
50 OPJ_UINT32 * p_tile_index,
51 OPJ_UINT32* p_data_size,
52 OPJ_INT32 * p_tile_x0,
53 OPJ_INT32 * p_tile_y0,
54 OPJ_INT32 * p_tile_x1,
55 OPJ_INT32 * p_tile_y1,
56 OPJ_UINT32 * p_nb_comps,
57 opj_bool * p_should_go_on,
58 struct opj_stream_private *p_cio,
59 struct opj_event_mgr * p_manager);
60 opj_bool (*opj_decode_tile_data)(void * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,struct opj_stream_private *p_cio,struct opj_event_mgr * p_manager);
61 opj_bool (* opj_end_decompress) (void *p_codec,struct opj_stream_private *cio,struct opj_event_mgr * p_manager);
62 void (* opj_destroy) (void * p_codec);
63 void (*opj_setup_decoder) (void * p_codec,opj_dparameters_t * p_param);
64 opj_bool (*opj_set_decode_area) (void * p_codec,OPJ_INT32 p_start_x,OPJ_INT32 p_end_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_y,struct opj_event_mgr * p_manager);
69 typedef struct opj_compression
71 opj_bool (* opj_start_compress) (void *p_codec,struct opj_stream_private *cio,struct opj_image * p_image, struct opj_event_mgr * p_manager);
72 opj_bool (* opj_encode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
73 opj_bool (* opj_write_tile) (void * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,struct opj_stream_private * p_cio,struct opj_event_mgr * p_manager);
74 opj_bool (* opj_end_compress) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
75 void (* opj_destroy) (void * p_codec);
76 void (*opj_setup_encoder) (void * p_codec,opj_cparameters_t * p_param,struct opj_image * p_image, struct opj_event_mgr * p_manager);
80 typedef struct opj_codec_private
83 { /* code-blocks informations */
84 opj_decompression_t m_decompression;
85 opj_compression_t m_compression;
88 opj_event_mgr_t* m_event_mgr;
89 unsigned is_decompressor : 1;
94 * Default callback function.
97 void opj_default_callback (const char *msg, void *client_data)
99 //FIXME V2 -> V1 cf below
102 void set_default_event_handler(opj_event_mgr_t * p_manager)
105 //p_manager->m_error_data = 00;
106 //p_manager->m_warning_data = 00;
107 //p_manager->m_info_data = 00;
108 p_manager->error_handler = opj_default_callback;
109 p_manager->info_handler = opj_default_callback;
110 p_manager->warning_handler = opj_default_callback;
113 OPJ_UINT32 opj_read_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
115 OPJ_UINT32 l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
116 return l_nb_read ? l_nb_read : -1;
119 OPJ_UINT32 opj_write_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
121 return fwrite(p_buffer,1,p_nb_bytes,p_file);
124 OPJ_SIZE_T opj_skip_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
127 (fseek(p_user_data,p_nb_bytes,SEEK_CUR))
134 opj_bool opj_seek_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
137 (fseek(p_user_data,p_nb_bytes,SEEK_SET))
144 /* ---------------------------------------------------------------------- */
148 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
150 OPJ_ARG_NOT_USED(lpReserved);
151 OPJ_ARG_NOT_USED(hModule);
153 switch (ul_reason_for_call) {
154 case DLL_PROCESS_ATTACH :
156 case DLL_PROCESS_DETACH :
158 case DLL_THREAD_ATTACH :
159 case DLL_THREAD_DETACH :
165 #endif /* OPJ_STATIC */
168 /* ---------------------------------------------------------------------- */
171 const char* OPJ_CALLCONV opj_version(void) {
172 return PACKAGE_VERSION;
175 opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
176 opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
177 if(!dinfo) return NULL;
178 dinfo->is_decompressor = OPJ_TRUE;
182 /* get a J2K decoder handle */
183 dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
184 if(!dinfo->j2k_handle) {
190 /* get a JP2 decoder handle */
191 dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
192 if(!dinfo->jp2_handle) {
203 dinfo->codec_format = format;
208 opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT p_format)
210 opj_codec_private_t *l_info = 00;
212 l_info = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
216 memset(l_info, 0, sizeof(opj_codec_private_t));
218 l_info->is_decompressor = 1;
224 l_info->m_codec_data.m_decompression.opj_decode = (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))j2k_decode;
225 l_info->m_codec_data.m_decompression.opj_end_decompress = (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *))j2k_end_decompress;
226 l_info->m_codec_data.m_decompression.opj_read_header = (opj_bool (*) (
235 struct opj_stream_private *,
236 struct opj_event_mgr * )) j2k_read_header;
237 l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))j2k_destroy;
238 l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) j2k_setup_decoder;
239 l_info->m_codec_data.m_decompression.opj_read_tile_header = (opj_bool (*) (
249 struct opj_stream_private *,
250 struct opj_event_mgr * )) j2k_read_tile_header;
251 l_info->m_codec_data.m_decompression.opj_decode_tile_data = (opj_bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *, struct opj_event_mgr * )) j2k_decode_tile;
252 l_info->m_codec_data.m_decompression.opj_set_decode_area = (opj_bool (*) (void *,OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) j2k_set_decode_area;
253 l_info->m_codec = j2k_create_decompress_v2();
263 /* get a JP2 decoder handle */
265 l_info->m_codec_data.m_decompression.opj_decode = (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))opj_jp2_decode;
266 l_info->m_codec_data.m_decompression.opj_end_decompress = (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
267 l_info->m_codec_data.m_decompression.opj_read_header = (opj_bool (*) (
277 struct opj_stream_private *,
278 struct opj_event_mgr * )) jp2_read_header;
280 l_info->m_codec_data.m_decompression.opj_read_tile_header = (
291 struct opj_stream_private *,
292 struct opj_event_mgr * )) jp2_read_tile_header;
294 l_info->m_codec_data.m_decompression.opj_decode_tile_data = (opj_bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *, struct opj_event_mgr * )) opj_jp2_decode_tile;
296 l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
297 l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder;
298 l_info->m_codec_data.m_decompression.opj_set_decode_area = (opj_bool (*) (void *,OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) jp2_set_decode_area;
301 l_info->m_codec = jp2_create(OPJ_TRUE);
317 // FIXME set_default_event_handler(&(l_info->m_event_mgr));
318 return (opj_codec_t*) l_info;
322 void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
324 /* destroy the codec */
325 switch(dinfo->codec_format) {
328 j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
331 jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
337 /* destroy the decompressor */
342 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
344 memset(parameters, 0, sizeof(opj_dparameters_t));
345 /* default decoding parameters */
346 parameters->cp_layer = 0;
347 parameters->cp_reduce = 0;
348 parameters->cp_limit_decoding = NO_LIMITATION;
350 parameters->decod_format = -1;
351 parameters->cod_format = -1;
354 parameters->jpwl_correct = OPJ_FALSE;
355 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
356 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
357 #endif /* USE_JPWL */
362 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
363 if(dinfo && parameters) {
364 switch(dinfo->codec_format) {
367 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
370 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
379 opj_bool OPJ_CALLCONV opj_setup_decoder_v2(opj_codec_t *p_info, opj_dparameters_t *parameters, opj_event_mgr_t* event_mgr)
381 if (p_info && parameters) {
382 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
384 if (! l_info->is_decompressor) {
388 l_info->m_codec_data.m_decompression.opj_setup_decoder(l_info->m_codec,parameters);
390 if (event_mgr == NULL)
392 l_info->m_event_mgr->error_handler = opj_default_callback ;
393 l_info->m_event_mgr->warning_handler = opj_default_callback ;
394 l_info->m_event_mgr->info_handler = opj_default_callback ;
395 l_info->m_event_mgr->client_data = stderr;
398 l_info->m_event_mgr = event_mgr;
404 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
405 return opj_decode_with_info(dinfo, cio, NULL);
408 opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
410 switch(dinfo->codec_format) {
412 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
414 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
416 return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
425 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
426 opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
427 if(!cinfo) return NULL;
428 cinfo->is_decompressor = OPJ_FALSE;
431 /* get a J2K coder handle */
432 cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
433 if(!cinfo->j2k_handle) {
439 /* get a JP2 coder handle */
440 cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
441 if(!cinfo->jp2_handle) {
453 cinfo->codec_format = format;
458 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
460 /* destroy the codec */
461 switch(cinfo->codec_format) {
463 j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
466 jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
473 /* destroy the decompressor */
478 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
480 memset(parameters, 0, sizeof(opj_cparameters_t));
481 /* default coding parameters */
482 parameters->cp_cinema = OFF;
483 parameters->max_comp_size = 0;
484 parameters->numresolution = 6;
485 parameters->cp_rsiz = STD_RSIZ;
486 parameters->cblockw_init = 64;
487 parameters->cblockh_init = 64;
488 parameters->prog_order = LRCP;
489 parameters->roi_compno = -1; /* no ROI */
490 parameters->subsampling_dx = 1;
491 parameters->subsampling_dy = 1;
492 parameters->tp_on = 0;
493 parameters->decod_format = -1;
494 parameters->cod_format = -1;
495 parameters->tcp_rates[0] = 0;
496 parameters->tcp_numlayers = 0;
497 parameters->cp_disto_alloc = 0;
498 parameters->cp_fixed_alloc = 0;
499 parameters->cp_fixed_quality = 0;
500 parameters->jpip_on = OPJ_FALSE;
503 parameters->jpwl_epc_on = OPJ_FALSE;
504 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
507 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
508 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
509 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
514 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
515 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
516 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
517 parameters->jpwl_pprot[i] = 0; /* absent */
520 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
521 parameters->jpwl_sens_addr = 0; /* 0 means auto */
522 parameters->jpwl_sens_range = 0; /* 0 means packet */
523 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
526 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
527 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
528 parameters->jpwl_sens_TPH[i] = -1; /* absent */
531 #endif /* USE_JPWL */
538 * Sets the stream to be a file stream. The FILE must have been open previously.
539 * @param p_stream the stream to modify
540 * @param p_file handler to an already open file.
542 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
544 return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
547 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file, OPJ_UINT32 p_size, opj_bool p_is_read_stream)
549 opj_stream_t* l_stream = 00;
555 l_stream = opj_stream_create(p_size,p_is_read_stream);
561 opj_stream_set_user_data(l_stream,p_file);
562 opj_stream_set_read_function(l_stream,(opj_stream_read_fn) opj_read_from_file);
563 opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
564 opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
565 opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
569 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
570 if(cinfo && parameters && image) {
571 switch(cinfo->codec_format) {
573 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
576 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
586 opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
588 opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
589 "To extract the index, use the opj_encode_with_info() function.\n"
590 "No index will be generated during this encoding\n");
591 return opj_encode_with_info(cinfo, cio, image, NULL);
594 opj_bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
595 if(cinfo && cio && image) {
596 switch(cinfo->codec_format) {
598 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
600 return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);
610 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
613 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
614 opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
615 opj_free(tile_info->thresh);
616 opj_free(tile_info->packet);
617 opj_free(tile_info->tp);
618 opj_free(tile_info->marker);
620 opj_free(cstr_info->tile);
621 opj_free(cstr_info->marker);
622 opj_free(cstr_info->numdecompos);
627 opj_bool OPJ_CALLCONV opj_read_header (
628 opj_codec_t *p_codec,
629 opj_image_t ** p_image,
630 OPJ_INT32 * p_tile_x0,
631 OPJ_INT32 * p_tile_y0,
632 OPJ_UINT32 * p_tile_width,
633 OPJ_UINT32 * p_tile_height,
634 OPJ_UINT32 * p_nb_tiles_x,
635 OPJ_UINT32 * p_nb_tiles_y,
641 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
642 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
644 (! l_info->is_decompressor)
648 return l_info->m_codec_data.m_decompression.opj_read_header(
658 //&(l_info->m_event_mgr));
659 l_info->m_event_mgr);
664 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info)
669 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
671 (l_info->is_decompressor)
673 l_info->m_codec_data.m_decompression.opj_destroy(l_info->m_codec);
677 l_info->m_codec_data.m_compression.opj_destroy(l_info->m_codec);
679 l_info->m_codec = 00;