Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dzi viewer would loop forever due to some faulty math #43

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
switch to a closed form computation - it passed all the tests, what c…
…ould go wrong?
froyo-np committed Nov 20, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 2e49dc709b0bcf0a31c144342e7f0f3193ee9d9d
2 changes: 1 addition & 1 deletion packages/dzi/src/loader.test.ts
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ describe('tiling math', () => {
const pretend_max_image_width = 512;
expect(firstSuitableLayer(pretend_max_image_width, 4096)).toEqual(9);
});
it('never picks a layer that cant exist', () => {
it('never picks a layer that cant exist (negative layer indexes)', () => {
expect(firstSuitableLayer(512, 0.00001)).toEqual(0);
});
});
20 changes: 14 additions & 6 deletions packages/dzi/src/loader.ts
Original file line number Diff line number Diff line change
@@ -121,15 +121,23 @@ export function tileWithOverlap(total: number, step: number, overlap: number): I
function boxFromRowCol(row: Interval, col: Interval) {
return Box2D.create([col.min, row.min], [col.max, row.max]);
}

const logBaseHalf = (x: number) => Math.log2(x) / Math.log2(0.5);

export function imageSizeAtLayer(dzi: DziImage, layer: number) {
const { size } = dzi;
const { size: dim } = dzi;
const layerMaxSize = 2 ** (isFinite(layer) ? Math.max(0, layer) : 0);
let total: vec2 = [size.width, size.height];
const size: vec2 = [dim.width, dim.height];
// the question is how many times do we need to divide size
// by 2 to make it less than layerMaxSize?
// solve for N, X = the larger the image dimensions:
// X * (0.5^N) <= maxLayerSize ...
// 0.5^N = maxLayerSize/X ...
// log_0.5(maxLayerSize/X) = N
const bigger = Math.max(size[0], size[1]);
const N = Math.ceil(logBaseHalf(layerMaxSize / bigger))
return Vec2.ceil(Vec2.scale(size, 0.5 ** N));

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue here is that the while loop below will use ceil after dividing the total by 2 - that means that if maxLayerSize was a number less than 1 (for example 2**-1 == 0.5), then the progressive dividing of total would never reach the exit condition, resulting in an infinite loop.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using 0 as a fallback value is fine, because 2**0 = 1

while (total[0] > layerMaxSize || total[1] > layerMaxSize) {
total = Vec2.ceil(Vec2.scale(total, 1 / 2));
}
return total;
}
export function tilesInLayer(dzi: DziImage, layer: number): box2D[][] {
const { overlap, tileSize } = dzi;