RawTile TileManager::getNewTile( int resolution, int tile, int xangle, int yangle, int layers, CompressionType ctype ){

  RawTile ttt;

  // Get a raw tile from the IIPImage image object
  ttt = image->getTile( xangle, yangle, resolution, layers, tile );


  // Apply the watermark if we have one.
  // Do this before inserting into cache so that we cache watermarked tiles
  if( watermark && watermark->isSet() ){

    if( loglevel >= 4 ) insert_timer.start();
    unsigned int tw = ttt.padded? image->getTileWidth() : ttt.width;
    unsigned int th = ttt.padded? image->getTileHeight() : ttt.height;

    watermark->apply( ttt.data, tw, th, ttt.channels, ttt.bpc );
    if( loglevel >= 4 ) *logfile << "TileManager :: Watermark applied: " << insert_timer.getTime()
				 << " microseconds" << endl;
  }


  // We need to crop our edge tiles if they are padded
  if( ((ttt.width != image->getTileWidth()) || (ttt.height != image->getTileHeight())) && ttt.padded ){
    if( loglevel >= 5 ) * logfile << "TileManager :: Cropping tile" << endl;
    this->crop( &ttt );
  }


  // Add our uncompressed tile directly into our cache
  if( ctype == UNCOMPRESSED ){
    // Add to our tile cache
    if( loglevel >= 4 ) insert_timer.start();
    tileCache->insert( ttt );
    if( loglevel >= 4 ) *logfile << "TileManager :: Tile cache insertion time: " << insert_timer.getTime()
				 << " microseconds" << endl;
    return ttt;
  }


  switch( ctype ){

   case JPEG:
    // Do our JPEG compression iff we have an 8 bit per channel image
    if( ttt.bpc == 8 && (ttt.channels==1 || ttt.channels==3) ){
      if( loglevel >= 4 ) compression_timer.start();
      compressor->Compress( ttt );
      if( loglevel >= 4 ) *logfile << "TileManager :: JPEG Compression Time: "
				   << compression_timer.getTime() << " microseconds" << endl;
    }
    break;


   case PNG:
    if( loglevel >=2 ) compression_timer.start();
    compressor->Compress( ttt );
    if( loglevel >= 2 ) *logfile << "TileManager :: PNG Compression Time: "
				 << compression_timer.getTime() << " microseconds" << endl;
    break;


   case DEFLATE:
    // No deflate for the time being ;-)
    if( loglevel >= 4 ) *logfile << "TileManager :: DEFLATE Compression requested: Not currently available" << endl;
    break;


   default:
     break;

  }


  // Add to our tile cache
  if( loglevel >= 4 ) insert_timer.start();
  tileCache->insert( ttt );
  if( loglevel >= 4 ) *logfile << "TileManager :: Tile cache insertion time: " << insert_timer.getTime()
			       << " microseconds" << endl;


  return ttt;

}
RawTile TileManager::getNewTile( int resolution, int tile, int xangle, int yangle, int layers, CompressionType ctype ){

  RawTile ttt;

   
  ttt = image->getTile( xangle, yangle, resolution, layers, tile );


   
   
  if( watermark && watermark->isSet() ){

    if( loglevel >= 4 ) insert_timer.start();
    unsigned int tw = ttt.padded? image->getTileWidth() : ttt.width;
    unsigned int th = ttt.padded? image->getTileHeight() : ttt.height;

    watermark->apply( ttt.data, tw, th, ttt.channels, ttt.bpc );
    if( loglevel >= 4 ) *logfile << "TileManager :: Watermark applied: " << insert_timer.getTime()
				 << " microseconds" << endl;
  }


   
  if( ((ttt.width != image->getTileWidth()) || (ttt.height != image->getTileHeight())) && ttt.padded ){
    if( loglevel >= 5 ) * logfile << "TileManager :: Cropping tile" << endl;
    this->crop( &ttt );
  }


   
  if( ctype == UNCOMPRESSED ){
     
    if( loglevel >= 4 ) insert_timer.start();
    tileCache->insert( ttt );
    if( loglevel >= 4 ) *logfile << "TileManager :: Tile cache insertion time: " << insert_timer.getTime()
				 << " microseconds" << endl;
    return ttt;
  }


  switch( ctype ){

   case JPEG:
     
    if( ttt.bpc == 8 && (ttt.channels==1 || ttt.channels==3) ){
      if( loglevel >= 4 ) compression_timer.start();
      compressor->Compress( ttt );
      if( loglevel >= 4 ) *logfile << "TileManager :: JPEG Compression Time: "
				   << compression_timer.getTime() << " microseconds" << endl;
    }
    break;


   case PNG:
    if( loglevel >=2 ) compression_timer.start();
    compressor->Compress( ttt );
    if( loglevel >= 2 ) *logfile << "TileManager :: PNG Compression Time: "
				 << compression_timer.getTime() << " microseconds" << endl;
    break;


   case DEFLATE:
     
    if( loglevel >= 4 ) *logfile << "TileManager :: DEFLATE Compression requested: Not currently available" << endl;
    break;


   default:
     break;

  }


   
  if( loglevel >= 4 ) insert_timer.start();
  tileCache->insert( ttt );
  if( loglevel >= 4 ) *logfile << "TileManager :: Tile cache insertion time: " << insert_timer.getTime()
			       << " microseconds" << endl;


  return ttt;

}