geotiff.js
geotiff.js copied to clipboard
fix: Fallback to format=1 if not format for sample index
It seems the intent this code in getReaderForSample is to always ensure format is defined. However, I ran into an edge case when loading an OME-TIFF where for some reason SampleFormat is defined but the format for specific sample index is missing, causing format: undefined, and throwing:
Error: Unsupported data format/bitsPerSample
A snapshot of this edge case from the debugger:
You can see format: undefined. This PR keeps the current behavior but falls back to format: 1 in the additional case where SampleFormat is defined but there is no entry for the sampleIndex.
Apologies for the screenshots. The changes are very minimal, I just want to make sure this is the intended behavior. It seems the intent of the code is to always have format defined, but this is an edge case where it is not.
Steps to reproduce:
- Download https://cf.10xgenomics.com/samples/xenium/1.0.1/Xenium_FFPE_Human_Breast_Cancer_Rep1/Xenium_FFPE_Human_Breast_Cancer_Rep1_he_image.ome.tif
mkdir geotiff-bug
cd geotiff-bug
npm init -y
npm install geotiff
touch index.js # add following code
// index.mjs
import * as GeoTIFF from "geotiff";
let tiff = await GeoTIFF.fromFile("../../Downloads/Xenium_FFPE_Human_Breast_Cancer_Rep1_he_image.ome.tif");
let img = await tiff.getImage();
let data = await img.readRasters({ window: [0, 0, 512, 512], interleave: true });
console.log(data);
node index.mjs
file:///Users/manzt/demos/tgo/node_modules/.pnpm/[email protected]/node_modules/geotiff/dist-module/geotiffimage.js:337
throw Error('Unsupported data format/bitsPerSample');
^
Error: Unsupported data format/bitsPerSample
at GeoTIFFImage.getReaderForSample (file:///Users/manzt/demos/tgo/node_modules/.pnpm/[email protected]/node_modules/geotiff/dist-module/geotiffimage.js:337:11)
at GeoTIFFImage._readRaster (file:///Users/manzt/demos/tgo/node_modules/.pnpm/[email protected]/node_modules/geotiff/dist-module/geotiffimage.js:465:31)
at GeoTIFFImage.readRasters (file:///Users/manzt/demos/tgo/node_modules/.pnpm/[email protected]/node_modules/geotiff/dist-module/geotiffimage.js:616:31)
at async file:///Users/manzt/demos/tgo/index.mjs:5:12
Node.js v20.10.0