64 static OPJ_SIZE_T ossim_opj_istream_read(
void * p_buffer,
65 OPJ_SIZE_T p_nb_bytes,
74 std::streamsize bytesToRead = ossim::min<std::streamsize>(
75 (std::streamsize)p_nb_bytes,
76 (std::streamsize)(usrStr->
m_length - usrStr->
m_str->tellg() ) );
78 usrStr->
m_str->read( (
char*) p_buffer, bytesToRead );
79 count = usrStr->
m_str->gcount();
81 if ( usrStr->
m_str->eof() )
84 usrStr->
m_str->clear();
87 else if ( !usrStr->
m_str->good() )
89 usrStr->
m_str->clear();
99 static OPJ_OFF_T ossim_opj_istream_skip(OPJ_OFF_T p_nb_bytes,
void * p_user_data)
101 OPJ_OFF_T skipped = p_nb_bytes;
107 std::streampos p1 = usrStr->
m_str->tellg();
108 usrStr->
m_str->seekg( p_nb_bytes, std::ios_base::cur );
109 std::streampos p2 = usrStr->
m_str->tellg();
110 if ( ( p2 - p1 ) != p_nb_bytes )
120 static OPJ_BOOL ossim_opj_istream_seek(OPJ_OFF_T p_nb_bytes,
void * p_user_data)
127 usrStr->
m_str->seekg( p_nb_bytes, std::ios_base::cur );
133 static void ossim_opj_free_user_istream_data(
void * p_user_data )
147 std::streamoff fileOffset,
150 static const char MODULE[] =
"ossimOpjDecoder::decode";
157 << MODULE <<
"entered...\nrect: " << rect
158 <<
"\nresLevel: " << resLevel << std::endl;
162 if ( in && tile && !rect.
hasNans())
164 in->seekg( fileOffset, std::ios_base::beg );
166 opj_dparameters_t param;
167 opj_codec_t* codec = 0;
168 opj_image_t* image = 0;;
169 opj_stream_t* stream = 0;
172 userStream->
m_str = in;
176 in->seekg(0, std::ios_base::end);
183 in->seekg(fileOffset, std::ios_base::beg);
185 stream = opj_stream_default_create(OPJ_TRUE);
188 opj_stream_destroy(stream);
189 std::string errMsg = MODULE;
190 errMsg +=
" ERROR: opj_setup_decoder failed!";
194 opj_stream_set_read_function(stream, ossim_opj_istream_read);
195 opj_stream_set_skip_function(stream, ossim_opj_istream_skip);
196 opj_stream_set_seek_function(stream, ossim_opj_istream_seek);
199 opj_stream_set_user_data_length(stream, userStream->
m_length);
201 opj_stream_set_user_data(stream, userStream,
202 ossim_opj_free_user_istream_data);
204 opj_stream_set_user_data_length(stream, userStream->
m_length);
208 opj_set_default_decoder_parameters(¶m);
210 param.decod_format = format;
217 param.cp_reduce = resLevel;
219 codec = opj_create_decompress( (CODEC_FORMAT)format );
223 opj_set_info_handler (codec, NULL, 00);
228 if ( opj_setup_decoder(codec, ¶m) ==
false )
230 opj_stream_destroy(stream);
231 opj_destroy_codec(codec);
232 std::string errMsg = MODULE;
233 errMsg +=
" ERROR: opj_setup_decoder failed!";
238 if ( opj_read_header(stream, codec, &image) ==
false )
240 opj_stream_destroy(stream);
241 opj_destroy_codec(codec);
242 opj_image_destroy(image);
243 std::string errMsg = MODULE;
244 errMsg +=
" ERROR: opj_read_header failed!";
252 if ( opj_set_decoded_resolution_factor(codec, resLevel) ==
false)
254 opj_stream_destroy(stream);
255 opj_destroy_codec(codec);
256 opj_image_destroy(image);
257 std::string errMsg = MODULE;
258 errMsg +=
" ERROR: opj_set_decoded_resolution_factor failed!";
271 if ( opj_set_decode_area(codec, image, resRect.
ul().
x, resRect.
ul().
y,
272 resRect.
lr().
x+1, resRect.
lr().
y+1) ==
false )
274 opj_stream_destroy(stream);
275 opj_destroy_codec(codec);
276 opj_image_destroy(image);
277 std::string errMsg = MODULE;
278 errMsg +=
" ERROR: opj_set_decode_area failed!";
283 if (
opj_decode(codec, stream, image) ==
false )
285 opj_stream_destroy(stream);
286 opj_destroy_codec(codec);
287 opj_image_destroy(image);
288 std::string errMsg = MODULE;
289 errMsg +=
" ERROR: opj_decode failed!";
294 if(image->icc_profile_buf) {
295 #if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2) 298 free(image->icc_profile_buf);
299 image->icc_profile_buf = NULL; image->icc_profile_len = 0;
314 if ( opj_read_tile_header( codec,
325 opj_stream_destroy(stream);
326 opj_destroy_codec(codec);
327 opj_image_destroy(image);
328 std::string errMsg = MODULE;
329 errMsg +=
" ERROR: opj_read_tile_header failed!";
335 std::cout <<
"tile_index: " << tile_index
336 <<
"\ndata_size: " << data_size
337 <<
"\ncurrent_tile_x0: " << current_tile_x0
338 <<
"\ncurrent_tile_y0: " << current_tile_y0
339 <<
"\ncurrent_tile_x1: " << current_tile_x1
340 <<
"\ncurrent_tile_y1: " << current_tile_y1
341 <<
"\nnb_comps: " << nb_comps
346 if ( opj_decode_tile_data(codec, tile_index,l_data,l_data_size,l_stream) ==
false)
348 opj_stream_destroy(l_stream);
349 opj_destroy_codec(l_codec);
350 opj_image_destroy(l_image);
351 std::string errMsg = MODULE;
352 errMsg +=
" ERROR: opj_read_tile_header failed!";
359 if (opj_end_decompress(codec,stream) ==
false )
361 opj_stream_destroy(stream);
362 opj_destroy_codec(codec);
363 opj_image_destroy(image);
364 std::string errMsg = MODULE;
365 errMsg +=
" ERROR: opj_end_decompress failed!";
372 opj_stream_destroy(stream);
373 opj_destroy_codec(codec);
374 opj_image_destroy(image);
381 in->seekg(fileOffset, std::ios_base::beg );
395 if ( image->color_space == OPJ_CLRSPC_SRGB )
405 <<
"ossim::copyOpjImage WARNING!\nUnhandle scalar: " 412 <<
"ossim::copyOpjImage WARNING!\nUnhandle color space: " 413 << image->color_space <<
"\n";
428 if ( image->numcomps == BANDS )
432 T* buf = (T*)tile->
getBuf(band);
435 if ( image->comps[band].h == LINES )
441 if ( image->comps[band].w == SAMPS )
445 buf[offset] = (T)(image->comps[band].data[offset]);
458 <<
"ossim::copyOpjSrgbImage WARNING: sample mismatch!\n";
465 <<
"ossim::copyOpjSrgbImage WARNING: line mismatch!\n";
472 <<
"ossim::copyOpjSrgbImage WARNING: band mismatch!\n";
488 { 0x00,0x00,0x00,0x0c,0x6a,0x50,0x20,0x20,0x0d,0x0a,0x87,0x0a };
489 const ossim_uint8 JP2_MAGIC[4] = { 0x0d,0x0a,0x87,0x0a };
490 const ossim_uint8 J2K_CODESTREAM_MAGIC[4] = { 0xff,0x4f,0xff,0x51 };
495 str->read((
char*)buf, 12);
497 if ( ( std::memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 ) ||
498 ( std::memcmp(buf, JP2_MAGIC, 4) == 0 ) )
500 result = OPJ_CODEC_JP2;
502 else if ( std::memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0 )
504 result = OPJ_CODEC_J2K;
515 std::ios_base::fmtflags f = out.flags();
519 out << std::setiosflags(std::ios_base::right) << std::setfill(
' ')
520 <<
"opj_codestream_info:\n\n" 521 << std::setw(W) <<
"D_max: " 522 << info.D_max <<
"\n" 523 << std::setw(W) <<
"packno: " 524 << info.packno <<
"\n" 525 << std::setw(W) <<
"index_write: " 526 <<info.index_write <<
"\n" 527 << std::setw(W) <<
"image_w: " 528 << info.image_w <<
"\n" 529 << std::setw(W) <<
"image_h: " 530 << info.image_h <<
"\n" 531 << std::setw(W) <<
"proj: " 533 << std::setw(W) <<
"tile_x: " 534 << info.tile_x <<
"\n" 535 << std::setw(W) <<
"tile_y: " 536 << info.tile_y <<
"\n" 537 << std::setw(W) <<
"tile_Ox: " 538 << info.tile_Ox <<
"\n" 539 << std::setw(W) <<
"tile_Oy: " 540 << info.tile_Oy <<
"\n" 541 << std::setw(W) <<
"tw: " 543 << std::setw(W) <<
"th: " 545 << std::setw(W) <<
"numcomps: " 546 << info.numcomps <<
"\n" 547 << std::setw(W) <<
"numlayers: " 548 << info.numlayers <<
"\n";
550 for (
int i = 0; i < info.numcomps; ++i)
552 std::string s =
"numdecompos[";
555 out << std::setw(W) << s << info.numdecompos[i] <<
"\n";
558 out << std::setw(W) <<
"marknum: " 559 << info.marknum <<
"\n" 561 << std::setw(W) <<
"maxmarknum: " 562 << info.maxmarknum <<
"\n" 563 << std::setw(W) <<
"main_head_start: " 564 << info.main_head_start <<
"\n" 565 << std::setw(W) <<
"main_head_end: " 566 << info.main_head_end <<
"\n" 567 << std::setw(W) <<
"codestream_size: " 568 << info.codestream_size <<
"\n" 581 std::ios_base::fmtflags f = out.flags();
586 out << std::setiosflags(std::ios_base::left) << std::setfill(
' ')
587 <<
"opj_cparameters:\n\n" 589 << std::setw(W) <<
"tile_size_on:" 590 << param.tile_size_on <<
"\n" 592 << std::setw(W) <<
"cp_tx0: " 593 << param.cp_tx0 <<
"\n" 595 << std::setw(W) <<
"cp_ty0: " 596 << param.cp_ty0 <<
"\n" 598 << std::setw(W) <<
"cp_tdx: " 599 << param.cp_tdx <<
"\n" 601 << std::setw(W) <<
"cp_tdy: " 602 << param.cp_tdy <<
"\n" 604 << std::setw(W) <<
"cp_disto_alloc: " 605 << param.cp_disto_alloc <<
"\n" 607 << std::setw(W) <<
"cp_fixed_alloc: " 608 << param.cp_fixed_alloc <<
"\n" 610 << std::setw(W) <<
"cp_fixed_quality: " 611 << param.cp_fixed_quality <<
"\n" 617 << std::setw(W) <<
"csty:" 618 << param.csty <<
"\n" 620 << std::setw(W) <<
"prog_order: " 625 << std::setw(W) <<
"numpocs:" 626 << param.numpocs <<
"\n" 628 << std::setw(W) <<
"tcp_numlayers:" 629 << param.tcp_numlayers <<
"\n";
631 for ( i = 0; i < param.tcp_numlayers; ++i )
633 out <<
"tcp_rate[" << i << std::setw(14)<<
"]: "<< std::setw(4) << param.tcp_rates[i] <<
"\n" 634 <<
"tcp_distoratio[" << i << std::setw(8) <<
"]: " << std::setw(4) << param.tcp_distoratio[i] <<
"\n";
637 out << std::setw(W) <<
"numresolution:" 638 << param.numresolution <<
"\n" 640 << std::setw(W) <<
"cblockw_init:" 641 << param.cblockw_init <<
"\n" 643 << std::setw(W) <<
"cblockh_init:" 644 << param.cblockh_init <<
"\n" 646 << std::setw(W) <<
"mode:" 647 << param.mode <<
"\n" 649 << std::setw(W) <<
"irreversible:" 650 << param.irreversible <<
"\n" 652 << std::setw(W) <<
"roi_compn:" 653 << param.roi_compno <<
"\n" 655 << std::setw(W) <<
"roi_shift:" 656 << param.roi_shift <<
"\n" 658 << std::setw(W) <<
"res_spec:" 659 << param.res_spec <<
"\n" 670 << std::setw(W) <<
"outfile:" 671 << param.outfile <<
"\n" 679 << std::setw(W) <<
"image_offset_x0:" 680 << param.image_offset_x0 <<
"\n" 682 << std::setw(W) <<
"image_offset_y0:" 683 << param.image_offset_y0 <<
"\n" 685 << std::setw(W) <<
"subsampling_dx:" 686 << param.subsampling_dx <<
"\n" 688 << std::setw(W) <<
"image_offset_dy:" 689 << param.subsampling_dy <<
"\n" 691 << std::setw(W) <<
"decod_format:" 692 << param.decod_format <<
"\n" 694 << std::setw(W) <<
"cod_format:" 695 << param.cod_format <<
"\n" 697 << std::setw(W) <<
"jpwl_epc_on:" 698 << param.jpwl_epc_on <<
"\n" 700 << std::setw(W) <<
"jpwl_hprot_MH:" 701 << param.jpwl_hprot_MH <<
"\n" 703 <<
"jpwl_hprot_TPH_tileno:\n" 705 <<
"jpwl_pprot_TPH:\n" 707 <<
"jpwl_pprot_tileno:\n" 709 <<
"jpwl_pprot_packno:\n" 713 << std::setw(W) <<
"jpwl_sens_size:" 714 << param.jpwl_sens_size <<
"\n" 716 << std::setw(W) <<
"jpwl_sens_addr:" 717 << param.jpwl_sens_addr <<
"\n" 719 << std::setw(W) <<
"jpwl_sens_range:" 720 << param.jpwl_sens_range <<
"\n" 722 << std::setw(W) <<
"jpwl_sens_MH:" 723 << param.jpwl_sens_MH <<
"\n" 725 <<
"jpwl_sens_TPH_tileno:\n" 727 <<
"jpwl_sens_TPH:\n" 731 << std::setw(W) <<
"max_comp_size:" 732 << param.max_comp_size <<
"\n" 734 << std::setw(W) <<
"cp_rsiz:" 735 << param.cp_rsiz <<
"\n" 737 << std::setw(W) <<
"tp_on:" 738 << param.tp_on <<
"\n" 740 << std::setw(W) <<
"tp_flag:" 741 << param.tp_flag <<
"\n" 743 << std::setw(W) <<
"tcp_mct:" 744 << int(param.tcp_mct) <<
"\n" 746 << std::setw(W) <<
"jpip_on:" 747 << param.jpip_on <<
"\n" 751 << std::setw(W) <<
"max_cs_size:" 752 << param.max_cs_size <<
"\n" 754 << std::setw(W) <<
"rsiz:" 755 << param.rsiz <<
"\n" 768 std::ios_base::fmtflags f = out.flags();
772 out << std::setiosflags(std::ios_base::right) << std::setfill(
' ')
773 <<
"opj_dparameters:\n\n" 774 << std::setw(W) <<
"cp_reduce: " 775 << param.cp_reduce <<
"\n" 776 << std::setw(W) <<
"cp_layer: " 777 << param.cp_layer <<
"\n" 778 << std::setw(W) <<
"infile: " 779 << param.infile <<
"\n" 780 << std::setw(W) <<
"outfile: " 781 << param.outfile <<
"\n" 782 << std::setw(W) <<
"decod_format: " 783 << param.decod_format <<
"\n" 784 << std::setw(W) <<
"cod_format: " 785 << param.cod_format <<
"\n" 786 << std::setw(W) <<
"jpwl_correct: " 787 << param.jpwl_correct <<
"\n" 788 << std::setw(W) <<
"jpwl_exp_comps: " 789 << param.jpwl_exp_comps <<
"\n" 790 << std::setw(W) <<
"jpwl_max_tiles: " 791 << param.jpwl_max_tiles <<
"\n" 792 << std::setw(W) <<
"cp_limit_decoding: " 804 std::ios_base::fmtflags f = out.flags();
808 out << std::setiosflags(std::ios_base::right) << std::setfill(
' ')
810 << std::setw(W) <<
"x0: " 812 << std::setw(W) <<
"y0: " 814 << std::setw(W) <<
"x1: " 816 << std::setw(W) <<
"y1: " 818 << std::setw(W) <<
"numcomps: " 819 << image.numcomps <<
"\n" 820 << std::setw(W) <<
"color_space: ";
821 if ( image.color_space == OPJ_CLRSPC_UNKNOWN ) out <<
"UNKNOWN\n";
822 else if ( image.color_space == OPJ_CLRSPC_UNSPECIFIED) out <<
"UNSPECIFIED\n";
823 else if ( image.color_space == OPJ_CLRSPC_SRGB) out <<
"SRGB\n";
824 else if ( image.color_space == OPJ_CLRSPC_GRAY) out <<
"GRAY\n";
825 else if ( image.color_space == OPJ_CLRSPC_SYCC) out <<
"SYCC\n";
826 else if ( image.color_space == OPJ_CLRSPC_EYCC) out <<
"EYCC\n";
827 else if ( image.color_space == OPJ_CLRSPC_CMYK) out <<
"CMYK\n";
831 std::string s =
"comps[";
834 out << s << std::endl;
836 print(out, (image.comps[i]));
850 std::ios_base::fmtflags f = out.flags();
854 out << std::setiosflags(std::ios_base::right) << std::setfill(
' ')
855 <<
"opj_image_comp:\n\n" 856 << std::setw(W) <<
"dx: " 858 << std::setw(W) <<
"dy: " 860 << std::setw(W) <<
"w: " 862 << std::setw(W) <<
"h: " 864 << std::setw(W) <<
"x0: " 866 << std::setw(W) <<
"y0: " 868 << std::setw(W) <<
"prec: " 870 << std::setw(W) <<
"bpp: " 872 << std::setw(W) <<
"sgnd: " 874 << std::setw(W) <<
"resno_decoded: " 875 << comp.resno_decoded <<
"\n" 876 << std::setw(W) <<
"factor: " 888 std::string result =
"";
virtual ossim_uint32 getWidth() const
virtual ossim_uint32 getNumberOfBands() const
std::basic_ifstream< char > ifstream
Class for char input file streams.
static ossimString toString(bool aValue)
Numeric to string methods.
const ossimIpt & ul() const
virtual ossim_uint32 getHeight() const
std::ostream & print(H5::H5File *file, std::ostream &out)
Print method.
void opj_warning_callback(const char *msg, void *)
Callback method for warnings.
void color_apply_icc_profile(opj_image *image)
std::ostream & print(std::ostream &out, kdu_core::kdu_codestream &cs)
Convenience print method for kdu_codestream.
unsigned int ossim_uint32
void opj_info_callback(const char *msg, void *)
Callback method for info.
bool copyOpjSrgbImage(T dummy, opj_image *image, ossimImageData *tile)
const ossimIpt & lr() const
bool opj_decode(std::ifstream *in, const ossimIrect &rect, ossim_uint32 resLevel, ossim_int32 format, std::streamoff fileOffset, ossimImageData *tile)
std::string getProgressionOrderString(ossim_int32 prog_order)
virtual ossimScalarType getScalarType() const
std::basic_istream< char > istream
Base class for char input streams.
To hold stream and offset.
virtual const void * getBuf() const
void opj_error_callback(const char *msg, void *)
Callback method for errors.
ossim_int32 getCodecFormat(std::istream *str)
Gets codec format from magic number.
bool copyOpjImage(opj_image *image, ossimImageData *tile)
unsigned char ossim_uint8
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
std::basic_ostream< char > ostream
Base class for char output streams.
const std::string & string() const