- opj_dparameters_t l_param;
- opj_codec_t * l_codec;
- opj_image_t * l_image;
- FILE * l_file;
- opj_stream_t * l_stream;
- OPJ_UINT32 l_data_size;
- OPJ_UINT32 l_max_data_size = 1000;
- OPJ_UINT32 l_tile_index;
- OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
- opj_bool l_go_on = OPJ_TRUE;
- OPJ_INT32 l_tile_x0,l_tile_y0;
- OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y,l_nb_comps;
- OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
-
- PROFINIT();
-
-
- if
- (! l_data)
- {
- return 1;
- }
- opj_set_default_decoder_parameters(&l_param);
-
- /** you may here add custom decoding parameters */
- /* do not use layer decoding limitations */
- l_param.cp_layer = 0;
-
- /* do not use resolutions reductions */
- l_param.cp_reduce = 0;
-
- /* to decode only a part of the image data */
- //opj_restrict_decoding(&l_param,0,0,1000,1000);
-
- l_codec = opj_create_decompress_v2(CODEC_J2K);
- if
- (! l_codec)
- {
- free(l_data);
- return 1;
- }
-
- /* catch events using our callbacks and give a local context */
- opj_set_info_handler(l_codec, info_callback,00);
- opj_set_warning_handler(l_codec, warning_callback,00);
- opj_set_error_handler(l_codec, error_callback,00);
- opj_event_mgr_t l_event_mgr;
-
- if
- (! opj_setup_decoder_v2(l_codec,&l_param))
- {
- free(l_data);
- opj_destroy_codec(l_codec);
- return 1;
- }
-
- l_file = fopen(INPUT_FILE,"rb");
- if
- (! l_file)
- {
- fprintf(stdout, "Error opening input file\n");
- free(l_data);
- opj_destroy_codec(l_codec);
- return 1;
- }
-
- l_stream = opj_stream_create_default_file_stream(l_file,OPJ_TRUE);
-
- if
- (! opj_read_header(l_codec,
- &l_image,
- &l_tile_x0,
- &l_tile_y0,
- &l_tile_width,
- &l_tile_height,
- &l_nb_tiles_x,
- &l_nb_tiles_y,
- l_stream))
- {
- free(l_data);
- opj_stream_destroy(l_stream);
- fclose(l_file);
- opj_destroy_codec(l_codec);
- return 1;
- }
- printf("Setting decoding area to %d,%d,%d,%d\n", DA_X0, DA_Y0, DA_X1, DA_Y1);
- opj_set_decode_area(l_codec, DA_X0, DA_Y0, DA_X1, DA_Y1);
- while
- (l_go_on)
- {
- if
- (! opj_read_tile_header(
- l_codec,
- &l_tile_index,
- &l_data_size,
- &l_current_tile_x0,
- &l_current_tile_y0,
- &l_current_tile_x1,
- &l_current_tile_y1,
- &l_nb_comps,
- &l_go_on,
- l_stream))
- {
- free(l_data);
- opj_stream_destroy(l_stream);
- fclose(l_file);
- opj_destroy_codec(l_codec);
- opj_image_destroy(l_image);
- return 1;
- }
- if
- (l_go_on)
- {
- if
- (l_data_size > l_max_data_size)
- {
- l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
- if
- (! l_data)
- {
- opj_stream_destroy(l_stream);
- fclose(l_file);
- opj_destroy_codec(l_codec);
- opj_image_destroy(l_image);
- return 1;
- }
- l_max_data_size = l_data_size;
- }
-
- if
- (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream))
- {
- free(l_data);
- opj_stream_destroy(l_stream);
- fclose(l_file);
- opj_destroy_codec(l_codec);
- opj_image_destroy(l_image);
- return 1;
- }
- /** now should inspect image to know the reduction factor and then how to behave with data */
- }
- }
- if
- (! opj_end_decompress(l_codec,l_stream))
- {
- free(l_data);
- opj_stream_destroy(l_stream);
- fclose(l_file);
- opj_destroy_codec(l_codec);
- opj_image_destroy(l_image);
- return 1;
- }
- free(l_data);
- opj_stream_destroy(l_stream);
- fclose(l_file);
- opj_destroy_codec(l_codec);
- opj_image_destroy(l_image);
-
- // Print profiling
- PROFPRINT();
-
- return 0;
+ opj_dparameters_t l_param;
+ opj_codec_t * l_codec;
+ opj_image_t * l_image;
+ opj_stream_t * l_stream;
+ OPJ_UINT32 l_data_size;
+ OPJ_UINT32 l_max_data_size = 1000;
+ OPJ_UINT32 l_tile_index;
+ OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
+ OPJ_BOOL l_go_on = OPJ_TRUE;
+ OPJ_UINT32 l_nb_comps=0 ;
+ OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
+
+ int da_x0=0;
+ int da_y0=0;
+ int da_x1=1000;
+ int da_y1=1000;
+ const char *input_file;
+
+ /* should be test_tile_decoder 0 0 1000 1000 tte1.j2k */
+ if( argc == 6 )
+ {
+ da_x0=atoi(argv[1]);
+ da_y0=atoi(argv[2]);
+ da_x1=atoi(argv[3]);
+ da_y1=atoi(argv[4]);
+ input_file = argv[5];
+
+ }
+ else
+ {
+ da_x0=0;
+ da_y0=0;
+ da_x1=1000;
+ da_y1=1000;
+ input_file = "test.j2k";
+ }
+
+ if (! l_data) {
+ return EXIT_FAILURE;
+ }
+
+ l_stream = opj_stream_create_default_file_stream(input_file,OPJ_TRUE);
+ if (!l_stream){
+ free(l_data);
+ fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
+ return EXIT_FAILURE;
+ }
+
+ /* Set the default decoding parameters */
+ opj_set_default_decoder_parameters(&l_param);
+
+ /* */
+ l_param.decod_format = infile_format(input_file);
+
+ /** you may here add custom decoding parameters */
+ /* do not use layer decoding limitations */
+ l_param.cp_layer = 0;
+
+ /* do not use resolutions reductions */
+ l_param.cp_reduce = 0;
+
+ /* to decode only a part of the image data */
+ /*opj_restrict_decoding(&l_param,0,0,1000,1000);*/
+
+
+ switch(l_param.decod_format) {
+ case J2K_CFMT: /* JPEG-2000 codestream */
+ {
+ /* Get a decoder handle */
+ l_codec = opj_create_decompress(OPJ_CODEC_J2K);
+ break;
+ }
+ case JP2_CFMT: /* JPEG 2000 compressed image data */
+ {
+ /* Get a decoder handle */
+ l_codec = opj_create_decompress(OPJ_CODEC_JP2);
+ break;
+ }
+ default:
+ {
+ fprintf(stderr, "ERROR -> Not a valid JPEG2000 file!\n");
+ free(l_data);
+ opj_stream_destroy(l_stream);
+ return EXIT_FAILURE;
+ }
+ }
+
+ /* catch events using our callbacks and give a local context */
+ opj_set_info_handler(l_codec, info_callback,00);
+ opj_set_warning_handler(l_codec, warning_callback,00);
+ opj_set_error_handler(l_codec, error_callback,00);
+
+ /* Setup the decoder decoding parameters using user parameters */
+ if (! opj_setup_decoder(l_codec, &l_param))
+ {
+ fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
+ free(l_data);
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ return EXIT_FAILURE;
+ }
+
+ /* Read the main header of the codestream and if necessary the JP2 boxes*/
+ if (! opj_read_header(l_stream, l_codec, &l_image))
+ {
+ fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n");
+ free(l_data);
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ return EXIT_FAILURE;
+ }
+
+ if (!opj_set_decode_area(l_codec, l_image, da_x0, da_y0,da_x1, da_y1)){
+ fprintf(stderr, "ERROR -> j2k_to_image: failed to set the decoded area\n");
+ free(l_data);
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ opj_image_destroy(l_image);
+ return EXIT_FAILURE;
+ }
+
+
+ while (l_go_on)
+ {
+ if (! opj_read_tile_header( l_codec,
+ l_stream,
+ &l_tile_index,
+ &l_data_size,
+ &l_current_tile_x0,
+ &l_current_tile_y0,
+ &l_current_tile_x1,
+ &l_current_tile_y1,
+ &l_nb_comps,
+ &l_go_on))
+ {
+ free(l_data);
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ opj_image_destroy(l_image);
+ return EXIT_FAILURE;
+ }
+
+ if (l_go_on)
+ {
+ if (l_data_size > l_max_data_size)
+ {
+ OPJ_BYTE *l_new_data = (OPJ_BYTE *) realloc(l_data, l_data_size);
+ if (! l_new_data)
+ {
+ free(l_new_data);
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ opj_image_destroy(l_image);
+ return EXIT_FAILURE;
+ }
+ l_data = l_new_data;
+ l_max_data_size = l_data_size;
+ }
+
+ if (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream))
+ {
+ free(l_data);
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ opj_image_destroy(l_image);
+ return EXIT_FAILURE;
+ }
+ /** now should inspect image to know the reduction factor and then how to behave with data */
+ }
+ }
+
+ if (! opj_end_decompress(l_codec,l_stream))
+ {
+ free(l_data);
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ opj_image_destroy(l_image);
+ return EXIT_FAILURE;
+ }
+
+ /* Free memory */
+ free(l_data);
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ opj_image_destroy(l_image);
+
+ /* Print profiling*/
+ /*PROFPRINT();*/
+
+ return EXIT_SUCCESS;