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

  // Update our timestamp
  updateTimestamp( filename );

  // Set our error handlers
  kdu_customize_warnings(&pretty_cout);
  kdu_customize_errors(&pretty_cerr);

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

  // Open the JPX or JP2 file
  try{
    src.open( filename.c_str(), true );
    if( jpx_input.open( &src, false ) != 1 ) throw 1;
  }
  catch (...){
    throw file_error( "Kakadu :: Unable to open '"+filename+"'"); // Rethrow the exception
  }


  // Get our JPX codestream
  try{
    jpx_stream = jpx_input.access_codestream(0);
    if( !jpx_stream.exists() ) throw 1;
  }
  catch (...){
    throw file_error( "Kakadu :: No codestream in file '"+filename+"'"); // Rethrow exception
  }


  // Open the underlying JPEG2000 codestream
  input = NULL;
  input = jpx_stream.open_stream();

  // Create codestream
  codestream.create(input);
  if( !codestream.exists() ) throw file_error( "Kakadu :: Unable to create codestream for '"+filename+"'"); // Throw exception

  // Set up the cache size and allow restarting
  //codestream.augment_cache_threshold(1024);

  // Set Kakadu read mode
  switch( kdu_readmode ) {
    case KDU_FUSSY:
      codestream.set_fussy();
      break;
    case KDU_RESILIENT:
      codestream.set_resilient();
      break;
    case KDU_FAST:
    default:
      codestream.set_fast();
  }

  codestream.set_persistent();
  //  codestream.enable_restart();

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

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

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

   
  updateTimestamp( filename );

   
  kdu_customize_warnings(&pretty_cout);
  kdu_customize_errors(&pretty_cerr);

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

   
  try{
    src.open( filename.c_str(), true );
    if( jpx_input.open( &src, false ) != 1 ) throw 1;
  }
  catch (...){
    throw file_error( "Kakadu :: Unable to open '"+filename+"'");  
  }


   
  try{
    jpx_stream = jpx_input.access_codestream(0);
    if( !jpx_stream.exists() ) throw 1;
  }
  catch (...){
    throw file_error( "Kakadu :: No codestream in file '"+filename+"'");  
  }


   
  input = NULL;
  input = jpx_stream.open_stream();

   
  codestream.create(input);
  if( !codestream.exists() ) throw file_error( "Kakadu :: Unable to create codestream for '"+filename+"'");  

   
   

   
  switch( kdu_readmode ) {
    case KDU_FUSSY:
      codestream.set_fussy();
      break;
    case KDU_RESILIENT:
      codestream.set_resilient();
      break;
    case KDU_FAST:
    default:
      codestream.set_fast();
  }

  codestream.set_persistent();
   

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

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

}