diff --git a/img/black.tif b/img/black.tif new file mode 100644 index 0000000..a055375 Binary files /dev/null and b/img/black.tif differ diff --git a/src/__tests__/decode.test.ts b/src/__tests__/decode.test.ts index 2ff6ad6..0b8d9aa 100644 --- a/src/__tests__/decode.test.ts +++ b/src/__tests__/decode.test.ts @@ -149,6 +149,14 @@ const files: TiffFile[] = [ components: 1, alpha: false, }, + { + name: 'black.tif', + width: 9192, + height: 2690, + bitsPerSample: 16, + components: 1, + alpha: false, + }, ]; const cases = files.map( (file) => [file.name, file, readImage(file.name)] as const, diff --git a/src/hacks.ts b/src/hacks.ts new file mode 100644 index 0000000..25932c0 --- /dev/null +++ b/src/hacks.ts @@ -0,0 +1,15 @@ +import TiffIfd from './tiffIfd'; + +export function guessStripByteCounts(ifd: TiffIfd): number[] { + if (ifd.compression !== 1) { + throw new Error( + 'missing mandatory StripByteCounts field in compressed image', + ); + } + const bytesPerStrip = + ifd.rowsPerStrip * + ifd.width * + ifd.samplesPerPixel * + (ifd.bitsPerSample / 8); + return new Array(ifd.stripOffsets.length).fill(bytesPerStrip); +} diff --git a/src/tiffDecoder.ts b/src/tiffDecoder.ts index 3e77871..0889430 100644 --- a/src/tiffDecoder.ts +++ b/src/tiffDecoder.ts @@ -1,5 +1,6 @@ import { IOBuffer } from 'iobuffer'; +import { guessStripByteCounts } from './hacks'; import { applyHorizontalDifferencing8Bit, applyHorizontalDifferencing16Bit, @@ -202,7 +203,7 @@ export default class TIFFDecoder extends IOBuffer { const rowsPerStrip = ifd.rowsPerStrip; const maxPixels = rowsPerStrip * width * ifd.samplesPerPixel; const stripOffsets = ifd.stripOffsets; - const stripByteCounts = ifd.stripByteCounts; + const stripByteCounts = ifd.stripByteCounts || guessStripByteCounts(ifd); let remainingPixels = size; let pixel = 0;