int main(int argc, char *argv[]) { int ret; opj_dparameters_t parameters; /* decompression parameters */ img_fol_t img_fol; opj_image_t *image = NULL; FILE *fsrc = NULL; bool bResult; int num_images; int i,imageno; dircnt_t *dirptr; opj_codec_t* dinfo = NULL; /* handle to a decompressor */ opj_stream_t *cio = NULL; opj_codestream_info_t cstr_info; /* Codestream information structure */ char indexfilename[OPJ_PATH_LEN]; /* index file name */ OPJ_INT32 l_tile_x0,l_tile_y0; OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y; /* configure the event callbacks (not required) */ /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); /* Initialize indexfilename and img_fol */ *indexfilename = 0; 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){ num_images=get_num_images(img_fol.imgdirpath); dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); if(dirptr){ dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); // Stores at max 10 image file names dirptr->filename = (char**) malloc(num_images*sizeof(char*)); if(!dirptr->filename_buf){ return EXIT_FAILURE; } for(i=0;i<num_images;i++){ dirptr->filename[i] = dirptr->filename_buf + i*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; } /*Encoding image one by one*/ for(imageno = 0; imageno < num_images ; imageno++) { image = NULL; 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; } cio = opj_stream_create_default_file_stream(fsrc,true); /* decode the code-stream */ /* ---------------------- */ switch (parameters.decod_format) { case J2K_CFMT: { /* JPEG-2000 codestream */ /* get a decoder handle */ dinfo = opj_create_decompress(CODEC_J2K); break; } case JP2_CFMT: { /* JPEG 2000 compressed image data */ /* get a decoder handle */ dinfo = opj_create_decompress(CODEC_JP2); break; } case JPT_CFMT: { /* JPEG 2000, JPIP */ /* get a decoder handle */ dinfo = opj_create_decompress(CODEC_JPT); break; } default: fprintf(stderr, "skipping file..\n"); opj_stream_destroy(cio); continue; } /* catch events using our callbacks and give a local context */ /* setup the decoder decoding parameters using user parameters */ opj_setup_decoder(dinfo, ¶meters); /* decode the stream and fill the image structure */ /* if (*indexfilename) // If need to extract codestream information image = opj_decode_with_info(dinfo, cio, &cstr_info); else */ bResult = opj_read_header( dinfo, &image, &l_tile_x0, &l_tile_y0, &l_tile_width, &l_tile_height, &l_nb_tiles_x, &l_nb_tiles_y, cio); //image = opj_decode(dinfo, cio); //bResult = bResult && (image != 00); //bResult = bResult && opj_end_decompress(dinfo,cio); //if // (!image) //{ // fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); // opj_destroy_codec(dinfo); // opj_stream_destroy(cio); // fclose(fsrc); // return EXIT_FAILURE; //} /* dump image */ if(!image) { fprintf(stderr, "ERROR -> j2k_to_image: failed to read header\n"); return EXIT_FAILURE; } j2k_dump_image(stdout, image); /* dump cp */ //j2k_dump_cp(stdout, image, dinfo->m_codec); /* close the byte stream */ opj_stream_destroy(cio); fclose(fsrc); /* Write the index to disk */ if (*indexfilename) { char bSuccess; bSuccess = write_index_file(&cstr_info, indexfilename); if (bSuccess) { fprintf(stderr, "Failed to output index file\n"); ret = EXIT_FAILURE; } } /* free remaining structures */ if (dinfo) { opj_destroy_codec(dinfo); } /* free codestream information structure */ if (*indexfilename) opj_destroy_cstr_info(&cstr_info); /* free image data structure */ opj_image_destroy(image); } return ret; }
int main(int argc, char **argv) { opj_dparameters_t parameters; /* decompression parameters */ img_fol_t img_fol; opj_event_mgr_t event_mgr; /* event manager */ opj_image_t *image = NULL; FILE *fsrc = NULL; unsigned char *src = NULL; int file_length; int num_images; int i,imageno; dircnt_t *dirptr = NULL; opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */ opj_cio_t *cio = NULL; opj_codestream_info_t cstr_info; /* Codestream information structure */ char indexfilename[OPJ_PATH_LEN]; /* index file name */ /* configure the event callbacks (not required) */ memset(&event_mgr, 0, sizeof(opj_event_mgr_t)); event_mgr.error_handler = error_callback; event_mgr.warning_handler = warning_callback; event_mgr.info_handler = info_callback; /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); /* Initialize indexfilename and img_fol */ *indexfilename = 0; 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 1; } /* Initialize reading of directory */ if(img_fol.set_imgdir==1){ num_images=get_num_images(img_fol.imgdirpath); dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); if(dirptr){ dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ dirptr->filename = (char**) malloc(num_images*sizeof(char*)); if(!dirptr->filename_buf){ return 1; } for(i=0;i<num_images;i++){ dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN; } } if(load_images(dirptr,img_fol.imgdirpath)==1){ return 1; } if (num_images==0){ fprintf(stdout,"Folder is empty\n"); return 1; } }else{ num_images=1; } /*Encoding image one by one*/ for(imageno = 0; imageno < num_images ; imageno++) { image = NULL; 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 1; } fseek(fsrc, 0, SEEK_END); file_length = ftell(fsrc); fseek(fsrc, 0, SEEK_SET); src = (unsigned char *) malloc(file_length); if (fread(src, 1, file_length, fsrc) != (size_t)file_length) { free(src); fclose(fsrc); fprintf(stderr, "\nERROR: fread return a number of element different from the expected.\n"); return 1; } fclose(fsrc); /* decode the code-stream */ /* ---------------------- */ switch(parameters.decod_format) { case J2K_CFMT: { /* JPEG-2000 codestream */ /* get a decoder handle */ dinfo = opj_create_decompress(CODEC_J2K); /* catch events using our callbacks and give a local context */ opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); /* setup the decoder decoding parameters using user parameters */ opj_setup_decoder(dinfo, ¶meters); /* open a byte stream */ cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length); /* decode the stream and fill the image structure */ if (*indexfilename) /* If need to extract codestream information*/ image = opj_decode_with_info(dinfo, cio, &cstr_info); else image = opj_decode(dinfo, cio); if(!image) { fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); opj_destroy_decompress(dinfo); opj_cio_close(cio); free(src); return 1; } /* close the byte stream */ opj_cio_close(cio); /* Write the index to disk */ if (*indexfilename) { opj_bool bSuccess; bSuccess = write_index_file(&cstr_info, indexfilename); if (bSuccess) { fprintf(stderr, "Failed to output index file\n"); } } } break; case JP2_CFMT: { /* JPEG 2000 compressed image data */ /* get a decoder handle */ dinfo = opj_create_decompress(CODEC_JP2); /* catch events using our callbacks and give a local context */ opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); /* setup the decoder decoding parameters using the current image and user parameters */ opj_setup_decoder(dinfo, ¶meters); /* open a byte stream */ cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length); /* decode the stream and fill the image structure */ if (*indexfilename) /* If need to extract codestream information*/ image = opj_decode_with_info(dinfo, cio, &cstr_info); else image = opj_decode(dinfo, cio); if(!image) { fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); opj_destroy_decompress(dinfo); opj_cio_close(cio); free(src); return 1; } /* close the byte stream */ opj_cio_close(cio); /* Write the index to disk */ if (*indexfilename) { opj_bool bSuccess; bSuccess = write_index_file(&cstr_info, indexfilename); if (bSuccess) { fprintf(stderr, "Failed to output index file\n"); } } } break; case JPT_CFMT: { /* JPEG 2000, JPIP */ /* get a decoder handle */ dinfo = opj_create_decompress(CODEC_JPT); /* catch events using our callbacks and give a local context */ opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); /* setup the decoder decoding parameters using user parameters */ opj_setup_decoder(dinfo, ¶meters); /* open a byte stream */ cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length); /* decode the stream and fill the image structure */ if (*indexfilename) /* If need to extract codestream information*/ image = opj_decode_with_info(dinfo, cio, &cstr_info); else image = opj_decode(dinfo, cio); if(!image) { fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); opj_destroy_decompress(dinfo); opj_cio_close(cio); free(src); return 1; } /* close the byte stream */ opj_cio_close(cio); /* Write the index to disk */ if (*indexfilename) { opj_bool bSuccess; bSuccess = write_index_file(&cstr_info, indexfilename); if (bSuccess) { fprintf(stderr, "Failed to output index file\n"); } } } break; default: fprintf(stderr, "skipping file..\n"); continue; } /* free the memory containing the code-stream */ free(src); src = NULL; if(image->color_space == CLRSPC_SYCC) { color_sycc_to_rgb(image); } if(image->icc_profile_buf) { #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) color_apply_icc_profile(image); #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 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 /* HAVE_LIBTIFF */ case RAW_DFMT: /* RAW */ 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 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 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 /* HAVE_LIBPNG */ /* Can happen if output file is TIFF or PNG * and HAVE_LIBTIF or HAVE_LIBPNG is undefined */ default: fprintf(stderr,"Outfile %s not generated\n",parameters.outfile); } /* free remaining structures */ if(dinfo) { opj_destroy_decompress(dinfo); } /* free codestream information structure */ if (*indexfilename) opj_destroy_cstr_info(&cstr_info); /* free image data structure */ opj_image_destroy(image); } return 0; }
int main(int argc, char *argv[]) { opj_dparameters_t parameters; /* decompression parameters */ img_fol_t img_fol; opj_event_mgr_t event_mgr; /* event manager */ opj_image_t *image = NULL; FILE *fsrc = NULL, *fout = NULL; unsigned char *src = NULL; int file_length; int num_images; int i,imageno; dircnt_t *dirptr = NULL; opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */ opj_cio_t *cio = NULL; opj_codestream_info_t cstr_info; /* Codestream information structure */ char indexfilename[OPJ_PATH_LEN]; /* index file name */ /* configure the event callbacks (not required) */ memset(&event_mgr, 0, sizeof(opj_event_mgr_t)); event_mgr.error_handler = error_callback; event_mgr.warning_handler = warning_callback; event_mgr.info_handler = info_callback; /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); /* Initialize indexfilename and img_fol */ *indexfilename = 0; 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 1; } /* Initialize reading of directory */ if(img_fol.set_imgdir==1){ num_images=get_num_images(img_fol.imgdirpath); dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); if(dirptr){ dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ dirptr->filename = (char**) malloc(num_images*sizeof(char*)); if(!dirptr->filename_buf){ return 1; } for(i=0;i<num_images;i++){ dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN; } } if(load_images(dirptr,img_fol.imgdirpath)==1){ return 1; } if (num_images==0){ fprintf(stdout,"Folder is empty\n"); return 1; } }else{ num_images=1; } /* */ if (parameters.outfile[0] != 0) { fout = fopen(parameters.outfile,"w"); if (!fout) { fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.outfile); return 1; } } else fout = stdout; /*Encoding image one by one*/ for(imageno = 0; imageno < num_images ; imageno++) { image = NULL; 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 1; } fseek(fsrc, 0, SEEK_END); file_length = ftell(fsrc); fseek(fsrc, 0, SEEK_SET); src = (unsigned char *) malloc(file_length); if (fread(src, 1, file_length, fsrc) != (size_t)file_length) { free(src); fclose(fsrc); fclose(fout); fprintf(stderr, "\nERROR: fread return a number of element different from the expected.\n"); return 1; } fclose(fsrc); /* decode the code-stream */ /* ---------------------- */ switch(parameters.decod_format) { case J2K_CFMT: { /* JPEG-2000 codestream */ /* get a decoder handle */ dinfo = opj_create_decompress(CODEC_J2K); /* catch events using our callbacks and give a local context */ opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); /* setup the decoder decoding parameters using user parameters */ opj_setup_decoder(dinfo, ¶meters); /* open a byte stream */ cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length); /* decode the stream and fill the image structure */ if (*indexfilename) /* If need to extract codestream information*/ image = opj_decode_with_info(dinfo, cio, &cstr_info); else image = opj_decode(dinfo, cio); if(!image) { fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); opj_destroy_decompress(dinfo); opj_cio_close(cio); fclose(fout); free(src); return 1; } /* dump image */ j2k_dump_image(fout, image); /* dump cp */ j2k_dump_cp(fout, image, ((opj_j2k_t*)dinfo->j2k_handle)->cp); /* close the byte stream */ opj_cio_close(cio); /* Write the index to disk */ if (*indexfilename) { opj_bool bSuccess; bSuccess = write_index_file(&cstr_info, indexfilename); if (bSuccess) { fprintf(stderr, "Failed to output index file\n"); } } } break; case JP2_CFMT: { /* JPEG 2000 compressed image data */ /* get a decoder handle */ dinfo = opj_create_decompress(CODEC_JP2); /* catch events using our callbacks and give a local context */ opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); /* setup the decoder decoding parameters using the current image and user parameters */ opj_setup_decoder(dinfo, ¶meters); /* open a byte stream */ cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length); /* decode the stream and fill the image structure */ if (*indexfilename) /* If need to extract codestream information*/ image = opj_decode_with_info(dinfo, cio, &cstr_info); else image = opj_decode(dinfo, cio); if(!image) { fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); opj_destroy_decompress(dinfo); opj_cio_close(cio); fclose(fout); free(src); return 1; } /* dump image */ if(image->icc_profile_buf) { free(image->icc_profile_buf); image->icc_profile_buf = NULL; } j2k_dump_image(fout, image); /* dump cp */ j2k_dump_cp(fout, image, ((opj_jp2_t*)dinfo->jp2_handle)->j2k->cp); /* close the byte stream */ opj_cio_close(cio); /* Write the index to disk */ if (*indexfilename) { opj_bool bSuccess; bSuccess = write_index_file(&cstr_info, indexfilename); if (bSuccess) { fprintf(stderr, "Failed to output index file\n"); } } } break; case JPT_CFMT: { /* JPEG 2000, JPIP */ /* get a decoder handle */ dinfo = opj_create_decompress(CODEC_JPT); /* catch events using our callbacks and give a local context */ opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); /* setup the decoder decoding parameters using user parameters */ opj_setup_decoder(dinfo, ¶meters); /* open a byte stream */ cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length); /* decode the stream and fill the image structure */ if (*indexfilename) /* If need to extract codestream information*/ image = opj_decode_with_info(dinfo, cio, &cstr_info); else image = opj_decode(dinfo, cio); if(!image) { fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); opj_destroy_decompress(dinfo); opj_cio_close(cio); fclose(fout); free(src); return 1; } /* close the byte stream */ opj_cio_close(cio); /* Write the index to disk */ if (*indexfilename) { opj_bool bSuccess; bSuccess = write_index_file(&cstr_info, indexfilename); if (bSuccess) { fprintf(stderr, "Failed to output index file\n"); } } } break; default: fprintf(stderr, "skipping file..\n"); continue; } /* free the memory containing the code-stream */ free(src); src = NULL; /* free remaining structures */ if(dinfo) { opj_destroy_decompress(dinfo); } /* free codestream information structure */ if (*indexfilename) opj_destroy_cstr_info(&cstr_info); /* free image data structure */ opj_image_destroy(image); } fclose(fout); return EXIT_SUCCESS; }
BOOL LLImageJ2COJ::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decode_time, S32 first_channel, S32 max_channel_count) { raw_image.decodedComment = LLImageMetaDataReader::ExtractKDUUploadComment(base.getData(), base.getDataSize()); LLTimer decode_timer; opj_dparameters_t parameters; /* decompression parameters */ opj_event_mgr_t event_mgr; /* event manager */ opj_image_t *image = NULL; opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */ opj_cio_t *cio = NULL; /* configure the event callbacks (not required) */ memset(&event_mgr, 0, sizeof(opj_event_mgr_t)); event_mgr.error_handler = error_callback; event_mgr.warning_handler = warning_callback; event_mgr.info_handler = info_callback; /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); parameters.cp_reduce = base.getRawDiscardLevel(); /* decode the code-stream */ /* ---------------------- */ /* JPEG-2000 codestream */ /* get a decoder handle */ dinfo = opj_create_decompress(CODEC_J2K); /* catch events using our callbacks and give a local context */ opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); /* setup the decoder decoding parameters using user parameters */ opj_setup_decoder(dinfo, ¶meters); /* open a byte stream */ cio = opj_cio_open((opj_common_ptr)dinfo, base.getData(), base.getDataSize()); /* decode the stream and fill the image structure. Also fill in an additional structur to get the decoding result. This structure is a bit unusual in that it is not received through opj, but still has somt dynamically allocated fields that need to be cleared up at the end by calling a destroy function. */ opj_codestream_info_t cinfo; memset(&cinfo, 0, sizeof(opj_codestream_info_t)); image = opj_decode_with_info(dinfo, cio, &cinfo); /* close the byte stream */ opj_cio_close(cio); /* free remaining structures */ if(dinfo) { opj_destroy_decompress(dinfo); } // The image decode failed if the return was NULL or the component // count was zero. The latter is just a sanity check before we // dereference the array. if(!image) { LL_WARNS ("Openjpeg") << "Failed to decode image at discard: " << (S32)base.getRawDiscardLevel() << ". No image." << LL_ENDL; if (base.getRawDiscardLevel() == 0) { base.decodeFailed(); } return TRUE; // done } S32 img_components = image->numcomps; if( !img_components ) // < 1 ||img_components > 4 ) { LL_WARNS("Openjpeg") << "Failed to decode image at discard: " << (S32)base.getRawDiscardLevel() << ". Wrong number of components: " << img_components << LL_ENDL; if (image) { opj_destroy_cstr_info(&cinfo); opj_image_destroy(image); } if (base.getRawDiscardLevel() == 0) { base.decodeFailed(); } return TRUE; // done } // sometimes we get bad data out of the cache - check to see if the decode succeeded int decompdifference = 0; if (cinfo.numdecompos) // sanity { for (int comp = 0; comp < image->numcomps; comp++) { /* get maximum decomposition level difference, first field is from the COD header and the second is what is actually met in the codestream, NB: if everything was ok, this calculation will return what was set in the cp_reduce value! */ decompdifference = llmax(decompdifference, cinfo.numdecompos[comp] - image->comps[comp].resno_decoded); } if (decompdifference < 0) // sanity { decompdifference = 0; } } /* if OpenJPEG failed to decode all requested decomposition levels the difference will be greater than this level */ if (decompdifference > base.getRawDiscardLevel()) { LL_WARNS("Openjpeg") << "Not enough data for requested discard level " << (S32)base.getRawDiscardLevel() << ", difference: " << (decompdifference - base.getRawDiscardLevel()) << llendl; opj_destroy_cstr_info(&cinfo); opj_image_destroy(image); if (base.getRawDiscardLevel() == 0) { base.decodeFailed(); } return TRUE; } if(img_components <= first_channel) { LL_WARNS("Openjpeg") << "Trying to decode more channels than are present in image, numcomps: " << img_components << " first_channel: " << first_channel << LL_ENDL; if (image) { opj_destroy_cstr_info(&cinfo); opj_image_destroy(image); } if (base.getRawDiscardLevel() == 0) { base.decodeFailed(); } return TRUE; } // Copy image data into our raw image format (instead of the separate channel format S32 channels = img_components - first_channel; if( channels > max_channel_count ) channels = max_channel_count; // Component buffers are allocated in an image width by height buffer. // The image placed in that buffer is ceil(width/2^factor) by // ceil(height/2^factor) and if the factor isn't zero it will be at the // top left of the buffer with black filled in the rest of the pixels. // It is integer math so the formula is written in ceildivpo2. // (Assuming all the components have the same width, height and // factor.) S32 comp_width = image->comps[0].w; S32 f=image->comps[0].factor; S32 width = ceildivpow2(image->x1 - image->x0, f); S32 height = ceildivpow2(image->y1 - image->y0, f); raw_image.resize(width, height, channels); U8 *rawp = raw_image.getData(); // first_channel is what channel to start copying from // dest is what channel to copy to. first_channel comes from the // argument, dest always starts writing at channel zero. for (S32 comp = first_channel, dest=0; comp < first_channel + channels; comp++, dest++) { if (image->comps[comp].data) { S32 offset = dest; for (S32 y = (height - 1); y >= 0; y--) { for (S32 x = 0; x < width; x++) { rawp[offset] = image->comps[comp].data[y*comp_width + x]; offset += channels; } } } else // Some rare OpenJPEG versions have this bug. { LL_WARNS("Openjpeg") << "Failed to decode image! (NULL comp data - OpenJPEG bug)" << LL_ENDL; opj_destroy_cstr_info(&cinfo); opj_image_destroy(image); if (base.getRawDiscardLevel() == 0) { base.decodeFailed(); } return TRUE; // done } } /* free opj data structures */ if (image) { opj_destroy_cstr_info(&cinfo); opj_image_destroy(image); } return TRUE; // done }
/* -------------------------------------------------------------------------- */ int main(int argc, char *argv[]) { FILE *fout = NULL; opj_dparameters_t parameters; /* Decompression parameters */ opj_image_t* image = NULL; /* Image structure */ opj_codec_t* l_codec = NULL; /* Handle to a decompressor */ opj_stream_t *l_stream = NULL; /* Stream */ opj_codestream_info_v2_t* cstr_info = NULL; opj_codestream_index_t* cstr_index = NULL; int32_t num_images, imageno; img_fol_t img_fol; dircnt_t *dirptr = NULL; #ifdef MSD bool l_go_on = true; uint32_t l_max_data_size = 1000; uint8_t * l_data = (uint8_t *) malloc(1000); #endif /* Set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); /* Initialize img_fol */ memset(&img_fol,0,sizeof(img_fol_t)); img_fol.flag = OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND; /* Parse input and get user encoding parameters */ if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 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; } /* Try to open for writing the output file if necessary */ if (parameters.outfile[0] != 0) { fout = fopen(parameters.outfile,"w"); if (!fout) { fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile); return EXIT_FAILURE; } } else fout = stdout; /* Read the header of each 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 */ /* ---------------------------------------- */ 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); return EXIT_FAILURE; } /* Read 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_dump: failed to setup the decoder\n"); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); fclose(fout); 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_dump: failed to read the header\n"); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); fclose(fout); return EXIT_FAILURE; } opj_dump_codec(l_codec, img_fol.flag, fout ); cstr_info = opj_get_cstr_info(l_codec); cstr_index = opj_get_cstr_index(l_codec); /* close the byte stream */ opj_stream_destroy(l_stream); /* free remaining structures */ if (l_codec) { opj_destroy_codec(l_codec); } /* destroy the image header */ opj_image_destroy(image); /* destroy the codestream index */ opj_destroy_cstr_index(&cstr_index); /* destroy the codestream info */ opj_destroy_cstr_info(&cstr_info); } /* Close the output file */ fclose(fout); return EXIT_SUCCESS; }
OpenJPEG_readHeader(OpenJPEGReaderImpl *impl, nrt_Error *error) { opj_stream_t *stream = NULL; opj_image_t *image = NULL; opj_codec_t *codec = NULL; opj_codestream_info_v2_t* codeStreamInfo = NULL; NRT_BOOL rc = NRT_SUCCESS; OPJ_UINT32 tileWidth, tileHeight; OPJ_UINT32 imageWidth, imageHeight; if (!OpenJPEG_setup(impl, &stream, &codec, error)) { goto CATCH_ERROR; } if (!opj_read_header(stream, codec, &image)) { /*nrt_Error_init(error, "Error reading header", NRT_CTXT, NRT_ERR_UNK);*/ goto CATCH_ERROR; } codeStreamInfo = opj_get_cstr_info(codec); if (!codeStreamInfo) { /*nrt_Error_init(error, "Error reading code stream", NRT_CTXT, NRT_ERR_UNK);*/ goto CATCH_ERROR; } tileWidth = codeStreamInfo->tdx; tileHeight = codeStreamInfo->tdy; /* sanity checking */ if (!image) { nrt_Error_init(error, "NULL image after reading header", NRT_CTXT, NRT_ERR_UNK); goto CATCH_ERROR; } if (image->x0 >= image->x1 || image->y0 >= image->y1) { nrt_Error_init(error, "Invalid image offsets", NRT_CTXT, NRT_ERR_UNK); goto CATCH_ERROR; } if (image->numcomps == 0) { nrt_Error_init(error, "No image components found", NRT_CTXT, NRT_ERR_UNK); goto CATCH_ERROR; } /* TODO: We need special handling that's not implemented in readTile() to * accommodate partial tiles with more than one band. */ imageWidth = image->x1 - image->x0; imageHeight = image->y1 - image->y0; if (image->numcomps > 1 && (imageWidth % tileWidth != 0 || imageHeight % tileHeight != 0)) { nrt_Error_init(error, "No image components found", NRT_CTXT, NRT_ERR_UNK); goto CATCH_ERROR; } if (!impl->container) { /* initialize the container */ nrt_Uint32 idx; j2k_Component **components = NULL; int imageType; if (!(components = (j2k_Component**)J2K_MALLOC( sizeof(j2k_Component*) * image->numcomps))) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); goto CATCH_ERROR; } for(idx = 0; idx < image->numcomps; ++idx) { opj_image_comp_t cmp = image->comps[idx]; if (!(components[idx] = j2k_Component_construct(cmp.w, cmp.h, cmp.prec, cmp.sgnd, cmp.x0, cmp.y0, cmp.dx, cmp.dy, error))) { goto CATCH_ERROR; } } switch(image->color_space) { case OPJ_CLRSPC_SRGB: imageType = J2K_TYPE_RGB; break; case OPJ_CLRSPC_GRAY: imageType = J2K_TYPE_MONO; break; default: imageType = J2K_TYPE_UNKNOWN; } if (!(impl->container = j2k_Container_construct(image->x1 - image->x0, image->y1 - image->y0, image->numcomps, components, tileWidth, tileHeight, imageType, error))) { goto CATCH_ERROR; } } goto CLEANUP; CATCH_ERROR: { rc = NRT_FAILURE; } CLEANUP: { opj_destroy_cstr_info(&codeStreamInfo); OpenJPEG_cleanup(&stream, &codec, &image); } return rc; }