Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Last active October 29, 2019 23:58
Show Gist options
  • Save lovasoa/a1442d684a6cabb6e7fe790e4f765f02 to your computer and use it in GitHub Desktop.
Save lovasoa/a1442d684a6cabb6e7fe790e4f765f02 to your computer and use it in GitHub Desktop.
zoomify viewer initialization function
function get_tile_counts(width, height, tile_width, tile_height, numtiles) {
var level_count = 0, level_widths=[], level_heights=[], level_tile_count=[], level_tile_count_x=[], level_tile_count_y=[];
var imageW = width;
var imageH = height;
var tileWidth = tile_width;
var tileHeight = tile_height;
width = imageW;
height = imageH;
while (width > tileWidth || height > tileHeight) {
width /= 2;
height /= 2;
level_count++;
}
width = imageW;
height = imageH;
var total_tiles = 0;
for (i = level_count - 1; 0 <= i; i--) {
level_widths[i] = width;
level_heights[i] = height;
level_tile_count_x[i] = Math.ceil(level_widths[i] / tileWidth);
level_tile_count_y[i] = Math.ceil(level_heights[i] / tileHeight);
level_tile_count[i] = level_tile_count_x[i] * level_tile_count_y[i];
width /= 2;
height /= 2;
total_tiles += level_tile_count[i];
}
if (total_tiles != numtiles) {
level_widths = [];
level_heights = [];
level_tile_count_x = [];
level_tile_count_y = [];
level_count = 1;
width = imageW;
height = imageH;
var level_size_ratio = 2;
while (width > tileWidth || height > tileHeight) {
width = Math.floor(imageW / level_size_ratio);
height = Math.floor(imageH / level_size_ratio);
level_size_ratio *= 2;
if (width % 2) width++;
if (height % 2) height++;
level_count++;
}
width = imageW;
height = imageH;
level_size_ratio = 2;
var tileCounter = 0;
for (i = level_count - 1; 0 <= i; i--) {
level_tile_count_x[i] = Math.floor(width / tileWidth);
if (width % tileWidth) level_tile_count_x[i]++;
level_tile_count_y[i] = Math.floor(height / tileHeight);
if (height % tileHeight) level_tile_count_y[i]++;
level_tile_count[i] = level_tile_count_x[i] * level_tile_count_y[i];
tileCounter += level_tile_count[i];
level_widths[i] = width;
level_heights[i] = height;
width = Math.floor(imageW / level_size_ratio);
height = Math.floor(imageH / level_size_ratio);
level_size_ratio *= 2;
if (width % 2) width++;
if (height % 2) height++;
}
if (tileCounter != numtiles) {
console.log("Image tile count does not match value in image XML. If the count is invalid display problems can result.");
}
}
return {level_tile_count_y, level_tile_count_x, level_tile_count, level_widths, level_heights}
}
function init_image(width, height, tile_width, tile_height, numtiles, c, e, U, m, h, l) {
Z.useCanvas && (x && (Q = x.getContext("2d")), La = B.getContext("2d"), R = F.getContext("2d"), Ya = ca.getContext("2d"));
Z.imageW = width;
Z.imageH = height;
Z.imageCtrX = Z.imageW / 2;
Z.imageCtrY = Z.imageH / 2;
Z.imageD = Math.round(Math.sqrt(width * width + height * height));
tileWidth = tile_width;
tileHeight = tile_height;
width = Z.imageW;
height = Z.imageH;
while (width > tileWidth || height > tileHeight) {
width /= 2;
height /= 2;
level_count++;
}
width = Z.imageW;
height = Z.imageH;
var total_tiles = 0;
for (i = level_count - 1; 0 <= i; i--) {
level_widths[i] = width;
level_heights[i] = height;
level_tile_count_x[i] = Math.ceil(level_widths[i] / tileWidth);
level_tile_count_y[i] = Math.ceil(level_heights[i] / tileHeight);
level_tile_count[i] = level_tile_count_x[i] * level_tile_count_y[i];
width /= 2;
height /= 2;
total_tiles += level_tile_count[i];
}
if (total_tiles != numtiles && ("ZoomifyImageFolder" == Z.tileSource || "ZoomifyImageFile" == Z.tileSource || "ZoomifyPFFFile" == Z.tileSource)) {
level_widths = [];
level_heights = [];
level_tile_count_x = [];
level_tile_count_y = [];
level_count = 1;
width = Z.imageW;
height = Z.imageH;
level_size_ratio = 2;
while (width > tileWidth || height > tileHeight) {
width = Math.floor(Z.imageW / level_size_ratio);
height = Math.floor(Z.imageH / level_size_ratio);
level_size_ratio *= 2;
if (width % 2) width++;
if (height % 2) height++;
level_count++;
}
width = Z.imageW;
height = Z.imageH;
level_size_ratio = 2;
var tileCounter = 0;
for (i = level_count - 1; 0 <= i; i--) {
level_tile_count_x[i] = Math.floor(width / tileWidth);
if (width % tileWidth) level_tile_count_x[i]++;
level_tile_count_y[i] = Math.floor(height / tileHeight);
if (height % tileHeight) level_tile_count_y[i]++;
level_tile_count[i] = level_tile_count_x[i] * level_tile_count_y[i];
tileCounter += level_tile_count[i];
level_widths[i] = width;
level_heights[i] = height;
width = Math.floor(Z.imageW / level_size_ratio);
height = Math.floor(Z.imageH / level_size_ratio);
level_size_ratio *= 2;
if (width % 2) width++;
if (height % 2) height++;
}
if (tileCounter != numtiles) {
Z.Utils.showMessage("Image tile count does not match value in image XML. If the count is invalid display problems can result.", false, Z.messageDurationStandard, "center");
}
}
z(C, A, Xb, Yb);
k.validateXYZDefaults();
level_count > Zb ? (aa = pa(qa, Z.initialZ), x && Q.scale(aa, aa)) : Q = Za = x = null;
Ba = pa(L, Z.initialZ);
Ma = D = pa(v, Z.initialZ);
if (Z.useCanvas) try {
R.scale(D, D)
} catch (p) {
console.log("In function z751Continue scaling canvas: " + p)
}
k.z619();
total_tiles = Z.initialX;
i = Z.initialY;
width = Z.initialZ;
height = Z.initialR;
if ("undefined" === typeof total_tiles || null === total_tiles) total_tiles = Z.imageW / 2;
if ("undefined" === typeof i ||
null === i) i = Z.imageH / 2;
"undefined" === typeof width || null === width ? width = Z.fitZ : 1 < width && width > Z.maxZ && (width /= 100);
if ("undefined" === typeof height || null === height) height = Z.imageR;
width = eb(width);
total_tiles = Na(total_tiles, i, width, height, "image");
Z.imageX = image_x = total_tiles.x;
Z.imageY = image_y = total_tiles.y;
Z.imageZ = width;
height != Z.imageR && Z.Utils.rotateElement(n, height);
Z.imageR = height;
k.updateView(!0);
Z.Utils.addEventListener(y, "mousedown", ja);
Z.Utils.addEventListener(y, "mousemove", Z.Utils.preventDefault);
Z.Utils.addEventListener(y, "touchstart", ja);
Z.Utils.addEventListener(y, "touchmove", ja);
Z.Utils.addEventListener(y, "touchend",
ja);
Z.Utils.addEventListener(y, "touchcancel", ja);
Z.Utils.addEventListener(y, "gesturestart", ja);
Z.Utils.addEventListener(y, "gesturechange", ja);
Z.Utils.addEventListener(y, "gestureend", ja);
Z.Utils.addEventListener(B, "contextmenu", Z.Utils.preventDefault);
Z.Utils.addEventListener(F, "contextmenu", Z.Utils.preventDefault);
k.setStatus("initializedViewport", !0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment