UNPKG

image-in-browser

Version:

Package for encoding / decoding images, transforming images, applying filters, drawing primitives on images on the client side (no need for server Node.js)

136 lines 4.1 kB
import { InputBuffer } from '../common/input-buffer.js'; import { ExifData } from '../exif/exif-data.js'; import { FrameType } from '../image/frame-type.js'; import { ImageFormat } from './image-format.js'; import { TiffImage } from './tiff/tiff-image.js'; import { TiffInfo } from './tiff/tiff-info.js'; export class TiffDecoder { constructor() { this._info = undefined; this._exifData = undefined; } get info() { return this._info; } get exifData() { return this._exifData; } get format() { return ImageFormat.tiff; } get numFrames() { return this._info !== undefined ? this._info.images.length : 0; } readHeader(p) { const byteOrder = p.readUint16(); if (byteOrder !== TiffDecoder._tiffLittleEndian && byteOrder !== TiffDecoder._tiffBigEndian) { return undefined; } let bigEndian = false; if (byteOrder === TiffDecoder._tiffBigEndian) { p.bigEndian = true; bigEndian = true; } else { p.bigEndian = false; bigEndian = false; } let signature = 0; signature = p.readUint16(); if (signature !== TiffDecoder._tiffSignature) { return undefined; } let offset = p.readUint32(); const ifdOffset = offset; const p2 = InputBuffer.from(p); p2.offset = offset; const images = []; while (offset !== 0) { let img = undefined; try { img = new TiffImage(p2); if (!img.isValid) { break; } } catch (error) { break; } images.push(img); offset = p2.readUint32(); if (offset !== 0) { p2.offset = offset; } } return images.length > 0 ? new TiffInfo({ bigEndian: bigEndian, signature: signature, ifdOffset: ifdOffset, images: images, }) : undefined; } isValidFile(bytes) { const buffer = new InputBuffer({ buffer: bytes, }); return this.readHeader(buffer) !== undefined; } startDecode(bytes) { this._input = new InputBuffer({ buffer: bytes, }); this._info = this.readHeader(this._input); if (this.info !== undefined) { const buffer = new InputBuffer({ buffer: bytes, }); this._exifData = ExifData.fromInputBuffer(buffer); } return this._info; } decodeFrame(frameIndex) { if (this._info === undefined) { return undefined; } const image = this._info.images[frameIndex].decode(this._input); if (this._exifData !== undefined) { image.exifData = this._exifData; } return image; } decode(opt) { var _a; const bytes = opt.bytes; this._input = new InputBuffer({ buffer: bytes, }); this._info = this.readHeader(this._input); if (this._info === undefined) { return undefined; } const len = this.numFrames; if (len === 1 || opt.frameIndex !== undefined) { return this.decodeFrame((_a = opt.frameIndex) !== null && _a !== void 0 ? _a : 0); } const image = this.decodeFrame(0); if (image === undefined) { return undefined; } image.exifData = ExifData.fromInputBuffer(new InputBuffer({ buffer: bytes, })); image.frameType = FrameType.page; for (let i = 1; i < len; ++i) { const frame = this.decodeFrame(i); image.addFrame(frame); } return image; } } TiffDecoder._tiffSignature = 42; TiffDecoder._tiffLittleEndian = 0x4949; TiffDecoder._tiffBigEndian = 0x4d4d; //# sourceMappingURL=tiff-decoder.js.map