1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
23×
23×
23×
23×
20×
20×
20×
20×
17×
28×
28×
17×
2×
2×
2×
1×
1×
1×
2×
1×
19×
19×
19×
19×
19×
19×
29×
29×
19×
19×
19×
19×
19×
1×
19×
1×
19×
14×
1×
13×
13×
13×
13×
13×
13×
13×
52×
19×
19×
1×
1×
3×
3×
1×
1×
5×
1×
4×
4×
4×
1×
1×
1×
1×
1×
3×
1×
| goog.provide('ol.source.Zoomify');
goog.require('ol');
goog.require('ol.ImageTile');
goog.require('ol.TileState');
goog.require('ol.TileUrlFunction');
goog.require('ol.asserts');
goog.require('ol.dom');
goog.require('ol.extent');
goog.require('ol.source.TileImage');
goog.require('ol.tilegrid.TileGrid');
/**
* @classdesc
* Layer source for tile data in Zoomify format.
*
* @constructor
* @extends {ol.source.TileImage}
* @param {olx.source.ZoomifyOptions=} opt_options Options.
* @api
*/
ol.source.Zoomify = function(opt_options) {
var options = opt_options || {};
var size = options.size;
var tierSizeCalculation = options.tierSizeCalculation !== undefined ?
options.tierSizeCalculation :
ol.source.Zoomify.TierSizeCalculation_.DEFAULT;
var imageWidth = size[0];
var imageHeight = size[1];
var tierSizeInTiles = [];
var tileSize = ol.DEFAULT_TILE_SIZE;
switch (tierSizeCalculation) {
case ol.source.Zoomify.TierSizeCalculation_.DEFAULT:
while (imageWidth > tileSize || imageHeight > tileSize) {
tierSizeInTiles.push([
Math.ceil(imageWidth / tileSize),
Math.ceil(imageHeight / tileSize)
]);
tileSize += tileSize;
}
break;
case ol.source.Zoomify.TierSizeCalculation_.TRUNCATED:
var width = imageWidth;
var height = imageHeight;
while (width > tileSize || height > tileSize) {
tierSizeInTiles.push([
Math.ceil(width / tileSize),
Math.ceil(height / tileSize)
]);
width >>= 1;
height >>= 1;
}
break;
default:
ol.asserts.assert(false, 53); // Unknown `tierSizeCalculation` configured
break;
}
tierSizeInTiles.push([1, 1]);
tierSizeInTiles.reverse();
var resolutions = [1];
var tileCountUpToTier = [0];
var i, ii;
for (i = 1, ii = tierSizeInTiles.length; i < ii; i++) {
resolutions.push(1 << i);
tileCountUpToTier.push(
tierSizeInTiles[i - 1][0] * tierSizeInTiles[i - 1][1] +
tileCountUpToTier[i - 1]
);
}
resolutions.reverse();
var extent = [0, -size[1], size[0], 0];
var tileGrid = new ol.tilegrid.TileGrid({
extent: extent,
origin: ol.extent.getTopLeft(extent),
resolutions: resolutions
});
var url = options.url;
if (url && url.indexOf('{TileGroup}') == -1) {
url += '{TileGroup}/{z}-{x}-{y}.jpg';
}
var urls = ol.TileUrlFunction.expandUrl(url);
/**
* @param {string} template Template.
* @return {ol.TileUrlFunctionType} Tile URL function.
*/
function createFromTemplate(template) {
return (
/**
* @param {ol.TileCoord} tileCoord Tile Coordinate.
* @param {number} pixelRatio Pixel ratio.
* @param {ol.proj.Projection} projection Projection.
* @return {string|undefined} Tile URL.
*/
function(tileCoord, pixelRatio, projection) {
if (!tileCoord) {
return undefined;
} else {
var tileCoordZ = tileCoord[0];
var tileCoordX = tileCoord[1];
var tileCoordY = -tileCoord[2] - 1;
var tileIndex =
tileCoordX +
tileCoordY * tierSizeInTiles[tileCoordZ][0] +
tileCountUpToTier[tileCoordZ];
var tileGroup = (tileIndex / ol.DEFAULT_TILE_SIZE) | 0;
var localContext = {
'z': tileCoordZ,
'x': tileCoordX,
'y': tileCoordY,
'TileGroup': 'TileGroup' + tileGroup
};
return template.replace(/\{(\w+?)\}/g, function(m, p) {
return localContext[p];
});
}
});
}
var tileUrlFunction = ol.TileUrlFunction.createFromTileUrlFunctions(urls.map(createFromTemplate));
ol.source.TileImage.call(this, {
attributions: options.attributions,
cacheSize: options.cacheSize,
crossOrigin: options.crossOrigin,
logo: options.logo,
projection: options.projection,
reprojectionErrorThreshold: options.reprojectionErrorThreshold,
tileClass: ol.source.Zoomify.Tile_,
tileGrid: tileGrid,
tileUrlFunction: tileUrlFunction
});
};
ol.inherits(ol.source.Zoomify, ol.source.TileImage);
/**
* @constructor
* @extends {ol.ImageTile}
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @param {ol.TileState} state State.
* @param {string} src Image source URI.
* @param {?string} crossOrigin Cross origin.
* @param {ol.TileLoadFunctionType} tileLoadFunction Tile load function.
* @private
*/
ol.source.Zoomify.Tile_ = function(
tileCoord, state, src, crossOrigin, tileLoadFunction) {
ol.ImageTile.call(this, tileCoord, state, src, crossOrigin, tileLoadFunction);
/**
* @private
* @type {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement}
*/
this.zoomifyImage_ = null;
};
ol.inherits(ol.source.Zoomify.Tile_, ol.ImageTile);
/**
* @inheritDoc
*/
ol.source.Zoomify.Tile_.prototype.getImage = function() {
if (this.zoomifyImage_) {
return this.zoomifyImage_;
}
var tileSize = ol.DEFAULT_TILE_SIZE;
var image = ol.ImageTile.prototype.getImage.call(this);
if (this.state == ol.TileState.LOADED) {
Iif (image.width == tileSize && image.height == tileSize) {
this.zoomifyImage_ = image;
return image;
} else {
var context = ol.dom.createCanvasContext2D(tileSize, tileSize);
context.drawImage(image, 0, 0);
this.zoomifyImage_ = context.canvas;
return context.canvas;
}
} else {
return image;
}
};
/**
* @enum {string}
* @private
*/
ol.source.Zoomify.TierSizeCalculation_ = {
DEFAULT: 'default',
TRUNCATED: 'truncated'
};
|