diff --git a/src/formats/jpeg/jpeg-quantize.ts b/src/formats/jpeg/jpeg-quantize.ts index f6e3515..f61f479 100644 --- a/src/formats/jpeg/jpeg-quantize.ts +++ b/src/formats/jpeg/jpeg-quantize.ts @@ -222,10 +222,12 @@ export abstract class JpegQuantize { // convert to 8-bit integers for (let i = 0; i < 64; ++i) { - dataOut[i] = - JpegQuantize._dctClip[ - JpegQuantize._dctClipOffset + 128 + BitUtils.sshR(p[i] + 8, 4) - ]; + const index = + JpegQuantize._dctClipOffset + 128 + BitUtils.sshR(p[i] + 8, 4); + if (index < 0) { + break; + } + dataOut[i] = JpegQuantize._dctClip[index]; } } diff --git a/src/formats/jpeg/jpeg-scan.ts b/src/formats/jpeg/jpeg-scan.ts index 5ccb4f2..662e31f 100644 --- a/src/formats/jpeg/jpeg-scan.ts +++ b/src/formats/jpeg/jpeg-scan.ts @@ -157,8 +157,9 @@ export class JpegScan { if (this._bitsData === 0xff) { const nextByte = this.input.read(); if (nextByte !== 0) { - const marker = ((this._bitsData << 8) | nextByte).toString(16); - throw new LibError(`unexpected marker: ${marker}`); + // const marker = ((this._bitsData << 8) | nextByte).toString(16); + // throw new LibError(`unexpected marker: ${marker}`); + return undefined; } } @@ -197,10 +198,16 @@ export class JpegScan { } private receiveAndExtend(length: number | undefined): number { + if (length === undefined) { + return 0; + } if (length === 1) { return this.readBit() === 1 ? 1 : -1; } - const n = this.receive(length!)!; + const n = this.receive(length); + if (n === undefined) { + return 0; + } if (n >= 1 << ((length ?? 0) - 1)) { return n; } @@ -215,7 +222,10 @@ export class JpegScan { let k = 1; while (k < 64) { - const rs = this.decodeHuffman(component.huffmanTableAC)!; + const rs = this.decodeHuffman(component.huffmanTableAC); + if (rs === undefined) { + break; + } let s = rs & 15; const r = rs >>> 4; if (s === 0) {