void OpenJPEGImage::openImage()
{
  string filename = getFileName( currentX, currentY );

  // Update our timestamp
  updateTimestamp( filename );

  // Create decompression codec
  _codec = opj_create_decompress( OPJ_CODEC_JP2 );

  // Set info, warning and error handlers for codec
#ifdef DEBUG
  opj_set_info_handler( _codec, info_callback, NULL );
  opj_set_warning_handler( _codec, warning_callback, NULL );
#endif
  opj_set_error_handler( _codec, error_callback, NULL );

  // Setup decoder
  opj_dparameters_t parameters; // Set default decoder parameters
  opj_set_default_decoder_parameters( &parameters );
  if( !opj_setup_decoder( _codec, &parameters ) ){
    throw file_error( "OpenJPEG :: openImage() :: error setting up decoder" );
  }

#ifdef DEBUG
  Timer timer;
  timer.start();
#endif

  // Open the JPEG2000 file in read mode
  if( !(_stream = opj_stream_create_default_file_stream( filename.c_str(), true) ) ){
    throw file_error( "OpenJPEG :: Unable to open '" + filename + "'" );
  }

#ifdef DEBUG
  logfile << "OpenJPEG :: openImage() :: " << "Stream created" << endl;
#endif

  // Read header
  if( !opj_read_header( _stream, _codec, &_image ) ){
    throw file_error( "OpenJPEG :: process() :: opj_read_header() failed" );
  }

#ifdef DEBUG
  logfile << "OpenJPEG :: openImage() :: " << "Header read" << endl;
#endif

  // Load our metadata if not already loaded
  if( bpc == 0 ) loadImageInfo( currentX, currentY );

#ifdef DEBUG
  logfile << "OpenJPEG :: openImage() :: " << timer.getTime() << " microseconds" << endl;
#endif

}
void OpenJPEGImage::openImage()
{
  string filename = getFileName( currentX, currentY );

   
  updateTimestamp( filename );

   
  _codec = opj_create_decompress( OPJ_CODEC_JP2 );

   
#ifdef DEBUG
  opj_set_info_handler( _codec, info_callback, NULL );
  opj_set_warning_handler( _codec, warning_callback, NULL );
#endif
  opj_set_error_handler( _codec, error_callback, NULL );

   
  opj_dparameters_t parameters;  
  opj_set_default_decoder_parameters( &parameters );
  if( !opj_setup_decoder( _codec, &parameters ) ){
    throw file_error( "OpenJPEG :: openImage() :: error setting up decoder" );
  }

#ifdef DEBUG
  Timer timer;
  timer.start();
#endif

   
  if( !(_stream = opj_stream_create_default_file_stream( filename.c_str(), true) ) ){
    throw file_error( "OpenJPEG :: Unable to open '" + filename + "'" );
  }

#ifdef DEBUG
  logfile << "OpenJPEG :: openImage() :: " << "Stream created" << endl;
#endif

   
  if( !opj_read_header( _stream, _codec, &_image ) ){
    throw file_error( "OpenJPEG :: process() :: opj_read_header() failed" );
  }

#ifdef DEBUG
  logfile << "OpenJPEG :: openImage() :: " << "Header read" << endl;
#endif

   
  if( bpc == 0 ) loadImageInfo( currentX, currentY );

#ifdef DEBUG
  logfile << "OpenJPEG :: openImage() :: " << timer.getTime() << " microseconds" << endl;
#endif

}