2013-05-01 08:46:16 +04:00
|
|
|
/*
|
2013-05-14 08:00:24 +04:00
|
|
|
* OpenSeadragon - DziTileSource
|
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.
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
(function( $ ){
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
/**
|
2013-11-16 10:19:53 +04:00
|
|
|
* @class DziTileSource
|
|
|
|
* @memberof OpenSeadragon
|
2012-02-01 00:59:09 +04:00
|
|
|
* @extends OpenSeadragon.TileSource
|
2012-08-29 22:46:34 +04:00
|
|
|
* @param {Number|Object} width - the pixel width of the image or the idiomatic
|
|
|
|
* options object which is used instead of positional arguments.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @param {Number} height
|
|
|
|
* @param {Number} tileSize
|
|
|
|
* @param {Number} tileOverlap
|
|
|
|
* @param {String} tilesUrl
|
|
|
|
* @param {String} fileFormat
|
|
|
|
* @param {OpenSeadragon.DisplayRect[]} displayRects
|
|
|
|
* @property {String} tilesUrl
|
|
|
|
* @property {String} fileFormat
|
|
|
|
* @property {OpenSeadragon.DisplayRect[]} displayRects
|
2013-06-19 21:33:25 +04:00
|
|
|
*/
|
2013-02-14 01:27:17 +04:00
|
|
|
$.DziTileSource = function( width, height, tileSize, tileOverlap, tilesUrl, fileFormat, displayRects, minLevel, maxLevel ) {
|
2012-01-24 07:48:45 +04:00
|
|
|
var i,
|
|
|
|
rect,
|
2012-06-05 15:52:00 +04:00
|
|
|
level,
|
|
|
|
options;
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2012-06-05 15:52:00 +04:00
|
|
|
if( $.isPlainObject( width ) ){
|
|
|
|
options = width;
|
|
|
|
}else{
|
|
|
|
options = {
|
|
|
|
width: arguments[ 0 ],
|
|
|
|
height: arguments[ 1 ],
|
|
|
|
tileSize: arguments[ 2 ],
|
|
|
|
tileOverlap: arguments[ 3 ],
|
|
|
|
tilesUrl: arguments[ 4 ],
|
|
|
|
fileFormat: arguments[ 5 ],
|
2013-02-14 01:27:17 +04:00
|
|
|
displayRects: arguments[ 6 ],
|
2013-06-19 21:33:25 +04:00
|
|
|
minLevel: arguments[ 7 ],
|
2013-02-14 01:27:17 +04:00
|
|
|
maxLevel: arguments[ 8 ]
|
2012-06-05 15:52:00 +04:00
|
|
|
};
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
this._levelRects = {};
|
2012-06-05 15:52:00 +04:00
|
|
|
this.tilesUrl = options.tilesUrl;
|
|
|
|
this.fileFormat = options.fileFormat;
|
|
|
|
this.displayRects = options.displayRects;
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2011-12-14 03:34:12 +04:00
|
|
|
if ( this.displayRects ) {
|
2012-01-24 07:48:45 +04:00
|
|
|
for ( i = this.displayRects.length - 1; i >= 0; i-- ) {
|
|
|
|
rect = this.displayRects[ i ];
|
|
|
|
for ( level = rect.minLevel; level <= rect.maxLevel; level++ ) {
|
|
|
|
if ( !this._levelRects[ level ] ) {
|
|
|
|
this._levelRects[ level ] = [];
|
2011-12-14 03:29:25 +04:00
|
|
|
}
|
2012-01-24 07:48:45 +04:00
|
|
|
this._levelRects[ level ].push( rect );
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
}
|
2011-12-14 03:34:12 +04:00
|
|
|
}
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2012-06-05 15:52:00 +04:00
|
|
|
$.TileSource.apply( this, [ options ] );
|
2011-12-14 03:34:12 +04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-11-16 10:19:53 +04:00
|
|
|
$.extend( $.DziTileSource.prototype, $.TileSource.prototype, /** @lends OpenSeadragon.DziTileSource.prototype */{
|
2012-06-05 15:52:00 +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 ){
|
2012-09-28 21:35:02 +04:00
|
|
|
var ns;
|
|
|
|
if ( data.Image ) {
|
|
|
|
ns = data.Image.xmlns;
|
2014-08-15 02:06:29 +04:00
|
|
|
} else if ( data.documentElement && "Image" == data.documentElement.localName ) {
|
2012-09-28 21:35:02 +04:00
|
|
|
ns = data.documentElement.namespaceURI;
|
|
|
|
}
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2012-09-28 21:35:02 +04:00
|
|
|
return ( "http://schemas.microsoft.com/deepzoom/2008" == ns ||
|
|
|
|
"http://schemas.microsoft.com/deepzoom/2009" == ns );
|
|
|
|
},
|
2012-06-05 15:52:00 +04:00
|
|
|
|
|
|
|
/**
|
2013-06-19 21:33:25 +04:00
|
|
|
*
|
2012-06-05 15:52:00 +04:00
|
|
|
* @function
|
2012-08-29 22:46:34 +04:00
|
|
|
* @param {Object|XMLDocument} data - the raw configuration
|
|
|
|
* @param {String} url - the url the data was retreived from if any.
|
2013-06-19 21:33:25 +04:00
|
|
|
* @return {Object} options - A dictionary of keyword arguments sufficient
|
2012-08-29 22:46:34 +04:00
|
|
|
* to configure this tile sources constructor.
|
2012-06-05 15:52:00 +04:00
|
|
|
*/
|
2012-08-29 22:46:34 +04:00
|
|
|
configure: function( data, url ){
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-06-19 01:55:19 +04:00
|
|
|
var options;
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2012-08-29 22:46:34 +04:00
|
|
|
if( !$.isPlainObject(data) ){
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2012-08-29 22:46:34 +04:00
|
|
|
options = configureFromXML( this, data );
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2012-08-29 22:46:34 +04:00
|
|
|
}else{
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2012-08-29 22:46:34 +04:00
|
|
|
options = configureFromObject( this, data );
|
2012-06-05 15:52:00 +04:00
|
|
|
}
|
|
|
|
|
2013-03-27 00:03:07 +04:00
|
|
|
if (url && !options.tilesUrl) {
|
2014-04-11 00:08:19 +04:00
|
|
|
options.tilesUrl = url.replace(/([^\/]+)\.(dzi|xml|js)(\?.*|$)/, '$1_files/');
|
2014-05-31 11:30:25 +04:00
|
|
|
|
|
|
|
if (url.search(/\.(dzi|xml|js)\?/) != -1) {
|
|
|
|
options.queryParams = url.match(/\?.*/);
|
|
|
|
}else{
|
|
|
|
options.queryParams = '';
|
|
|
|
}
|
2012-06-05 15:52:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @param {Number} level
|
|
|
|
* @param {Number} x
|
|
|
|
* @param {Number} y
|
|
|
|
*/
|
2012-01-24 07:48:45 +04:00
|
|
|
getTileUrl: function( level, x, y ) {
|
2014-04-11 00:08:19 +04:00
|
|
|
return [ this.tilesUrl, level, '/', x, '_', y, '.', this.fileFormat, this.queryParams ].join( '' );
|
2011-12-14 03:29:25 +04:00
|
|
|
},
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @param {Number} level
|
|
|
|
* @param {Number} x
|
|
|
|
* @param {Number} y
|
|
|
|
*/
|
2012-01-24 07:48:45 +04:00
|
|
|
tileExists: function( level, x, y ) {
|
|
|
|
var rects = this._levelRects[ level ],
|
|
|
|
rect,
|
|
|
|
scale,
|
|
|
|
xMin,
|
|
|
|
yMin,
|
|
|
|
xMax,
|
|
|
|
yMax,
|
|
|
|
i;
|
|
|
|
|
|
|
|
if ( !rects || !rects.length ) {
|
2011-12-14 03:29:25 +04:00
|
|
|
return true;
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
for ( i = rects.length - 1; i >= 0; i-- ) {
|
|
|
|
rect = rects[ i ];
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( level < rect.minLevel || level > rect.maxLevel ) {
|
2011-12-14 03:29:25 +04:00
|
|
|
continue;
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
scale = this.getLevelScale( level );
|
|
|
|
xMin = rect.x * scale;
|
|
|
|
yMin = rect.y * scale;
|
|
|
|
xMax = xMin + rect.width * scale;
|
|
|
|
yMax = yMin + rect.height * scale;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
xMin = Math.floor( xMin / this.tileSize );
|
|
|
|
yMin = Math.floor( yMin / this.tileSize );
|
|
|
|
xMax = Math.ceil( xMax / this.tileSize );
|
|
|
|
yMax = Math.ceil( yMax / this.tileSize );
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( xMin <= x && x < xMax && yMin <= y && y < yMax ) {
|
2011-12-14 03:29:25 +04:00
|
|
|
return true;
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2011-12-14 03:29:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
|
2012-06-05 15:52:00 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @inner
|
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
function configureFromXML( tileSource, xmlDoc ){
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2012-06-05 15:52:00 +04:00
|
|
|
if ( !xmlDoc || !xmlDoc.documentElement ) {
|
|
|
|
throw new Error( $.getString( "Errors.Xml" ) );
|
|
|
|
}
|
|
|
|
|
2012-08-29 22:46:34 +04:00
|
|
|
var root = xmlDoc.documentElement,
|
2014-08-15 02:06:29 +04:00
|
|
|
rootName = root.localName,
|
2012-08-29 22:46:34 +04:00
|
|
|
configuration = null,
|
|
|
|
displayRects = [],
|
2012-06-05 15:52:00 +04:00
|
|
|
dispRectNodes,
|
|
|
|
dispRectNode,
|
|
|
|
rectNode,
|
|
|
|
sizeNode,
|
|
|
|
i;
|
|
|
|
|
|
|
|
if ( rootName == "Image" ) {
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2012-06-05 15:52:00 +04:00
|
|
|
try {
|
|
|
|
sizeNode = root.getElementsByTagName( "Size" )[ 0 ];
|
2012-08-29 22:46:34 +04:00
|
|
|
configuration = {
|
2012-06-05 15:52:00 +04:00
|
|
|
Image: {
|
|
|
|
xmlns: "http://schemas.microsoft.com/deepzoom/2008",
|
2013-03-26 22:35:43 +04:00
|
|
|
Url: root.getAttribute( "Url" ),
|
2012-06-05 15:52:00 +04:00
|
|
|
Format: root.getAttribute( "Format" ),
|
|
|
|
DisplayRect: null,
|
2013-06-19 21:33:25 +04:00
|
|
|
Overlap: parseInt( root.getAttribute( "Overlap" ), 10 ),
|
2013-02-13 07:40:08 +04:00
|
|
|
TileSize: parseInt( root.getAttribute( "TileSize" ), 10 ),
|
2012-06-05 15:52:00 +04:00
|
|
|
Size: {
|
2013-02-13 07:40:08 +04:00
|
|
|
Height: parseInt( sizeNode.getAttribute( "Height" ), 10 ),
|
|
|
|
Width: parseInt( sizeNode.getAttribute( "Width" ), 10 )
|
2012-06-05 15:52:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-29 22:46:34 +04:00
|
|
|
if ( !$.imageFormatSupported( configuration.Image.Format ) ) {
|
2012-06-05 15:52:00 +04:00
|
|
|
throw new Error(
|
2012-08-29 22:46:34 +04:00
|
|
|
$.getString( "Errors.ImageFormat", configuration.Image.Format.toUpperCase() )
|
2012-06-05 15:52:00 +04:00
|
|
|
);
|
|
|
|
}
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2012-06-05 15:52:00 +04:00
|
|
|
dispRectNodes = root.getElementsByTagName( "DisplayRect" );
|
|
|
|
for ( i = 0; i < dispRectNodes.length; i++ ) {
|
|
|
|
dispRectNode = dispRectNodes[ i ];
|
|
|
|
rectNode = dispRectNode.getElementsByTagName( "Rect" )[ 0 ];
|
|
|
|
|
|
|
|
displayRects.push({
|
|
|
|
Rect: {
|
2013-02-13 07:40:08 +04:00
|
|
|
X: parseInt( rectNode.getAttribute( "X" ), 10 ),
|
|
|
|
Y: parseInt( rectNode.getAttribute( "Y" ), 10 ),
|
|
|
|
Width: parseInt( rectNode.getAttribute( "Width" ), 10 ),
|
|
|
|
Height: parseInt( rectNode.getAttribute( "Height" ), 10 ),
|
2013-02-20 07:53:54 +04:00
|
|
|
MinLevel: parseInt( dispRectNode.getAttribute( "MinLevel" ), 10 ),
|
2013-02-13 07:40:08 +04:00
|
|
|
MaxLevel: parseInt( dispRectNode.getAttribute( "MaxLevel" ), 10 )
|
2012-06-05 15:52:00 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if( displayRects.length ){
|
2012-08-29 22:46:34 +04:00
|
|
|
configuration.Image.DisplayRect = displayRects;
|
2012-06-05 15:52:00 +04:00
|
|
|
}
|
|
|
|
|
2012-08-29 22:46:34 +04:00
|
|
|
return configureFromObject( tileSource, configuration );
|
2012-06-05 15:52:00 +04:00
|
|
|
|
|
|
|
} catch ( e ) {
|
2013-06-19 21:33:25 +04:00
|
|
|
throw (e instanceof Error) ?
|
|
|
|
e :
|
2012-06-05 15:52:00 +04:00
|
|
|
new Error( $.getString("Errors.Dzi") );
|
|
|
|
}
|
|
|
|
} else if ( rootName == "Collection" ) {
|
|
|
|
throw new Error( $.getString( "Errors.Dzc" ) );
|
|
|
|
} else if ( rootName == "Error" ) {
|
2013-06-19 23:37:57 +04:00
|
|
|
return $._processDZIError( root );
|
2012-06-05 15:52:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error( $.getString( "Errors.Dzi" ) );
|
2013-02-13 07:40:08 +04:00
|
|
|
}
|
2012-06-05 15:52:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @inner
|
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
function configureFromObject( tileSource, configuration ){
|
|
|
|
var imageData = configuration.Image,
|
|
|
|
tilesUrl = imageData.Url,
|
|
|
|
fileFormat = imageData.Format,
|
|
|
|
sizeData = imageData.Size,
|
|
|
|
dispRectData = imageData.DisplayRect || [],
|
2013-02-13 07:40:08 +04:00
|
|
|
width = parseInt( sizeData.Width, 10 ),
|
|
|
|
height = parseInt( sizeData.Height, 10 ),
|
|
|
|
tileSize = parseInt( imageData.TileSize, 10 ),
|
|
|
|
tileOverlap = parseInt( imageData.Overlap, 10 ),
|
2012-09-29 16:18:32 +04:00
|
|
|
displayRects = [],
|
2012-06-05 15:52:00 +04:00
|
|
|
rectData,
|
|
|
|
i;
|
|
|
|
|
|
|
|
//TODO: need to figure out out to better handle image format compatibility
|
|
|
|
// which actually includes additional file formats like xml and pdf
|
2013-06-19 21:33:25 +04:00
|
|
|
// and plain text for various tilesource implementations to avoid low
|
2012-06-05 15:52:00 +04:00
|
|
|
// level errors.
|
|
|
|
//
|
|
|
|
// For now, just don't perform the check.
|
|
|
|
//
|
|
|
|
/*if ( !imageFormatSupported( fileFormat ) ) {
|
|
|
|
throw new Error(
|
|
|
|
$.getString( "Errors.ImageFormat", fileFormat.toUpperCase() )
|
|
|
|
);
|
|
|
|
}*/
|
|
|
|
|
|
|
|
for ( i = 0; i < dispRectData.length; i++ ) {
|
|
|
|
rectData = dispRectData[ i ].Rect;
|
|
|
|
|
2012-09-29 16:18:32 +04:00
|
|
|
displayRects.push( new $.DisplayRect(
|
2013-02-13 07:40:08 +04:00
|
|
|
parseInt( rectData.X, 10 ),
|
|
|
|
parseInt( rectData.Y, 10 ),
|
|
|
|
parseInt( rectData.Width, 10 ),
|
|
|
|
parseInt( rectData.Height, 10 ),
|
2013-02-20 07:53:54 +04:00
|
|
|
parseInt( rectData.MinLevel, 10 ),
|
2013-02-13 07:40:08 +04:00
|
|
|
parseInt( rectData.MaxLevel, 10 )
|
2012-06-05 15:52:00 +04:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2013-03-26 17:02:34 +04:00
|
|
|
return $.extend(true, {
|
2012-06-05 15:52:00 +04:00
|
|
|
width: width, /* width *required */
|
|
|
|
height: height, /* height *required */
|
|
|
|
tileSize: tileSize, /* tileSize *required */
|
|
|
|
tileOverlap: tileOverlap, /* tileOverlap *required */
|
|
|
|
minLevel: null, /* minLevel */
|
|
|
|
maxLevel: null, /* maxLevel */
|
|
|
|
tilesUrl: tilesUrl, /* tilesUrl */
|
|
|
|
fileFormat: fileFormat, /* fileFormat */
|
2012-09-29 16:18:32 +04:00
|
|
|
displayRects: displayRects /* displayRects */
|
2013-03-26 17:02:34 +04:00
|
|
|
}, configuration );
|
2012-06-05 15:52:00 +04:00
|
|
|
|
2013-02-13 07:40:08 +04:00
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
}( OpenSeadragon ));
|