Use resolution level dimensions provided in the info.json "sizes" field to determine tile sizes as well as the number of tiles that exist at a particular resolution. Fall back to calculation using ceil() if no resolution sizes provided. Avoids rounding errors for edge tiles and fixes https://github.com/openseadragon/openseadragon/issues/2321

This commit is contained in:
Ruven 2023-04-17 21:08:18 +02:00
parent 60a6a610e0
commit 0c358c140d
No known key found for this signature in database
GPG Key ID: B54C2EF1C087BEFB

View File

@ -2,7 +2,7 @@
* OpenSeadragon - IIIFTileSource * OpenSeadragon - IIIFTileSource
* *
* Copyright (C) 2009 CodePlex Foundation * Copyright (C) 2009 CodePlex Foundation
* Copyright (C) 2010-2022 OpenSeadragon contributors * Copyright (C) 2010-2023 OpenSeadragon contributors
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are * modification, are permitted provided that the following conditions are
@ -333,7 +333,45 @@ $.extend( $.IIIFTileSource.prototype, $.TileSource.prototype, /** @lends OpenSea
} }
} }
return $.TileSource.prototype.getNumTiles.call(this, level); // Use supplied list of scaled resolution sizes if these exist
var levelSize = this.getLevelSize(level);
if( levelSize ) {
var x = Math.ceil( levelSize.width / this.getTileWidth(level) ),
y = Math.ceil( levelSize.height / this.getTileHeight(level) );
return new $.Point( x, y );
}
// Otherwise call default TileSource->getNumTiles() function
else {
return $.TileSource.prototype.getNumTiles.call(this, level);
}
},
/**
* Determine image size at a given resolution level using the info.json "sizes" field
* Returns null if this information is not present
* @function {Number} level
*/
getLevelSize: function( level ) {
var numLevels = this.maxLevel - this.minLevel;
// Need to take into account that the list may or may not include the full resolution size
if( this.sizes && ((this.sizes.length === numLevels) ||
(this.sizes.length === numLevels + 1)) ) {
var levelWidth, levelHeight;
if( this.sizes.length === numLevels ) {
levelWidth = (level === this.sizes.length) ? this.width : this.sizes[level].width;
levelHeight = (level === this.sizes.length) ? this.height : this.sizes[level].height;
}
else {
levelWidth = this.sizes[level].width;
levelHeight = this.sizes[level].height;
}
return { width: levelWidth, height: levelHeight };
}
else {
return null;
}
}, },
@ -375,10 +413,9 @@ $.extend( $.IIIFTileSource.prototype, $.TileSource.prototype, /** @lends OpenSea
var IIIF_ROTATION = '0', var IIIF_ROTATION = '0',
//## get the scale (level as a decimal) //## get the scale (level as a decimal)
scale = Math.pow( 0.5, this.maxLevel - level ), scale = Math.pow( 0.5, this.maxLevel - level ),
//# image dimensions at this level //# image dimensions at this level
levelWidth = Math.round( this.width * scale ), levelWidth,
levelHeight = Math.round( this.height * scale ), levelHeight,
//## iiif region //## iiif region
tileWidth, tileWidth,
@ -396,6 +433,18 @@ $.extend( $.IIIFTileSource.prototype, $.TileSource.prototype, /** @lends OpenSea
iiifQuality, iiifQuality,
uri; uri;
// Use supplied list of scaled resolution sizes if these exist
var levelSize = this.getLevelSize( level );
if( levelSize ) {
levelWidth = levelSize.width;
levelHeight = levelSize.height;
}
// Otherwise calculate the sizes ourselves
else {
levelWidth = Math.ceil( this.width * scale );
levelHeight = Math.ceil( this.height * scale );
}
tileWidth = this.getTileWidth(level); tileWidth = this.getTileWidth(level);
tileHeight = this.getTileHeight(level); tileHeight = this.getTileHeight(level);
iiifTileSizeWidth = Math.round( tileWidth / scale ); iiifTileSizeWidth = Math.round( tileWidth / scale );
@ -426,8 +475,8 @@ $.extend( $.IIIFTileSource.prototype, $.TileSource.prototype, /** @lends OpenSea
} else { } else {
iiifRegion = [ iiifTileX, iiifTileY, iiifTileW, iiifTileH ].join( ',' ); iiifRegion = [ iiifTileX, iiifTileY, iiifTileW, iiifTileH ].join( ',' );
} }
iiifSizeW = Math.round( iiifTileW * scale ); iiifSizeW = Math.min( tileWidth, levelWidth - (x * tileWidth) );
iiifSizeH = Math.round( iiifTileH * scale ); iiifSizeH = Math.min( tileHeight, levelHeight - (y * tileHeight) );
if ( this.version === 2 && iiifSizeW === this.width ) { if ( this.version === 2 && iiifSizeW === this.width ) {
iiifSize = "full"; iiifSize = "full";
} else if ( this.version === 3 && iiifSizeW === this.width && iiifSizeH === this.height ) { } else if ( this.version === 3 && iiifSizeW === this.width && iiifSizeH === this.height ) {