static Image *ReadJP2Image(const ImageInfo *image_info,ExceptionInfo *exception) { const char *option; Image *image; int jp2_status; MagickBooleanType status; opj_codec_t *jp2_codec; opj_codestream_index_t *codestream_index = (opj_codestream_index_t *) NULL; opj_dparameters_t parameters; opj_image_t *jp2_image; opj_stream_t *jp2_stream; register ssize_t i; ssize_t y; unsigned char sans[4]; /* Open image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickCoreSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickCoreSignature); image=AcquireImage(image_info,exception); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } /* Initialize JP2 codec. */ if (ReadBlob(image,4,sans) != 4) { image=DestroyImageList(image); return((Image *) NULL); } (void) SeekBlob(image,SEEK_SET,0); if (LocaleCompare(image_info->magick,"JPT") == 0) jp2_codec=opj_create_decompress(OPJ_CODEC_JPT); else if (IsJ2K(sans,4) != MagickFalse) jp2_codec=opj_create_decompress(OPJ_CODEC_J2K); else jp2_codec=opj_create_decompress(OPJ_CODEC_JP2); opj_set_warning_handler(jp2_codec,JP2WarningHandler,exception); opj_set_error_handler(jp2_codec,JP2ErrorHandler,exception); opj_set_default_decoder_parameters(¶meters); option=GetImageOption(image_info,"jp2:reduce-factor"); if (option != (const char *) NULL) parameters.cp_reduce=StringToInteger(option); option=GetImageOption(image_info,"jp2:quality-layers"); if (option != (const char *) NULL) parameters.cp_layer=StringToInteger(option); if (opj_setup_decoder(jp2_codec,¶meters) == 0) { opj_destroy_codec(jp2_codec); ThrowReaderException(DelegateError,"UnableToManageJP2Stream"); } jp2_stream=opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE,1); opj_stream_set_read_function(jp2_stream,JP2ReadHandler); opj_stream_set_write_function(jp2_stream,JP2WriteHandler); opj_stream_set_seek_function(jp2_stream,JP2SeekHandler); opj_stream_set_skip_function(jp2_stream,JP2SkipHandler); opj_stream_set_user_data(jp2_stream,image,NULL); opj_stream_set_user_data_length(jp2_stream,GetBlobSize(image)); if (opj_read_header(jp2_stream,jp2_codec,&jp2_image) == 0) { opj_stream_destroy(jp2_stream); opj_destroy_codec(jp2_codec); ThrowReaderException(DelegateError,"UnableToDecodeImageFile"); } jp2_status=1; if ((image->columns != 0) && (image->rows != 0)) { /* Extract an area from the image. */ jp2_status=opj_set_decode_area(jp2_codec,jp2_image, (OPJ_INT32) image->extract_info.x,(OPJ_INT32) image->extract_info.y, (OPJ_INT32) (image->extract_info.x+(ssize_t) image->columns), (OPJ_INT32) (image->extract_info.y+(ssize_t) image->rows)); if (jp2_status == 0) { opj_stream_destroy(jp2_stream); opj_destroy_codec(jp2_codec); opj_image_destroy(jp2_image); ThrowReaderException(DelegateError,"UnableToDecodeImageFile"); } } if ((image_info->number_scenes != 0) && (image_info->scene != 0)) jp2_status=opj_get_decoded_tile(jp2_codec,jp2_stream,jp2_image, (unsigned int) image_info->scene-1); else if (image->ping == MagickFalse) { jp2_status=opj_decode(jp2_codec,jp2_stream,jp2_image); if (jp2_status != 0) jp2_status=opj_end_decompress(jp2_codec,jp2_stream); } if (jp2_status == 0) { opj_stream_destroy(jp2_stream); opj_destroy_codec(jp2_codec); opj_image_destroy(jp2_image); ThrowReaderException(DelegateError,"UnableToDecodeImageFile"); } opj_stream_destroy(jp2_stream); for (i=0; i < (ssize_t) jp2_image->numcomps; i++) { if ((jp2_image->comps[i].dx == 0) || (jp2_image->comps[i].dy == 0)) { opj_destroy_codec(jp2_codec); opj_image_destroy(jp2_image); ThrowReaderException(CoderError,"IrregularChannelGeometryNotSupported") } }
/* -------------------------------------------------------------------------- */ int main(int argc, char **argv) { FILE *fsrc = NULL; opj_dparameters_t parameters; /* decompression parameters */ opj_image_t* image = NULL; opj_stream_t *l_stream = NULL; /* Stream */ opj_codec_t* l_codec = NULL; /* Handle to a decompressor */ opj_codestream_index_t* cstr_index = NULL; char indexfilename[OPJ_PATH_LEN]; /* index file name */ OPJ_INT32 num_images, imageno; img_fol_t img_fol; dircnt_t *dirptr = NULL; int failed = 0; /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); /* FIXME Initialize indexfilename and img_fol */ *indexfilename = 0; /* Initialize img_fol */ memset(&img_fol,0,sizeof(img_fol_t)); /* parse input and get user encoding parameters */ if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol, indexfilename) == 1) { return EXIT_FAILURE; } /* Initialize reading of directory */ if(img_fol.set_imgdir==1){ int it_image; num_images=get_num_images(img_fol.imgdirpath); dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); if(dirptr){ dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*)); if(!dirptr->filename_buf){ return EXIT_FAILURE; } for(it_image=0;it_image<num_images;it_image++){ dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN; } } if(load_images(dirptr,img_fol.imgdirpath)==1){ return EXIT_FAILURE; } if (num_images==0){ fprintf(stdout,"Folder is empty\n"); return EXIT_FAILURE; } }else{ num_images=1; } /*Decoding image one by one*/ for(imageno = 0; imageno < num_images ; imageno++) { fprintf(stderr,"\n"); if(img_fol.set_imgdir==1){ if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) { fprintf(stderr,"skipping file...\n"); continue; } } /* read the input file and put it in memory */ /* ---------------------------------------- */ fsrc = fopen(parameters.infile, "rb"); if (!fsrc) { fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile); return EXIT_FAILURE; } l_stream = opj_stream_create_default_file_stream(fsrc,1); if (!l_stream){ fclose(fsrc); fprintf(stderr, "ERROR -> failed to create the stream from the file\n"); return EXIT_FAILURE; } /* decode the JPEG2000 stream */ /* ---------------------- */ switch(parameters.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; } case JPT_CFMT: /* JPEG 2000, JPIP */ { /* Get a decoder handle */ l_codec = opj_create_decompress(OPJ_CODEC_JPT); break; } default: fprintf(stderr, "skipping file..\n"); opj_stream_destroy(l_stream); continue; } /* 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, ¶meters) ){ fprintf(stderr, "ERROR -> opj_compress: failed to setup the decoder\n"); opj_stream_destroy(l_stream); fclose(fsrc); 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, &image)){ fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n"); opj_stream_destroy(l_stream); fclose(fsrc); opj_destroy_codec(l_codec); opj_image_destroy(image); return EXIT_FAILURE; } if (!parameters.nb_tile_to_decode) { /* Optional if you want decode the entire image */ if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0, (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)){ fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n"); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); fclose(fsrc); return EXIT_FAILURE; } /* Get the decoded image */ if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) { fprintf(stderr,"ERROR -> opj_decompress: failed to decode image!\n"); opj_destroy_codec(l_codec); opj_stream_destroy(l_stream); opj_image_destroy(image); fclose(fsrc); return EXIT_FAILURE; } } else { /* It is just here to illustrate how to use the resolution after set parameters */ /*if (!opj_set_decoded_resolution_factor(l_codec, 5)) { fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n"); opj_destroy_codec(l_codec); opj_stream_destroy(l_stream); opj_image_destroy(image); fclose(fsrc); return EXIT_FAILURE; }*/ if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) { fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n"); opj_destroy_codec(l_codec); opj_stream_destroy(l_stream); opj_image_destroy(image); fclose(fsrc); return EXIT_FAILURE; } fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index); } /* Close the byte stream */ opj_stream_destroy(l_stream); fclose(fsrc); if(image->color_space == OPJ_CLRSPC_SYCC){ color_sycc_to_rgb(image); /* FIXME */ } if( image->color_space != OPJ_CLRSPC_SYCC && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy && image->comps[1].dx != 1 ) image->color_space = OPJ_CLRSPC_SYCC; else if (image->numcomps <= 2) image->color_space = OPJ_CLRSPC_GRAY; if(image->icc_profile_buf) { #if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2) color_apply_icc_profile(image); /* FIXME */ #endif free(image->icc_profile_buf); image->icc_profile_buf = NULL; image->icc_profile_len = 0; } /* create output image */ /* ------------------- */ switch (parameters.cod_format) { case PXM_DFMT: /* PNM PGM PPM */ if (imagetopnm(image, parameters.outfile)) { fprintf(stderr,"Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; case PGX_DFMT: /* PGX */ if(imagetopgx(image, parameters.outfile)){ fprintf(stderr,"Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; case BMP_DFMT: /* BMP */ if(imagetobmp(image, parameters.outfile)){ fprintf(stderr,"Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; #ifdef OPJ_HAVE_LIBTIFF case TIF_DFMT: /* TIFF */ if(imagetotif(image, parameters.outfile)){ fprintf(stderr,"Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; #endif /* OPJ_HAVE_LIBTIFF */ case RAW_DFMT: /* RAW */ if(imagetoraw(image, parameters.outfile)){ fprintf(stderr,"Error generating raw file. Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); } break; case RAWL_DFMT: /* RAWL */ if(imagetorawl(image, parameters.outfile)){ fprintf(stderr,"Error generating rawl file. Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); } break; case TGA_DFMT: /* TGA */ if(imagetotga(image, parameters.outfile)){ fprintf(stderr,"Error generating tga file. Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); } break; #ifdef OPJ_HAVE_LIBPNG case PNG_DFMT: /* PNG */ if(imagetopng(image, parameters.outfile)){ fprintf(stderr,"Error generating png file. Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); } break; #endif /* OPJ_HAVE_LIBPNG */ /* Can happen if output file is TIFF or PNG * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined */ default: fprintf(stderr,"Outfile %s not generated\n",parameters.outfile); failed = 1; } /* free remaining structures */ if (l_codec) { opj_destroy_codec(l_codec); } /* free image data structure */ opj_image_destroy(image); /* destroy the codestream index */ opj_destroy_cstr_index(&cstr_index); } return failed ? EXIT_FAILURE : EXIT_SUCCESS; }
int main (int argc, char *argv[]) { 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; int da_x0=0; int da_y0=0; int da_x1=1000; int da_y1=1000; char input_file[64]; /* 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]); strcpy(input_file,argv[5]); } else { da_x0=0; da_y0=0; da_x1=1000; da_y1=1000; strcpy(input_file,"test.j2k"); } 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); 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_stream, l_codec, &l_image)) { 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, l_image, da_x0, da_y0, da_x1, da_y1); 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); 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; }
JP2KImage read_jp2k_data(const char* fname, int format) #endif { JP2KImage jp; init_jp2k(&jp); opj_stream_t* stream = NULL; opj_codec_t* codec = NULL; opj_dparameters_t parameters; opj_set_default_decoder_parameters(¶meters); #if OPENJPEG_VER < JP2K_VER_21 stream = opj_stream_create_default_file_stream(fp, 1); // 2.0.0 #else stream = opj_stream_create_default_file_stream(fname, 1); // 2.1.0 #endif if (stream==NULL){ jp.state = ERROR_GRAPH_RDFILE; return jp; } if (format==JP2K_FMT_J2K) { // JPEG 2000 codestream codec = opj_create_decompress(OPJ_CODEC_J2K); } else if (format==JP2K_FMT_JP2) { // JPEG 2000 compressed image data codec = opj_create_decompress(OPJ_CODEC_JP2); } else if (format==JP2K_FMT_JPT) { // JPEG 2000 JPIP codec = opj_create_decompress(OPJ_CODEC_JPT); } else { print_message("JBXL::readJPEG2KData: ERROR: unknown file format!\n"); opj_stream_destroy(stream); return jp; } if (!opj_setup_decoder(codec, ¶meters) ){ opj_stream_destroy(stream); opj_destroy_codec(codec); jp.state = ERROR_GRAPH; return jp; } if (!opj_read_header(stream, codec, &jp.image)){ opj_stream_destroy(stream); opj_destroy_codec(codec); jp.state = ERROR_GRAPH; return jp; } if (!opj_set_decode_area(codec, jp.image, 0, 0, 0, 0)){ opj_stream_destroy(stream); opj_destroy_codec(codec); free_jp2k(&jp); jp.state = ERROR_GRAPH; return jp; } if (!(opj_decode(codec, stream, jp.image) && opj_end_decompress(codec, stream))) { opj_destroy_codec(codec); opj_stream_destroy(stream); free_jp2k(&jp); jp.state = ERROR_GRAPH; return jp; } setup_jp2k(&jp); opj_stream_destroy(stream); opj_destroy_codec(codec); return jp; }
/* -------------------------------------------------------------------------- */ int main(int argc, char **argv) { opj_decompress_parameters parameters; /* decompression parameters */ opj_image_t* image = NULL; opj_stream_t *l_stream = NULL; /* Stream */ opj_codec_t* l_codec = NULL; /* Handle to a decompressor */ opj_codestream_index_t* cstr_index = NULL; char indexfilename[OPJ_PATH_LEN]; /* index file name */ OPJ_INT32 num_images, imageno; img_fol_t img_fol; dircnt_t *dirptr = NULL; int failed = 0; OPJ_FLOAT64 t, tCumulative = 0; OPJ_UINT32 numDecompressedImages = 0; /* set decoding parameters to default values */ set_default_parameters(¶meters); /* FIXME Initialize indexfilename and img_fol */ *indexfilename = 0; /* Initialize img_fol */ memset(&img_fol,0,sizeof(img_fol_t)); /* parse input and get user encoding parameters */ if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol, indexfilename) == 1) { destroy_parameters(¶meters); return EXIT_FAILURE; } /* Initialize reading of directory */ if(img_fol.set_imgdir==1){ int it_image; num_images=get_num_images(img_fol.imgdirpath); dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); if(dirptr){ dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*)); if(!dirptr->filename_buf){ destroy_parameters(¶meters); return EXIT_FAILURE; } for(it_image=0;it_image<num_images;it_image++){ dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN; } } if(load_images(dirptr,img_fol.imgdirpath)==1){ destroy_parameters(¶meters); return EXIT_FAILURE; } if (num_images==0){ fprintf(stdout,"Folder is empty\n"); destroy_parameters(¶meters); return EXIT_FAILURE; } }else{ num_images=1; } /*Decoding image one by one*/ for(imageno = 0; imageno < num_images ; imageno++) { fprintf(stderr,"\n"); if(img_fol.set_imgdir==1){ if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) { fprintf(stderr,"skipping file...\n"); destroy_parameters(¶meters); continue; } } /* read the input file and put it in memory */ /* ---------------------------------------- */ l_stream = opj_stream_create_default_file_stream(parameters.infile,1); if (!l_stream){ fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile); destroy_parameters(¶meters); return EXIT_FAILURE; } /* decode the JPEG2000 stream */ /* ---------------------- */ switch(parameters.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; } case JPT_CFMT: /* JPEG 2000, JPIP */ { /* Get a decoder handle */ l_codec = opj_create_decompress(OPJ_CODEC_JPT); break; } default: fprintf(stderr, "skipping file..\n"); destroy_parameters(¶meters); opj_stream_destroy(l_stream); continue; } /* 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); t = opj_clock(); /* Setup the decoder decoding parameters using user parameters */ if ( !opj_setup_decoder(l_codec, &(parameters.core)) ){ fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n"); destroy_parameters(¶meters); 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, &image)){ fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n"); destroy_parameters(¶meters); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); return EXIT_FAILURE; } if (!parameters.nb_tile_to_decode) { /* Optional if you want decode the entire image */ if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0, (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)){ fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n"); destroy_parameters(¶meters); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); return EXIT_FAILURE; } /* Get the decoded image */ if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) { fprintf(stderr,"ERROR -> opj_decompress: failed to decode image!\n"); destroy_parameters(¶meters); opj_destroy_codec(l_codec); opj_stream_destroy(l_stream); opj_image_destroy(image); return EXIT_FAILURE; } } else { /* It is just here to illustrate how to use the resolution after set parameters */ /*if (!opj_set_decoded_resolution_factor(l_codec, 5)) { fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n"); opj_destroy_codec(l_codec); opj_stream_destroy(l_stream); opj_image_destroy(image); return EXIT_FAILURE; }*/ if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) { fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n"); destroy_parameters(¶meters); opj_destroy_codec(l_codec); opj_stream_destroy(l_stream); opj_image_destroy(image); return EXIT_FAILURE; } fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index); } tCumulative += opj_clock() - t; numDecompressedImages++; /* Close the byte stream */ opj_stream_destroy(l_stream); if( image->color_space != OPJ_CLRSPC_SYCC && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy && image->comps[1].dx != 1 ) image->color_space = OPJ_CLRSPC_SYCC; else if (image->numcomps <= 2) image->color_space = OPJ_CLRSPC_GRAY; if(image->color_space == OPJ_CLRSPC_SYCC){ color_sycc_to_rgb(image); } else if((image->color_space == OPJ_CLRSPC_CMYK) && (parameters.cod_format != TIF_DFMT)){ color_cmyk_to_rgb(image); } else if(image->color_space == OPJ_CLRSPC_EYCC){ color_esycc_to_rgb(image); } if(image->icc_profile_buf) { #if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2) if(image->icc_profile_len) color_apply_icc_profile(image); else color_cielab_to_rgb(image); #endif free(image->icc_profile_buf); image->icc_profile_buf = NULL; image->icc_profile_len = 0; } /* Force output precision */ /* ---------------------- */ if (parameters.precision != NULL) { OPJ_UINT32 compno; for (compno = 0; compno < image->numcomps; ++compno) { OPJ_UINT32 precno = compno; OPJ_UINT32 prec; if (precno >= parameters.nb_precision) { precno = parameters.nb_precision - 1U; } prec = parameters.precision[precno].prec; if (prec == 0) { prec = image->comps[compno].prec; } switch (parameters.precision[precno].mode) { case OPJ_PREC_MODE_CLIP: clip_component(&(image->comps[compno]), prec); break; case OPJ_PREC_MODE_SCALE: scale_component(&(image->comps[compno]), prec); break; default: break; } } } /* Upsample components */ /* ------------------- */ if (parameters.upsample) { image = upsample_image_components(image); if (image == NULL) { fprintf(stderr, "ERROR -> opj_decompress: failed to upsample image components!\n"); destroy_parameters(¶meters); opj_destroy_codec(l_codec); return EXIT_FAILURE; } } /* Force RGB output */ /* ---------------- */ if (parameters.force_rgb) { switch (image->color_space) { case OPJ_CLRSPC_SRGB: break; case OPJ_CLRSPC_GRAY: image = convert_gray_to_rgb(image); break; default: fprintf(stderr, "ERROR -> opj_decompress: don't know how to convert image to RGB colorspace!\n"); opj_image_destroy(image); image = NULL; break; } if (image == NULL) { fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n"); destroy_parameters(¶meters); opj_destroy_codec(l_codec); return EXIT_FAILURE; } } /* create output image */ /* ------------------- */ switch (parameters.cod_format) { case PXM_DFMT: /* PNM PGM PPM */ if (imagetopnm(image, parameters.outfile, parameters.split_pnm)) { fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); } break; case PGX_DFMT: /* PGX */ if(imagetopgx(image, parameters.outfile)){ fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); } break; case BMP_DFMT: /* BMP */ if(imagetobmp(image, parameters.outfile)){ fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); } break; #ifdef OPJ_HAVE_LIBTIFF case TIF_DFMT: /* TIFF */ if(imagetotif(image, parameters.outfile)){ fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); } break; #endif /* OPJ_HAVE_LIBTIFF */ case RAW_DFMT: /* RAW */ if(imagetoraw(image, parameters.outfile)){ fprintf(stderr,"[ERROR] Error generating raw file. Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); } break; case RAWL_DFMT: /* RAWL */ if(imagetorawl(image, parameters.outfile)){ fprintf(stderr,"[ERROR] Error generating rawl file. Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); } break; case TGA_DFMT: /* TGA */ if(imagetotga(image, parameters.outfile)){ fprintf(stderr,"[ERROR] Error generating tga file. Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); } break; #ifdef OPJ_HAVE_LIBPNG case PNG_DFMT: /* PNG */ if(imagetopng(image, parameters.outfile)){ fprintf(stderr,"[ERROR] Error generating png file. Outfile %s not generated\n",parameters.outfile); failed = 1; } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); } break; #endif /* OPJ_HAVE_LIBPNG */ /* Can happen if output file is TIFF or PNG * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined */ default: fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile); failed = 1; } /* free remaining structures */ if (l_codec) { opj_destroy_codec(l_codec); } /* free image data structure */ opj_image_destroy(image); /* destroy the codestream index */ opj_destroy_cstr_index(&cstr_index); if(failed) remove(parameters.outfile); } destroy_parameters(¶meters); if (numDecompressedImages) { fprintf(stdout, "decode time: %d ms\n", (int)( (tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages)); } return failed ? EXIT_FAILURE : EXIT_SUCCESS; }
bool ossim::opj_decode( std::ifstream* in, const ossimIrect& rect, ossim_uint32 resLevel, ossim_int32 format, // OPJ_CODEC_FORMAT std::streamoff fileOffset, ossimImageData* tile) { static const char MODULE[] = "ossimOpjDecoder::decode"; bool status = false; if ( traceDebug() ) { ossimNotify(ossimNotifyLevel_DEBUG) << MODULE << "entered...\nrect: " << rect << "\nresLevel: " << resLevel << std::endl; } // Need to check for NAN in rect if ( in && tile && !rect.hasNans()) { in->seekg( fileOffset, std::ios_base::beg ); opj_dparameters_t param; opj_codec_t* codec = 0; opj_image_t* image = 0;; opj_stream_t* stream = 0; opj_user_istream* userStream = new opj_user_istream(); userStream->m_str = in; userStream->m_offset = fileOffset; /* Set the length to avoid an assert */ in->seekg(0, std::ios_base::end); // Fix: length must be passed in for nift blocks. userStream->m_length = in->tellg(); // Set back to front: in->clear(); in->seekg(fileOffset, std::ios_base::beg); stream = opj_stream_default_create(OPJ_TRUE); if (!stream) { opj_stream_destroy(stream); std::string errMsg = MODULE; errMsg += " ERROR: opj_setup_decoder failed!"; throw ossimException(errMsg); } opj_stream_set_read_function(stream, ossim_opj_istream_read); opj_stream_set_skip_function(stream, ossim_opj_istream_skip); opj_stream_set_seek_function(stream, ossim_opj_istream_seek); // Fix: length must be passed in for nift blocks. opj_stream_set_user_data_length(stream, userStream->m_length); opj_stream_set_user_data(stream, userStream, ossim_opj_free_user_istream_data); opj_stream_set_user_data_length(stream, userStream->m_length); /* Set the default decoding parameters */ opj_set_default_decoder_parameters(¶m); param.decod_format = format; /** you may here add custom decoding parameters */ /* do not use layer decoding limitations */ param.cp_layer = 0; /* do not use resolutions reductions */ param.cp_reduce = resLevel; codec = opj_create_decompress( (CODEC_FORMAT)format ); // catch events using our callbacks and give a local context //opj_set_info_handler (codec, ossim::opj_info_callback, 00); opj_set_info_handler (codec, NULL, 00); opj_set_warning_handler(codec, ossim::opj_warning_callback,00); opj_set_error_handler (codec, ossim::opj_error_callback, 00); // Setup the decoder decoding parameters using user parameters if ( opj_setup_decoder(codec, ¶m) == false ) { opj_stream_destroy(stream); opj_destroy_codec(codec); std::string errMsg = MODULE; errMsg += " ERROR: opj_setup_decoder failed!"; throw ossimException(errMsg); } // Read the main header of the codestream and if necessary the JP2 boxes. if ( opj_read_header(stream, codec, &image) == false ) { opj_stream_destroy(stream); opj_destroy_codec(codec); opj_image_destroy(image); std::string errMsg = MODULE; errMsg += " ERROR: opj_read_header failed!"; throw ossimException(errMsg); } // tmp drb: // opj_stream_destroy(stream); // return; if ( opj_set_decoded_resolution_factor(codec, resLevel) == false) { opj_stream_destroy(stream); opj_destroy_codec(codec); opj_image_destroy(image); std::string errMsg = MODULE; errMsg += " ERROR: opj_set_decoded_resolution_factor failed!"; throw ossimException(errMsg); } //ossim_float32 res = resLevel; ossimIrect resRect = rect * (1 << resLevel); //std::cout << "resRect.ul(): " << resRect.ul() // << "\nresRect.lr(): " << resRect.lr() // << std::endl; // if ( opj_set_decode_area(codec, image, rect.ul().x, rect.ul().y, // rect.lr().x+1, rect.lr().y+1) == false ) if ( opj_set_decode_area(codec, image, resRect.ul().x, resRect.ul().y, resRect.lr().x+1, resRect.lr().y+1) == false ) { opj_stream_destroy(stream); opj_destroy_codec(codec); opj_image_destroy(image); std::string errMsg = MODULE; errMsg += " ERROR: opj_set_decode_area failed!"; throw ossimException(errMsg); } // Get the decoded image: if ( opj_decode(codec, stream, image) == false ) { opj_stream_destroy(stream); opj_destroy_codec(codec); opj_image_destroy(image); std::string errMsg = MODULE; errMsg += " ERROR: opj_decode failed!"; throw ossimException(errMsg); } // ossim::print(std::cout, *image); if(image->icc_profile_buf) { #if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2) color_apply_icc_profile(image); /* FIXME */ #endif free(image->icc_profile_buf); image->icc_profile_buf = NULL; image->icc_profile_len = 0; } status = ossim::copyOpjImage(image, tile); #if 0 ossim_uint32 tile_index = 0; ossim_uint32 data_size = 0; ossim_int32 current_tile_x0 = 0; ossim_int32 current_tile_y0 = 0; ossim_int32 current_tile_x1 = 0; ossim_int32 current_tile_y1 = 0; ossim_uint32 nb_comps = 0; OPJ_BOOL go_on = 1; if ( opj_read_tile_header( codec, stream, &tile_index, &data_size, ¤t_tile_x0, ¤t_tile_y0, ¤t_tile_x1, ¤t_tile_y1, &nb_comps, &go_on) == false ) { opj_stream_destroy(stream); opj_destroy_codec(codec); opj_image_destroy(image); std::string errMsg = MODULE; errMsg += " ERROR: opj_read_tile_header failed!"; throw ossimException(errMsg); } #endif #if 0 std::cout << "tile_index: " << tile_index << "\ndata_size: " << data_size << "\ncurrent_tile_x0: " << current_tile_x0 << "\ncurrent_tile_y0: " << current_tile_y0 << "\ncurrent_tile_x1: " << current_tile_x1 << "\ncurrent_tile_y1: " << current_tile_y1 << "\nnb_comps: " << nb_comps << std::endl; #endif #if 0 if ( opj_decode_tile_data(codec, tile_index,l_data,l_data_size,l_stream) == false) { opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(l_image); std::string errMsg = MODULE; errMsg += " ERROR: opj_read_tile_header failed!"; throw ossimException(errMsg); } #endif #if 0 if (opj_end_decompress(codec,stream) == false ) { opj_stream_destroy(stream); opj_destroy_codec(codec); opj_image_destroy(image); std::string errMsg = MODULE; errMsg += " ERROR: opj_end_decompress failed!"; throw ossimException(errMsg); } #endif /* Free memory */ opj_stream_destroy(stream); opj_destroy_codec(codec); opj_image_destroy(image); // Tmp drb: if ( in->eof() ) { in->clear(); } in->seekg(fileOffset, std::ios_base::beg ); } // Matches: if ( in && tile ) return status; } // End: ossim::opj_decode( ... )
void* OPJSupport::decompressJPEG2KWithBuffer(void* inputBuffer, void* jp2Data, long jp2DataSize, long *decompressedBufferSize, int *colorModel) { //decoderMutex.lock(); opj_dparameters_t parameters; int i; int width, height; OPJ_BOOL hasAlpha, fails = OPJ_FALSE; OPJ_CODEC_FORMAT codec_format; unsigned char rc, gc, bc, ac; if (jp2DataSize<12) { //decoderMutex.unlock(); return 0; } /*-----------------------------------------------*/ decode_info_t decodeInfo; memset(&decodeInfo, 0, sizeof(decode_info_t)); opj_set_default_decoder_parameters(¶meters); parameters.decod_format = buffer_format(jp2Data); // Create the stream opj_buffer_info_t bufferInfo; bufferInfo.cur = bufferInfo.buf = (OPJ_BYTE *)jp2Data; bufferInfo.len = (OPJ_SIZE_T) jp2DataSize; // Create the stream decodeInfo.stream = opj_stream_create_buffer_stream(&bufferInfo , OPJ_STREAM_READ); //GROK //decodeInfo.stream = opj_stream_create_buffer_stream((OPJ_BYTE *)jp2Data, (OPJ_SIZE_T) jp2DataSize , OPJ_STREAM_READ); if (!decodeInfo.stream) { fprintf(stderr,"%s:%d:\n\tNO decodeInfo.stream\n",__FILE__,__LINE__); //decoderMutex.unlock(); return NULL; } /*-----------------------------------------------*/ switch (parameters.decod_format) { case J2K_CFMT: /* JPEG-2000 codestream */ codec_format = OPJ_CODEC_J2K; break; case JP2_CFMT: /* JPEG 2000 compressed image data */ codec_format = OPJ_CODEC_JP2; break; case JPT_CFMT: /* JPEG 2000, JPIP */ codec_format = OPJ_CODEC_JPT; break; case -1: default: release(&decodeInfo); fprintf(stderr,"%s:%d: decode format missing\n",__FILE__,__LINE__); //decoderMutex.unlock(); return NULL; } /*-----------------------------------------------*/ while(1) { int user_changed_tile=0, user_changed_reduction=0; int max_tiles=0, max_reduction=0; fails = OPJ_TRUE; decodeInfo.codec = opj_create_decompress(codec_format); if (decodeInfo.codec == NULL) { fprintf(stderr,"%s:%d:\n\tNO codec\n",__FILE__,__LINE__); break; } #ifdef OPJ_VERBOSE opj_set_info_handler(decodeInfo.codec, info_callback, this); opj_set_warning_handler(decodeInfo.codec, warning_callback, this); #endif opj_set_error_handler(decodeInfo.codec, error_callback, this); // Setup the decoder decoding parameters if ( !opj_setup_decoder(decodeInfo.codec, ¶meters)) { fprintf(stderr,"%s:%d:\n\topj_setup_decoder failed\n",__FILE__,__LINE__); break; } if (user_changed_tile && user_changed_reduction) { int reduction=0; opj_set_decoded_resolution_factor(decodeInfo.codec, reduction); } /* Read the main header of the codestream and if necessary the JP2 boxes * see openjpeg.c * For OPJ_CODEC_JP2 it will call 'opj_jp2_read_header()' in jp2.c:2276 * then call 'opj_j2k_read_header()' */ if( !opj_read_header(decodeInfo.stream, decodeInfo.codec, &(decodeInfo.image))) { fprintf(stderr,"%s:%d:\n\topj_read_header failed\n",__FILE__,__LINE__); break; } if ( !(user_changed_tile && user_changed_reduction) || (max_tiles <= 0) || (max_reduction <= 0) ) { decodeInfo.cstr_info = opj_get_cstr_info(decodeInfo.codec); max_reduction = decodeInfo.cstr_info->m_default_tile_info.tccp_info->numresolutions; max_tiles = decodeInfo.cstr_info->tw * decodeInfo.cstr_info->th; decodeInfo.cstr_index = opj_get_cstr_index(decodeInfo.codec); } if (!parameters.nb_tile_to_decode) { int user_changed_area=0; if(user_changed_area) { } /* Optional if you want decode the entire image */ if (!opj_set_decode_area(decodeInfo.codec, decodeInfo.image, (OPJ_INT32)parameters.DA_x0, (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)) { fprintf(stderr,"%s:%d:\n\topj_set_decode_area failed\n",__FILE__,__LINE__); break; } /* Get the decoded image */ if (!opj_decode(decodeInfo.codec, decodeInfo.stream, decodeInfo.image)) { fprintf(stderr,"%s:%d:\n\topj_decode failed\n",__FILE__,__LINE__); //decoderMutex.unlock(); return NULL; } if (!opj_end_decompress(decodeInfo.codec, decodeInfo.stream)) { fprintf(stderr,"%s:%d:\n\topj_end_decompress failed\n",__FILE__,__LINE__); break; } } else { if (!opj_get_decoded_tile(decodeInfo.codec, decodeInfo.stream, decodeInfo.image, parameters.tile_index)) { fprintf(stderr,"%s:%d:\n\topj_get_decoded_tile failed\n",__FILE__,__LINE__); break; } } fails = OPJ_FALSE; break; } // while decodeInfo.deleteImage = fails; if (fails) { //decoderMutex.unlock(); return NULL; } decodeInfo.deleteImage = OPJ_TRUE; if(decodeInfo.image->color_space == OPJ_CLRSPC_SYCC) { //disable for now //color_sycc_to_rgb(decodeInfo.image); } if (decodeInfo.image->color_space != OPJ_CLRSPC_SYCC && decodeInfo.image->numcomps == 3 && decodeInfo.image->comps[0].dx == decodeInfo.image->comps[0].dy && decodeInfo.image->comps[1].dx != 1) { decodeInfo.image->color_space = OPJ_CLRSPC_SYCC; } else if(decodeInfo.image->numcomps <= 2) { decodeInfo.image->color_space = OPJ_CLRSPC_GRAY; } if (decodeInfo.image->icc_profile_buf) { #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) color_apply_icc_profile(decodeInfo.image); #endif free(decodeInfo.image->icc_profile_buf); decodeInfo.image->icc_profile_buf = NULL; decodeInfo.image->icc_profile_len = 0; } //--- width = decodeInfo.image->comps[0].w; height = decodeInfo.image->comps[0].h; long depth = (decodeInfo.image->comps[0].prec + 7)/8; long decompressSize = width * height * decodeInfo.image->numcomps * depth; if (decompressedBufferSize) *decompressedBufferSize = decompressSize;; if (!inputBuffer ) { inputBuffer = malloc(decompressSize); } if (colorModel) *colorModel = 0; if ((decodeInfo.image->numcomps >= 3 && decodeInfo.image->comps[0].dx == decodeInfo.image->comps[1].dx && decodeInfo.image->comps[1].dx == decodeInfo.image->comps[2].dx && decodeInfo.image->comps[0].dy == decodeInfo.image->comps[1].dy && decodeInfo.image->comps[1].dy == decodeInfo.image->comps[2].dy && decodeInfo.image->comps[0].prec == decodeInfo.image->comps[1].prec && decodeInfo.image->comps[1].prec == decodeInfo.image->comps[2].prec )/* RGB[A] */ || (decodeInfo.image->numcomps == 2 && decodeInfo.image->comps[0].dx == decodeInfo.image->comps[1].dx && decodeInfo.image->comps[0].dy == decodeInfo.image->comps[1].dy && decodeInfo.image->comps[0].prec == decodeInfo.image->comps[1].prec ) ) /* GA */ { int has_alpha4, has_alpha2, has_rgb; int *red, *green, *blue, *alpha; if (colorModel) *colorModel = 1; alpha = NULL; has_rgb = (decodeInfo.image->numcomps == 3); has_alpha4 = (decodeInfo.image->numcomps == 4); has_alpha2 = (decodeInfo.image->numcomps == 2); hasAlpha = (has_alpha4 || has_alpha2); if(has_rgb) { red = decodeInfo.image->comps[0].data; green = decodeInfo.image->comps[1].data; blue = decodeInfo.image->comps[2].data; if(has_alpha4) { alpha = decodeInfo.image->comps[3].data; } } /* if(has_rgb) */ else { red = green = blue = decodeInfo.image->comps[0].data; if(has_alpha2) { alpha = decodeInfo.image->comps[1].data; } } /* if(has_rgb) */ ac = 255;/* 255: FULLY_OPAQUE; 0: FULLY_TRANSPARENT */ unsigned char* ptrIBody = (unsigned char*)inputBuffer; for(i = 0; i < width*height; i++) { rc = (unsigned char)*red++; gc = (unsigned char)*green++; bc = (unsigned char)*blue++; if(hasAlpha) { ac = (unsigned char)*alpha++;; } /* A R G B */ //*ptrIBody++ = (int)((ac<<24) | (rc<<16) | (gc<<8) | bc); *ptrIBody = rc; ptrIBody++; *ptrIBody = gc; ptrIBody++; *ptrIBody = bc; ptrIBody++; if (hasAlpha) { *ptrIBody = ac; ptrIBody++; } } /* for(i) */ }/* if (decodeInfo.image->numcomps >= 3 */ else if(decodeInfo.image->numcomps == 1) /* Grey */ { /* 1 component 8 or 16 bpp decodeInfo.image */ int *grey = decodeInfo.image->comps[0].data; if(decodeInfo.image->comps[0].prec <= 8) { char* ptrBBody = (char*)inputBuffer; for(i=0; i<width*height; i++) { *ptrBBody++ = *grey++; } /* Replace image8 buffer: */ } else /* prec[9:16] */ { int *grey = decodeInfo.image->comps[0].data; //int ushift = 0, dshift = 0, force16 = 0; short* ptrSBody = (short*)inputBuffer; for(i=0; i<width*height; i++) { //disable shift up for signed data: don't know why we are doing this *ptrSBody++ = *grey++; } /* Replace image16 buffer: */ } } else { int *grey; fprintf(stderr,"%s:%d:Can show only first component of decodeInfo.image\n" " components(%d) prec(%d) color_space[%d](%s)\n" " RECT(%d,%d,%d,%d)\n",__FILE__,__LINE__,decodeInfo.image->numcomps, decodeInfo.image->comps[0].prec, decodeInfo.image->color_space,clr_space(decodeInfo.image->color_space), decodeInfo.image->x0,decodeInfo.image->y0,decodeInfo.image->x1,decodeInfo.image->y1 ); for(i = 0; i < decodeInfo.image->numcomps; ++i) { fprintf(stderr,"[%d]dx(%d) dy(%d) w(%d) h(%d) signed(%u)\n",i, decodeInfo.image->comps[i].dx ,decodeInfo.image->comps[i].dy, decodeInfo.image->comps[i].w,decodeInfo.image->comps[i].h, decodeInfo.image->comps[i].sgnd); } /* 1 component 8 or 16 bpp decodeInfo.image */ grey = decodeInfo.image->comps[0].data; if(decodeInfo.image->comps[0].prec <= 8) { char* ptrBBody = (char*)inputBuffer; for(i=0; i<width*height; i++) { *ptrBBody++ = *grey++; } /* Replace image8 buffer: */ } else /* prec[9:16] */ { int *grey; //int ushift = 0, dshift = 0, force16 = 0; grey = decodeInfo.image->comps[0].data; short* ptrSBody = (short*)inputBuffer; for(i=0; i<width*height; i++) { *ptrSBody++ = *grey++; } /* Replace image16 buffer: */ } } release(&decodeInfo); opj_destroy_cstr_index(&(decodeInfo.cstr_index)); //decoderMutex.unlock(); return inputBuffer; }
int main (int argc, char *argv[]) { 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; char input_file[64]; /* 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]); strcpy(input_file,argv[5]); } else { da_x0=0; da_y0=0; da_x1=1000; da_y1=1000; strcpy(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; }
OpenJPEGReader_readRegion(J2K_USER_DATA *data, nrt_Uint32 x0, nrt_Uint32 y0, nrt_Uint32 x1, nrt_Uint32 y1, nrt_Uint8 **buf, nrt_Error *error) { OpenJPEGReaderImpl *impl = (OpenJPEGReaderImpl*) data; opj_stream_t *stream = NULL; opj_image_t *image = NULL; opj_codec_t *codec = NULL; nrt_Uint64 bufSize; nrt_Uint64 offset = 0; nrt_Uint32 componentBytes, nComponents; if (!OpenJPEG_setup(impl, &stream, &codec, error)) { goto CATCH_ERROR; } /* unfortunately, we need to read the header every time ... */ if (!opj_read_header(stream, codec, &image)) { /*nrt_Error_init(error, "Error reading header", NRT_CTXT, NRT_ERR_UNK);*/ goto CATCH_ERROR; } if (x1 == 0) x1 = j2k_Container_getWidth(impl->container, error); if (y1 == 0) y1 = j2k_Container_getHeight(impl->container, error); /* only decode what we want */ if (!opj_set_decode_area(codec, image, x0, y0, x1, y1)) { /*nrt_Error_init(error, "Error decoding area", NRT_CTXT, NRT_ERR_UNK);*/ goto CATCH_ERROR; } nComponents = j2k_Container_getNumComponents(impl->container, error); componentBytes = (j2k_Container_getPrecision(impl->container, error) - 1) / 8 + 1; bufSize = (nrt_Uint64)(x1 - x0) * (y1 - y0) * componentBytes * nComponents; if (buf && !*buf) { *buf = (nrt_Uint8*)J2K_MALLOC(bufSize); if (!*buf) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); goto CATCH_ERROR; } } { int keepGoing; OPJ_UINT32 tileIndex, reqSize; OPJ_INT32 tileX0, tileY0, tileX1, tileY1; do { if (!opj_read_tile_header(codec, stream, &tileIndex, &reqSize, &tileX0, &tileY0, &tileX1, &tileY1, &nComponents, &keepGoing)) { /*nrt_Error_init(error, "Error reading tile header", NRT_CTXT, NRT_ERR_UNK);*/ goto CATCH_ERROR; } if (keepGoing) { if (!opj_decode_tile_data(codec, tileIndex, (*buf + offset), reqSize, stream)) { /*nrt_Error_init(error, "Error decoding tile", NRT_CTXT, NRT_ERR_UNK);*/ goto CATCH_ERROR; } offset += reqSize; } } while (keepGoing); } goto CLEANUP; CATCH_ERROR: { bufSize = 0; } CLEANUP: { OpenJPEG_cleanup(&stream, &codec, &image); } return bufSize; }
OpenJPEGReader_readTile(J2K_USER_DATA *data, nrt_Uint32 tileX, nrt_Uint32 tileY, nrt_Uint8 **buf, nrt_Error *error) { OpenJPEGReaderImpl *impl = (OpenJPEGReaderImpl*) data; opj_stream_t *stream = NULL; opj_image_t *image = NULL; opj_codec_t *codec = NULL; nrt_Uint32 bufSize; const OPJ_UINT32 tileWidth = j2k_Container_getTileWidth(impl->container, error); const OPJ_UINT32 tileHeight = j2k_Container_getTileHeight(impl->container, error); size_t numBitsPerPixel = 0; size_t numBytesPerPixel = 0; nrt_Uint64 fullBufSize = 0; if (!OpenJPEG_setup(impl, &stream, &codec, error)) { goto CATCH_ERROR; } /* unfortunately, we need to read the header every time ... */ if (!opj_read_header(stream, codec, &image)) { /*nrt_Error_init(error, "Error reading header", NRT_CTXT, NRT_ERR_UNK);*/ goto CATCH_ERROR; } /* only decode what we want */ if (!opj_set_decode_area(codec, image, tileWidth * tileX, tileHeight * tileY, tileWidth * (tileX + 1), tileHeight * (tileY + 1))) { /*nrt_Error_init(error, "Error decoding area", NRT_CTXT, NRT_ERR_UNK);*/ goto CATCH_ERROR; } { int keepGoing; OPJ_UINT32 tileIndex, nComponents; OPJ_INT32 tileX0, tileY0, tileX1, tileY1; if (!opj_read_tile_header(codec, stream, &tileIndex, &bufSize, &tileX0, &tileY0, &tileX1, &tileY1, &nComponents, &keepGoing)) { /*nrt_Error_init(error, "Error reading tile header", NRT_CTXT, NRT_ERR_UNK);*/ goto CATCH_ERROR; } if (keepGoing) { /* TODO: The way blockIO->cntl->blockOffsetInc is currently * implemented in ImageIO.c corresponds with how a * non-compressed partial block would be laid out in a * NITF - the actual extra columns would have been read. * Not sure how the J2K data is laid out on disk but * OpenJPEG is hiding this from us if the extra columns are * present there. So whenever we get a partial tile that * isn't at the full width, we need to add in these extra * columns of 0's ourselves. Potentially we could update * ImageIO.c to not require this instead. Note that we * don't need to pad out the extra rows for a partial block * that isn't the full height because ImageIO will never try * to memcpy these in - we only need to get the stride to * work out correctly. */ const OPJ_UINT32 thisTileWidth = tileX1 - tileX0; const OPJ_UINT32 thisTileHeight = tileY1 - tileY0; if (thisTileWidth < tileWidth) { /* TODO: The current approach below only works for single band * imagery. For RGB data, I believe it is stored as all * red, then all green, then all blue, so we would need * a temp buffer rather than reusing the current buffer. */ if (nComponents != 1) { nrt_Error_init( error, "Partial tile width not implemented for multi-band", NRT_CTXT, NRT_ERR_UNK); goto CATCH_ERROR; } numBitsPerPixel = j2k_Container_getPrecision(impl->container, error); numBytesPerPixel = (numBitsPerPixel / 8) + (numBitsPerPixel % 8 != 0); fullBufSize = tileWidth * thisTileHeight * numBytesPerPixel; } else { fullBufSize = bufSize; } if (buf && !*buf) { *buf = (nrt_Uint8*)J2K_MALLOC(fullBufSize); if (!*buf) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); goto CATCH_ERROR; } } if (!opj_decode_tile_data(codec, tileIndex, *buf, bufSize, stream)) { /*nrt_Error_init(error, "Error decoding tile", NRT_CTXT, NRT_ERR_UNK);*/ goto CATCH_ERROR; } if (thisTileWidth < tileWidth) { /* We have a tile that isn't as wide as it "should" be * Need to add in the extra columns ourselves. By marching * through the rows backwards, we can do this in place. */ const size_t srcStride = thisTileWidth * numBytesPerPixel; const size_t destStride = tileWidth * numBytesPerPixel; const size_t numLeftoverBytes = destStride - srcStride; OPJ_UINT32 lastRow = thisTileHeight - 1; size_t srcOffset = lastRow * srcStride; size_t destOffset = lastRow * destStride; OPJ_UINT32 ii; nrt_Uint8* bufPtr = *buf; for (ii = 0; ii < thisTileHeight; ++ii, srcOffset -= srcStride, destOffset -= destStride) { nrt_Uint8* const dest = bufPtr + destOffset; memmove(dest, bufPtr + srcOffset, srcStride); memset(dest + srcStride, 0, numLeftoverBytes); } } } } goto CLEANUP; CATCH_ERROR: { fullBufSize = 0; } CLEANUP: { OpenJPEG_cleanup(&stream, &codec, &image); } return fullBufSize; }
void* OPJSupport::decompressJPEG2KWithBuffer ( void* inputBuffer, void* jp2Data, long jp2DataSize, long *decompressedBufferSize, int *colorModel){ opj_dparameters_t parameters; OPJ_BOOL hasFile = OPJ_FALSE; opj_buffer_info_t buf_info; int i, decod_format; int width, height; OPJ_BOOL hasAlpha, fails = OPJ_FALSE; OPJ_CODEC_FORMAT codec_format; unsigned char rc, gc, bc, ac; decode_info_t decodeInfo; memset(&decodeInfo, 0, sizeof(decode_info_t)); memset(&buf_info, 0, sizeof(opj_buffer_info_t)); if (jp2Data != NULL) { buf_info.len = jp2DataSize; buf_info.buf = (OPJ_BYTE*)jp2Data; buf_info.cur = buf_info.buf; } opj_set_default_decoder_parameters(¶meters); decod_format = buffer_format(&buf_info); if(decod_format == -1) { fprintf(stderr,"%s:%d: decode format missing\n",__FILE__,__LINE__); release(&decodeInfo); return 0; } /*-----------------------------------------------*/ if(decod_format == J2K_CFMT) codec_format = OPJ_CODEC_J2K; else if(decod_format == JP2_CFMT) codec_format = OPJ_CODEC_JP2; else if(decod_format == JPT_CFMT) codec_format = OPJ_CODEC_JPT; else { /* clarified in infile_format() : */ release(&decodeInfo); return 0; } parameters.decod_format = decod_format; while(1) { int tile_index=-1, user_changed_tile=0, user_changed_reduction=0; int max_tiles=0, max_reduction=0; fails = OPJ_TRUE; decodeInfo.stream = opj_stream_create_buffer_stream(&buf_info, 1); if(decodeInfo.stream == NULL) { fprintf(stderr,"%s:%d: NO decodeInfo.stream\n",__FILE__,__LINE__); break; } decodeInfo.codec = opj_create_decompress(codec_format); if(decodeInfo.codec == NULL) { fprintf(stderr,"%s:%d: NO coded\n",__FILE__,__LINE__); break; } // opj_set_info_handler(decodeInfo.codec, error_callback, this); // opj_set_info_handler(decodeInfo.codec, warning_callback, this); // opj_set_info_handler(decodeInfo.codec, info_callback, this); if( !opj_setup_decoder(decodeInfo.codec, ¶meters)) { fprintf(stderr,"%s:%d:\n\topj_setup_decoder failed\n",__FILE__,__LINE__); break; } if(user_changed_tile && user_changed_reduction) { int reduction=0; opj_set_decoded_resolution_factor(decodeInfo.codec, reduction); } if( !opj_read_header(decodeInfo.stream, decodeInfo.codec, &decodeInfo.image)) { fprintf(stderr,"%s:%d:\n\topj_read_header failed\n",__FILE__,__LINE__); break; } if( !(user_changed_tile && user_changed_reduction) || (max_tiles <= 0) || (max_reduction <= 0) ) { opj_codestream_info_v2_t *cstr; cstr = opj_get_cstr_info(decodeInfo.codec); max_reduction = cstr->m_default_tile_info.tccp_info->numresolutions; max_tiles = cstr->tw * cstr->th; } if(tile_index < 0) { unsigned int x0, y0, x1, y1; int user_changed_area=0; x0 = y0 = x1 = y1 = 0; if(user_changed_area) { } if( !opj_set_decode_area(decodeInfo.codec, decodeInfo.image, x0, y0, x1, y1)) { fprintf(stderr,"%s:%d:\n\topj_set_decode_area failed\n",__FILE__,__LINE__); break; } if( !opj_decode(decodeInfo.codec, decodeInfo.stream, decodeInfo.image)) { fprintf(stderr,"%s:%d:\n\topj_decode failed\n",__FILE__,__LINE__); break; } } /* if(tile_index < 0) */ else { if( !opj_get_decoded_tile(decodeInfo.codec, decodeInfo.stream, decodeInfo.image, tile_index)) { fprintf(stderr,"%s:%d:\n\topj_get_decoded_tile failed\n",__FILE__,__LINE__); break; } } if( !opj_end_decompress(decodeInfo.codec, decodeInfo.stream)) { fprintf(stderr,"%s:%d:\n\topj_end_decompress failed\n",__FILE__,__LINE__); break; } fails = OPJ_FALSE; break; } decodeInfo.deleteImage = fails; release(&decodeInfo); if(fails) { return 0; } decodeInfo.deleteImage = OPJ_TRUE; if(decodeInfo.image->color_space != OPJ_CLRSPC_SYCC && decodeInfo.image->numcomps == 3 && decodeInfo.image->comps[0].dx == decodeInfo.image->comps[0].dy && decodeInfo.image->comps[1].dx != 1) decodeInfo.image->color_space = OPJ_CLRSPC_SYCC; else if(decodeInfo.image->numcomps <= 2) decodeInfo.image->color_space = OPJ_CLRSPC_GRAY; if(decodeInfo.image->color_space == OPJ_CLRSPC_SYCC) { //disable for now //color_sycc_to_rgb(decodeInfo.image); } if(decodeInfo.image->icc_profile_buf) { #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) color_apply_icc_profile(decodeInfo.image); #endif free(decodeInfo.image->icc_profile_buf); decodeInfo.image->icc_profile_buf = NULL; decodeInfo.image->icc_profile_len = 0; } width = decodeInfo.image->comps[0].w; height = decodeInfo.image->comps[0].h; long depth = (decodeInfo.image->comps[0].prec + 7)/8; long decompressSize = width * height * decodeInfo.image->numcomps * depth; if (decompressedBufferSize) *decompressedBufferSize = decompressSize;; if (!inputBuffer ) { inputBuffer = malloc(decompressSize); } if (colorModel) *colorModel = 0; if ((decodeInfo.image->numcomps >= 3 && decodeInfo.image->comps[0].dx == decodeInfo.image->comps[1].dx && decodeInfo.image->comps[1].dx == decodeInfo.image->comps[2].dx && decodeInfo.image->comps[0].dy == decodeInfo.image->comps[1].dy && decodeInfo.image->comps[1].dy == decodeInfo.image->comps[2].dy && decodeInfo.image->comps[0].prec == decodeInfo.image->comps[1].prec && decodeInfo.image->comps[1].prec == decodeInfo.image->comps[2].prec )/* RGB[A] */ || (decodeInfo.image->numcomps == 2 && decodeInfo.image->comps[0].dx == decodeInfo.image->comps[1].dx && decodeInfo.image->comps[0].dy == decodeInfo.image->comps[1].dy && decodeInfo.image->comps[0].prec == decodeInfo.image->comps[1].prec ) ) /* GA */ { int has_alpha4, has_alpha2, has_rgb; int *red, *green, *blue, *alpha; if (colorModel) *colorModel = 1; alpha = NULL; has_rgb = (decodeInfo.image->numcomps == 3); has_alpha4 = (decodeInfo.image->numcomps == 4); has_alpha2 = (decodeInfo.image->numcomps == 2); hasAlpha = (has_alpha4 || has_alpha2); if(has_rgb) { red = decodeInfo.image->comps[0].data; green = decodeInfo.image->comps[1].data; blue = decodeInfo.image->comps[2].data; if(has_alpha4) { alpha = decodeInfo.image->comps[3].data; } } /* if(has_rgb) */ else { red = green = blue = decodeInfo.image->comps[0].data; if(has_alpha2) { alpha = decodeInfo.image->comps[1].data; } } /* if(has_rgb) */ ac = 255;/* 255: FULLY_OPAQUE; 0: FULLY_TRANSPARENT */ unsigned char* ptrIBody = (unsigned char*)inputBuffer; for(i = 0; i < width*height; i++) { rc = (unsigned char) *red++; gc = (unsigned char)*green++; bc = (unsigned char)*blue++; if(hasAlpha) { ac = (unsigned char)*alpha++; // A R G B unsigned int pixel = (int)((ac<<24) | (rc<<16) | (gc<<8) | bc); *reinterpret_cast<unsigned int*>(ptrIBody) = pixel; ptrIBody += sizeof(unsigned int); } else { *ptrIBody++ = bc; *ptrIBody++ = gc; *ptrIBody++ = rc; } } /* for(i) */ }/* if (decodeInfo.image->numcomps >= 3 */ else if(decodeInfo.image->numcomps == 1) /* Grey */ { /* 1 component 8 or 16 bpp decodeInfo.image */ int *grey = decodeInfo.image->comps[0].data; if(decodeInfo.image->comps[0].prec <= 8) { char* ptrBBody = (char*)inputBuffer; for(i=0; i<width*height; i++) { *ptrBBody++ = *grey++; } /* Replace image8 buffer: */ } else /* prec[9:16] */ { int *grey; int ushift = 0, dshift = 0, force16 = 0; grey = decodeInfo.image->comps[0].data; short* ptrSBody = (short*)inputBuffer; for(i=0; i<width*height; i++) { //disable shift up for signed data: don't know why we are doing this *ptrSBody++ = *grey++; } /* Replace image16 buffer: */ } } else { int *grey; fprintf(stderr,"%s:%d:Can show only first component of decodeInfo.image\n" " components(%d) prec(%d) color_space[%d](%s)\n" " RECT(%d,%d,%d,%d)\n",__FILE__,__LINE__,decodeInfo.image->numcomps, decodeInfo.image->comps[0].prec, decodeInfo.image->color_space,clr_space(decodeInfo.image->color_space), decodeInfo.image->x0,decodeInfo.image->y0,decodeInfo.image->x1,decodeInfo.image->y1 ); for(i = 0; i < decodeInfo.image->numcomps; ++i) { fprintf(stderr,"[%d]dx(%d) dy(%d) w(%d) h(%d) signed(%u)\n",i, decodeInfo.image->comps[i].dx ,decodeInfo.image->comps[i].dy, decodeInfo.image->comps[i].w,decodeInfo.image->comps[i].h, decodeInfo.image->comps[i].sgnd); } /* 1 component 8 or 16 bpp decodeInfo.image */ grey = decodeInfo.image->comps[0].data; if(decodeInfo.image->comps[0].prec <= 8) { char* ptrBBody = (char*)inputBuffer; for(i=0; i<width*height; i++) { *ptrBBody++ = *grey++; } /* Replace image8 buffer: */ } else /* prec[9:16] */ { int *grey; int ushift = 0, dshift = 0, force16 = 0; grey = decodeInfo.image->comps[0].data; short* ptrSBody = (short*)inputBuffer; for(i=0; i<width*height; i++) { *ptrSBody++ = *grey++; } /* Replace image16 buffer: */ } } release(&decodeInfo); return inputBuffer; }
/* -------------------------------------------------------------------------- */ int jp2_decompress_main(char *infile, char *buf, int bufsize) { int i; int *dataptr; opj_dparameters_t parameters; /* decompression parameters */ opj_image_t* image = NULL; opj_stream_t *l_stream = NULL; /* Stream */ opj_codec_t* l_codec = NULL; /* Handle to a decompressor */ opj_codestream_index_t* cstr_index = NULL; char indexfilename[OPJ_PATH_LEN]; /* index file name */ OPJ_INT32 num_images, imageno; img_fol_t img_fol; dircnt_t *dirptr = NULL; /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); /* FIXME Initialize indexfilename and img_fol */ *indexfilename = 0; /* Initialize img_fol */ memset(&img_fol,0,sizeof(img_fol_t)); /* parse input and get user encoding parameters */ if (0 != parse_cmdline_decoder(¶meters,&img_fol, infile)) { return EXIT_FAILURE; } /* read the input file and put it in memory */ /* ---------------------------------------- */ l_stream = opj_stream_create_default_file_stream_v3(parameters.infile,1); if (!l_stream){ return EXIT_FAILURE; } /* decode the JPEG2000 stream */ /* ---------------------- */ switch(parameters.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; } case JPT_CFMT: /* JPEG 2000, JPIP */ { /* Get a decoder handle */ l_codec = opj_create_decompress(OPJ_CODEC_JPT); break; } default: fprintf(stderr, "skipping file..\n"); opj_stream_destroy_v3(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, ¶meters) ){ fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n"); opj_stream_destroy_v3(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, &image)){ fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n"); opj_stream_destroy_v3(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); return EXIT_FAILURE; } if (!parameters.nb_tile_to_decode) { /* Optional if you want decode the entire image */ if (!opj_set_decode_area(l_codec, image, parameters.DA_x0, parameters.DA_y0, parameters.DA_x1, parameters.DA_y1)){ fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n"); opj_stream_destroy_v3(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); return EXIT_FAILURE; } /* Get the decoded image */ if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) { fprintf(stderr,"ERROR -> opj_decompress: failed to decode image!\n"); opj_destroy_codec(l_codec); opj_stream_destroy_v3(l_stream); opj_image_destroy(image); return EXIT_FAILURE; } } else { /* It is just here to illustrate how to use the resolution after set parameters */ /*if (!opj_set_decoded_resolution_factor(l_codec, 5)) { fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n"); opj_destroy_codec(l_codec); opj_stream_destroy_v3(l_stream); opj_image_destroy(image); return EXIT_FAILURE; }*/ if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) { fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n"); opj_destroy_codec(l_codec); opj_stream_destroy_v3(l_stream); opj_image_destroy(image); return EXIT_FAILURE; } fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index); } /* Close the byte stream */ opj_stream_destroy_v3(l_stream); if(image->color_space == OPJ_CLRSPC_SYCC){ color_sycc_to_rgb(image); /* FIXME */ } if( image->color_space != OPJ_CLRSPC_SYCC && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy && image->comps[1].dx != 1 ) image->color_space = OPJ_CLRSPC_SYCC; else if (image->numcomps <= 2) image->color_space = OPJ_CLRSPC_GRAY; if(image->icc_profile_buf) { #if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2) color_apply_icc_profile(image); /* FIXME */ #endif free(image->icc_profile_buf); image->icc_profile_buf = NULL; image->icc_profile_len = 0; } /* create output image */ /* ------------------- */ switch (parameters.cod_format) { case PXM_DFMT: /* PNM PGM PPM */ if (imagetopnm(image, parameters.outfile)) { fprintf(stdout,"Outfile %s not generated\n",parameters.outfile); } else { fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; case PGX_DFMT: /* PGX */ if(imagetopgx(image, parameters.outfile)){ fprintf(stdout,"Outfile %s not generated\n",parameters.outfile); } else { fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; case BMP_DFMT: /* BMP */ if(imagetobmp(image, parameters.outfile)){ fprintf(stdout,"Outfile %s not generated\n",parameters.outfile); } else { fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; #ifdef OPJ_HAVE_LIBTIFF case TIF_DFMT: /* TIFF */ if(imagetotif(image, parameters.outfile)){ fprintf(stdout,"Outfile %s not generated\n",parameters.outfile); } else { fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; #endif /* OPJ_HAVE_LIBTIFF */ case RAW_DFMT: /* RAW */ dataptr = &image->comps[0].data[0]; char mask = 0xFF; for (i = 0; i < bufsize; i++) { *buf++ = *dataptr++ & mask; } /* if(imagetoraw(image, parameters.outfile)){ fprintf(stdout,"Error generating raw file. Outfile %s not generated\n",parameters.outfile); } else { fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); } */ break; case RAWL_DFMT: /* RAWL */ if(imagetorawl(image, parameters.outfile)){ fprintf(stdout,"Error generating rawl file. Outfile %s not generated\n",parameters.outfile); } else { fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); } break; case TGA_DFMT: /* TGA */ if(imagetotga(image, parameters.outfile)){ fprintf(stdout,"Error generating tga file. Outfile %s not generated\n",parameters.outfile); } else { fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); } break; #ifdef OPJ_HAVE_LIBPNG case PNG_DFMT: /* PNG */ if(imagetopng(image, parameters.outfile)){ fprintf(stdout,"Error generating png file. Outfile %s not generated\n",parameters.outfile); } else { fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); } break; #endif /* OPJ_HAVE_LIBPNG */ /* Can happen if output file is TIFF or PNG * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined */ default: fprintf(stderr,"Outfile %s not generated\n",parameters.outfile); } /* free remaining structures */ if (l_codec) { opj_destroy_codec(l_codec); } /* free image data structure */ opj_image_destroy(image); /* destroy the codestream index */ opj_destroy_cstr_index(&cstr_index); return EXIT_SUCCESS; }
/*! * pixReadStreamJp2k() * * Input: stream * reduction (scaling factor: 1, 2, 4, 8) * box (<optional> for extracting a subregion), can be null * hint (a bitwise OR of L_JP2K_* values; 0 for default) * debug (output callback messages, etc) * Return: pix (8 or 32 bpp), or null on error * * Notes: * (1) See pixReadJp2k() for usage. */ PIX * pixReadStreamJp2k(FILE *fp, l_uint32 reduction, BOX *box, l_int32 hint, l_int32 debug) { const char *opjVersion; l_int32 i, j, index, bx, by, bw, bh, val, rval, gval, bval, aval; l_int32 w, h, wpl, bps, spp, xres, yres, reduce, prec, colorspace; l_uint32 pixel; l_uint32 *data, *line; opj_dparameters_t parameters; /* decompression parameters */ opj_image_t *image = NULL; opj_codec_t *l_codec = NULL; /* handle to decompressor */ opj_stream_t *l_stream = NULL; /* opj stream */ PIX *pix = NULL; PROCNAME("pixReadStreamJp2k"); if (!fp) return (PIX *)ERROR_PTR("fp not defined", procName, NULL); opjVersion = opj_version(); if (opjVersion[0] != '2') { L_ERROR("version is %s; must be 2.0 or higher\n", procName, opjVersion); return NULL; } if ((opjVersion[2] - 0x30) != OPJ_VERSION_MINOR) { L_ERROR("version %s: differs from minor = %d\n", procName, opjVersion, OPJ_VERSION_MINOR); return NULL; } /* Get the resolution and the bits/sample */ rewind(fp); fgetJp2kResolution(fp, &xres, &yres); freadHeaderJp2k(fp, NULL, NULL, &bps, NULL); rewind(fp); if (bps > 8) { L_ERROR("found %d bps; can only handle 8 bps\n", procName, bps); return NULL; } /* Set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); /* Find and set the reduce parameter, which is log2(reduction). * Valid reductions are powers of 2, and are determined when the * compressed string is made. A request for an invalid reduction * will cause an error in opj_read_header(), and no image will * be returned. */ for (reduce = 0; (1L << reduce) < reduction; reduce++) { } if ((1L << reduce) != reduction) { L_ERROR("invalid reduction %d; not power of 2\n", procName, reduction); return NULL; } parameters.cp_reduce = reduce; /* Open decompression 'stream'. In 2.0, we could call this: * opj_stream_create_default_file_stream(fp, 1) * but the file stream interface was removed in 2.1. */ if ((l_stream = opjCreateStream(fp, 1)) == NULL) { L_ERROR("failed to open the stream\n", procName); return NULL; } if ((l_codec = opj_create_decompress(OPJ_CODEC_JP2)) == NULL) { L_ERROR("failed to make the codec\n", procName); opj_stream_destroy(l_stream); return NULL; } /* Catch and report events using callbacks */ if (debug) { opj_set_info_handler(l_codec, info_callback, NULL); opj_set_warning_handler(l_codec, warning_callback, NULL); opj_set_error_handler(l_codec, error_callback, NULL); } /* Setup the decoding parameters using user parameters */ if (!opj_setup_decoder(l_codec, ¶meters)){ L_ERROR("failed to set up decoder\n", procName); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); return NULL; } /* Read the main header of the codestream and, if necessary, * the JP2 boxes */ if(!opj_read_header(l_stream, l_codec, &image)){ L_ERROR("failed to read the header\n", procName); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); return NULL; } /* Set up to decode a rectangular region */ if (box) { boxGetGeometry(box, &bx, &by, &bw, &bh); if (!opj_set_decode_area(l_codec, image, bx, by, bx + bw, by + bh)) { L_ERROR("failed to set the region for decoding\n", procName); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); return NULL; } } /* Get the decoded image */ if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) { L_ERROR("failed to decode the image\n", procName); opj_destroy_codec(l_codec); opj_stream_destroy(l_stream); opj_image_destroy(image); return NULL; } /* Close the byte stream */ opj_stream_destroy(l_stream); /* Get the image parameters */ spp = image->numcomps; w = image->comps[0].w; h = image->comps[0].h; prec = image->comps[0].prec; if (prec != bps) L_WARNING("precision %d != bps %d!\n", procName, prec, bps); if (debug) { L_INFO("w = %d, h = %d, bps = %d, spp = %d\n", procName, w, h, bps, spp); colorspace = image->color_space; if (colorspace == OPJ_CLRSPC_SRGB) L_INFO("colorspace is sRGB\n", procName); else if (colorspace == OPJ_CLRSPC_GRAY) L_INFO("colorspace is grayscale\n", procName); else if (colorspace == OPJ_CLRSPC_SYCC) L_INFO("colorspace is YUV\n", procName); } /* Free the codec structure */ if (l_codec) opj_destroy_codec(l_codec); /* Convert the image to a pix */ if (spp == 1) pix = pixCreate(w, h, 8); else pix = pixCreate(w, h, 32); pixSetResolution(pix, xres, yres); data = pixGetData(pix); wpl = pixGetWpl(pix); index = 0; if (spp == 1) { for (i = 0; i < h; i++) { line = data + i * wpl; for (j = 0; j < w; j++) { val = image->comps[0].data[index]; SET_DATA_BYTE(line, j, val); index++; } } } else if (spp == 2) { /* convert to RGBA */ for (i = 0; i < h; i++) { line = data + i * wpl; for (j = 0; j < w; j++) { val = image->comps[0].data[index]; aval = image->comps[1].data[index]; composeRGBAPixel(val, val, val, aval, &pixel); line[j] = pixel; index++; } } } else if (spp >= 3) { for (i = 0; i < h; i++) { line = data + i * wpl; for (j = 0; j < w; j++) { rval = image->comps[0].data[index]; gval = image->comps[1].data[index]; bval = image->comps[2].data[index]; if (spp == 3) { composeRGBPixel(rval, gval, bval, &pixel); } else { /* spp == 4 */ aval = image->comps[3].data[index]; composeRGBAPixel(rval, gval, bval, aval, &pixel); } line[j] = pixel; index++; } } } /* Free the opj image data structure */ opj_image_destroy(image); return pix; }