diff --git a/src/common/input-buffer.ts b/src/common/input-buffer.ts index f5dffdc..b359f81 100644 --- a/src/common/input-buffer.ts +++ b/src/common/input-buffer.ts @@ -1,11 +1,13 @@ /** @format */ import { LibError } from '../error/lib-error'; +import { ArrayUtils } from './array-utils'; import { BitUtils } from './bit-utils'; import { StringUtils } from './string-utils'; +import { TypedArray } from './typings'; -export interface InputBufferInitOptions { - buffer: Uint8Array; +export interface InputBufferInitOptions { + buffer: T; offset?: number; length?: number; bigEndian?: boolean; @@ -14,9 +16,12 @@ export interface InputBufferInitOptions { /** * A buffer that can be read as a stream of bytes. */ -export class InputBuffer { - private readonly _buffer: Uint8Array; - public get buffer(): Uint8Array { +export class InputBuffer { + private _buffer: T; + public set buffer(v: T) { + this._buffer = v; + } + public get buffer(): T { return this._buffer; } @@ -70,7 +75,7 @@ export class InputBuffer { /** * Create a InputStream for reading from an Array */ - constructor(opt: InputBufferInitOptions) { + constructor(opt: InputBufferInitOptions) { this._buffer = opt.buffer; this._bigEndian = opt.bigEndian ?? false; this._offset = opt.offset ?? 0; @@ -82,9 +87,13 @@ export class InputBuffer { /** * Create a copy of **other**. */ - public static from(other: InputBuffer, offset?: number, length?: number) { + public static from( + other: InputBuffer, + offset?: number, + length?: number + ) { const offsetFromOther = offset ?? 0; - const result = new InputBuffer({ + const result = new InputBuffer({ buffer: other._buffer, bigEndian: other._bigEndian, offset: other._offset + offsetFromOther, @@ -108,26 +117,56 @@ export class InputBuffer { /** * Access the buffer relative from the current position. */ - public getByte(index: number): number { + public get(index: number): number { return this._buffer[this._offset + index]; } /** * Set a buffer element relative to the current position. */ - public setByte(index: number, value: number) { + public set(index: number, value: number) { return (this._buffer[this._offset + index] = value); } + /** + * Copy data from **other** to this buffer, at **start** offset from the + * current read position, and **length** number of bytes. **offset** is + * the offset in **other** to start reading. + */ + public memcpy( + start: number, + length: number, + other: InputBuffer | T, + offset: number = 0 + ): void { + if (other instanceof InputBuffer) { + ArrayUtils.copyRange( + other.buffer, + other.offset + offset, + this._buffer, + this.offset + start, + length + ); + } else { + ArrayUtils.copyRange( + other, + offset, + this._buffer, + this.offset + start, + length + ); + } + } + /** * Set a range of bytes in this buffer to **value**, at **start** offset from the * current read position, and **length** number of bytes. */ public memset(start: number, length: number, value: number): void { this._buffer.fill( + value, this._offset + start, - this._offset + start + length, - value + this._offset + start + length ); } @@ -138,10 +177,14 @@ export class InputBuffer { * read position is used. If **length** is not specified, the remainder of this * stream is used. */ - public subarray(count: number, position?: number, offset = 0): InputBuffer { + public subarray( + count: number, + position?: number, + offset = 0 + ): InputBuffer { let pos = position !== undefined ? this._start + position : this._offset; pos += offset; - return new InputBuffer({ + return new InputBuffer({ buffer: this._buffer, bigEndian: this._bigEndian, offset: pos, @@ -169,7 +212,7 @@ export class InputBuffer { * Read **count** bytes from an **offset** of the current read position, without * moving the read position. */ - public peekBytes(count: number, offset = 0): InputBuffer { + public peek(count: number, offset = 0): InputBuffer { return this.subarray(count, undefined, offset); } @@ -181,25 +224,28 @@ export class InputBuffer { } /** - * Read a single byte. + * Read a single value. */ - public readByte(): number { + public read(): number { return this._buffer[this._offset++]; } - public readInt8(): number { - return BitUtils.uint8ToInt8(this.readByte()); - } - /** * Read **count** bytes from the stream. */ - public readBytes(count: number): InputBuffer { + public readRange(count: number): InputBuffer { const bytes = this.subarray(count); this._offset += bytes.length; return bytes; } + /** + * Read 8-bit integer from the stream. + */ + public readInt8(): number { + return BitUtils.uint8ToInt8(this.read()); + } + /** * Read a null-terminated string, or if **length** is provided, that number of * bytes returned as a string. @@ -208,7 +254,7 @@ export class InputBuffer { if (length === undefined) { const codes: number[] = []; while (!this.isEOS) { - const c = this.readByte(); + const c = this.read(); if (c === 0) { return String.fromCharCode(...codes); } @@ -217,7 +263,7 @@ export class InputBuffer { throw new LibError('EOF reached without finding string terminator.'); } - const s = this.readBytes(length); + const s = this.readRange(length); const bytes = s.toUint8Array(); const result = String.fromCharCode(...bytes); return result; @@ -229,7 +275,7 @@ export class InputBuffer { public readStringUtf8(): string { const codes: number[] = []; while (!this.isEOS) { - const c = this.readByte(); + const c = this.read(); if (c === 0) { const array = new Uint8Array(codes); return StringUtils.utf8Decoder.decode(array); @@ -342,21 +388,31 @@ export class InputBuffer { ); } - public toUint8Array(offset?: number, length?: number): Uint8Array { - const correctedOffset = offset ?? 0; - const correctedLength = length ?? this.length - correctedOffset; - return new Uint8Array( - this._buffer.buffer, - this._buffer.byteOffset + this._offset + correctedOffset, - correctedLength + public toUint8Array(offset: number = 0, length?: number): Uint8Array { + const correctedLength = length ?? this.length - offset; + if (this._buffer instanceof Uint8Array) { + return new Uint8Array( + this._buffer.buffer, + this._buffer.byteOffset + this._offset + offset, + correctedLength + ); + } + return Uint8Array.from( + this._buffer.subarray( + this._offset + offset, + this._offset + offset + correctedLength + ) ); } - public toUint32Array(offset?: number): Uint32Array { - const correctedOffset = offset ?? 0; - return new Uint32Array( - this._buffer.buffer, - this._buffer.byteOffset + this._offset + correctedOffset - ); + public toUint32Array(offset: number = 0): Uint32Array { + if (this._buffer instanceof Uint8Array) { + return new Uint32Array( + this._buffer.buffer, + this._buffer.byteOffset + this._offset + offset + ); + } + const uint8array = this.toUint8Array(); + return new Uint32Array(uint8array.buffer); } } diff --git a/src/exif/exif-data.ts b/src/exif/exif-data.ts index a00191f..588ff90 100644 --- a/src/exif/exif-data.ts +++ b/src/exif/exif-data.ts @@ -99,7 +99,10 @@ export class ExifData extends IfdContainer { } } - private readEntry(block: InputBuffer, blockOffset: number): ExifEntry { + private readEntry( + block: InputBuffer, + blockOffset: number + ): ExifEntry { const tag = block.readUint16(); const format = block.readUint16(); const count = block.readUint32(); @@ -123,7 +126,7 @@ export class ExifData extends IfdContainer { return entry; } - const data = block.readBytes(size); + const data = block.readRange(size); switch (f) { case IfdValueType.none: @@ -176,7 +179,7 @@ export class ExifData extends IfdContainer { return new ExifData(dirs); } - public static fromInputBuffer(input: InputBuffer) { + public static fromInputBuffer(input: InputBuffer) { const data = new ExifData(); data.read(input); return data; @@ -309,7 +312,7 @@ export class ExifData extends IfdContainer { out.bigEndian = saveEndian; } - public read(block: InputBuffer): boolean { + public read(block: InputBuffer): boolean { const saveEndian = block.bigEndian; block.bigEndian = true; diff --git a/src/exif/ifd-value/ifd-ascii-value.ts b/src/exif/ifd-value/ifd-ascii-value.ts index 009696d..40327fc 100644 --- a/src/exif/ifd-value/ifd-ascii-value.ts +++ b/src/exif/ifd-value/ifd-ascii-value.ts @@ -27,7 +27,7 @@ export class IfdAsciiValue extends IfdValue { } } - public static data(data: InputBuffer, length: number) { + public static data(data: InputBuffer, length: number) { // The final byte is a null terminator const value = length > 0 ? data.readString(length - 1) : ''; return new IfdAsciiValue(value); diff --git a/src/exif/ifd-value/ifd-byte-value.ts b/src/exif/ifd-value/ifd-byte-value.ts index b324039..732c8f4 100644 --- a/src/exif/ifd-value/ifd-byte-value.ts +++ b/src/exif/ifd-value/ifd-byte-value.ts @@ -27,7 +27,11 @@ export class IfdByteValue extends IfdValue { } } - public static data(data: InputBuffer, offset?: number, length?: number) { + public static data( + data: InputBuffer, + offset?: number, + length?: number + ) { const array = data.toUint8Array(offset, length); return new IfdByteValue(array); } diff --git a/src/exif/ifd-value/ifd-double-value.ts b/src/exif/ifd-value/ifd-double-value.ts index 0b9e372..d4a85fa 100644 --- a/src/exif/ifd-value/ifd-double-value.ts +++ b/src/exif/ifd-value/ifd-double-value.ts @@ -27,7 +27,7 @@ export class IfdDoubleValue extends IfdValue { } } - public static data(data: InputBuffer, length: number) { + public static data(data: InputBuffer, length: number) { const array = new Float64Array(length); for (let i = 0; i < length; ++i) { array[i] = data.readFloat64(); diff --git a/src/exif/ifd-value/ifd-long-value.ts b/src/exif/ifd-value/ifd-long-value.ts index 79c4cc8..74e3cd8 100644 --- a/src/exif/ifd-value/ifd-long-value.ts +++ b/src/exif/ifd-value/ifd-long-value.ts @@ -27,7 +27,7 @@ export class IfdLongValue extends IfdValue { } } - public static data(data: InputBuffer, length: number) { + public static data(data: InputBuffer, length: number) { const array = new Uint32Array(length); for (let i = 0; i < length; ++i) { array[i] = data.readUint32(); diff --git a/src/exif/ifd-value/ifd-rational-value.ts b/src/exif/ifd-value/ifd-rational-value.ts index 26e2e61..d3f11ac 100644 --- a/src/exif/ifd-value/ifd-rational-value.ts +++ b/src/exif/ifd-value/ifd-rational-value.ts @@ -27,7 +27,7 @@ export class IfdRationalValue extends IfdValue { } } - public static data(data: InputBuffer, length: number) { + public static data(data: InputBuffer, length: number) { const array = new Array(); for (let i = 0; i < length; i++) { const r = new Rational(data.readUint32(), data.readUint32()); diff --git a/src/exif/ifd-value/ifd-sbyte-value.ts b/src/exif/ifd-value/ifd-sbyte-value.ts index da36e0c..abd0d86 100644 --- a/src/exif/ifd-value/ifd-sbyte-value.ts +++ b/src/exif/ifd-value/ifd-sbyte-value.ts @@ -27,7 +27,11 @@ export class IfdSByteValue extends IfdValue { } } - public static data(data: InputBuffer, offset?: number, length?: number) { + public static data( + data: InputBuffer, + offset?: number, + length?: number + ) { const array = new Int8Array( new Int8Array(data.toUint8Array(offset, length).buffer) ); diff --git a/src/exif/ifd-value/ifd-short-value.ts b/src/exif/ifd-value/ifd-short-value.ts index fa92272..f534104 100644 --- a/src/exif/ifd-value/ifd-short-value.ts +++ b/src/exif/ifd-value/ifd-short-value.ts @@ -27,7 +27,7 @@ export class IfdShortValue extends IfdValue { } } - public static data(data: InputBuffer, length: number) { + public static data(data: InputBuffer, length: number) { const array = new Uint16Array(length); for (let i = 0; i < length; ++i) { array[i] = data.readUint16(); diff --git a/src/exif/ifd-value/ifd-single-value.ts b/src/exif/ifd-value/ifd-single-value.ts index 2f05c78..3de5eef 100644 --- a/src/exif/ifd-value/ifd-single-value.ts +++ b/src/exif/ifd-value/ifd-single-value.ts @@ -27,7 +27,7 @@ export class IfdSingleValue extends IfdValue { } } - public static data(data: InputBuffer, length: number) { + public static data(data: InputBuffer, length: number) { const array = new Float32Array(length); for (let i = 0; i < length; ++i) { array[i] = data.readFloat32(); diff --git a/src/exif/ifd-value/ifd-slong-value.ts b/src/exif/ifd-value/ifd-slong-value.ts index c43cc01..d1ecc9f 100644 --- a/src/exif/ifd-value/ifd-slong-value.ts +++ b/src/exif/ifd-value/ifd-slong-value.ts @@ -28,7 +28,7 @@ export class IfdSLongValue extends IfdValue { } } - public static data(data: InputBuffer, length: number) { + public static data(data: InputBuffer, length: number) { const array = new Int32Array(length); for (let i = 0; i < length; ++i) { array[i] = data.readInt32(); diff --git a/src/exif/ifd-value/ifd-srational-value.ts b/src/exif/ifd-value/ifd-srational-value.ts index b8cf4a5..3dbfcf0 100644 --- a/src/exif/ifd-value/ifd-srational-value.ts +++ b/src/exif/ifd-value/ifd-srational-value.ts @@ -28,7 +28,7 @@ export class IfdSRationalValue extends IfdValue { } } - public static data(data: InputBuffer, length: number) { + public static data(data: InputBuffer, length: number) { const array = new Array(); for (let i = 0; i < length; i++) { const r = new Rational(data.readInt32(), data.readInt32()); diff --git a/src/exif/ifd-value/ifd-sshort-value.ts b/src/exif/ifd-value/ifd-sshort-value.ts index ce35875..54fa5c9 100644 --- a/src/exif/ifd-value/ifd-sshort-value.ts +++ b/src/exif/ifd-value/ifd-sshort-value.ts @@ -27,7 +27,7 @@ export class IfdSShortValue extends IfdValue { } } - public static data(data: InputBuffer, length: number) { + public static data(data: InputBuffer, length: number) { const array = new Int16Array(length); for (let i = 0; i < length; ++i) { array[i] = data.readInt16(); diff --git a/src/exif/ifd-value/ifd-undefined-value.ts b/src/exif/ifd-value/ifd-undefined-value.ts index 9702453..09e3a67 100644 --- a/src/exif/ifd-value/ifd-undefined-value.ts +++ b/src/exif/ifd-value/ifd-undefined-value.ts @@ -27,7 +27,11 @@ export class IfdUndefinedValue extends IfdValue { } } - public static data(data: InputBuffer, offset?: number, length?: number) { + public static data( + data: InputBuffer, + offset?: number, + length?: number + ) { const array = new Uint8Array(data.toUint8Array(offset, length)); return new IfdUndefinedValue(array); } diff --git a/src/formats/bmp-decoder.ts b/src/formats/bmp-decoder.ts index f971e7a..3314e21 100644 --- a/src/formats/bmp-decoder.ts +++ b/src/formats/bmp-decoder.ts @@ -8,7 +8,7 @@ import { BmpInfo } from './bmp/bmp-info'; import { Decoder, DecoderDecodeOptions } from './decoder'; export class BmpDecoder implements Decoder { - protected _input?: InputBuffer; + protected _input?: InputBuffer; protected _info?: BmpInfo; protected _forceRgba: boolean; @@ -25,7 +25,7 @@ export class BmpDecoder implements Decoder { */ public isValidFile(bytes: Uint8Array): boolean { return BmpFileHeader.isValidFile( - new InputBuffer({ + new InputBuffer({ buffer: bytes, }) ); @@ -35,7 +35,7 @@ export class BmpDecoder implements Decoder { if (!this.isValidFile(bytes)) { return undefined; } - this._input = new InputBuffer({ + this._input = new InputBuffer({ buffer: bytes, }); this._info = new BmpInfo(this._input); @@ -91,7 +91,7 @@ export class BmpDecoder implements Decoder { for (let y = image.height - 1; y >= 0; --y) { const line = inf.readBottomUp ? y : image.height - 1 - y; - const row = this._input.readBytes(rowStride); + const row = this._input.readRange(rowStride); const w = image.width; let x = 0; const p = image.getPixel(0, line); diff --git a/src/formats/bmp/bmp-file-header.ts b/src/formats/bmp/bmp-file-header.ts index 961a506..bd7a8ed 100644 --- a/src/formats/bmp/bmp-file-header.ts +++ b/src/formats/bmp/bmp-file-header.ts @@ -20,7 +20,7 @@ export class BmpFileHeader { return this._imageOffset; } - constructor(b: InputBuffer) { + constructor(b: InputBuffer) { if (!BmpFileHeader.isValidFile(b)) { throw new LibError('Not a bitmap file.'); } @@ -31,7 +31,7 @@ export class BmpFileHeader { this._imageOffset = b.readInt32(); } - public static isValidFile(b: InputBuffer): boolean { + public static isValidFile(b: InputBuffer): boolean { if (b.length < 2) { return false; } diff --git a/src/formats/bmp/bmp-info.ts b/src/formats/bmp/bmp-info.ts index e21f100..36e718e 100644 --- a/src/formats/bmp/bmp-info.ts +++ b/src/formats/bmp/bmp-info.ts @@ -128,7 +128,7 @@ export class BmpInfo implements DecodeInfo { ); } - constructor(p: InputBuffer, header?: BmpFileHeader) { + constructor(p: InputBuffer, header?: BmpFileHeader) { this._header = header ?? new BmpFileHeader(p); this._startPos = p.offset; this._headerSize = p.readUint32(); @@ -235,48 +235,48 @@ export class BmpInfo implements DecodeInfo { } } - private readPalette(input: InputBuffer): void { + private readPalette(input: InputBuffer): void { const numColors = this._totalColors === 0 ? 1 << this._bitsPerPixel : this._totalColors; const numChannels = 3; this._palette = new PaletteUint8(numColors, numChannels); for (let i = 0; i < numColors; ++i) { - const b = input.readByte(); - const g = input.readByte(); - const r = input.readByte(); + const b = input.read(); + const g = input.read(); + const r = input.read(); // ignored - const a = input.readByte(); + const a = input.read(); this._palette.setRgba(i, r, g, b, a); } } public decodePixel( - input: InputBuffer, + input: InputBuffer, pixel: (r: number, g: number, b: number, a: number) => void ): void { if (this._palette !== undefined) { if (this._bitsPerPixel === 1) { - const bi = input.readByte(); + const bi = input.read(); for (let i = 7; i >= 0; --i) { const b = (bi >>> i) & 0x1; pixel(b, 0, 0, 0); } return; } else if (this._bitsPerPixel === 2) { - const bi = input.readByte(); + const bi = input.read(); for (let i = 6; i >= 0; i -= 2) { const b = (bi >>> i) & 0x2; pixel(b, 0, 0, 0); } } else if (this._bitsPerPixel === 4) { - const bi = input.readByte(); + const bi = input.read(); const b1 = (bi >>> 4) & 0xf; pixel(b1, 0, 0, 0); const b2 = bi & 0xf; pixel(b2, 0, 0, 0); return; } else if (this._bitsPerPixel === 8) { - const b = input.readByte(); + const b = input.read(); pixel(b, 0, 0, 0); return; } @@ -307,16 +307,16 @@ export class BmpInfo implements DecodeInfo { this._bitsPerPixel === 32 && this._compression === BmpCompressionMode.none ) { - const b = input.readByte(); - const g = input.readByte(); - const r = input.readByte(); - const a = input.readByte(); + const b = input.read(); + const g = input.read(); + const r = input.read(); + const a = input.read(); pixel(r, g, b, this.ignoreAlphaChannel ? 255 : a); return; } else if (this._bitsPerPixel === 24) { - const b = input.readByte(); - const g = input.readByte(); - const r = input.readByte(); + const b = input.read(); + const g = input.read(); + const r = input.read(); pixel(r, g, b, 255); return; } else if (this._bitsPerPixel === 16) { diff --git a/src/formats/dib-decoder.ts b/src/formats/dib-decoder.ts index 2c408d7..bd3bd14 100644 --- a/src/formats/dib-decoder.ts +++ b/src/formats/dib-decoder.ts @@ -5,7 +5,11 @@ import { BmpDecoder } from './bmp-decoder'; import { BmpInfo } from './bmp/bmp-info'; export class DibDecoder extends BmpDecoder { - constructor(input: InputBuffer, info: BmpInfo, forceRgba = false) { + constructor( + input: InputBuffer, + info: BmpInfo, + forceRgba = false + ) { super(forceRgba); this._input = input; this._info = info; diff --git a/src/formats/gif-decoder.ts b/src/formats/gif-decoder.ts index e58aebb..57990de 100644 --- a/src/formats/gif-decoder.ts +++ b/src/formats/gif-decoder.ts @@ -39,7 +39,7 @@ export class GifDecoder implements Decoder { private static readonly _interlacedOffset: number[] = [0, 4, 2, 1]; private static readonly _interlacedJump: number[] = [8, 8, 4, 2]; - private _input?: InputBuffer; + private _input?: InputBuffer; private _info?: GifInfo; @@ -150,12 +150,12 @@ export class GifDecoder implements Decoder { const width = this._input.readUint16(); const height = this._input.readUint16(); - const b = this._input.readByte(); + const b = this._input.read(); const colorResolution = (((b & 0x70) + 1) >>> 4) + 1; const bitsPerPixel = (b & 0x07) + 1; const backgroundColor = new ColorUint8( - new Uint8Array([this._input.readByte()]) + new Uint8Array([this._input.read()]) ); this._input.skip(1); @@ -167,9 +167,9 @@ export class GifDecoder implements Decoder { // Get the global color map: for (let i = 0; i < globalColorMap.numColors; ++i) { - const r = this._input.readByte(); - const g = this._input.readByte(); - const b = this._input.readByte(); + const r = this._input.read(); + const g = this._input.read(); + const b = this._input.read(); globalColorMap.setColor(i, r, g, b); } } @@ -207,23 +207,23 @@ export class GifDecoder implements Decoder { if (this._input === undefined || this._input.isEOS) { return true; } - let b = this._input.readByte(); + let b = this._input.read(); while (b !== 0 && !this._input.isEOS) { this._input.skip(b); if (this._input.isEOS) { return true; } - b = this._input.readByte(); + b = this._input.read(); } return true; } - private readApplicationExt(input: InputBuffer): void { - const blockSize = input.readByte(); + private readApplicationExt(input: InputBuffer): void { + const blockSize = input.read(); const tag = input.readString(blockSize); if (tag === 'NETSCAPE2.0') { - const b1 = input.readByte(); - const b2 = input.readByte(); + const b1 = input.read(); + const b2 = input.read(); if (b1 === 0x03 && b2 === 0x01) { this._repeat = input.readUint16(); } @@ -232,19 +232,19 @@ export class GifDecoder implements Decoder { } } - private readGraphicsControlExt(input: InputBuffer): void { + private readGraphicsControlExt(input: InputBuffer): void { /* const blockSize: number = */ - input.readByte(); - const b = input.readByte(); + input.read(); + const b = input.read(); this._duration = input.readUint16(); - this._transparent = input.readByte(); + this._transparent = input.read(); /* const endBlock: number = */ - input.readByte(); + input.read(); this._disposalMethod = (b >>> 2) & 0x7; // const userInput: number = (b >>> 1) & 0x1; this._transparentFlag = b & 0x1; - const recordType = input.peekBytes(1).getByte(0); + const recordType = input.peek(1).get(0); if (recordType === GifDecoder._imageDescRecordType) { input.skip(1); const gifImage = this.skipImage(); @@ -480,7 +480,7 @@ export class GifDecoder implements Decoder { let nextByte = 0; if (this._buffer![0] === 0) { // Needs to read the next buffer - this one is empty: - this._buffer![0] = this._input!.readByte(); + this._buffer![0] = this._input!.read(); // There shouldn't be any empty data blocks here as the LZW spec // says the LZW termination code should come first. Therefore we @@ -489,7 +489,7 @@ export class GifDecoder implements Decoder { return undefined; } - const from = this._input!.readBytes(this._buffer![0]).toUint8Array(); + const from = this._input!.readRange(this._buffer![0]).toUint8Array(); ArrayUtils.copyRange(from, 0, this._buffer!, 1, this._buffer![0]); @@ -521,7 +521,7 @@ export class GifDecoder implements Decoder { this.initDecode(); } - this._bitsPerPixel = this._input.readByte(); + this._bitsPerPixel = this._input.read(); this._clearCode = 1 << this._bitsPerPixel; this._eofCode = this._clearCode + 1; this._runningCode = this._eofCode + 1; @@ -590,7 +590,7 @@ export class GifDecoder implements Decoder { * Is the given file a valid Gif image? */ public isValidFile(bytes: Uint8Array): boolean { - this._input = new InputBuffer({ + this._input = new InputBuffer({ buffer: bytes, }); return this.getInfo(); @@ -601,7 +601,7 @@ export class GifDecoder implements Decoder { * If the file is not a valid Gif image, undefined is returned. */ public startDecode(bytes: Uint8Array): GifInfo | undefined { - this._input = new InputBuffer({ + this._input = new InputBuffer({ buffer: bytes, }); @@ -611,7 +611,7 @@ export class GifDecoder implements Decoder { try { while (!this._input.isEOS) { - const recordType = this._input.readByte(); + const recordType = this._input.read(); switch (recordType) { case GifDecoder._imageDescRecordType: { const gifImage = this.skipImage(); @@ -637,7 +637,7 @@ export class GifDecoder implements Decoder { break; } case GifDecoder._extensionRecordType: { - const extCode = this._input.readByte(); + const extCode = this._input.read(); if (extCode === GifDecoder._applicationExt) { this.readApplicationExt(this._input); } else if (extCode === GifDecoder._graphicControlExt) { diff --git a/src/formats/gif/gif-image-desc.ts b/src/formats/gif/gif-image-desc.ts index a0c5b63..9040122 100644 --- a/src/formats/gif/gif-image-desc.ts +++ b/src/formats/gif/gif-image-desc.ts @@ -61,13 +61,13 @@ export class GifImageDesc { return this._inputPosition; } - constructor(input: InputBuffer) { + constructor(input: InputBuffer) { this._x = input.readUint16(); this._y = input.readUint16(); this._width = input.readUint16(); this._height = input.readUint16(); - const b = input.readByte(); + const b = input.read(); const bitsPerPixel = (b & 0x07) + 1; this._interlaced = (b & 0x40) !== 0; @@ -75,12 +75,7 @@ export class GifImageDesc { if ((b & 0x80) !== 0) { this._colorMap = new GifColorMap(1 << bitsPerPixel); for (let i = 0; i < this._colorMap.numColors; ++i) { - this._colorMap.setColor( - i, - input.readByte(), - input.readByte(), - input.readByte() - ); + this._colorMap.setColor(i, input.read(), input.read(), input.read()); } } diff --git a/src/formats/ico-decoder.ts b/src/formats/ico-decoder.ts index d96f19e..7f47a0b 100644 --- a/src/formats/ico-decoder.ts +++ b/src/formats/ico-decoder.ts @@ -12,7 +12,7 @@ import { MemoryImage } from '../image/image'; import { FrameType } from '../image/frame-type'; export class IcoDecoder implements Decoder { - private _input?: InputBuffer; + private _input?: InputBuffer; private _info?: IcoInfo; public get numFrames(): number { @@ -20,7 +20,7 @@ export class IcoDecoder implements Decoder { } public isValidFile(bytes: Uint8Array): boolean { - this._input = new InputBuffer({ + this._input = new InputBuffer({ buffer: bytes, }); this._info = IcoInfo.read(this._input); @@ -28,7 +28,7 @@ export class IcoDecoder implements Decoder { } public startDecode(bytes: Uint8Array): IcoInfo | undefined { - this._input = new InputBuffer({ + this._input = new InputBuffer({ buffer: bytes, }); this._info = IcoInfo.read(this._input); @@ -77,7 +77,7 @@ export class IcoDecoder implements Decoder { const imageBuffer = this._input.buffer.subarray( this._input.start + imageInfo.bytesOffset, this._input.start + imageInfo.bytesOffset + imageInfo.bytesSize - ); + ) as Uint8Array; const png = new PngDecoder(); if (png.isValidFile(imageBuffer)) { @@ -96,11 +96,11 @@ export class IcoDecoder implements Decoder { dummyBmpHeader.writeUint32(0); const bmpInfo = new IcoBmpInfo( - new InputBuffer({ + new InputBuffer({ buffer: imageBuffer, }), new BmpFileHeader( - new InputBuffer({ + new InputBuffer({ buffer: dummyBmpHeader.getBytes(), }) ) @@ -121,7 +121,7 @@ export class IcoDecoder implements Decoder { bmpInfo.header.imageOffset = offset; dummyBmpHeader.length -= 4; dummyBmpHeader.writeUint32(offset); - const inp = new InputBuffer({ + const inp = new InputBuffer({ buffer: imageBuffer, }); const bmp = new DibDecoder(inp, bmpInfo, true); @@ -139,10 +139,10 @@ export class IcoDecoder implements Decoder { // AND bitmask for (let y = 0; y < bmpInfo.height; y++) { const line = bmpInfo.readBottomUp ? y : image.height - 1 - y; - const row = inp.readBytes(rowLength); + const row = inp.readRange(rowLength); const p = image.getPixel(0, line); for (let x = 0; x < bmpInfo.width; ) { - const b = row.readByte(); + const b = row.read(); for (let j = 7; j > -1 && x < bmpInfo.width; j--) { if ((b & (1 << j)) !== 0) { // set the pixel to completely transparent. diff --git a/src/formats/ico/ico-info.ts b/src/formats/ico/ico-info.ts index cf01472..b1052a4 100644 --- a/src/formats/ico/ico-info.ts +++ b/src/formats/ico/ico-info.ts @@ -44,7 +44,7 @@ export class IcoInfo implements DecodeInfo { this._images = images; } - public static read(input: InputBuffer): IcoInfo | undefined { + public static read(input: InputBuffer): IcoInfo | undefined { if (input.readUint16() !== 0) { return undefined; } @@ -64,9 +64,9 @@ export class IcoInfo implements DecodeInfo { imageCount, (_) => new IcoInfoImage({ - width: input.readByte(), - height: input.readByte(), - colorPalette: input.readByte(), + width: input.read(), + height: input.read(), + colorPalette: input.read(), // ignore 1 byte colorPlanes: (input.skip(1), input).readUint16(), bitsPerPixel: input.readUint16(), diff --git a/src/formats/jpeg-decoder.ts b/src/formats/jpeg-decoder.ts index 7b76a12..f2e516a 100644 --- a/src/formats/jpeg-decoder.ts +++ b/src/formats/jpeg-decoder.ts @@ -11,7 +11,7 @@ import { JpegInfo } from './jpeg/jpeg-info'; * Decode a jpeg encoded image. */ export class JpegDecoder implements Decoder { - private _input?: InputBuffer; + private _input?: InputBuffer; private _info?: JpegInfo; public get numFrames(): number { @@ -26,7 +26,7 @@ export class JpegDecoder implements Decoder { } public startDecode(bytes: Uint8Array): JpegInfo | undefined { - this._input = new InputBuffer({ + this._input = new InputBuffer({ buffer: bytes, bigEndian: true, }); @@ -40,7 +40,7 @@ export class JpegDecoder implements Decoder { } const jpeg = new JpegData(); - jpeg.read(this._input.buffer); + jpeg.read(this._input.buffer as Uint8Array); if (jpeg.frames.length !== 1) { throw new LibError('Only single frame JPEGs supported.'); } diff --git a/src/formats/jpeg/jpeg-data.ts b/src/formats/jpeg/jpeg-data.ts index 7903df8..8eb29dc 100644 --- a/src/formats/jpeg/jpeg-data.ts +++ b/src/formats/jpeg/jpeg-data.ts @@ -44,8 +44,8 @@ export class JpegData { // JPEG limit on sampling factors public static readonly maxSamplingFactor = 4; - private _input!: InputBuffer; - public get input(): InputBuffer { + private _input!: InputBuffer; + public get input(): InputBuffer { return this._input; } @@ -199,16 +199,16 @@ export class JpegData { // Fill bytes case 0xff: - if (this._input.getByte(0) !== 0xff) { + if (this._input.get(0) !== 0xff) { this._input.skip(-1); } break; default: if ( - this._input.getByte(-3) === 0xff && - this._input.getByte(-2) >= 0xc0 && - this._input.getByte(-2) <= 0xfe + this._input.get(-3) === 0xff && + this._input.get(-2) >= 0xc0 && + this._input.get(-2) <= 0xfe ) { // Could be incorrect encoding -- last 0xFF byte of the previous // block was eaten by the encoder @@ -235,15 +235,15 @@ export class JpegData { } public validate(bytes: Uint8Array): boolean { - this._input = new InputBuffer({ + this._input = new InputBuffer({ buffer: bytes, bigEndian: true, }); // Some other formats have embedded jpeg, or jpeg-like data. // Only validate if the image starts with the StartOfImage tag. - const soiCheck = this._input.peekBytes(2); - if (soiCheck.getByte(0) !== 0xff || soiCheck.getByte(1) !== 0xd8) { + const soiCheck = this._input.peek(2); + if (soiCheck.get(0) !== 0xff || soiCheck.get(1) !== 0xd8) { return false; } @@ -292,7 +292,7 @@ export class JpegData { } public readInfo(bytes: Uint8Array): JpegInfo | undefined { - this._input = new InputBuffer({ + this._input = new InputBuffer({ buffer: bytes, bigEndian: true, }); @@ -346,7 +346,7 @@ export class JpegData { } public read(bytes: Uint8Array): void { - this._input = new InputBuffer({ + this._input = new InputBuffer({ buffer: bytes, bigEndian: true, }); @@ -483,12 +483,12 @@ export class JpegData { return Math.trunc(val * one) & 0xffffffff; } - private readBlock(): InputBuffer { + private readBlock(): InputBuffer { const length = this._input.readUint16(); if (length < 2) { throw new LibError('Invalid Block'); } - return this._input.readBytes(length - 2); + return this._input.readRange(length - 2); } private nextMarker(): number { @@ -499,7 +499,7 @@ export class JpegData { do { do { - c = this._input.readByte(); + c = this._input.read(); } while (c !== 0xff && !this._input.isEOS); if (this._input.isEOS) { @@ -507,14 +507,14 @@ export class JpegData { } do { - c = this._input.readByte(); + c = this._input.read(); } while (c === 0xff && !this._input.isEOS); } while (c === 0 && !this._input.isEOS); return c; } - private readExifData(block: InputBuffer): void { + private readExifData(block: InputBuffer): void { // Exif Header // Exif\0\0 const exifSignature = 0x45786966; @@ -529,25 +529,25 @@ export class JpegData { this.exifData.read(block); } - private readAppData(marker: number, block: InputBuffer): void { + private readAppData(marker: number, block: InputBuffer): void { const appData = block; if (marker === JpegMarker.app0) { // 'JFIF\0' if ( - appData.getByte(0) === 0x4a && - appData.getByte(1) === 0x46 && - appData.getByte(2) === 0x49 && - appData.getByte(3) === 0x46 && - appData.getByte(4) === 0 + appData.get(0) === 0x4a && + appData.get(1) === 0x46 && + appData.get(2) === 0x49 && + appData.get(3) === 0x46 && + appData.get(4) === 0 ) { - const majorVersion = appData.getByte(5); - const minorVersion = appData.getByte(6); - const densityUnits = appData.getByte(7); - const xDensity = (appData.getByte(8) << 8) | appData.getByte(9); - const yDensity = (appData.getByte(10) << 8) | appData.getByte(11); - const thumbWidth = appData.getByte(12); - const thumbHeight = appData.getByte(13); + const majorVersion = appData.get(5); + const minorVersion = appData.get(6); + const densityUnits = appData.get(7); + const xDensity = (appData.get(8) << 8) | appData.get(9); + const yDensity = (appData.get(10) << 8) | appData.get(11); + const thumbWidth = appData.get(12); + const thumbHeight = appData.get(13); const thumbSize = 3 * thumbWidth * thumbHeight; const thumbData = appData.subarray(14 + thumbSize, undefined, 14); @@ -569,17 +569,17 @@ export class JpegData { } else if (marker === JpegMarker.app14) { // 'Adobe\0' if ( - appData.getByte(0) === 0x41 && - appData.getByte(1) === 0x64 && - appData.getByte(2) === 0x6f && - appData.getByte(3) === 0x62 && - appData.getByte(4) === 0x65 && - appData.getByte(5) === 0 + appData.get(0) === 0x41 && + appData.get(1) === 0x64 && + appData.get(2) === 0x6f && + appData.get(3) === 0x62 && + appData.get(4) === 0x65 && + appData.get(5) === 0 ) { - const version = appData.getByte(6); - const flags0 = (appData.getByte(7) << 8) | appData.getByte(8); - const flags1 = (appData.getByte(9) << 8) | appData.getByte(10); - const transformCode = appData.getByte(11); + const version = appData.get(6); + const flags0 = (appData.get(7) << 8) | appData.get(8); + const flags1 = (appData.get(9) << 8) | appData.get(10); + const transformCode = appData.get(11); this._adobe = new JpegAdobe(version, flags0, flags1, transformCode); } } else if (marker === JpegMarker.com) { @@ -593,7 +593,7 @@ export class JpegData { } } - private readDQT(block: InputBuffer): void { + private readDQT(block: InputBuffer): void { while (!block.isEOS) { let n = block.read(); const prec = n >>> 4; @@ -610,8 +610,7 @@ export class JpegData { const tableData = this._quantizationTables[n]; if (tableData !== undefined) { for (let i = 0; i < JpegData.dctSize2; i++) { - const tmp: number = - prec !== 0 ? block.readUint16() : block.readByte(); + const tmp: number = prec !== 0 ? block.readUint16() : block.read(); tableData[JpegData.dctZigZag[i]] = tmp; } } @@ -622,26 +621,26 @@ export class JpegData { } } - private readFrame(marker: number, block: InputBuffer): void { + private readFrame(marker: number, block: InputBuffer): void { if (this._frame !== undefined) { throw new LibError('Duplicate JPG frame data found.'); } const extended = marker === JpegMarker.sof1; const progressive = marker === JpegMarker.sof2; - const precision = block.readByte(); + const precision = block.read(); const scanLines = block.readUint16(); const samplesPerLine = block.readUint16(); - const numComponents = block.readByte(); + const numComponents = block.read(); const components = new Map(); const componentsOrder = new Array(); for (let i = 0; i < numComponents; i++) { - const componentId = block.readByte(); - const x = block.readByte(); + const componentId = block.read(); + const x = block.read(); const h = (x >>> 4) & 15; const v = x & 15; - const qId = block.readByte(); + const qId = block.read(); componentsOrder.push(componentId); const component = new JpegComponent(h, v, this._quantizationTables, qId); components.set(componentId, component); @@ -662,18 +661,18 @@ export class JpegData { this.frames.push(this._frame); } - private readDHT(block: InputBuffer): void { + private readDHT(block: InputBuffer): void { while (!block.isEOS) { - let index = block.readByte(); + let index = block.read(); const bits = new Uint8Array(16); let count = 0; for (let j = 0; j < 16; j++) { - bits[j] = block.readByte(); + bits[j] = block.read(); count += bits[j]; } - const huffmanValues = block.readBytes(count).toUint8Array(); + const huffmanValues = block.readRange(count).toUint8Array(); let ht: Array | undefined> = []; if ((index & 0x10) !== 0) { @@ -693,20 +692,20 @@ export class JpegData { } } - private readDRI(block: InputBuffer): void { + private readDRI(block: InputBuffer): void { this._resetInterval = block.readUint16(); } - private readSOS(block: InputBuffer): void { - const n = block.readByte(); + private readSOS(block: InputBuffer): void { + const n = block.read(); if (n < 1 || n > JpegData.maxCompsInScan) { throw new LibError('Invalid SOS block'); } const components = new Array(); for (let i = 0; i < n; i++) { - const id = block.readByte(); - const c = block.readByte(); + const id = block.read(); + const c = block.read(); if (!this._frame!.components.has(id)) { throw new LibError('Invalid Component in SOS block'); @@ -725,9 +724,9 @@ export class JpegData { } } - const spectralStart = block.readByte(); - const spectralEnd = block.readByte(); - const successiveApproximation = block.readByte(); + const spectralStart = block.read(); + const spectralEnd = block.read(); + const successiveApproximation = block.read(); const ah = (successiveApproximation >>> 4) & 15; const al = successiveApproximation & 15; diff --git a/src/formats/jpeg/jpeg-jfif.ts b/src/formats/jpeg/jpeg-jfif.ts index c45eaaa..46e25e5 100644 --- a/src/formats/jpeg/jpeg-jfif.ts +++ b/src/formats/jpeg/jpeg-jfif.ts @@ -38,8 +38,8 @@ export class JpegJfif { return this._yDensity; } - private _thumbData: InputBuffer; - public get thumbData(): InputBuffer { + private _thumbData: InputBuffer; + public get thumbData(): InputBuffer { return this._thumbData; } @@ -51,7 +51,7 @@ export class JpegJfif { densityUnits: number, xDensity: number, yDensity: number, - thumbData: InputBuffer + thumbData: InputBuffer ) { this._thumbWidth = thumbWidth; this._thumbHeight = thumbHeight; diff --git a/src/formats/jpeg/jpeg-scan.ts b/src/formats/jpeg/jpeg-scan.ts index a309118..5ccb4f2 100644 --- a/src/formats/jpeg/jpeg-scan.ts +++ b/src/formats/jpeg/jpeg-scan.ts @@ -16,8 +16,8 @@ export type DecodeFunction = ( ) => void; export class JpegScan { - private _input: InputBuffer; - public get input(): InputBuffer { + private _input: InputBuffer; + public get input(): InputBuffer { return this._input; } @@ -117,7 +117,7 @@ export class JpegScan { } constructor( - input: InputBuffer, + input: InputBuffer, frame: JpegFrame, components: Array, spectralStart: number, @@ -153,9 +153,9 @@ export class JpegScan { return undefined; } - this._bitsData = this._input.readByte(); + this._bitsData = this._input.read(); if (this._bitsData === 0xff) { - const nextByte = this.input.readByte(); + const nextByte = this.input.read(); if (nextByte !== 0) { const marker = ((this._bitsData << 8) | nextByte).toString(16); throw new LibError(`unexpected marker: ${marker}`); @@ -445,8 +445,8 @@ export class JpegScan { // Find marker this._bitsCount = 0; - const m1 = this._input.getByte(0); - const m2 = this._input.getByte(1); + const m1 = this._input.get(0); + const m2 = this._input.get(1); if (m1 === 0xff) { if (m2 >= JpegMarker.rst0 && m2 <= JpegMarker.rst7) { this._input.skip(2); diff --git a/src/formats/jpeg/jpeg-utils.ts b/src/formats/jpeg/jpeg-utils.ts index ac06abe..964b1ec 100644 --- a/src/formats/jpeg/jpeg-utils.ts +++ b/src/formats/jpeg/jpeg-utils.ts @@ -9,7 +9,9 @@ export class JpegUtils { // Exif\0\0 private static readonly _exifSignature = 0x45786966; - private readExifData(block: InputBuffer | undefined): ExifData | undefined { + private readExifData( + block: InputBuffer | undefined + ): ExifData | undefined { if (block === undefined) { return undefined; } @@ -41,29 +43,37 @@ export class JpegUtils { out.writeBytes(exifBytes); } - private readBlock(input: InputBuffer): InputBuffer | undefined { + private readBlock( + input: InputBuffer + ): InputBuffer | undefined { const length = input.readUint16(); if (length < 2) { return undefined; } - return input.readBytes(length - 2); + return input.readRange(length - 2); } - private skipBlock(input: InputBuffer, output?: OutputBuffer): boolean { + private skipBlock( + input: InputBuffer, + output?: OutputBuffer + ): boolean { const length = input.readUint16(); output?.writeUint16(length); if (length < 2) { return false; } if (output !== undefined) { - output.writeBuffer(input.readBytes(length - 2)); + output.writeBuffer(input.readRange(length - 2)); } else { input.skip(length - 2); } return true; } - private nextMarker(input: InputBuffer, output?: OutputBuffer): number { + private nextMarker( + input: InputBuffer, + output?: OutputBuffer + ): number { let c = 0; if (input.isEOS) { return c; @@ -71,7 +81,7 @@ export class JpegUtils { do { do { - c = input.readByte(); + c = input.read(); output?.writeByte(c); } while (c !== 0xff && !input.isEOS); @@ -80,7 +90,7 @@ export class JpegUtils { } do { - c = input.readByte(); + c = input.read(); output?.writeByte(c); } while (c === 0xff && !input.isEOS); } while (c === 0 && !input.isEOS); @@ -89,15 +99,15 @@ export class JpegUtils { } public decodeExif(data: Uint8Array): ExifData | undefined { - const input = new InputBuffer({ + const input = new InputBuffer({ buffer: data, bigEndian: true, }); // Some other formats have embedded jpeg, or jpeg-like data. // Only validate if the image starts with the StartOfImage tag. - const soiCheck = input.peekBytes(2); - if (soiCheck.getByte(0) !== 0xff || soiCheck.getByte(1) !== 0xd8) { + const soiCheck = input.peek(2); + if (soiCheck.get(0) !== 0xff || soiCheck.get(1) !== 0xd8) { return undefined; } @@ -127,15 +137,15 @@ export class JpegUtils { } public injectExif(exif: ExifData, data: Uint8Array): Uint8Array | undefined { - const input = new InputBuffer({ + const input = new InputBuffer({ buffer: data, bigEndian: true, }); // Some other formats have embedded jpeg, or jpeg-like data. // Only validate if the image starts with the StartOfImage tag. - const soiCheck = input.peekBytes(2); - if (soiCheck.getByte(0) !== 0xff || soiCheck.getByte(1) !== 0xd8) { + const soiCheck = input.peek(2); + if (soiCheck.get(0) !== 0xff || soiCheck.get(1) !== 0xd8) { return undefined; } @@ -174,7 +184,7 @@ export class JpegUtils { this.writeAPP1(output, exif); // No need to parse the remaining individual blocks, just write out // the remainder of the file. - output.writeBuffer(input.readBytes(input.length)); + output.writeBuffer(input.readRange(input.length)); return output.getBytes(); } @@ -191,7 +201,7 @@ export class JpegUtils { this.writeAPP1(output, exif); // No need to parse the remaining individual blocks, just write out // the remainder of the file. - output.writeBuffer(input.readBytes(input.length)); + output.writeBuffer(input.readRange(input.length)); return output.getBytes(); } } diff --git a/src/formats/png-decoder.ts b/src/formats/png-decoder.ts index a69fc5b..e871a83 100644 --- a/src/formats/png-decoder.ts +++ b/src/formats/png-decoder.ts @@ -29,8 +29,8 @@ import { Pixel } from '../image/pixel'; * Decode a PNG encoded image. */ export class PngDecoder implements Decoder { - private _input?: InputBuffer; - public get input(): InputBuffer | undefined { + private _input?: InputBuffer; + public get input(): InputBuffer | undefined { return this._input; } @@ -137,7 +137,7 @@ export class PngDecoder implements Decoder { * Process a pass of an interlaced image. */ private processPass( - input: InputBuffer, + input: InputBuffer, image: MemoryImage, xOffset: number, yOffset: number, @@ -169,8 +169,8 @@ export class PngDecoder implements Decoder { srcY < passHeight; ++srcY, dstY += yStep, ri = 1 - ri, this._progressY++ ) { - const filterType = input.readByte() as PngFilterType; - inData[ri] = input.readBytes(rowBytes).toUint8Array(); + const filterType = input.read() as PngFilterType; + inData[ri] = input.readRange(rowBytes).toUint8Array(); const row = inData[ri]!; const prevRow = inData[1 - ri]; @@ -183,7 +183,7 @@ export class PngDecoder implements Decoder { // reset the bit stream counter. this.resetBits(); - const rowInput = new InputBuffer({ + const rowInput = new InputBuffer({ buffer: row, bigEndian: true, }); @@ -212,7 +212,7 @@ export class PngDecoder implements Decoder { } } - private process(input: InputBuffer, image: MemoryImage): void { + private process(input: InputBuffer, image: MemoryImage): void { let channels = 1; if (this._info.colorType === PngColorType.grayscaleAlpha) { channels = 2; @@ -238,8 +238,8 @@ export class PngDecoder implements Decoder { const pIter = image[Symbol.iterator](); let pIterRes = pIter.next(); for (let y = 0, ri = 0; y < h; ++y, ri = 1 - ri) { - const filterType = input.readByte() as PngFilterType; - inData[ri] = input.readBytes(rowBytes).toUint8Array(); + const filterType = input.read() as PngFilterType; + inData[ri] = input.readRange(rowBytes).toUint8Array(); const row = inData[ri]; const prevRow = inData[1 - ri]; @@ -252,7 +252,7 @@ export class PngDecoder implements Decoder { // reset the bit stream counter. this.resetBits(); - const rowInput = new InputBuffer({ + const rowInput = new InputBuffer({ buffer: inData[ri], bigEndian: true, }); @@ -273,13 +273,13 @@ export class PngDecoder implements Decoder { /** * Read a number of bits from the input stream. */ - private readBits(input: InputBuffer, numBits: number): number { + private readBits(input: InputBuffer, numBits: number): number { if (numBits === 0) { return 0; } if (numBits === 8) { - return input.readByte(); + return input.read(); } if (numBits === 16) { @@ -293,7 +293,7 @@ export class PngDecoder implements Decoder { } // Input byte - const octet = input.readByte(); + const octet = input.read(); // Concat octet this._bitBuffer = octet << this._bitBufferLen; @@ -333,7 +333,7 @@ export class PngDecoder implements Decoder { /** * Read the next pixel from the input stream. */ - private readPixel(input: InputBuffer, pixel: number[]): void { + private readPixel(input: InputBuffer, pixel: number[]): void { switch (this._info.colorType) { case PngColorType.grayscale: pixel[0] = this.readBits(input, this._info.bits!); @@ -411,14 +411,14 @@ export class PngDecoder implements Decoder { * Is the given file a valid PNG image? */ public isValidFile(bytes: Uint8Array): boolean { - this._input = new InputBuffer({ + this._input = new InputBuffer({ buffer: bytes, bigEndian: true, }); - const headerBytes = this._input.readBytes(8); + const headerBytes = this._input.readRange(8); const expectedHeaderBytes = [137, 80, 78, 71, 13, 10, 26, 10]; for (let i = 0; i < 8; ++i) { - if (headerBytes.getByte(i) !== expectedHeaderBytes[i]) { + if (headerBytes.get(i) !== expectedHeaderBytes[i]) { return false; } } @@ -441,7 +441,7 @@ export class PngDecoder implements Decoder { switch (chunkType) { case 'tEXt': { - const txtData = this._input.readBytes(chunkSize).toUint8Array(); + const txtData = this._input.readRange(chunkSize).toUint8Array(); const l = txtData.length; for (let i = 0; i < l; ++i) { if (txtData[i] === 0) { @@ -460,15 +460,15 @@ export class PngDecoder implements Decoder { } break; case 'IHDR': { - const hdr = InputBuffer.from(this._input.readBytes(chunkSize)); + const hdr = InputBuffer.from(this._input.readRange(chunkSize)); const hdrBytes: Uint8Array = hdr.toUint8Array(); this._info.width = hdr.readUint32(); this._info.height = hdr.readUint32(); - this._info.bits = hdr.readByte(); - this._info.colorType = hdr.readByte(); - this._info.compressionMethod = hdr.readByte(); - this._info.filterMethod = hdr.readByte(); - this._info.interlaceMethod = hdr.readByte(); + this._info.bits = hdr.read(); + this._info.colorType = hdr.read(); + this._info.compressionMethod = hdr.read(); + this._info.filterMethod = hdr.read(); + this._info.interlaceMethod = hdr.read(); if (this._info.filterMethod !== 0) { return undefined; @@ -513,7 +513,7 @@ export class PngDecoder implements Decoder { break; } case 'PLTE': { - this._info.palette = this._input.readBytes(chunkSize).toUint8Array(); + this._info.palette = this._input.readRange(chunkSize).toUint8Array(); const crc = this._input.readUint32(); const computedCrc = PngDecoder.crc(chunkType, this._info.palette); if (crc !== computedCrc) { @@ -523,7 +523,7 @@ export class PngDecoder implements Decoder { } case 'tRNS': { this._info.transparency = this._input - .readBytes(chunkSize) + .readRange(chunkSize) .toUint8Array(); const crc = this._input.readUint32(); const computedCrc = PngDecoder.crc( @@ -579,8 +579,8 @@ export class PngDecoder implements Decoder { const yOffset = this._input.readUint32(); const delayNum = this._input.readUint16(); const delayDen = this._input.readUint16(); - const dispose = this._input.readByte() as PngDisposeMode; - const blend = this._input.readByte() as PngBlendMode; + const dispose = this._input.read() as PngDisposeMode; + const blend = this._input.read() as PngBlendMode; // CRC this._input.skip(4); @@ -609,7 +609,7 @@ export class PngDecoder implements Decoder { } case 'bKGD': { if (this._info.colorType === PngColorType.indexed) { - const paletteIndex = this._input.readByte(); + const paletteIndex = this._input.read(); chunkSize--; const p3 = paletteIndex * 3; const r = this._info.palette![p3]!; @@ -656,9 +656,9 @@ export class PngDecoder implements Decoder { case 'iCCP': { this._info.iccpName = this._input.readString(); // 0: deflate - this._info.iccpCompression = this._input.readByte(); + this._info.iccpCompression = this._input.read(); chunkSize -= this._info.iccpName.length + 2; - const profile = this._input.readBytes(chunkSize); + const profile = this._input.readRange(chunkSize); this._info.iccpData = profile.toUint8Array(); // CRC this._input.skip(4); @@ -704,7 +704,7 @@ export class PngDecoder implements Decoder { this._input.offset = this._info.idat[i]; const chunkSize = this._input.readUint32(); const chunkType = this._input.readString(4); - const data = this._input.readBytes(chunkSize).toUint8Array(); + const data = this._input.readRange(chunkSize).toUint8Array(); totalSize += data.length; dataBlocks.push(data); const crc = this._input.readUint32(); @@ -736,7 +736,7 @@ export class PngDecoder implements Decoder { this._input.readString(4); // Sequence number this._input.skip(4); - const data = this._input.readBytes(chunkSize - 4).toUint8Array(); + const data = this._input.readRange(chunkSize - 4).toUint8Array(); totalSize += data.length; dataBlocks.push(data); } @@ -769,7 +769,7 @@ export class PngDecoder implements Decoder { } // Input is the decompressed data. - const input = new InputBuffer({ + const input = new InputBuffer({ buffer: uncompressed, bigEndian: true, }); diff --git a/src/formats/tga-decoder.ts b/src/formats/tga-decoder.ts index b5012e0..fee3ab3 100644 --- a/src/formats/tga-decoder.ts +++ b/src/formats/tga-decoder.ts @@ -11,7 +11,7 @@ import { TgaInfo } from './tga/tga-info'; * Decode a TGA image. This only supports the 24-bit and 32-bit uncompressed format. */ export class TgaDecoder implements Decoder { - private _input: InputBuffer | undefined = undefined; + private _input: InputBuffer | undefined = undefined; private _info: TgaInfo | undefined = undefined; public get numFrames(): number { @@ -23,7 +23,7 @@ export class TgaDecoder implements Decoder { return; } - const cm = new InputBuffer({ + const cm = new InputBuffer({ buffer: colorMap, }); @@ -42,10 +42,10 @@ export class TgaDecoder implements Decoder { } else { const hasAlpha = this._info.colorMapDepth === 32; for (let i = 0; i < this._info.colorMapLength; ++i) { - const b = cm.readByte(); - const g = cm.readByte(); - const r = cm.readByte(); - const a = hasAlpha ? cm.readByte() : 255; + const b = cm.read(); + const g = cm.read(); + const r = cm.read(); + const a = hasAlpha ? cm.read() : 255; palette.setRed(i, r); palette.setGreen(i, g); palette.setBlue(i, b); @@ -80,12 +80,12 @@ export class TgaDecoder implements Decoder { let y = h - 1; let x = 0; while (!this._input.isEOS && y >= 0) { - const c = this._input.readByte(); + const c = this._input.read(); const count = (c & rleMask) + 1; if ((c & rleBit) !== 0) { if (bpp === 8) { - const r = this._input.readByte(); + const r = this._input.read(); for (let i = 0; i < count; ++i) { image.setPixelR(x++, y, r); if (x >= w) { @@ -113,10 +113,10 @@ export class TgaDecoder implements Decoder { } } } else { - const b = this._input.readByte(); - const g = this._input.readByte(); - const r = this._input.readByte(); - const a = hasAlpha ? this._input.readByte() : 255; + const b = this._input.read(); + const g = this._input.read(); + const r = this._input.read(); + const a = hasAlpha ? this._input.read() : 255; for (let i = 0; i < count; ++i) { image.setPixelRgba(x++, y, r, g, b, a); if (x >= w) { @@ -131,7 +131,7 @@ export class TgaDecoder implements Decoder { } else { if (bpp === 8) { for (let i = 0; i < count; ++i) { - const r = this._input.readByte(); + const r = this._input.read(); image.setPixelR(x++, y, r); if (x >= w) { x = 0; @@ -162,10 +162,10 @@ export class TgaDecoder implements Decoder { } } else { for (let i = 0; i < count; ++i) { - const b = this._input.readByte(); - const g = this._input.readByte(); - const r = this._input.readByte(); - const a = hasAlpha ? this._input.readByte() : 255; + const b = this._input.read(); + const g = this._input.read(); + const r = this._input.read(); + const a = hasAlpha ? this._input.read() : 255; image.setPixelRgba(x++, y, r, g, b, a); if (x >= w) { x = 0; @@ -218,7 +218,7 @@ export class TgaDecoder implements Decoder { if (bpp === 8) { for (let y = image.height - 1; y >= 0; --y) { for (let x = 0; x < image.width; ++x) { - const index = this._input.readByte(); + const index = this._input.read(); image.setPixelR(x, y, index); } } @@ -236,10 +236,10 @@ export class TgaDecoder implements Decoder { } else { for (let y = image.height - 1; y >= 0; --y) { for (let x = 0; x < image.width; ++x) { - const b = this._input.readByte(); - const g = this._input.readByte(); - const r = this._input.readByte(); - const a = hasAlpha ? this._input.readByte() : 255; + const b = this._input.read(); + const g = this._input.read(); + const r = this._input.read(); + const a = hasAlpha ? this._input.read() : 255; image.setPixelRgba(x, y, r, g, b, a); } } @@ -252,7 +252,7 @@ export class TgaDecoder implements Decoder { * Is the given file a valid TGA image? */ public isValidFile(bytes: Uint8Array): boolean { - const input = new InputBuffer({ + const input = new InputBuffer({ buffer: bytes, }); @@ -263,9 +263,9 @@ export class TgaDecoder implements Decoder { public startDecode(bytes: Uint8Array): TgaInfo | undefined { this._info = new TgaInfo(); - this._input = new InputBuffer({ buffer: bytes }); + this._input = new InputBuffer({ buffer: bytes }); - const header = this._input.readBytes(18); + const header = this._input.readRange(18); this._info.read(header); if (!this._info.isValid()) { return undefined; @@ -275,8 +275,8 @@ export class TgaDecoder implements Decoder { // Decode colormap if (this._info.hasColorMap) { - const size = this._info.colorMapLength * (this._info.colorMapDepth >> 3); - this._info.colorMap = this._input.readBytes(size).toUint8Array(); + const size = this._info.colorMapLength * (this._info.colorMapDepth >>> 3); + this._info.colorMap = this._input.readRange(size).toUint8Array(); } this._info.imageOffset = this._input.offset; diff --git a/src/formats/tga/tga-info.ts b/src/formats/tga/tga-info.ts index 83197b1..64023eb 100644 --- a/src/formats/tga/tga-info.ts +++ b/src/formats/tga/tga-info.ts @@ -117,15 +117,15 @@ export class TgaInfo implements DecodeInfo { ); } - public read(header: InputBuffer): void { + public read(header: InputBuffer): void { if (header.length < 18) { return; } // 0 - this._idLength = header.readByte(); + this._idLength = header.read(); // 1 - this._colorMapType = header.readByte(); - const it = header.readByte(); + this._colorMapType = header.read(); + const it = header.read(); // 2 this._imageType = it < TgaImageTypeLength ? (it as TgaImageType) : TgaImageType.none; @@ -134,7 +134,7 @@ export class TgaInfo implements DecodeInfo { // 5 this._colorMapLength = header.readUint16(); // 7 - this._colorMapDepth = header.readByte(); + this._colorMapDepth = header.read(); // 8 this._offsetX = header.readUint16(); // 10 @@ -144,9 +144,9 @@ export class TgaInfo implements DecodeInfo { // 14 this._height = header.readUint16(); // 16 - this._pixelDepth = header.readByte(); + this._pixelDepth = header.read(); // 17 - this._flags = header.readByte(); + this._flags = header.read(); this._screenOrigin = (this._flags & 0x30) >>> 4; } diff --git a/src/formats/tiff-decoder.ts b/src/formats/tiff-decoder.ts index 5ec6395..4680c88 100644 --- a/src/formats/tiff-decoder.ts +++ b/src/formats/tiff-decoder.ts @@ -13,7 +13,7 @@ export class TiffDecoder implements Decoder { private static readonly _tiffLittleEndian = 0x4949; private static readonly _tiffBigEndian = 0x4d4d; - private _input!: InputBuffer; + private _input!: InputBuffer; private _info: TiffInfo | undefined = undefined; public get info(): TiffInfo | undefined { @@ -36,7 +36,7 @@ export class TiffDecoder implements Decoder { /** * Read the TIFF header and IFD blocks. */ - private readHeader(p: InputBuffer): TiffInfo | undefined { + private readHeader(p: InputBuffer): TiffInfo | undefined { const byteOrder = p.readUint16(); if ( byteOrder !== TiffDecoder._tiffLittleEndian && @@ -99,7 +99,7 @@ export class TiffDecoder implements Decoder { * Is the given file a valid TIFF image? */ public isValidFile(bytes: Uint8Array): boolean { - const buffer = new InputBuffer({ + const buffer = new InputBuffer({ buffer: bytes, }); return this.readHeader(buffer) !== undefined; @@ -110,12 +110,12 @@ export class TiffDecoder implements Decoder { * If the file is not a valid TIFF image, undefined is returned. */ public startDecode(bytes: Uint8Array): TiffInfo | undefined { - this._input = new InputBuffer({ + this._input = new InputBuffer({ buffer: bytes, }); this._info = this.readHeader(this._input); if (this.info !== undefined) { - const buffer = new InputBuffer({ + const buffer = new InputBuffer({ buffer: bytes, }); this._exifData = ExifData.fromInputBuffer(buffer); @@ -148,7 +148,7 @@ export class TiffDecoder implements Decoder { public decode(opt: DecoderDecodeOptions): MemoryImage | undefined { const bytes = opt.bytes; - this._input = new InputBuffer({ + this._input = new InputBuffer({ buffer: bytes, }); @@ -167,7 +167,7 @@ export class TiffDecoder implements Decoder { return undefined; } image.exifData = ExifData.fromInputBuffer( - new InputBuffer({ + new InputBuffer({ buffer: bytes, }) ); diff --git a/src/formats/tiff/tiff-bit-reader.ts b/src/formats/tiff/tiff-bit-reader.ts index 40ac170..ff97a26 100644 --- a/src/formats/tiff/tiff-bit-reader.ts +++ b/src/formats/tiff/tiff-bit-reader.ts @@ -9,9 +9,9 @@ export class TiffBitReader { private _bitPosition = 0; - private _input: InputBuffer; + private _input: InputBuffer; - constructor(input: InputBuffer) { + constructor(input: InputBuffer) { this._input = input; } @@ -26,7 +26,7 @@ export class TiffBitReader { if (this._bitPosition === 0) { this._bitPosition = 8; - this._bitBuffer = this._input.readByte(); + this._bitBuffer = this._input.read(); } let value = 0; @@ -37,13 +37,13 @@ export class TiffBitReader { (this._bitBuffer & TiffBitReader._bitMask[this._bitPosition]); nBits -= this._bitPosition; this._bitPosition = 8; - this._bitBuffer = this._input.readByte(); + this._bitBuffer = this._input.read(); } if (nBits > 0) { if (this._bitPosition === 0) { this._bitPosition = 8; - this._bitBuffer = this._input.readByte(); + this._bitBuffer = this._input.read(); } value = diff --git a/src/formats/tiff/tiff-entry.ts b/src/formats/tiff/tiff-entry.ts index 81091b7..1d86aad 100644 --- a/src/formats/tiff/tiff-entry.ts +++ b/src/formats/tiff/tiff-entry.ts @@ -21,7 +21,7 @@ export interface TiffEntryInitOptions { tag: number; type: number; count: number; - p: InputBuffer; + p: InputBuffer; valueOffset: number; } @@ -51,8 +51,8 @@ export class TiffEntry { return this._value; } - private _p: InputBuffer; - public get p(): InputBuffer { + private _p: InputBuffer; + public get p(): InputBuffer { return this._p; } @@ -82,7 +82,7 @@ export class TiffEntry { } this._p.offset = this._valueOffset; - const data = this.p.readBytes(this._count * this.typeSize); + const data = this.p.readRange(this._count * this.typeSize); switch (this._type) { case IfdValueType.byte: return (this._value = IfdByteValue.data(data, this._count)); diff --git a/src/formats/tiff/tiff-fax-decoder.ts b/src/formats/tiff/tiff-fax-decoder.ts index e59e27f..d1ad960 100644 --- a/src/formats/tiff/tiff-fax-decoder.ts +++ b/src/formats/tiff/tiff-fax-decoder.ts @@ -545,7 +545,7 @@ export class TiffFaxDecoder { private _changingElemSize = 0; private _prevChangingElements?: Array; private _currChangingElements?: Array; - private _data!: InputBuffer; + private _data!: InputBuffer; private _bitPointer = 0; private _bytePointer = 0; @@ -576,31 +576,30 @@ export class TiffFaxDecoder { const bp = this._bytePointer; if (this._fillOrder === 1) { - b = this._data.getByte(bp); + b = this._data.get(bp); if (bp === l) { next = 0x00; next2next = 0x00; } else if (bp + 1 === l) { - next = this._data.getByte(bp + 1); + next = this._data.get(bp + 1); next2next = 0x00; } else { - next = this._data.getByte(bp + 1); - next2next = this._data.getByte(bp + 2); + next = this._data.get(bp + 1); + next2next = this._data.get(bp + 2); } } else if (this._fillOrder === 2) { - b = TiffFaxDecoder._flipTable[this._data.getByte(bp) & 0xff]; + b = TiffFaxDecoder._flipTable[this._data.get(bp) & 0xff]; if (bp === l) { next = 0x00; next2next = 0x00; } else if (bp + 1 === l) { - next = TiffFaxDecoder._flipTable[this._data.getByte(bp + 1) & 0xff]; + next = TiffFaxDecoder._flipTable[this._data.get(bp + 1) & 0xff]; next2next = 0x00; } else { - next = TiffFaxDecoder._flipTable[this._data.getByte(bp + 1) & 0xff]; - next2next = - TiffFaxDecoder._flipTable[this._data.getByte(bp + 2) & 0xff]; + next = TiffFaxDecoder._flipTable[this._data.get(bp + 1) & 0xff]; + next2next = TiffFaxDecoder._flipTable[this._data.get(bp + 2) & 0xff]; } } else { throw new LibError('TIFFFaxDecoder7'); @@ -649,18 +648,18 @@ export class TiffFaxDecoder { const bp = this._bytePointer; if (this._fillOrder === 1) { - b = this._data.getByte(bp); + b = this._data.get(bp); if (bp === l) { next = 0x00; } else { - next = this._data.getByte(bp + 1); + next = this._data.get(bp + 1); } } else if (this._fillOrder === 2) { - b = TiffFaxDecoder._flipTable[this._data.getByte(bp) & 0xff]; + b = TiffFaxDecoder._flipTable[this._data.get(bp) & 0xff]; if (bp === l) { next = 0x00; } else { - next = TiffFaxDecoder._flipTable[this._data.getByte(bp + 1) & 0xff]; + next = TiffFaxDecoder._flipTable[this._data.get(bp + 1) & 0xff]; } } else { throw new LibError('TIFFFaxDecoder7'); @@ -720,7 +719,7 @@ export class TiffFaxDecoder { } private setToBlack( - buffer: InputBuffer, + buffer: InputBuffer, lineOffset: number, bitOffset: number, numBits: number @@ -734,35 +733,32 @@ export class TiffFaxDecoder { const shift = bitNum & 0x7; if (shift > 0) { let maskVal = 1 << (7 - shift); - let val = buffer.getByte(byteNum); + let val = buffer.get(byteNum); while (maskVal > 0 && bitNum < lastBit) { val |= maskVal; maskVal >>>= 1; ++bitNum; } - buffer.setByte(byteNum, val); + buffer.set(byteNum, val); } // Fill in 8 bits at a time byteNum = bitNum >>> 3; while (bitNum < lastBit - 7) { - buffer.setByte(byteNum++, 255); + buffer.set(byteNum++, 255); bitNum += 8; } // Fill in remaining bits while (bitNum < lastBit) { - byteNum = bitNum >> 3; - buffer.setByte( - byteNum, - buffer.getByte(byteNum) | (1 << (7 - (bitNum & 0x7))) - ); + byteNum = bitNum >>> 3; + buffer.set(byteNum, buffer.get(byteNum) | (1 << (7 - (bitNum & 0x7)))); ++bitNum; } } private decodeNextScanline( - buffer: InputBuffer, + buffer: InputBuffer, lineOffset: number, bitOffset: number ): void { @@ -1128,8 +1124,8 @@ export class TiffFaxDecoder { * One-dimensional decoding methods */ public decode1D( - out: InputBuffer, - compData: InputBuffer, + out: InputBuffer, + compData: InputBuffer, startX: number, height: number ): void { @@ -1150,8 +1146,8 @@ export class TiffFaxDecoder { * Two-dimensional decoding methods */ public decode2D( - out: InputBuffer, - compData: InputBuffer, + out: InputBuffer, + compData: InputBuffer, startX: number, height: number, tiffT4Options: number @@ -1305,8 +1301,8 @@ export class TiffFaxDecoder { } public decodeT6( - out: InputBuffer, - compData: InputBuffer, + out: InputBuffer, + compData: InputBuffer, startX: number, height: number, tiffT6Options: number diff --git a/src/formats/tiff/tiff-image.ts b/src/formats/tiff/tiff-image.ts index 174c23f..6f37f30 100644 --- a/src/formats/tiff/tiff-image.ts +++ b/src/formats/tiff/tiff-image.ts @@ -173,7 +173,7 @@ export class TiffImage { return this._width !== 0 && this._height !== 0; } - constructor(p: InputBuffer) { + constructor(p: InputBuffer) { const p3 = InputBuffer.from(p); const numDirEntries = p.readUint16(); @@ -419,7 +419,7 @@ export class TiffImage { } private decodeBilevelTile( - p: InputBuffer, + p: InputBuffer, image: MemoryImage, tileX: number, tileY: number @@ -432,7 +432,7 @@ export class TiffImage { const byteCount = this._tileByteCounts![tileIndex]; - let byteData: InputBuffer | undefined = undefined; + let byteData: InputBuffer | undefined = undefined; if (this._compression === TiffCompression.packBits) { // Since the decompressed data will still be packed // 8 pixels into 1 byte, calculate bytesInThisTile @@ -443,17 +443,20 @@ export class TiffImage { bytesInThisTile = (Math.trunc(this._tileWidth / 8) + 1) * this._tileHeight; } - byteData = new InputBuffer({ + byteData = new InputBuffer({ buffer: new Uint8Array(this._tileWidth * this._tileHeight), }); - this.decodePackBits(p, bytesInThisTile, byteData.buffer); + this.decodePackBits(p, bytesInThisTile, byteData.buffer as Uint8Array); } else if (this._compression === TiffCompression.lzw) { - byteData = new InputBuffer({ + byteData = new InputBuffer({ buffer: new Uint8Array(this._tileWidth * this._tileHeight), }); const decoder = new LzwDecoder(); - decoder.decode(InputBuffer.from(p, 0, byteCount), byteData.buffer); + decoder.decode( + InputBuffer.from(p, 0, byteCount), + byteData.buffer as Uint8Array + ); // Horizontal Differencing Predictor if (this._predictor === 2) { @@ -466,15 +469,14 @@ export class TiffImage { i++ ) { const b = - byteData.getByte(count) + - byteData.getByte(count - this._samplesPerPixel); - byteData.setByte(count, b); + byteData.get(count) + byteData.get(count - this._samplesPerPixel); + byteData.set(count, b); count++; } } } } else if (this._compression === TiffCompression.ccittRle) { - byteData = new InputBuffer({ + byteData = new InputBuffer({ buffer: new Uint8Array(this._tileWidth * this._tileHeight), }); try { @@ -488,7 +490,7 @@ export class TiffImage { // skip } } else if (this._compression === TiffCompression.ccittFax3) { - byteData = new InputBuffer({ + byteData = new InputBuffer({ buffer: new Uint8Array(this._tileWidth * this._tileHeight), }); try { @@ -502,7 +504,7 @@ export class TiffImage { // skip } } else if (this._compression === TiffCompression.ccittFax4) { - byteData = new InputBuffer({ + byteData = new InputBuffer({ buffer: new Uint8Array(this._tileWidth * this._tileHeight), }); try { @@ -518,13 +520,13 @@ export class TiffImage { } else if (this._compression === TiffCompression.zip) { const data = p.toUint8Array(0, byteCount); const outData = inflate(data); - byteData = new InputBuffer({ + byteData = new InputBuffer({ buffer: outData, }); } else if (this._compression === TiffCompression.deflate) { const data = p.toUint8Array(0, byteCount); const outData = inflate(data); - byteData = new InputBuffer({ + byteData = new InputBuffer({ buffer: outData, }); } else if (this._compression === TiffCompression.none) { @@ -552,7 +554,7 @@ export class TiffImage { } private decodeTile( - p: InputBuffer, + p: InputBuffer, image: MemoryImage, tileX: number, tileY: number @@ -579,7 +581,7 @@ export class TiffImage { bytesInThisTile *= 4; } - let byteData: InputBuffer | undefined = undefined; + let byteData: InputBuffer | undefined = undefined; if ( this._bitsPerSample === 8 || this._bitsPerSample === 16 || @@ -589,7 +591,7 @@ export class TiffImage { if (this._compression === TiffCompression.none) { byteData = p; } else if (this._compression === TiffCompression.lzw) { - byteData = new InputBuffer({ + byteData = new InputBuffer({ buffer: new Uint8Array(bytesInThisTile), }); const decoder = new LzwDecoder(); @@ -605,30 +607,30 @@ export class TiffImage { count = this._samplesPerPixel * (j * this._tileWidth + 1); const len = this._tileWidth * this._samplesPerPixel; for (let i = this._samplesPerPixel; i < len; i++) { - byteData.setByte( + byteData.set( count, - byteData.getByte(count) + - byteData.getByte(count - this._samplesPerPixel) + byteData.get(count) + + byteData.get(count - this._samplesPerPixel) ); count++; } } } } else if (this._compression === TiffCompression.packBits) { - byteData = new InputBuffer({ + byteData = new InputBuffer({ buffer: new Uint8Array(bytesInThisTile), }); this.decodePackBits(p, bytesInThisTile, byteData.buffer); } else if (this._compression === TiffCompression.deflate) { const data = p.toUint8Array(0, byteCount); const outData = inflate(data); - byteData = new InputBuffer({ + byteData = new InputBuffer({ buffer: outData, }); } else if (this._compression === TiffCompression.zip) { const data = p.toUint8Array(0, byteCount); const outData = inflate(data); - byteData = new InputBuffer({ + byteData = new InputBuffer({ buffer: outData, }); } else if (this._compression === TiffCompression.oldJpeg) { @@ -680,7 +682,7 @@ export class TiffImage { sample = this._sampleFormat === TiffFormat.int ? byteData.readInt8() - : byteData.readByte(); + : byteData.read(); } else if (this._bitsPerSample === 16) { sample = this._sampleFormat === TiffFormat.int @@ -707,11 +709,11 @@ export class TiffImage { gray = this._sampleFormat === TiffFormat.int ? byteData.readInt8() - : byteData.readByte(); + : byteData.read(); alpha = this._sampleFormat === TiffFormat.int ? byteData.readInt8() - : byteData.readByte(); + : byteData.read(); } else if (this._bitsPerSample === 16) { gray = this._sampleFormat === TiffFormat.int @@ -760,15 +762,15 @@ export class TiffImage { r = this._sampleFormat === TiffFormat.int ? byteData.readInt8() - : byteData.readByte(); + : byteData.read(); g = this._sampleFormat === TiffFormat.int ? byteData.readInt8() - : byteData.readByte(); + : byteData.read(); b = this._sampleFormat === TiffFormat.int ? byteData.readInt8() - : byteData.readByte(); + : byteData.read(); } else if (this._bitsPerSample === 16) { r = this._sampleFormat === TiffFormat.int @@ -831,19 +833,19 @@ export class TiffImage { r = this._sampleFormat === TiffFormat.int ? byteData.readInt8() - : byteData.readByte(); + : byteData.read(); g = this._sampleFormat === TiffFormat.int ? byteData.readInt8() - : byteData.readByte(); + : byteData.read(); b = this._sampleFormat === TiffFormat.int ? byteData.readInt8() - : byteData.readByte(); + : byteData.read(); a = this._sampleFormat === TiffFormat.int ? byteData.readInt8() - : byteData.readByte(); + : byteData.read(); } else if (this._bitsPerSample === 16) { r = this._sampleFormat === TiffFormat.int @@ -919,7 +921,7 @@ export class TiffImage { * Uncompress packbits compressed image data. */ private decodePackBits( - data: InputBuffer, + data: InputBuffer, arraySize: number, dst: Uint8Array ): void { @@ -927,15 +929,15 @@ export class TiffImage { let dstCount = 0; while (dstCount < arraySize) { - const b = BitUtils.uint8ToInt8(data.getByte(srcCount++)); + const b = BitUtils.uint8ToInt8(data.get(srcCount++)); if (b >= 0 && b <= 127) { // literal run packet for (let i = 0; i < b + 1; ++i) { - dst[dstCount++] = data.getByte(srcCount++); + dst[dstCount++] = data.get(srcCount++); } } else if (b <= -1 && b >= -127) { // 2 byte encoded run packet - const repeat = data.getByte(srcCount++); + const repeat = data.get(srcCount++); for (let i = 0; i < -b + 1; ++i) { dst[dstCount++] = repeat; } @@ -946,7 +948,7 @@ export class TiffImage { } } - public decode(p: InputBuffer): MemoryImage { + public decode(p: InputBuffer): MemoryImage { const isFloat = this._sampleFormat === TiffFormat.float; const isInt = this._sampleFormat === TiffFormat.int; const format = diff --git a/src/formats/tiff/tiff-lzw-decoder.ts b/src/formats/tiff/tiff-lzw-decoder.ts index 814f528..dbf4080 100644 --- a/src/formats/tiff/tiff-lzw-decoder.ts +++ b/src/formats/tiff/tiff-lzw-decoder.ts @@ -90,7 +90,7 @@ export class LzwDecoder { this._tableIndex = 258; } - public decode(p: InputBuffer, out: Uint8Array): void { + public decode(p: InputBuffer, out: Uint8Array): void { this._out = out; const outLen = out.length; this._outPointer = 0; diff --git a/test/exif/exif.base.test.ts b/test/exif/exif.base.test.ts index c1fcedb..3e44e54 100644 --- a/test/exif/exif.base.test.ts +++ b/test/exif/exif.base.test.ts @@ -63,7 +63,7 @@ describe('Exif', () => { exif.write(out); const exif1 = new ExifData(); - const input = new InputBuffer({ + const input = new InputBuffer({ buffer: out.getBytes(), }); exif1.read(input);