Skip to content

Commit

Permalink
refactor: default to libvips for lzw decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed Jan 29, 2025
1 parent b52a030 commit f59a1bf
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 24 deletions.
17 changes: 7 additions & 10 deletions packages/tiler-sharp/src/pipeline/__tests__/decompress.lzw.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import assert from 'node:assert';
import { createHash } from 'node:crypto';
import { writeFileSync } from 'node:fs';
import { before, describe, it } from 'node:test';

import { fsa, LogConfig } from '@basemaps/shared';
// import { TestTiff } from '@basemaps/test';
import { TestTiff } from '@basemaps/test';
import { Tiff } from '@cogeotiff/core';

import { LzwDecompressor, LzwDecompressorSharp } from '../decompressor.lzw.js';
import { LzwDecompressorJs, LzwDecompressorSharp } from '../decompressor.lzw.js';

describe('decompressor.lzw', () => {
let tiff: Tiff;
before(async () => {
tiff = await Tiff.create(
fsa.source(fsa.toUrl('file:///home/blacha/data/elevation/nz-8m-dem-2012/8m-dem-small/qd.lzw.tiff')),
);
tiff = await Tiff.create(fsa.source(TestTiff.CompressLzw));
});

it('should decode a 64x64 lzw tile', async () => {
LogConfig.get().level = 'silent';
const hashes: Record<string, { hash: string; duration: number }> = {};
const hashesSharp: Record<string, { hash: string; duration: number }> = {};

let tileCount = 0;
for (const img of tiff.images) {
const imageId = img.id;
for (let x = 0; x < img.tileCount.x; x++) {
Expand All @@ -46,7 +44,7 @@ describe('decompressor.lzw', () => {
};

const startTimeB = performance.now();
const retB = await LzwDecompressor.decompress({
const retB = await LzwDecompressorJs.decompress({
tiff,
imageId,
x,
Expand All @@ -61,11 +59,10 @@ describe('decompressor.lzw', () => {
};

assert.equal(hashLzw, hashSharp, tileId);
tileCount++;
}
}
assert.equal(tileCount, 1);
}
// writeFileSync('./hashes.json', JSON.stringify(hashes, null, 2));

// writeFileSync('./hashesSharp.json', JSON.stringify(hashesSharp, null, 2));
});
});
12 changes: 1 addition & 11 deletions packages/tiler-sharp/src/pipeline/decompressor.lzw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,19 @@ async function toDecompressedInterleaved(
throw new Error('Output has missmatched tile size');
}

export const LzwDecompressor: Decompressor = {
export const LzwDecompressorJs: Decompressor = {
type: 'application/lzw',
async decompress(ctx: DecompressionContext): Promise<DecompressedInterleaved> {
// TODO: this decompressor is very slow it can take over 100ms+ to deocode a 512x512 tile
// for comparison the lerc decompressor takes <1ms for the same amount of data.

console.time('decompress-js');
const bytes = decompress(ctx.bytes);
console.timeEnd('decompress-js');

// console.log(ctx.tiff.source.url.href);
return toDecompressedInterleaved(bytes, ctx);
},
};
export const LzwDecompressorSharp: Decompressor = {
type: 'application/lzw',
async decompress(ctx: DecompressionContext): Promise<DecompressedInterleaved> {
// TODO: this decompressor is very slow it can take over 100ms+ to deocode a 512x512 tile
// for comparison the lerc decompressor takes <1ms for the same amount of data.
const image = ctx.tiff.images[ctx.imageId];
const [bitsPerSample, sampleFormat] = await Promise.all([
image.fetch(TiffTag.BitsPerSample),
Expand All @@ -80,11 +74,7 @@ export const LzwDecompressorSharp: Decompressor = {

const depth = tiffToDepth(bitsPerSample, sampleFormat?.[0]);

console.time('decompress-sharp');
const bytes = await decodeLzwTiff(image, depth, bitsPerSample.length, ctx.bytes);
console.timeEnd('decompress-sharp');

// console.log(ctx.tiff.source.url.href);
return toDecompressedInterleaved(bytes.decoded, ctx);
},
};
4 changes: 2 additions & 2 deletions packages/tiler-sharp/src/pipeline/decompressors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Compression } from '@cogeotiff/core';

import { Decompressor } from './decompressor.js';
import { LercDecompressor } from './decompressor.lerc.js';
import { LzwDecompressor } from './decompressor.lzw.js';
import { LzwDecompressorSharp } from './decompressor.lzw.js';

export const Decompressors: Record<string, Decompressor> = {
[Compression.Lerc]: LercDecompressor,
[Compression.Lzw]: LzwDecompressor,
[Compression.Lzw]: LzwDecompressorSharp,
};
1 change: 0 additions & 1 deletion packages/tiler-sharp/src/pipeline/lzw/lzw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export function decompress(input: ArrayBuffer): Uint8Array {
appendReversed(result, oldVal);
result.push(oldVal[oldVal.length - 1]);
addToDictionary(oldCode as number, oldVal[oldVal.length - 1]);
// console.log(oldCode, oldVal[oldVal.length - 1]);
oldCode = code;
}

Expand Down

0 comments on commit f59a1bf

Please sign in to comment.