geotiffjs / geotiff.js

geotiff.js is a small library to parse TIFF files for visualization or analysis. It is written in pure JavaScript, and is usable in both the browser and node.js applications.
https://geotiffjs.github.io/
MIT License
845 stars 175 forks source link

fix: Fallback to format=1 if not format for sample index #420

Open manzt opened 5 months ago

manzt commented 5 months ago

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:

image

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.

image

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.

manzt commented 5 months ago

Steps to reproduce:

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/geotiff@2.1.3/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/geotiff@2.1.3/node_modules/geotiff/dist-module/geotiffimage.js:337:11)
    at GeoTIFFImage._readRaster (file:///Users/manzt/demos/tgo/node_modules/.pnpm/geotiff@2.1.3/node_modules/geotiff/dist-module/geotiffimage.js:465:31)
    at GeoTIFFImage.readRasters (file:///Users/manzt/demos/tgo/node_modules/.pnpm/geotiff@2.1.3/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