Skip to content

Commit

Permalink
fix: Fix exception loading some PSD images, only read merged image fr…
Browse files Browse the repository at this point in the history
…om PSD
  • Loading branch information
yegor-pelykh committed Jun 17, 2024
1 parent 7c904e8 commit 3049b77
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
6 changes: 6 additions & 0 deletions src/formats/psd/psd-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,17 @@ export class PsdChannel {
if (n < 0) {
n = 1 - n;
const b = src.read();
if (_dstIndex + n > dst.length) {
n = dst.length - _dstIndex;
}
for (let i = 0; i < n; ++i) {
dst[_dstIndex++] = b;
}
} else {
n++;
if (_dstIndex + n > dst.length) {
n = dst.length - _dstIndex;
}
for (let i = 0; i < n; ++i) {
dst[_dstIndex++] = src.read();
}
Expand Down
36 changes: 21 additions & 15 deletions src/formats/psd/psd-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class PsdImage implements DecodeInfo {
return this._input;
}

private _imageResourceData: InputBuffer<Uint8Array> | undefined;
private _layerAndMaskData: InputBuffer<Uint8Array> | undefined;
// private _imageResourceData: InputBuffer<Uint8Array> | undefined;
// private _layerAndMaskData: InputBuffer<Uint8Array> | undefined;
private _imageData: InputBuffer<Uint8Array> | undefined;

private _width: number = 0;
Expand Down Expand Up @@ -119,10 +119,12 @@ export class PsdImage implements DecodeInfo {
this._input.readRange(len);

len = this._input.readUint32();
this._imageResourceData = this._input.readRange(len);
/* this._imageResourceData = */
this._input.readRange(len);

len = this._input.readUint32();
this._layerAndMaskData = this._input.readRange(len);
/* this._layerAndMaskData = */
this._input.readRange(len);

this._imageData = this._input.readRange(this._input.length);
}
Expand Down Expand Up @@ -512,6 +514,7 @@ export class PsdImage implements DecodeInfo {
// TODO: support indexed and duotone images.
}

/*
private readImageResources(): void {
this._imageResourceData!.rewind();
while (!this._imageResourceData!.isEOS) {
Expand Down Expand Up @@ -540,7 +543,9 @@ export class PsdImage implements DecodeInfo {
}
}
}
*/

/*
private readLayerAndMaskData(): void {
this._layerAndMaskData!.rewind();
let len = this._layerAndMaskData!.readUint32();
Expand Down Expand Up @@ -575,23 +580,24 @@ export class PsdImage implements DecodeInfo {
len = this._layerAndMaskData!.readUint32();
const maskData = this._layerAndMaskData!.readRange(len);
if (len > 0) {
/* let colorSpace: number = */
// let colorSpace: number =
maskData.readUint16();
/* let rc: number = */
// let rc: number =
maskData.readUint16();
/* let gc: number = */
// let gc: number =
maskData.readUint16();
/* let bc: number = */
// let bc: number =
maskData.readUint16();
/* let ac: number = */
// let ac: number =
maskData.readUint16();
// 0-100
/* let opacity: number = */
// let opacity: number =
maskData.readUint16();
/* let kind: number = */
// let kind: number =
maskData.read();
}
}
*/

private readMergeImageData(): void {
this._imageData!.rewind();
Expand Down Expand Up @@ -645,15 +651,15 @@ export class PsdImage implements DecodeInfo {
// Image Resource Block:
// Image resources are used to store non-pixel data associated with images,
// such as pen tool paths.
this.readImageResources();
// this.readImageResources();

this.readLayerAndMaskData();
// this.readLayerAndMaskData();

this.readMergeImageData();

this._input = undefined;
this._imageResourceData = undefined;
this._layerAndMaskData = undefined;
// this._imageResourceData = undefined;
// this._layerAndMaskData = undefined;
this._imageData = undefined;

return true;
Expand Down
6 changes: 0 additions & 6 deletions src/image/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,6 @@ export class MemoryImage implements Iterable<Pixel> {
const withPalette = opt.withPalette ?? false;
const paletteFormat = opt.paletteFormat ?? Format.uint8;

if (numChannels < 1 || numChannels > 4) {
throw new LibError(
`Invalid number of channels for image (${numChannels}). Must be between 1 and 4.`
);
}

this._iccProfile = opt.iccProfile;

if (opt.exifData !== undefined) {
Expand Down
Binary file added test/_input/psd/Unsupported compression.psd
Binary file not shown.
Binary file added test/_input/psd/index should be less than.psd
Binary file not shown.
Binary file added test/_input/psd/index should be less than_2.psd
Binary file not shown.
Binary file added test/_input/psd/rle_crash.psd
Binary file not shown.

0 comments on commit 3049b77

Please sign in to comment.