2013-05-01 08:46:16 +04:00
|
|
|
/*
|
2013-05-14 08:00:24 +04:00
|
|
|
* OpenSeadragon - LegacyTileSource
|
2013-05-01 08:46:16 +04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009 CodePlex Foundation
|
2013-05-14 07:32:09 +04:00
|
|
|
* Copyright (C) 2010-2013 OpenSeadragon contributors
|
2013-05-01 08:46:16 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
(function( $ ){
|
|
|
|
|
|
|
|
/**
|
2013-11-25 20:48:44 +04:00
|
|
|
* @class LegacyTileSource
|
|
|
|
* @classdesc The LegacyTileSource allows simple, traditional image pyramids to be loaded
|
2013-08-27 02:47:21 +04:00
|
|
|
* into an OpenSeadragon Viewer. Basically, this translates to the historically
|
|
|
|
* common practice of starting with a 'master' image, maybe a tiff for example,
|
|
|
|
* and generating a set of 'service' images like one or more thumbnails, a medium
|
|
|
|
* resolution image and a high resolution image in standard web formats like
|
|
|
|
* png or jpg.
|
2013-11-25 20:48:44 +04:00
|
|
|
*
|
2013-11-16 10:19:53 +04:00
|
|
|
* @memberof OpenSeadragon
|
2013-08-27 02:47:21 +04:00
|
|
|
* @extends OpenSeadragon.TileSource
|
|
|
|
* @param {Array} levels An array of file descriptions, each is an object with
|
|
|
|
* a 'url', a 'width', and a 'height'. Overriding classes can expect more
|
|
|
|
* properties but these properties are sufficient for this implementation.
|
|
|
|
* Additionally, the levels are required to be listed in order from
|
|
|
|
* smallest to largest.
|
|
|
|
* @property {Number} aspectRatio
|
|
|
|
* @property {Number} dimensions
|
|
|
|
* @property {Number} tileSize
|
|
|
|
* @property {Number} tileOverlap
|
|
|
|
* @property {Number} minLevel
|
|
|
|
* @property {Number} maxLevel
|
|
|
|
* @property {Array} levels
|
|
|
|
*/
|
|
|
|
$.LegacyTileSource = function( levels ) {
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
var options,
|
2012-06-05 15:52:00 +04:00
|
|
|
width,
|
|
|
|
height;
|
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
if( $.isArray( levels ) ){
|
|
|
|
options = {
|
|
|
|
type: 'legacy-image-pyramid',
|
|
|
|
levels: levels
|
|
|
|
};
|
|
|
|
}
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
//clean up the levels to make sure we support all formats
|
|
|
|
options.levels = filterFiles( options.levels );
|
|
|
|
|
2013-08-27 03:48:59 +04:00
|
|
|
if ( options.levels.length > 0 ) {
|
|
|
|
width = options.levels[ options.levels.length - 1 ].width;
|
|
|
|
height = options.levels[ options.levels.length - 1 ].height;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
width = 0;
|
|
|
|
height = 0;
|
|
|
|
$.console.error( "No supported image formats found" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$.extend( true, options, {
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
tileSize: Math.max( height, width ),
|
2013-08-27 02:47:21 +04:00
|
|
|
tileOverlap: 0,
|
2013-08-27 03:48:59 +04:00
|
|
|
minLevel: 0,
|
|
|
|
maxLevel: options.levels.length > 0 ? options.levels.length - 1 : 0
|
|
|
|
} );
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
$.TileSource.apply( this, [ options ] );
|
|
|
|
|
|
|
|
this.levels = options.levels;
|
|
|
|
};
|
2012-02-23 08:02:54 +04:00
|
|
|
|
2013-11-16 10:19:53 +04:00
|
|
|
$.extend( $.LegacyTileSource.prototype, $.TileSource.prototype, /** @lends OpenSeadragon.LegacyTileSource.prototype */{
|
2013-08-27 02:47:21 +04:00
|
|
|
/**
|
|
|
|
* 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 (
|
2013-06-19 21:33:25 +04:00
|
|
|
data.type &&
|
2012-06-05 15:52:00 +04:00
|
|
|
"legacy-image-pyramid" == data.type
|
|
|
|
) || (
|
|
|
|
data.documentElement &&
|
|
|
|
"legacy-image-pyramid" == data.documentElement.getAttribute('type')
|
|
|
|
);
|
2013-08-27 02:47:21 +04:00
|
|
|
},
|
2012-06-05 15:52:00 +04:00
|
|
|
|
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @function
|
|
|
|
* @param {Object|XMLDocument} configuration - the raw configuration
|
|
|
|
* @param {String} dataUrl - the url the data was retreived from if any.
|
|
|
|
* @return {Object} options - A dictionary of keyword arguments sufficient
|
|
|
|
* to configure this tile sources constructor.
|
|
|
|
*/
|
|
|
|
configure: function( configuration, dataUrl ){
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
var options;
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
if( !$.isPlainObject(configuration) ){
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
options = configureFromXML( this, configuration );
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
}else{
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
options = configureFromObject( this, configuration );
|
|
|
|
}
|
2012-02-23 08:02:54 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
return options;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @param {Number} level
|
|
|
|
*/
|
2013-08-27 03:48:59 +04:00
|
|
|
getLevelScale: function ( level ) {
|
2013-08-27 02:47:21 +04:00
|
|
|
var levelScale = NaN;
|
2013-08-27 03:48:59 +04:00
|
|
|
if ( this.levels.length > 0 && level >= this.minLevel && level <= this.maxLevel ) {
|
2013-08-27 02:47:21 +04:00
|
|
|
levelScale =
|
|
|
|
this.levels[ level ].width /
|
|
|
|
this.levels[ this.maxLevel ].width;
|
2012-02-23 08:02:54 +04:00
|
|
|
}
|
2013-08-27 02:47:21 +04:00
|
|
|
return levelScale;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @param {Number} level
|
|
|
|
*/
|
|
|
|
getNumTiles: function( level ) {
|
|
|
|
var scale = this.getLevelScale( level );
|
|
|
|
if ( scale ){
|
|
|
|
return new $.Point( 1, 1 );
|
|
|
|
} else {
|
|
|
|
return new $.Point( 0, 0 );
|
|
|
|
}
|
|
|
|
},
|
2012-02-23 08:02:54 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
/**
|
|
|
|
* This method is not implemented by this class other than to throw an Error
|
|
|
|
* announcing you have to implement it. Because of the variety of tile
|
|
|
|
* server technologies, and various specifications for building image
|
|
|
|
* pyramids, this method is here to allow easy integration.
|
|
|
|
* @function
|
|
|
|
* @param {Number} level
|
|
|
|
* @param {Number} x
|
|
|
|
* @param {Number} y
|
|
|
|
* @throws {Error}
|
|
|
|
*/
|
2013-08-27 03:48:59 +04:00
|
|
|
getTileUrl: function ( level, x, y ) {
|
2013-08-27 02:47:21 +04:00
|
|
|
var url = null;
|
2013-08-27 03:48:59 +04:00
|
|
|
if ( this.levels.length > 0 && level >= this.minLevel && level <= this.maxLevel ) {
|
2013-08-27 02:47:21 +04:00
|
|
|
url = this.levels[ level ].url;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
2013-08-27 03:48:59 +04:00
|
|
|
} );
|
2013-08-27 02:47:21 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method removes any files from the Array which dont conform to our
|
|
|
|
* basic requirements for a 'level' in the LegacyTileSource.
|
|
|
|
* @private
|
|
|
|
* @inner
|
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
function filterFiles( files ){
|
|
|
|
var filtered = [],
|
2012-06-05 15:52:00 +04:00
|
|
|
file,
|
|
|
|
i;
|
2013-08-27 02:47:21 +04:00
|
|
|
for( i = 0; i < files.length; i++ ){
|
|
|
|
file = files[ i ];
|
|
|
|
if( file.height &&
|
2013-06-19 21:33:25 +04:00
|
|
|
file.width &&
|
2016-10-20 12:32:13 +03:00
|
|
|
file.url ){
|
2013-08-27 02:47:21 +04:00
|
|
|
//This is sufficient to serve as a level
|
|
|
|
filtered.push({
|
|
|
|
url: file.url,
|
|
|
|
width: Number( file.width ),
|
|
|
|
height: Number( file.height )
|
|
|
|
});
|
2012-06-05 15:52:00 +04:00
|
|
|
}
|
2013-08-27 03:48:59 +04:00
|
|
|
else {
|
|
|
|
$.console.error( 'Unsupported image format: %s', file.url ? file.url : '<no URL>' );
|
|
|
|
}
|
2013-08-27 02:47:21 +04:00
|
|
|
}
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2017-01-08 17:52:57 +03:00
|
|
|
return filtered.sort(function(a, b) {
|
2013-08-27 02:47:21 +04:00
|
|
|
return a.height - b.height;
|
|
|
|
});
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
}
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @inner
|
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
function configureFromXML( tileSource, xmlDoc ){
|
|
|
|
|
|
|
|
if ( !xmlDoc || !xmlDoc.documentElement ) {
|
|
|
|
throw new Error( $.getString( "Errors.Xml" ) );
|
|
|
|
}
|
2013-08-27 02:25:57 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
var root = xmlDoc.documentElement,
|
|
|
|
rootName = root.tagName,
|
|
|
|
conf = null,
|
|
|
|
levels = [],
|
2012-06-05 15:52:00 +04:00
|
|
|
level,
|
|
|
|
i;
|
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
if ( rootName == "image" ) {
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
try {
|
|
|
|
conf = {
|
|
|
|
type: root.getAttribute( "type" ),
|
|
|
|
levels: []
|
|
|
|
};
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
levels = root.getElementsByTagName( "level" );
|
|
|
|
for ( i = 0; i < levels.length; i++ ) {
|
|
|
|
level = levels[ i ];
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2017-01-08 17:52:57 +03:00
|
|
|
conf.levels.push({
|
2013-08-27 02:47:21 +04:00
|
|
|
url: level.getAttribute( "url" ),
|
|
|
|
width: parseInt( level.getAttribute( "width" ), 10 ),
|
|
|
|
height: parseInt( level.getAttribute( "height" ), 10 )
|
|
|
|
});
|
|
|
|
}
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
return configureFromObject( tileSource, conf );
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
} catch ( e ) {
|
|
|
|
throw (e instanceof Error) ?
|
2013-06-19 21:33:25 +04:00
|
|
|
e :
|
2013-08-27 02:47:21 +04:00
|
|
|
new Error( 'Unknown error parsing Legacy Image Pyramid XML.' );
|
2012-06-05 15:52:00 +04:00
|
|
|
}
|
2013-08-27 02:47:21 +04:00
|
|
|
} else if ( rootName == "collection" ) {
|
|
|
|
throw new Error( 'Legacy Image Pyramid Collections not yet supported.' );
|
|
|
|
} else if ( rootName == "error" ) {
|
|
|
|
throw new Error( 'Error: ' + xmlDoc );
|
2013-08-27 02:25:57 +04:00
|
|
|
}
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
throw new Error( 'Unknown element ' + rootName );
|
|
|
|
}
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @inner
|
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
function configureFromObject( tileSource, configuration ){
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
return configuration.levels;
|
|
|
|
|
|
|
|
}
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
}( OpenSeadragon ));
|