From 2c4440b5a2b286d51816965eff55932e309bf46d Mon Sep 17 00:00:00 2001 From: Rob Sanderson Date: Fri, 25 Jul 2014 16:31:13 -0700 Subject: [PATCH] Combined IIIF tilesource and dynamic tileSize --- Gruntfile.js | 3 +- src/iiif1_1tilesource.js | 192 ---------------------- src/iiifmultitilesource.js | 300 +++++++++++++++++++++++++++++++++ src/iiiftilesource.js | 329 ------------------------------------- src/tilesource.js | 28 +++- 5 files changed, 320 insertions(+), 532 deletions(-) delete mode 100644 src/iiif1_1tilesource.js create mode 100644 src/iiifmultitilesource.js delete mode 100644 src/iiiftilesource.js diff --git a/Gruntfile.js b/Gruntfile.js index 0cd48304..c1cdc909 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -33,8 +33,7 @@ module.exports = function(grunt) { //"src/profiler.js", "src/tilesource.js", "src/dzitilesource.js", - "src/iiiftilesource.js", - "src/iiif1_1tilesource.js", + "src/iiifmultitilesource.js", "src/osmtilesource.js", "src/tmstilesource.js", "src/legacytilesource.js", diff --git a/src/iiif1_1tilesource.js b/src/iiif1_1tilesource.js deleted file mode 100644 index 6997ba20..00000000 --- a/src/iiif1_1tilesource.js +++ /dev/null @@ -1,192 +0,0 @@ -/* - * OpenSeadragon - IIIF1_1TileSource - * - * Copyright (C) 2009 CodePlex Foundation - * Copyright (C) 2010-2013 OpenSeadragon contributors - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of CodePlex Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -(function( $ ){ - -/** - * @class IIIF1_1TileSource - * @classdesc A client implementation of the International Image Interoperability - * Format: Image API 1.1 - * - * @memberof OpenSeadragon - * @extends OpenSeadragon.TileSource - * @see http://library.stanford.edu/iiif/image-api/ - */ -$.IIIF1_1TileSource = function( options ){ - - - $.extend( true, this, options ); - - - if ( !( this.height && this.width && this['@id'] ) ){ - throw new Error( 'IIIF required parameters not provided.' ); - } - - if ( ( this.profile && - this.profile == "http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level0" ) ){ - // what if not reporting a profile? - throw new Error( 'IIIF Image API 1.1 compliance level 1 or greater is required.' ); - } - - if ( this.tile_width ) { - options.tileSize = this.tile_width; - } else if ( this.tile_height ) { - options.tileSize = this.tile_height; - } else { - // use the largest of tileOptions that is smaller than the short - // dimension - - var shortDim = Math.min( this.height, this.width ), - tileOptions = [256,512,1024], - smallerTiles = []; - - for ( var c = 0; c < tileOptions.length; c++ ) { - if ( tileOptions[c] <= shortDim ) { - smallerTiles.push( tileOptions[c] ); - } - } - - if ( smallerTiles.length > 0 ) { - options.tileSize = Math.max.apply( null, smallerTiles ); - } else { - // If we're smaller than 256, just use the short side. - options.tileSize = shortDim; - } - this.tile_width = options.tileSize; // So that 'full' gets used for - this.tile_height = options.tileSize; // the region below - } - - if ( !options.maxLevel ) { - var mf = -1; - var scfs = this.scale_factors || this.scale_factor; - if ( scfs instanceof Array ) { - for ( var i = 0; i < scfs.length; i++ ) { - var cf = Number( scfs[i] ); - if ( !isNaN( cf ) && cf > mf ) { mf = cf; } - } - } - if ( mf < 0 ) { options.maxLevel = Number( Math.ceil( Math.log( Math.max( this.width, this.height ), 2 ) ) ); } - else { options.maxLevel = mf; } - } - - $.TileSource.apply( this, [ options ] ); -}; - -$.extend( $.IIIF1_1TileSource.prototype, $.TileSource.prototype, /** @lends OpenSeadragon.IIIF1_1TileSource.prototype */{ - /** - * Determine if the data and/or url imply the image service is supported by - * this tile source. - * @function - * @param {Object|Array} data - * @param {String} optional - url - */ - supports: function( data, url ) { - return ( data['@context'] && - data['@context'] == "http://library.stanford.edu/iiif/image-api/1.1/context.json" ); - }, - - /** - * - * @function - * @param {Object} data - the raw configuration - * @example IIIF 1.1 Info Looks like this (XML syntax is no more) - * { - * "@context" : "http://library.stanford.edu/iiif/image-api/1.1/context.json", - * "@id" : "http://iiif.example.com/prefix/1E34750D-38DB-4825-A38A-B60A345E591C", - * "width" : 6000, - * "height" : 4000, - * "scale_factors" : [ 1, 2, 4 ], - * "tile_width" : 1024, - * "tile_height" : 1024, - * "formats" : [ "jpg", "png" ], - * "qualities" : [ "native", "grey" ], - * "profile" : "http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level0" - * } - */ - configure: function( data ){ - return data; - }, - /** - * Responsible for retreiving the url which will return an image for the - * region specified by the given x, y, and level components. - * @function - * @param {Number} level - z index - * @param {Number} x - * @param {Number} y - * @throws {Error} - */ - getTileUrl: function( level, x, y ){ - - //# constants - - var IIIF_ROTATION = '0', - IIIF_QUALITY = 'native.jpg', - - //## get the scale (level as a decimal) - scale = Math.pow( 0.5, this.maxLevel - level ), - - //# image dimensions at this level - levelWidth = Math.ceil( this.width * scale ), - levelHeight = Math.ceil( this.height * scale ), - - //## iiif region - iiifTileSizeWidth = Math.ceil( this.tileSize / scale ), - iiifTileSizeHeight = Math.ceil( this.tileSize / scale ), - iiifRegion, - iiifTileX, - iiifTileY, - iiifTileW, - iiifTileH, - iiifSize, - uri; - - if ( levelWidth < this.tile_width && levelHeight < this.tile_height ){ - iiifSize = levelWidth + ","; - iiifRegion = 'full'; - } else { - iiifTileX = x * iiifTileSizeWidth; - iiifTileY = y * iiifTileSizeHeight; - iiifTileW = Math.min( iiifTileSizeWidth, this.width - iiifTileX ); - iiifTileH = Math.min( iiifTileSizeHeight, this.height - iiifTileY ); - - iiifSize = Math.ceil( iiifTileW * scale ) + ","; - - iiifRegion = [ iiifTileX, iiifTileY, iiifTileW, iiifTileH ].join( ',' ); - } - uri = [ this['@id'], iiifRegion, iiifSize, IIIF_ROTATION, IIIF_QUALITY ].join( '/' ); - return uri; - } - }); - -}( OpenSeadragon )); diff --git a/src/iiifmultitilesource.js b/src/iiifmultitilesource.js new file mode 100644 index 00000000..73deba73 --- /dev/null +++ b/src/iiifmultitilesource.js @@ -0,0 +1,300 @@ +/* + * OpenSeadragon - IIIFMultiTileSource + * + * Copyright (C) 2010-2013 OpenSeadragon contributors + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of CodePlex Foundation nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +(function( $ ){ + +/** + * @class IIIFMultiTileSource + * @classdesc A client implementation of the International Image Interoperability + * Format: Image API 1.0 - 2.0 + * + * @memberof OpenSeadragon + * @extends OpenSeadragon.TileSource + * @see http://iiif.io/api/image/ + */ +$.IIIFMultiTileSource = function( options ){ + + + $.extend( true, this, options ); + + if ( !( this.height && this.width && this['@id'] ) ) { + throw new Error( 'IIIF required parameters not provided.' ); + } + + options.tileSizePerScaleFactor = {}; + + if ( this.tile_width ) { + options.tileSize = this.tile_width; + } else if ( this.tile_height ) { + options.tileSize = this.tile_height; + } else if ( this.tiles ) { + // Version 2.0 forwards + if ( this.tiles.length == 1 ) { + options.tileSize = this.tiles[0].width; + this.scale_factors = this.tiles[0].scale_factors; + } else { + // Multiple tile sizes at different levels + this.scale_factors = []; + for (var t = 0; t < this.tiles.length; t++ ) { + for (var sf = 0; sf < this.tiles[t].scale_factors.length; sf++) { + var scaleFactor = this.tiles[t].scale_factors[sf]; + this.scale_factors.push(scaleFactor); + options.tileSizePerScaleFactor[scaleFactor] = this.tiles[t].width; + } + } + } + } else { + // use the largest of tileOptions that is smaller than the short dimension + + var shortDim = Math.min( this.height, this.width ), + tileOptions = [256,512,1024], + smallerTiles = []; + + for ( var c = 0; c < tileOptions.length; c++ ) { + if ( tileOptions[c] <= shortDim ) { + smallerTiles.push( tileOptions[c] ); + } + } + + if ( smallerTiles.length > 0 ) { + options.tileSize = Math.max.apply( null, smallerTiles ); + } else { + // If we're smaller than 256, just use the short side. + options.tileSize = shortDim; + } + this.tile_width = options.tileSize; // So that 'full' gets used for + this.tile_height = options.tileSize; // the region below + } + + if ( !options.maxLevel ) { + if ( !this.scale_factors ) { + options.maxLevel = Number( Math.ceil( Math.log( Math.max( this.width, this.height ), 2 ) ) ); + } else { + options.maxLevel = Math.floor( Math.pow( Math.max.apply(null, this.scale_factors), 0.5) ); + } + } + + $.TileSource.apply( this, [ options ] ); +}; + +$.extend( $.IIIFMultiTileSource.prototype, $.TileSource.prototype, /** @lends OpenSeadragon.IIIFMultiTileSource.prototype */{ + /** + * Determine if the data and/or url imply the image service is supported by + * this tile source. + * @function + * @param {Object|Array} data + * @param {String} optional - url + */ + supports: function( data, url ) { + // Version 2.0 and forwards + if (data.protocol && data.protocol == 'http://iiif.io/api/image') { + return true; + // Version 1.1 + } else if ( data['@context'] && ( + data['@context'] == "http://library.stanford.edu/iiif/image-api/1.1/context.json" || + data['@context'] == "http://iiif.io/api/image/1/context.json") ) { + // N.B. the iiif.io context is wrong, but where the representation lives so likely to be used + return true; + + // Version 1.0 + } else if ( data.profile && + data.profile.indexOf("http://library.stanford.edu/iiif/image-api/compliance.html") === 0) { + return true; + } else if ( data.identifier && data.width && data.height ) { + return true; + } else if ( data.documentElement && + "info" == data.documentElement.tagName && + "http://library.stanford.edu/iiif/image-api/ns/" == + data.documentElement.namespaceURI) { + return true; + + // Not IIIF + } else { + return false; + } + }, + + /** + * + * @function + * @param {Object} data - the raw configuration + * @example IIIF 1.1 Info Looks like this (XML syntax is no more) + * { + * "@context" : "http://library.stanford.edu/iiif/image-api/1.1/context.json", + * "@id" : "http://iiif.example.com/prefix/1E34750D-38DB-4825-A38A-B60A345E591C", + * "width" : 6000, + * "height" : 4000, + * "scale_factors" : [ 1, 2, 4 ], + * "tile_width" : 1024, + * "tile_height" : 1024, + * "formats" : [ "jpg", "png" ], + * "qualities" : [ "native", "grey" ], + * "profile" : "http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level0" + * } + */ + configure: function( data, url ){ + // Try to deduce our version and fake it upwards if needed + if ( !$.isPlainObject(data) ) { + var options = configureFromXml10( data ); + options['@context'] = "http://iiif.io/api/image/1.0/context.json"; + options['@id'] = url.replace('/info.xml', ''); + return options; + } else if ( !data['@context'] ) { + data['@context'] = 'http://iiif.io/api/image/1.0/context.json'; + data['@id'] = url.replace('/info.json', ''); + return data; + } else { + return data; + } + }, + + getTileSize: function( level ){ + var scaleFactor = Math.pow(2, this.maxLevel - level); + // cache it in case any external code is going to read it directly + if (this.tileSizePerScaleFactor && this.tileSizePerScaleFactor[scaleFactor]) { + this.tileSize = this.tileSizePerScaleFactor[scaleFactor]; + } + return this.tileSize; + }, + + /** + * Responsible for retreiving the url which will return an image for the + * region specified by the given x, y, and level components. + * @function + * @param {Number} level - z index + * @param {Number} x + * @param {Number} y + * @throws {Error} + */ + getTileUrl: function( level, x, y ){ + + //# constants + var IIIF_ROTATION = '0', + //## get the scale (level as a decimal) + scale = Math.pow( 0.5, this.maxLevel - level ), + + //# image dimensions at this level + levelWidth = Math.ceil( this.width * scale ), + levelHeight = Math.ceil( this.height * scale ), + + //## iiif region + iiifTileSizeWidth, + iiifTileSizeHeight, + iiifRegion, + iiifTileX, + iiifTileY, + iiifTileW, + iiifTileH, + iiifSize, + iiifQuality, + uri; + + iiifTileSizeWidth = Math.ceil( this.getTileSize(level) / scale ); + iiifTileSizeHeight = iiifTileSizeWidth; + + if ( this['@context'].indexOf('/1.0/context.json') > -1 || + this['@context'].indexOf('/1.1/context.json') > -1 || + this['@context'].indexOf('/1/context.json') > -1 ) { + iiifQuality = "native.jpg"; + } else { + iiifQuality = "default.jpg"; + } + + if ( levelWidth < this.tile_width && levelHeight < this.tile_height ){ + iiifSize = levelWidth + ","; + iiifRegion = 'full'; + } else { + iiifTileX = x * iiifTileSizeWidth; + iiifTileY = y * iiifTileSizeHeight; + iiifTileW = Math.min( iiifTileSizeWidth, this.width - iiifTileX ); + iiifTileH = Math.min( iiifTileSizeHeight, this.height - iiifTileY ); + iiifSize = Math.ceil( iiifTileW * scale ) + ","; + iiifRegion = [ iiifTileX, iiifTileY, iiifTileW, iiifTileH ].join( ',' ); + } + uri = [ this['@id'], iiifRegion, iiifSize, IIIF_ROTATION, iiifQuality ].join( '/' ); + + return uri; + } + + }); + + function configureFromXml10(xmlDoc) { + //parse the xml + if ( !xmlDoc || !xmlDoc.documentElement ) { + throw new Error( $.getString( "Errors.Xml" ) ); + } + + var root = xmlDoc.documentElement, + rootName = root.tagName, + configuration = null; + + if ( rootName == "info" ) { + try { + configuration = {}; + parseXML10( root, configuration ); + return configuration; + + } catch ( e ) { + throw (e instanceof Error) ? + e : + new Error( $.getString("Errors.IIIF") ); + } + } + throw new Error( $.getString( "Errors.IIIF" ) ); + } + + function parseXML10( node, configuration, property ) { + var i, + value; + if ( node.nodeType == 3 && property ) {//text node + value = node.nodeValue.trim(); + if( value.match(/^\d*$/)){ + value = Number( value ); + } + if( !configuration[ property ] ){ + configuration[ property ] = value; + }else{ + if( !$.isArray( configuration[ property ] ) ){ + configuration[ property ] = [ configuration[ property ] ]; + } + configuration[ property ].push( value ); + } + } else if( node.nodeType == 1 ){ + for( i = 0; i < node.childNodes.length; i++ ){ + parseXML10( node.childNodes[ i ], configuration, node.nodeName ); + } + } + } + + +}( OpenSeadragon )); diff --git a/src/iiiftilesource.js b/src/iiiftilesource.js deleted file mode 100644 index 2c8814ef..00000000 --- a/src/iiiftilesource.js +++ /dev/null @@ -1,329 +0,0 @@ -/* - * OpenSeadragon - IIIFTileSource - * - * Copyright (C) 2009 CodePlex Foundation - * Copyright (C) 2010-2013 OpenSeadragon contributors - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of CodePlex Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/* - * The getTileUrl implementation is based on Jon Stroop's Python version, - * which is released under the New BSD license: - * https://gist.github.com/jpstroop/4624253 - */ - - -(function( $ ){ - -/** - * @class IIIFTileSource - * @classdesc A client implementation of the International Image Interoperability - * Format: Image API Draft 0.2 - * - * @memberof OpenSeadragon - * @extends OpenSeadragon.TileSource - * @see http://library.stanford.edu/iiif/image-api/ - */ -$.IIIFTileSource = function( options ){ - - $.extend( true, this, options ); - - if( !(this.height && this.width && this.identifier && this.tilesUrl ) ){ - throw new Error('IIIF required parameters not provided.'); - } - - //TODO: at this point the base tile source implementation assumes - // a tile is a square and so only has one property tileSize - // to store it. It may be possible to make tileSize a vector - // OpenSeadraon.Point but would require careful implementation - // to preserve backward compatibility. - options.tileSize = this.tile_width; - - if (! options.maxLevel ) { - var mf = -1; - var scfs = this.scale_factors || this.scale_factor; - if ( scfs instanceof Array ) { - for ( var i = 0; i < scfs.length; i++ ) { - var cf = Number( scfs[i] ); - if ( !isNaN( cf ) && cf > mf ) { mf = cf; } - } - } - if ( mf < 0 ) { options.maxLevel = Number(Math.ceil(Math.log(Math.max(this.width, this.height), 2))); } - else { options.maxLevel = mf; } - } - - $.TileSource.apply( this, [ options ] ); -}; - -$.extend( $.IIIFTileSource.prototype, $.TileSource.prototype, /** @lends OpenSeadragon.IIIFTileSource.prototype */{ - /** - * Determine if the data and/or url imply the image service is supported by - * this tile source. - * @method - * @param {Object|Array} data - * @param {String} optional - url - */ - supports: function( data, url ){ - return ( - data.ns && - "http://library.stanford.edu/iiif/image-api/ns/" == data.ns - ) || ( - data.profile && ( - "http://library.stanford.edu/iiif/image-api/compliance.html#level1" == data.profile || - "http://library.stanford.edu/iiif/image-api/compliance.html#level2" == data.profile || - "http://library.stanford.edu/iiif/image-api/compliance.html#level3" == data.profile || - "http://library.stanford.edu/iiif/image-api/compliance.html" == data.profile - ) - ) || ( - data.documentElement && - "info" == data.documentElement.tagName && - "http://library.stanford.edu/iiif/image-api/ns/" == - data.documentElement.namespaceURI - ); - }, - - /** - * - * @method - * @param {Object|XMLDocument} data - the raw configuration - * @param {String} url - the url the data was retreived from if any. - * @return {Object} options - A dictionary of keyword arguments sufficient - * to configure this tile source via its constructor. - */ - configure: function( data, url ){ - var service, - options, - host; - - if( !$.isPlainObject(data) ){ - - options = configureFromXml( this, data ); - - }else{ - - options = configureFromObject( this, data ); - } - - if( url && !options.tilesUrl ){ - service = url.split('/'); - service.pop(); //info.json or info.xml - service = service.join('/'); - if( 'http' !== url.substring( 0, 4 ) ){ - host = location.protocol + '//' + location.host; - service = host + service; - } - options.tilesUrl = service.replace( - data.identifier, - '' - ); - } - - return options; - }, - - /** - * Responsible for retreiving the url which will return an image for the - * region speified by the given x, y, and level components. - * @method - * @param {Number} level - z index - * @param {Number} x - * @param {Number} y - * @throws {Error} - */ - getTileUrl: function( level, x, y ){ - - //# constants - var IIIF_ROTATION = '0', - IIIF_QUALITY = 'native.jpg', - - //## get the scale (level as a decimal) - scale = Math.pow( 0.5, this.maxLevel - level ), - - //## get iiif size - // iiif_size = 'pct:' + ( scale * 100 ), - - //# image dimensions at this level - level_width = Math.ceil( this.width * scale ), - level_height = Math.ceil( this.height * scale ), - - //## iiif region - iiif_tile_size_width = Math.ceil( this.tileSize / scale ), - iiif_tile_size_height = Math.ceil( this.tileSize / scale ), - iiif_region, - iiif_tile_x, - iiif_tile_y, - iiif_tile_w, - iiif_tile_h, - iiif_size; - - - if ( level_width < this.tile_width && level_height < this.tile_height ){ - iiif_size = level_width + ","; // + level_height; only one dim. for IIIF level 1 compliance - iiif_region = 'full'; - } else { - iiif_tile_x = x * iiif_tile_size_width; - iiif_tile_y = y * iiif_tile_size_height; - iiif_tile_w = Math.min( iiif_tile_size_width, this.width - iiif_tile_x ); - iiif_tile_h = Math.min( iiif_tile_size_height, this.height - iiif_tile_y ); - iiif_size = Math.ceil(iiif_tile_w * scale) + ","; - iiif_region = [ iiif_tile_x, iiif_tile_y, iiif_tile_w, iiif_tile_h ].join(','); - } - - return [ - this.tilesUrl, - this.identifier, - iiif_region, - iiif_size, - IIIF_ROTATION, - IIIF_QUALITY - ].join('/'); - } - - -}); - -/** - * @private - * @inner - * @function - * @example - * - * - * 1E34750D-38DB-4825-A38A-B60A345E591C - * 6000 - * 4000 - * - * 1 - * 2 - * 4 - * - * 1024 - * 1024 - * - * jpg - * png - * - * - * native - * grey - * - * - */ -function configureFromXml( tileSource, xmlDoc ){ - - //parse the xml - if ( !xmlDoc || !xmlDoc.documentElement ) { - throw new Error( $.getString( "Errors.Xml" ) ); - } - - var root = xmlDoc.documentElement, - rootName = root.tagName, - configuration = null; - - if ( rootName == "info" ) { - - try { - - configuration = { - "ns": root.namespaceURI - }; - - parseXML( root, configuration ); - - return configureFromObject( tileSource, configuration ); - - } catch ( e ) { - throw (e instanceof Error) ? - e : - new Error( $.getString("Errors.IIIF") ); - } - } - - throw new Error( $.getString( "Errors.IIIF" ) ); - -} - - -/** - * @private - * @inner - * @function - */ -function parseXML( node, configuration, property ){ - var i, - value; - if( node.nodeType == 3 && property ){//text node - value = node.nodeValue.trim(); - if( value.match(/^\d*$/)){ - value = Number( value ); - } - if( !configuration[ property ] ){ - configuration[ property ] = value; - }else{ - if( !$.isArray( configuration[ property ] ) ){ - configuration[ property ] = [ configuration[ property ] ]; - } - configuration[ property ].push( value ); - } - } else if( node.nodeType == 1 ){ - for( i = 0; i < node.childNodes.length; i++ ){ - parseXML( node.childNodes[ i ], configuration, node.nodeName ); - } - } -} - - -/** - * @private - * @inner - * @function - * @example - * { - * "profile" : "http://library.stanford.edu/iiif/image-api/compliance.html#level1", - * "identifier" : "1E34750D-38DB-4825-A38A-B60A345E591C", - * "width" : 6000, - * "height" : 4000, - * "scale_factors" : [ 1, 2, 4 ], - * "tile_width" : 1024, - * "tile_height" : 1024, - * "formats" : [ "jpg", "png" ], - * "quality" : [ "native", "grey" ] - * } - */ -function configureFromObject( tileSource, configuration ){ - //the image_host property is not part of the iiif standard but is included here to - //allow the info.json and info.xml specify a different server to load the - //images from so we can test the implementation. - if( configuration.image_host ){ - configuration.tilesUrl = configuration.image_host; - } - return configuration; -} - -}( OpenSeadragon )); diff --git a/src/tilesource.js b/src/tilesource.js index 90a80f7c..bcb553fb 100644 --- a/src/tilesource.js +++ b/src/tilesource.js @@ -194,6 +194,14 @@ $.TileSource = function( width, height, tileSize, tileOverlap, minLevel, maxLeve $.TileSource.prototype = /** @lends OpenSeadragon.TileSource.prototype */{ + /** + * @function + * @param {Number} level + */ + getTileSize: function( level ) { + return this.tileSize; + }, + /** * @function * @param {Number} level @@ -220,8 +228,8 @@ $.TileSource.prototype = /** @lends OpenSeadragon.TileSource.prototype */{ */ getNumTiles: function( level ) { var scale = this.getLevelScale( level ), - x = Math.ceil( scale * this.dimensions.x / this.tileSize ), - y = Math.ceil( scale * this.dimensions.y / this.tileSize ); + x = Math.ceil( scale * this.dimensions.x / this.getTileSize(level) ), + y = Math.ceil( scale * this.dimensions.y / this.getTileSize(level) ); return new $.Point( x, y ); }, @@ -245,10 +253,11 @@ $.TileSource.prototype = /** @lends OpenSeadragon.TileSource.prototype */{ */ getClosestLevel: function( rect ) { var i, - tilesPerSide = Math.floor( Math.max( rect.x, rect.y ) / this.tileSize ), + tilesPerSide, tiles; for( i = this.minLevel; i < this.maxLevel; i++ ){ tiles = this.getNumTiles( i ); + tilesPerSide = Math.floor( Math.max( rect.x, rect.y ) / this.getTileSize(i) ); if( Math.max( tiles.x, tiles.y ) + 1 >= tilesPerSide ){ break; } @@ -263,8 +272,8 @@ $.TileSource.prototype = /** @lends OpenSeadragon.TileSource.prototype */{ */ getTileAtPoint: function( level, point ) { var pixel = point.times( this.dimensions.x ).times( this.getLevelScale(level ) ), - tx = Math.floor( pixel.x / this.tileSize ), - ty = Math.floor( pixel.y / this.tileSize ); + tx = Math.floor( pixel.x / this.getTileSize(level) ), + ty = Math.floor( pixel.y / this.getTileSize(level) ); return new $.Point( tx, ty ); }, @@ -277,10 +286,11 @@ $.TileSource.prototype = /** @lends OpenSeadragon.TileSource.prototype */{ */ getTileBounds: function( level, x, y ) { var dimensionsScaled = this.dimensions.times( this.getLevelScale( level ) ), - px = ( x === 0 ) ? 0 : this.tileSize * x - this.tileOverlap, - py = ( y === 0 ) ? 0 : this.tileSize * y - this.tileOverlap, - sx = this.tileSize + ( x === 0 ? 1 : 2 ) * this.tileOverlap, - sy = this.tileSize + ( y === 0 ? 1 : 2 ) * this.tileOverlap, + tileSize = this.getTileSize(level), + px = ( x === 0 ) ? 0 : tileSize * x - this.tileOverlap, + py = ( y === 0 ) ? 0 : tileSize * y - this.tileOverlap, + sx = tileSize + ( x === 0 ? 1 : 2 ) * this.tileOverlap, + sy = tileSize + ( y === 0 ? 1 : 2 ) * this.tileOverlap, scale = 1.0 / dimensionsScaled.x; sx = Math.min( sx, dimensionsScaled.x - px );