/
pixi-tile-layer.js
376 lines (324 loc) · 13.1 KB
/
pixi-tile-layer.js
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
(function(CATMAID) {
"use strict";
/**
* Displays a grid of tiles from an image stack using the Pixi.js renderer.
*
* See CATMAID.TileLayer for parameters.
*
* @class PixiTileLayer
* @extends TileLayer
* @uses PixiLayer
* @constructor
*/
function PixiTileLayer() {
CATMAID.TileLayer.apply(this, arguments);
CATMAID.PixiLayer.call(this);
// If the selected source is block data, return an appropriate layer instead.
if (this.tileSource instanceof CATMAID.AbstractImageBlockSource &&
!(this instanceof CATMAID.PixiImageBlockLayer)) {
this.unregister();
return this.constructCopy({}, CATMAID.PixiImageBlockLayer);
}
// Replace tiles container.
this.stackViewer.getLayersView().removeChild(this.tilesContainer);
this.tilesContainer = this.renderer.view;
this.tilesContainer.className = 'sliceTiles';
this.tileConstructor = PIXI.Sprite;
this.stackViewer.getLayersView().appendChild(this.tilesContainer);
this._oldZoom = 0;
this._oldZ = undefined;
this._tileRequest = {};
this._updatePixiInterpolationMode();
}
PixiTileLayer.prototype = Object.create(CATMAID.TileLayer.prototype);
$.extend(PixiTileLayer.prototype, CATMAID.PixiLayer.prototype); // Mixin/multiple inherit PixiLayer.
PixiTileLayer.prototype.constructor = PixiTileLayer;
/** @inheritdoc */
PixiTileLayer.prototype._handleCanaryCheck = function (accessible) {
if (accessible.cors) {
return;
} else if (accessible.normal) {
CATMAID.warn('Stack mirror is not CORS accessible, so WebGL will not be used.');
this.switchToDomTileLayer();
} else {
CATMAID.TileLayer.prototype._handleCanaryCheck.call(this, accessible);
}
};
PixiTileLayer.prototype._updatePixiInterpolationMode = function () {
let linear = this.getEffectiveInterpolationMode() === CATMAID.StackLayer.INTERPOLATION_MODES.LINEAR;
this._pixiInterpolationMode = linear ? PIXI.SCALE_MODES.LINEAR : PIXI.SCALE_MODES.NEAREST;
};
/** @inheritdoc */
PixiTileLayer.prototype.setInterpolationMode = function (mode) {
CATMAID.StackLayer.prototype.setInterpolationMode.call(this, mode);
this._updatePixiInterpolationMode();
for (var i = 0; i < this._tiles.length; ++i) {
for (var j = 0; j < this._tiles[0].length; ++j) {
var texture = this._tiles[i][j].texture;
if (texture && texture.valid &&
texture.baseTexture.scaleMode !== this._pixiInterpolationMode) {
this._setTextureInterpolationMode(texture, this._pixiInterpolationMode);
}
}
}
this.redraw();
};
/** @inheritdoc */
PixiTileLayer.prototype.unregister = function () {
for (var i = 0; i < this._tiles.length; ++i) {
for (var j = 0; j < this._tiles[0].length; ++j) {
var tile = this._tiles[i][j];
if (tile.texture && tile.texture.valid) {
CATMAID.PixiContext.GlobalTextureManager.dec(tile.texture.baseTexture.imageUrl);
}
}
}
CATMAID.PixiLayer.prototype.unregister.call(this);
};
/**
* Initialise the tiles array and buffer.
*/
PixiTileLayer.prototype._initTiles = function (rows, cols) {
CATMAID.PixiLayer.prototype._initBatchContainer.call(this);
var graphic = new PIXI.Graphics();
graphic.beginFill(0xFFFFFF,0);
graphic.drawRect(0, 0, this.tileWidth, this.tileHeight);
graphic.endFill();
var emptyTex = graphic.generateCanvasTexture();
this._tiles = [];
this._tilesBuffer = [];
this._tileFirstR = 0;
this._tileFirstC = 0;
for (var i = 0; i < rows; ++i) {
this._tiles[i] = [];
this._tilesBuffer[i] = [];
for (var j = 0; j < cols; ++j) {
this._tiles[i][j] = new this.tileConstructor(emptyTex);
this.batchContainer.addChild(this._tiles[i][j]);
this._tiles[i][j].blendMode = PIXI.BLEND_MODES[this.blendMode];
this._tiles[i][j].position.x = j * this.tileWidth * this._anisotropy.x;
this._tiles[i][j].position.y = i * this.tileHeight * this._anisotropy.y;
if (this.tileSource.transposeTiles.has(this.stack.orientation)) {
this._tiles[i][j].scale.x = -1.0;
this._tiles[i][j].rotation = -Math.PI / 2.0;
}
this._tilesBuffer[i][j] = false;
}
}
this.setBlendMode(this.blendMode);
};
/** @inheritdoc */
PixiTileLayer.prototype.redraw = function (completionCallback, blocking) {
var scaledStackPosition = this.stackViewer.scaledPositionInStack(this.stack);
var tileInfo = this.tilesForLocation(
scaledStackPosition.xc,
scaledStackPosition.yc,
scaledStackPosition.z,
scaledStackPosition.s,
this.efficiencyThreshold);
if (this._anisotropy.x !== tileInfo.anisotropy.x ||
this._anisotropy.y !== tileInfo.anisotropy.y) {
return this.resize(this.stackViewer.viewWidth, this.stackViewer.viewHeight, completionCallback, blocking);
}
// By default all needed tiles are shown. This can be changed so that all
// tiles are hidden, e.g. if the current location is on a broken slice and
// CATMAID is configured to hide these sections.
var showTiles = true;
if (this.hideIfNearestSliceBroken) {
// Re-project the stack z without avoiding broken sections to determine
// if the nearest section is broken.
var linearStackZ = this.stack.projectToLinearStackZ(
this.stackViewer.projectCoordinates().z);
if (this.stack.isSliceBroken(linearStackZ)) {
this.batchContainer.visible = false;
showTiles = false;
} else {
this.setOpacity(this.opacity);
}
}
var rows = this._tiles.length, cols = this._tiles[0].length;
// If panning only (no scaling, no browsing through z)
if (this.stackViewer.z == this.stackViewer.old_z &&
this.stackViewer.s == this.stackViewer.old_s)
{
// Compute panning in X and Y
var xd = tileInfo.firstCol - this._tileFirstC;
var yd = tileInfo.firstRow - this._tileFirstR;
// Update the toroidal origin in the tiles array
this._tileOrigR = this.rowTransform(yd);
this._tileOrigC = this.colTransform(xd);
}
this._tileFirstC = tileInfo.firstCol;
this._tileFirstR = tileInfo.firstRow;
var top = tileInfo.top;
var left = tileInfo.left;
// Set tile grid offset and magnification on the whole container, rather than
// individual tiles.
this.batchContainer.position.x = left;
this.batchContainer.position.y = top;
this.batchContainer.scale.x = tileInfo.mag * tileInfo.anisotropy.x;
this.batchContainer.scale.y = tileInfo.mag * tileInfo.anisotropy.y;
var toLoad = [];
var loading = false;
var y = 0;
var slicePixelPosition = [tileInfo.z];
// Clamping to zero can be disable in the stack.
let clamp = this.stack.clamp;
let [minCol, minRow] = clamp ? [0, 0] : [tileInfo.firstCol, tileInfo.firstRow];
// Update tiles.
for (var i = this._tileOrigR, ti = 0; ti < rows; ++ti, i = (i+1) % rows) {
var r = tileInfo.firstRow + ti;
var x = 0;
for (var j = this._tileOrigC, tj = 0; tj < cols; ++tj, j = (j+1) % cols) {
var c = tileInfo.firstCol + tj;
var tile = this._tiles[i][j];
// Set tile positions to handle toroidal wrapping.
tile.position.x = x;
tile.position.y = y;
if (c >= minCol && c <= tileInfo.lastCol &&
r >= minRow && r <= tileInfo.lastRow && showTiles) {
var source = this.tileSource.getTileURL(project, this.stack, slicePixelPosition,
c, r, tileInfo.zoom);
if (source !== tile.texture.baseTexture.imageUrl) {
var texture = PIXI.utils.TextureCache[source];
if (texture) {
if (texture.valid) {
this._tilesBuffer[i][j] = false;
CATMAID.PixiContext.GlobalTextureManager.inc(source);
CATMAID.PixiContext.GlobalTextureManager.dec(tile.texture.baseTexture.imageUrl);
if (texture.baseTexture.scaleMode !== this._pixiInterpolationMode) {
this._setTextureInterpolationMode(texture, this._pixiInterpolationMode);
}
tile.texture = texture;
tile.visible = true;
} else {
loading = true;
tile.visible = false;
}
} else {
tile.visible = false;
toLoad.push(source);
this._tilesBuffer[i][j] = source;
}
} else {
tile.visible = true;
this._tilesBuffer[i][j] = false;
}
} else {
tile.visible = false;
this._tilesBuffer[i][j] = false;
}
x += this.tileWidth;
}
y += this.tileHeight;
}
if (tileInfo.z === this._oldZ &&
tileInfo.zoom === this._oldZoom) {
this._renderIfReady();
}
this._swapZoom = tileInfo.zoom;
this._swapZ = tileInfo.z;
// If any tiles need to be buffered (that are not already being buffered):
if (toLoad.length > 0) {
// Set a timeout for slow connections to swap in the buffer whether or
// not it has loaded. Do this before loading tiles in case they load
// immediately, so that the buffer will be cleared.
window.clearTimeout(this._swapBuffersTimeout);
this._swapBuffersTimeout = window.setTimeout(this._swapBuffers.bind(this, true), 3000);
var newRequest = CATMAID.PixiContext.GlobalTextureManager.load(
toLoad,
this.tileSource.getRequestHeaders(),
this._swapBuffers.bind(this, false, this._swapBuffersTimeout));
CATMAID.PixiContext.GlobalTextureManager.cancel(this._tileRequest);
this._tileRequest = newRequest;
loading = true;
} else if (!loading) {
this._oldZoom = this._swapZoom;
this._oldZ = this._swapZ;
this._renderIfReady();
}
if (typeof completionCallback !== 'undefined') {
if (loading && blocking) {
this._completionCallback = completionCallback;
} else {
this._completionCallback = null;
completionCallback();
}
}
};
/** @inheritdoc */
PixiTileLayer.prototype.resize = function (width, height, completionCallback, blocking) {
CATMAID.PixiLayer.prototype.resize.call(this, width, height);
CATMAID.TileLayer.prototype.resize.call(this, width, height, completionCallback, blocking);
};
/** @inheritdoc */
PixiTileLayer.prototype._swapBuffers = function (force, timeout) {
if (timeout && timeout !== this._swapBuffersTimeout) return;
window.clearTimeout(this._swapBuffersTimeout);
this._swapBuffersTimeout = null;
for (var i = 0; i < this._tiles.length; ++i) {
for (var j = 0; j < this._tiles[0].length; ++j) {
var source = this._tilesBuffer[i][j];
if (source) {
var texture = PIXI.utils.TextureCache[source];
var tile = this._tiles[i][j];
// Check whether the tile is loaded.
if (force || texture && texture.valid) {
this._tilesBuffer[i][j] = false;
CATMAID.PixiContext.GlobalTextureManager.inc(source);
CATMAID.PixiContext.GlobalTextureManager.dec(tile.texture.baseTexture.imageUrl);
tile.texture = texture || PIXI.Texture.fromImage(source);
if (tile.texture.baseTexture.scaleMode !== this._pixiInterpolationMode) {
this._setTextureInterpolationMode(tile.texture, this._pixiInterpolationMode);
}
tile.visible = true;
}
}
}
}
this._oldZoom = this._swapZoom;
this._oldZ = this._swapZ;
this._renderIfReady();
// If the redraw was blocking, its completion callback needs to be invoked
// now that the async redraw is finished.
if (this._completionCallback) {
var completionCallback = this._completionCallback;
this._completionCallback = null;
completionCallback();
}
};
/** @inheritdoc */
PixiTileLayer.prototype.constructCopy = function (override, constructor) {
var copy = CATMAID.TileLayer.prototype.constructCopy.apply(this, arguments);
if (copy instanceof PixiTileLayer) {
this.filters.forEach(function (filter) {
var filterCopy = new filter.constructor(
filter.displayName,
filter.pixiFilter.constructor,
filter.params,
copy);
copy.filters.push(filterCopy);
});
}
return copy;
};
/**
* Switch to a DOM tile layer by replacing this tile layer in the stack viewer
* with a new one.
*/
PixiTileLayer.prototype.switchToDomTileLayer = function () {
var newTileLayer = this.constructCopy({}, CATMAID.TileLayer);
var layerKey = this.stackViewer.getLayerKey(this);
this.stackViewer.replaceStackLayer(layerKey, newTileLayer);
};
/** @inheritdoc */
PixiTileLayer.prototype._tilePixel = function (tile, x, y) {
if (!tile.visible) return Promise.resolve();
var img = tile.texture.baseTexture.source;
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
context.drawImage(img, 0, 0);
return Promise.resolve(context.getImageData(x, y, 1, 1).data);
};
CATMAID.PixiTileLayer = PixiTileLayer;
})(CATMAID);