openseadragon/src/imagetilesource.js

268 lines
10 KiB
JavaScript
Raw Normal View History

2015-11-01 18:23:02 +03:00
/*
* OpenSeadragon - ImageTileSource
*
* 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 ImageTileSource
2015-11-05 02:47:41 +03:00
* @classdesc The ImageTileSource allows a simple image to be loaded
2015-11-01 18:23:02 +03:00
* into an OpenSeadragon Viewer.
* There are 2 ways to open an ImageTileSource:
* 1. viewer.open({type: 'image', url: fooUrl});
* 2. viewer.open(new OpenSeadragon.ImageTileSource({url: fooUrl}));
*
* With the first syntax, the crossOriginPolicy, ajaxWithCredentials and
* useCanvas options are inherited from the viewer if they are not
* specified directly in the options object.
2015-11-01 18:23:02 +03:00
*
* @memberof OpenSeadragon
* @extends OpenSeadragon.TileSource
* @param {Object} options Options object.
2015-11-05 02:47:41 +03:00
* @param {String} options.url URL of the image
* @param {Boolean} [options.buildPyramid=true] If set to true (default), a
2015-11-03 02:35:11 +03:00
* pyramid will be built internally to provide a better downsampling.
* @param {String|Boolean} [options.crossOriginPolicy=false] Valid values are
* 'Anonymous', 'use-credentials', and false. If false, image requests will
* not use CORS preventing internal pyramid building for images from other
* domains.
* @param {String|Boolean} [options.ajaxWithCredentials=false] Whether to set
* the withCredentials XHR flag for AJAX requests (when loading tile sources).
* @param {Boolean} [options.useCanvas=true] Set to false to prevent any use
* of the canvas API.
2015-11-01 18:23:02 +03:00
*/
$.ImageTileSource = function (options) {
2015-11-05 02:47:41 +03:00
options = $.extend({
buildPyramid: true,
crossOriginPolicy: false,
ajaxWithCredentials: false,
useCanvas: true
2015-11-05 02:47:41 +03:00
}, options);
2015-11-01 18:23:02 +03:00
$.TileSource.apply(this, [options]);
};
2016-09-28 09:40:28 +03:00
/* IE8 fix for tileSources type: 'image' */
$.getNatural = function (DOMelement) {
var img = new Image();
img.src = DOMelement.src;
return { width: img.width, height: img.height };
};
2015-11-01 18:23:02 +03:00
$.extend($.ImageTileSource.prototype, $.TileSource.prototype, /** @lends OpenSeadragon.ImageTileSource.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.type && data.type === "image";
},
/**
*
* @function
2015-11-01 21:25:25 +03:00
* @param {Object} options - the options
2015-11-01 18:23:02 +03:00
* @param {String} dataUrl - the url the image was retreived from, if any.
* @return {Object} options - A dictionary of keyword arguments sufficient
* to configure this tile sources constructor.
*/
2015-11-01 21:25:25 +03:00
configure: function (options, dataUrl) {
return options;
2015-11-01 18:23:02 +03:00
},
/**
* Responsible for retrieving, and caching the
* image metadata pertinent to this TileSources implementation.
* @function
* @param {String} url
* @throws {Error}
*/
getImageInfo: function (url) {
2015-11-10 01:57:39 +03:00
var image = this._image = new Image();
2015-11-01 18:23:02 +03:00
var _this = this;
2015-11-05 02:47:41 +03:00
if (this.crossOriginPolicy) {
image.crossOrigin = this.crossOriginPolicy;
2015-11-01 21:25:25 +03:00
}
2015-11-05 02:47:41 +03:00
if (this.ajaxWithCredentials) {
image.useCredentials = this.ajaxWithCredentials;
2015-11-01 21:25:25 +03:00
}
2015-11-03 02:35:11 +03:00
$.addEvent(image, 'load', function () {
2016-09-28 09:40:28 +03:00
_this.width = $.getNatural(image).width;
_this.height = $.getNatural(image).height;
2015-11-01 18:23:02 +03:00
_this.aspectRatio = _this.width / _this.height;
_this.dimensions = new $.Point(_this.width, _this.height);
_this._tileWidth = _this.width;
_this._tileHeight = _this.height;
_this.tileOverlap = 0;
_this.minLevel = 0;
2015-11-06 06:16:20 +03:00
_this.levels = _this._buildLevels();
_this.maxLevel = _this.levels.length - 1;
2015-11-01 18:23:02 +03:00
_this.ready = true;
2016-01-07 02:28:40 +03:00
// Note: this event is documented elsewhere, in TileSource
2015-11-01 18:23:02 +03:00
_this.raiseEvent('ready', {tileSource: _this});
});
2015-11-03 02:35:11 +03:00
$.addEvent(image, 'error', function () {
// Note: this event is documented elsewhere, in TileSource
2015-11-01 18:23:02 +03:00
_this.raiseEvent('open-failed', {
message: "Error loading image at " + url,
source: url
});
});
image.src = url;
},
/**
* @function
* @param {Number} level
*/
getLevelScale: function (level) {
var levelScale = NaN;
if (level >= this.minLevel && level <= this.maxLevel) {
levelScale =
this.levels[level].width /
this.levels[this.maxLevel].width;
}
return levelScale;
2015-11-01 18:23:02 +03:00
},
/**
* @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);
}
},
/**
2015-11-03 02:35:11 +03:00
* Retrieves a tile url
2015-11-01 18:23:02 +03:00
* @function
2015-11-03 02:35:11 +03:00
* @param {Number} level Level of the tile
* @param {Number} x x coordinate of the tile
* @param {Number} y y coordinate of the tile
2015-11-01 18:23:02 +03:00
*/
getTileUrl: function (level, x, y) {
var url = null;
if (level >= this.minLevel && level <= this.maxLevel) {
url = this.levels[level].url;
}
return url;
2015-11-03 02:35:11 +03:00
},
/**
* Retrieves a tile context 2D
* @function
* @param {Number} level Level of the tile
* @param {Number} x x coordinate of the tile
* @param {Number} y y coordinate of the tile
*/
getContext2D: function (level, x, y) {
var context = null;
if (level >= this.minLevel && level <= this.maxLevel) {
context = this.levels[level].context2D;
}
return context;
},
2015-11-13 00:55:47 +03:00
// private
//
// Builds the differents levels of the pyramid if possible
// (i.e. if canvas API enabled and no canvas tainting issue).
2015-11-06 06:16:20 +03:00
_buildLevels: function () {
2015-11-03 02:35:11 +03:00
var levels = [{
2015-11-10 01:57:39 +03:00
url: this._image.src,
2016-09-28 09:40:28 +03:00
width: $.getNatural(this._image).width,
height: $.getNatural(this._image).height
2015-11-03 02:35:11 +03:00
}];
2015-11-05 02:47:41 +03:00
if (!this.buildPyramid || !$.supportsCanvas || !this.useCanvas) {
2015-11-06 06:16:20 +03:00
// We don't need the image anymore. Allows it to be GC.
2015-11-10 01:57:39 +03:00
delete this._image;
2015-11-03 02:35:11 +03:00
return levels;
}
2015-11-10 01:57:39 +03:00
var currentWidth = this._image.naturalWidth;
var currentHeight = this._image.naturalHeight;
2015-11-03 02:35:11 +03:00
var bigCanvas = document.createElement("canvas");
var bigContext = bigCanvas.getContext("2d");
2015-11-03 02:35:11 +03:00
bigCanvas.width = currentWidth;
bigCanvas.height = currentHeight;
2015-11-10 01:57:39 +03:00
bigContext.drawImage(this._image, 0, 0, currentWidth, currentHeight);
2015-11-05 02:47:41 +03:00
// We cache the context of the highest level because the browser
// is a lot faster at downsampling something it already has
// downsampled before.
levels[0].context2D = bigContext;
2015-11-06 06:16:20 +03:00
// We don't need the image anymore. Allows it to be GC.
2015-11-10 01:57:39 +03:00
delete this._image;
2015-11-05 02:47:41 +03:00
if ($.isCanvasTainted(bigCanvas)) {
2015-11-03 02:35:11 +03:00
// If the canvas is tainted, we can't compute the pyramid.
return levels;
}
2015-11-05 02:47:41 +03:00
// We build smaller levels until either width or height becomes
// 1 pixel wide.
while (currentWidth >= 2 && currentHeight >= 2) {
2015-11-03 02:35:11 +03:00
currentWidth = Math.floor(currentWidth / 2);
currentHeight = Math.floor(currentHeight / 2);
var smallCanvas = document.createElement("canvas");
var smallContext = smallCanvas.getContext("2d");
2015-11-03 02:35:11 +03:00
smallCanvas.width = currentWidth;
smallCanvas.height = currentHeight;
smallContext.drawImage(bigCanvas, 0, 0, currentWidth, currentHeight);
2015-11-03 02:35:11 +03:00
levels.splice(0, 0, {
context2D: smallContext,
2015-11-03 02:35:11 +03:00
width: currentWidth,
height: currentHeight
});
bigCanvas = smallCanvas;
bigContext = smallContext;
}
return levels;
}
2015-11-03 02:35:11 +03:00
});
2015-11-01 18:23:02 +03:00
}(OpenSeadragon));