Skip to content

Commit

Permalink
switch to a closed form computation - it passed all the tests, what c…
Browse files Browse the repository at this point in the history
…ould go wrong?
  • Loading branch information
froyo-np committed Nov 20, 2024
1 parent b569d13 commit 2e49dc7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/dzi/src/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
20 changes: 14 additions & 6 deletions packages/dzi/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

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;
Expand Down

0 comments on commit 2e49dc7

Please sign in to comment.