/* Set the n-th directory as the current directory. NB: Directories are numbered starting at 0.  */
int TIFFSetDirectory(TIFF *tif, uint16 dirn)
/* Set the n-th directory as the current directory. NB: Directories are numbered starting at 0.  */
int TIFFSetDirectory(TIFF *tif, uint16 dirn)
{
	uint64 nextdir;
	uint16 n;
	if (!(tif->tif_flags&TIFF_BIGTIFF))
		nextdir = tif->tif_header.classic.tiff_diroff;
	else
		nextdir = tif->tif_header.big.tiff_diroff;
	for (n = dirn; n > 0 && nextdir != 0; n--)
		if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
			return (0);
	tif->tif_nextdiroff = nextdir;
	tif->tif_curdir = (dirn - n) - 1;
	tif->tif_dirnumber = 0;
	return (TIFFReadDirectory(tif));
}