Skip to content

Commit

Permalink
fix: support some malformed images without StripByteCounts
Browse files Browse the repository at this point in the history
Closes: #38
  • Loading branch information
targos committed Nov 5, 2021
1 parent 0cbe52b commit 56058a9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
Binary file added img/black.tif
Binary file not shown.
8 changes: 8 additions & 0 deletions src/__tests__/decode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions src/hacks.ts
Original file line number Diff line number Diff line change
@@ -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);
}
3 changes: 2 additions & 1 deletion src/tiffDecoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IOBuffer } from 'iobuffer';

import { guessStripByteCounts } from './hacks';
import {
applyHorizontalDifferencing8Bit,
applyHorizontalDifferencing16Bit,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 56058a9

Please sign in to comment.