Skip to content

Commit fa75154

Browse files
committed
be more forgiving when fileDirectory.SampleFormat does not have all values populated, such as in bioformats ome tif files
1 parent f15d770 commit fa75154

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/geotiffimage.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/** @module geotiffimage */
2-
import { getFloat16 } from '@petamoriken/float16';
2+
import {getFloat16} from '@petamoriken/float16';
33
import getAttribute from 'xml-utils/get-attribute.js';
44
import findTagsByName from 'xml-utils/find-tags-by-name.js';
55

6-
import { photometricInterpretations, ExtraSamplesValues } from './globals.js';
7-
import { fromWhiteIsZero, fromBlackIsZero, fromPalette, fromCMYK, fromYCbCr, fromCIELab } from './rgb.js';
8-
import { getDecoder } from './compression/index.js';
9-
import { resample, resampleInterleaved } from './resample.js';
6+
import {photometricInterpretations, ExtraSamplesValues} from './globals.js';
7+
import {fromWhiteIsZero, fromBlackIsZero, fromPalette, fromCMYK, fromYCbCr, fromCIELab} from './rgb.js';
8+
import {getDecoder} from './compression/index.js';
9+
import {resample, resampleInterleaved} from './resample.js';
1010

1111
/**
1212
* @typedef {Object} ReadRasterOptions
@@ -295,8 +295,8 @@ class GeoTIFFImage {
295295
}
296296

297297
getReaderForSample(sampleIndex) {
298-
const format = this.fileDirectory.SampleFormat
299-
? this.fileDirectory.SampleFormat[sampleIndex] : 1;
298+
const format = this.getSampleFormat(sampleIndex);
299+
300300
const bitsPerSample = this.fileDirectory.BitsPerSample[sampleIndex];
301301
switch (format) {
302302
case 1: // unsigned integer data
@@ -339,7 +339,8 @@ class GeoTIFFImage {
339339

340340
getSampleFormat(sampleIndex = 0) {
341341
return this.fileDirectory.SampleFormat
342-
? this.fileDirectory.SampleFormat[sampleIndex] : 1;
342+
? (this.fileDirectory.SampleFormat[sampleIndex] ?? Math.max.apply(null, this.fileDirectory.SampleFormat))
343+
: 1;
343344
}
344345

345346
getBitsPerSample(sampleIndex = 0) {
@@ -366,7 +367,7 @@ class GeoTIFFImage {
366367
const numTilesPerRow = Math.ceil(this.getWidth() / this.getTileWidth());
367368
const numTilesPerCol = Math.ceil(this.getHeight() / this.getTileHeight());
368369
let index;
369-
const { tiles } = this;
370+
const {tiles} = this;
370371
if (this.planarConfiguration === 1) {
371372
index = (y * numTilesPerRow) + x;
372373
} else if (this.planarConfiguration === 2) {
@@ -382,11 +383,11 @@ class GeoTIFFImage {
382383
offset = this.fileDirectory.StripOffsets[index];
383384
byteCount = this.fileDirectory.StripByteCounts[index];
384385
}
385-
const slice = (await this.source.fetch([{ offset, length: byteCount }], signal))[0];
386+
const slice = (await this.source.fetch([{offset, length: byteCount}], signal))[0];
386387

387388
let request;
388389
if (tiles === null || !tiles[index]) {
389-
// resolve each request by potentially applying array normalization
390+
// resolve each request by potentially applying array normalization
390391
request = (async () => {
391392
let data = await poolOrDecoder.decode(this.fileDirectory, slice);
392393
const sampleFormat = this.getSampleFormat();
@@ -415,7 +416,7 @@ class GeoTIFFImage {
415416
}
416417

417418
// cache the tile request
418-
return { x, y, sample, data: await request };
419+
return {x, y, sample, data: await request};
419420
}
420421

421422
/**
@@ -434,7 +435,7 @@ class GeoTIFFImage {
434435
* @returns {Promise<ReadRasterResult>}
435436
*/
436437
async _readRaster(imageWindow, samples, valueArrays, interleave, poolOrDecoder, width,
437-
height, resampleMethod, signal) {
438+
height, resampleMethod, signal) {
438439
const tileWidth = this.getTileWidth();
439440
const tileHeight = this.getTileHeight();
440441
const imageWidth = this.getWidth();
@@ -466,7 +467,7 @@ class GeoTIFFImage {
466467
}
467468

468469
const promises = [];
469-
const { littleEndian } = this;
470+
const {littleEndian} = this;
470471

471472
for (let yTile = minYTile; yTile < maxYTile; ++yTile) {
472473
for (let xTile = minXTile; xTile < maxXTile; ++xTile) {
@@ -517,7 +518,7 @@ class GeoTIFFImage {
517518
await Promise.all(promises);
518519

519520
if ((width && (imageWindow[2] - imageWindow[0]) !== width)
520-
|| (height && (imageWindow[3] - imageWindow[1]) !== height)) {
521+
|| (height && (imageWindow[3] - imageWindow[1]) !== height)) {
521522
let resampled;
522523
if (interleave) {
523524
resampled = resampleInterleaved(
@@ -558,9 +559,9 @@ class GeoTIFFImage {
558559
* @returns {Promise<ReadRasterResult>} the decoded arrays as a promise
559560
*/
560561
async readRasters({
561-
window: wnd, samples = [], interleave, pool = null,
562-
width, height, resampleMethod, fillValue, signal,
563-
} = {}) {
562+
window: wnd, samples = [], interleave, pool = null,
563+
width, height, resampleMethod, fillValue, signal,
564+
} = {}) {
564565
const imageWindow = wnd || [0, 0, this.getWidth(), this.getHeight()];
565566

566567
// check parameters
@@ -638,8 +639,10 @@ class GeoTIFFImage {
638639
* to be aborted
639640
* @returns {Promise<ReadRasterResult>} the RGB array as a Promise
640641
*/
641-
async readRGB({ window, interleave = true, pool = null, width, height,
642-
resampleMethod, enableAlpha = false, signal } = {}) {
642+
async readRGB({
643+
window, interleave = true, pool = null, width, height,
644+
resampleMethod, enableAlpha = false, signal
645+
} = {}) {
643646
const imageWindow = window || [0, 0, this.getWidth(), this.getHeight()];
644647

645648
// check parameters
@@ -697,7 +700,7 @@ class GeoTIFFImage {
697700
resampleMethod,
698701
signal,
699702
};
700-
const { fileDirectory } = this;
703+
const {fileDirectory} = this;
701704
const raster = await this.readRasters(subOptions);
702705

703706
const max = 2 ** this.fileDirectory.BitsPerSample[0];

0 commit comments

Comments
 (0)