Skip to content

Commit

Permalink
fix: Fixed TIFF tile and value decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor-pelykh committed Apr 12, 2024
1 parent 63b9caf commit 0f09f41
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/formats/tiff/tiff-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IfdDoubleValue } from '../../exif/ifd-value/ifd-double-value';
import { IfdLongValue } from '../../exif/ifd-value/ifd-long-value';
import { IfdRationalValue } from '../../exif/ifd-value/ifd-rational-value';
import { IfdSByteValue } from '../../exif/ifd-value/ifd-sbyte-value';
import { IfdShortValue } from '../../exif/ifd-value/ifd-short-value';
import { IfdSingleValue } from '../../exif/ifd-value/ifd-single-value';
import { IfdSLongValue } from '../../exif/ifd-value/ifd-slong-value';
import { IfdSRationalValue } from '../../exif/ifd-value/ifd-srational-value';
Expand Down Expand Up @@ -91,7 +92,7 @@ export class TiffEntry {
case IfdValueType.undefined:
return (this._value = IfdByteValue.data(data, this._count));
case IfdValueType.short:
return (this._value = IfdSShortValue.data(data, this._count));
return (this._value = IfdShortValue.data(data, this._count));
case IfdValueType.long:
return (this._value = IfdLongValue.data(data, this._count));
case IfdValueType.rational:
Expand Down
40 changes: 23 additions & 17 deletions src/formats/tiff/tiff-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,16 +655,8 @@ export class TiffImage {
);
}

for (
let y = 0, py = outY;
y < this._tileHeight && py < this._height;
++y, ++py
) {
for (
let x = 0, px = outX;
x < this._tileWidth && px < this._width;
++x, ++px
) {
for (let y = 0, py = outY; y < this._tileHeight; ++y, ++py) {
for (let x = 0, px = outX; x < this._tileWidth; ++x, ++px) {
if (this._samplesPerPixel === 1) {
if (this._sampleFormat === TiffFormat.float) {
let sample = 0;
Expand All @@ -675,7 +667,9 @@ export class TiffImage {
} else if (this._bitsPerSample === 16) {
sample = Float16.float16ToDouble(byteData.readUint16());
}
image.setPixelR(px, py, sample);
if (px < this._width && py < this._height) {
image.setPixelR(px, py, sample);
}
} else {
let sample = 0;
if (this._bitsPerSample === 8) {
Expand All @@ -700,7 +694,9 @@ export class TiffImage {
sample = mx - sample;
}

image.setPixelR(px, py, sample);
if (px < this._width && py < this._height) {
image.setPixelR(px, py, sample);
}
}
} else if (this._samplesPerPixel === 2) {
let gray = 0;
Expand Down Expand Up @@ -734,7 +730,9 @@ export class TiffImage {
: byteData.readUint32();
}

image.setPixelRgb(px, py, gray, alpha, 0);
if (px < this._width && py < this._height) {
image.setPixelRgb(px, py, gray, alpha, 0);
}
} else if (this._samplesPerPixel === 3) {
if (this._sampleFormat === TiffFormat.float) {
let r = 0.0;
Expand All @@ -753,7 +751,9 @@ export class TiffImage {
g = Float16.float16ToDouble(byteData.readUint16());
b = Float16.float16ToDouble(byteData.readUint16());
}
image.setPixelRgb(px, py, r, g, b);
if (px < this._width && py < this._height) {
image.setPixelRgb(px, py, r, g, b);
}
} else {
let r = 0;
let g = 0;
Expand Down Expand Up @@ -799,7 +799,9 @@ export class TiffImage {
: byteData.readUint32();
}

image.setPixelRgb(px, py, r, g, b);
if (px < this._width && py < this._height) {
image.setPixelRgb(px, py, r, g, b);
}
}
} else if (this._samplesPerPixel >= 4) {
if (this._sampleFormat === TiffFormat.float) {
Expand All @@ -823,7 +825,9 @@ export class TiffImage {
b = Float16.float16ToDouble(byteData.readUint16());
a = Float16.float16ToDouble(byteData.readUint16());
}
image.setPixelRgba(px, py, r, g, b, a);
if (px < this._width && py < this._height) {
image.setPixelRgba(px, py, r, g, b, a);
}
} else {
let r = 0;
let g = 0;
Expand Down Expand Up @@ -890,7 +894,9 @@ export class TiffImage {
a = Math.trunc(image.maxChannelValue);
}

image.setPixelRgba(px, py, r, g, b, a);
if (px < this._width && py < this._height) {
image.setPixelRgba(px, py, r, g, b, a);
}
}
}
}
Expand Down

0 comments on commit 0f09f41

Please sign in to comment.