Comparing version 5.0.3 to 6.0.0
@@ -34,3 +34,3 @@ import * as exif from './tags/exif'; | ||
const taglist = tags[this.kind].tagsById; | ||
for (let key of this.fields.keys()) { | ||
for (const key of this.fields.keys()) { | ||
if (taglist[key]) { | ||
@@ -37,0 +37,0 @@ this._map[taglist[key]] = this.fields.get(key); |
@@ -1,13 +0,13 @@ | ||
let types = new Map([ | ||
[1, [1, readByte]], | ||
[2, [1, readASCII]], | ||
[3, [2, readShort]], | ||
[4, [4, readLong]], | ||
[5, [8, readRational]], | ||
[6, [1, readSByte]], | ||
[7, [1, readByte]], | ||
[8, [2, readSShort]], | ||
[9, [4, readSLong]], | ||
[10, [8, readSRational]], | ||
[11, [4, readFloat]], | ||
const types = new Map([ | ||
[1, [1, readByte]], // BYTE | ||
[2, [1, readASCII]], // ASCII | ||
[3, [2, readShort]], // SHORT | ||
[4, [4, readLong]], // LONG | ||
[5, [8, readRational]], // RATIONAL | ||
[6, [1, readSByte]], // SBYTE | ||
[7, [1, readByte]], // UNDEFINED | ||
[8, [2, readSShort]], // SSHORT | ||
[9, [4, readSLong]], // SLONG | ||
[10, [8, readSRational]], // SRATIONAL | ||
[11, [4, readFloat]], // FLOAT | ||
[12, [8, readDouble]], // DOUBLE | ||
@@ -30,3 +30,3 @@ ]); | ||
return decoder.readUint8(); | ||
let array = new Uint8Array(count); | ||
const array = new Uint8Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -38,6 +38,6 @@ array[i] = decoder.readUint8(); | ||
function readASCII(decoder, count) { | ||
let strings = []; | ||
const strings = []; | ||
let currentString = ''; | ||
for (let i = 0; i < count; i++) { | ||
let char = String.fromCharCode(decoder.readUint8()); | ||
const char = String.fromCharCode(decoder.readUint8()); | ||
if (char === '\0') { | ||
@@ -61,3 +61,3 @@ strings.push(currentString); | ||
return decoder.readUint16(); | ||
let array = new Uint16Array(count); | ||
const array = new Uint16Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -71,3 +71,3 @@ array[i] = decoder.readUint16(); | ||
return decoder.readUint32(); | ||
let array = new Uint32Array(count); | ||
const array = new Uint32Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -82,3 +82,3 @@ array[i] = decoder.readUint32(); | ||
} | ||
let rationals = new Array(count); | ||
const rationals = new Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -92,3 +92,3 @@ rationals[i] = decoder.readUint32() / decoder.readUint32(); | ||
return decoder.readInt8(); | ||
let array = new Int8Array(count); | ||
const array = new Int8Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -102,3 +102,3 @@ array[i] = decoder.readInt8(); | ||
return decoder.readInt16(); | ||
let array = new Int16Array(count); | ||
const array = new Int16Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -112,3 +112,3 @@ array[i] = decoder.readInt16(); | ||
return decoder.readInt32(); | ||
let array = new Int32Array(count); | ||
const array = new Int32Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -123,3 +123,3 @@ array[i] = decoder.readInt32(); | ||
} | ||
let rationals = new Array(count); | ||
const rationals = new Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -133,3 +133,3 @@ rationals[i] = decoder.readInt32() / decoder.readInt32(); | ||
return decoder.readFloat32(); | ||
let array = new Float32Array(count); | ||
const array = new Float32Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -143,3 +143,3 @@ array[i] = decoder.readFloat32(); | ||
return decoder.readFloat64(); | ||
let array = new Float64Array(count); | ||
const array = new Float64Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -146,0 +146,0 @@ array[i] = decoder.readFloat64(); |
import TIFFDecoder from './tiffDecoder'; | ||
import TiffIfd from './tiffIfd'; | ||
function decodeTIFF(data, options) { | ||
@@ -14,3 +15,3 @@ const decoder = new TIFFDecoder(data); | ||
} | ||
export { decodeTIFF as decode, isMultiPage, pageCount }; | ||
export { decodeTIFF as decode, isMultiPage, pageCount, TiffIfd }; | ||
//# sourceMappingURL=index.js.map |
@@ -73,3 +73,3 @@ const tagsById = { | ||
const tagsByName = {}; | ||
for (let i in tagsById) { | ||
for (const i in tagsById) { | ||
tagsByName[tagsById[i]] = Number(i); | ||
@@ -76,0 +76,0 @@ } |
@@ -36,3 +36,3 @@ const tagsById = { | ||
const tagsByName = {}; | ||
for (let i in tagsById) { | ||
for (const i in tagsById) { | ||
tagsByName[tagsById[i]] = Number(i); | ||
@@ -39,0 +39,0 @@ } |
@@ -176,3 +176,3 @@ const tagsById = { | ||
const tagsByName = {}; | ||
for (let i in tagsById) { | ||
for (const i in tagsById) { | ||
tagsByName[tagsById[i]] = Number(i); | ||
@@ -179,0 +179,0 @@ } |
@@ -11,3 +11,2 @@ import { IOBuffer } from 'iobuffer'; | ||
ignoreImageData: false, | ||
onlyFirst: false, | ||
}; | ||
@@ -47,11 +46,29 @@ export default class TIFFDecoder extends IOBuffer { | ||
decode(options = {}) { | ||
options = Object.assign({}, defaultOptions, options); | ||
const { pages } = options; | ||
checkPages(pages); | ||
const maxIndex = pages ? Math.max(...pages) : Infinity; | ||
options = { ...defaultOptions, ...options }; | ||
const result = []; | ||
this.decodeHeader(); | ||
let index = 0; | ||
while (this._nextIFD) { | ||
result.push(this.decodeIFD(options, true)); | ||
if (options.onlyFirst) { | ||
return [result[0]]; | ||
if (pages) { | ||
if (pages.includes(index)) { | ||
result.push(this.decodeIFD(options, true)); | ||
} | ||
else { | ||
this.decodeIFD({ ignoreImageData: true }, true); | ||
} | ||
if (index === maxIndex) { | ||
break; | ||
} | ||
} | ||
else { | ||
result.push(this.decodeIFD(options, true)); | ||
} | ||
index++; | ||
} | ||
if (index < maxIndex && maxIndex !== Infinity) { | ||
throw new RangeError(`Index ${maxIndex} is out of bounds. The stack only contains ${index} images.`); | ||
} | ||
return result; | ||
@@ -120,3 +137,3 @@ } | ||
if (tag === 0x8769 || tag === 0x8825) { | ||
let currentOffset = this.offset; | ||
const currentOffset = this.offset; | ||
let kind = 'exif'; | ||
@@ -160,3 +177,3 @@ if (tag === 0x8769) { | ||
const bitDepth = ifd.bitsPerSample; | ||
const maxValue = Math.pow(2, bitDepth) - 1; | ||
const maxValue = 2 ** bitDepth - 1; | ||
for (let i = 0; i < ifd.data.length; i++) { | ||
@@ -181,5 +198,5 @@ ifd.data[i] = maxValue - ifd.data[i]; | ||
for (let i = 0; i < stripOffsets.length; i++) { | ||
let stripData = new DataView(this.buffer, this.byteOffset + stripOffsets[i], stripByteCounts[i]); | ||
const stripData = new DataView(this.buffer, this.byteOffset + stripOffsets[i], stripByteCounts[i]); | ||
// Last strip can be smaller | ||
let length = remainingPixels > maxPixels ? maxPixels : remainingPixels; | ||
const length = remainingPixels > maxPixels ? maxPixels : remainingPixels; | ||
remainingPixels -= length; | ||
@@ -297,2 +314,11 @@ let dataToFill = stripData; | ||
} | ||
function checkPages(pages) { | ||
if (pages) { | ||
for (const page of pages) { | ||
if (page < 0 || !Number.isInteger(page)) { | ||
throw new RangeError(`Index ${page} is invalid. Must be a positive integer.`); | ||
} | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=tiffDecoder.js.map |
@@ -22,4 +22,4 @@ import Ifd from './ifd'; | ||
get date() { | ||
let date = new Date(); | ||
let result = dateTimeRegex.exec(this.dateTime); | ||
const date = new Date(); | ||
const result = dateTimeRegex.exec(this.dateTime); | ||
if (result === null) { | ||
@@ -98,3 +98,3 @@ throw new Error(`invalid dateTime: ${this.dateTime}`); | ||
get maxSampleValue() { | ||
return this.get('MaxSampleValue') || Math.pow(2, this.bitsPerSample) - 1; | ||
return this.get('MaxSampleValue') || 2 ** this.bitsPerSample - 1; | ||
} | ||
@@ -120,3 +120,4 @@ get xResolution() { | ||
get sampleFormat() { | ||
return this.get('SampleFormat') || 1; | ||
const data = alwaysArray(this.get('SampleFormat') || 1); | ||
return data[0]; | ||
} | ||
@@ -123,0 +124,0 @@ get sMinSampleValue() { |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -55,3 +59,3 @@ if (k2 === undefined) k2 = k; | ||
const taglist = tags[this.kind].tagsById; | ||
for (let key of this.fields.keys()) { | ||
for (const key of this.fields.keys()) { | ||
if (taglist[key]) { | ||
@@ -58,0 +62,0 @@ this._map[taglist[key]] = this.fields.get(key); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.readData = exports.getByteLength = void 0; | ||
let types = new Map([ | ||
[1, [1, readByte]], | ||
[2, [1, readASCII]], | ||
[3, [2, readShort]], | ||
[4, [4, readLong]], | ||
[5, [8, readRational]], | ||
[6, [1, readSByte]], | ||
[7, [1, readByte]], | ||
[8, [2, readSShort]], | ||
[9, [4, readSLong]], | ||
[10, [8, readSRational]], | ||
[11, [4, readFloat]], | ||
const types = new Map([ | ||
[1, [1, readByte]], // BYTE | ||
[2, [1, readASCII]], // ASCII | ||
[3, [2, readShort]], // SHORT | ||
[4, [4, readLong]], // LONG | ||
[5, [8, readRational]], // RATIONAL | ||
[6, [1, readSByte]], // SBYTE | ||
[7, [1, readByte]], // UNDEFINED | ||
[8, [2, readSShort]], // SSHORT | ||
[9, [4, readSLong]], // SLONG | ||
[10, [8, readSRational]], // SRATIONAL | ||
[11, [4, readFloat]], // FLOAT | ||
[12, [8, readDouble]], // DOUBLE | ||
@@ -35,3 +35,3 @@ ]); | ||
return decoder.readUint8(); | ||
let array = new Uint8Array(count); | ||
const array = new Uint8Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -43,6 +43,6 @@ array[i] = decoder.readUint8(); | ||
function readASCII(decoder, count) { | ||
let strings = []; | ||
const strings = []; | ||
let currentString = ''; | ||
for (let i = 0; i < count; i++) { | ||
let char = String.fromCharCode(decoder.readUint8()); | ||
const char = String.fromCharCode(decoder.readUint8()); | ||
if (char === '\0') { | ||
@@ -66,3 +66,3 @@ strings.push(currentString); | ||
return decoder.readUint16(); | ||
let array = new Uint16Array(count); | ||
const array = new Uint16Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -76,3 +76,3 @@ array[i] = decoder.readUint16(); | ||
return decoder.readUint32(); | ||
let array = new Uint32Array(count); | ||
const array = new Uint32Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -87,3 +87,3 @@ array[i] = decoder.readUint32(); | ||
} | ||
let rationals = new Array(count); | ||
const rationals = new Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -97,3 +97,3 @@ rationals[i] = decoder.readUint32() / decoder.readUint32(); | ||
return decoder.readInt8(); | ||
let array = new Int8Array(count); | ||
const array = new Int8Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -107,3 +107,3 @@ array[i] = decoder.readInt8(); | ||
return decoder.readInt16(); | ||
let array = new Int16Array(count); | ||
const array = new Int16Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -117,3 +117,3 @@ array[i] = decoder.readInt16(); | ||
return decoder.readInt32(); | ||
let array = new Int32Array(count); | ||
const array = new Int32Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -128,3 +128,3 @@ array[i] = decoder.readInt32(); | ||
} | ||
let rationals = new Array(count); | ||
const rationals = new Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -138,3 +138,3 @@ rationals[i] = decoder.readInt32() / decoder.readInt32(); | ||
return decoder.readFloat32(); | ||
let array = new Float32Array(count); | ||
const array = new Float32Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -148,3 +148,3 @@ array[i] = decoder.readFloat32(); | ||
return decoder.readFloat64(); | ||
let array = new Float64Array(count); | ||
const array = new Float64Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -151,0 +151,0 @@ array[i] = decoder.readFloat64(); |
import TiffIfd from './tiffIfd'; | ||
import { BufferType, IDecodeOptions } from './types'; | ||
declare function decodeTIFF(data: BufferType, options?: IDecodeOptions): TiffIfd[]; | ||
import { BufferType, DecodeOptions } from './types'; | ||
declare function decodeTIFF(data: BufferType, options?: DecodeOptions): TiffIfd[]; | ||
declare function isMultiPage(data: BufferType): boolean; | ||
declare function pageCount(data: BufferType): number; | ||
export { decodeTIFF as decode, isMultiPage, pageCount, IDecodeOptions }; | ||
export { decodeTIFF as decode, isMultiPage, pageCount, DecodeOptions, TiffIfd }; |
@@ -6,4 +6,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.pageCount = exports.isMultiPage = exports.decode = void 0; | ||
exports.TiffIfd = exports.pageCount = exports.isMultiPage = exports.decode = void 0; | ||
const tiffDecoder_1 = __importDefault(require("./tiffDecoder")); | ||
const tiffIfd_1 = __importDefault(require("./tiffIfd")); | ||
exports.TiffIfd = tiffIfd_1.default; | ||
function decodeTIFF(data, options) { | ||
@@ -10,0 +12,0 @@ const decoder = new tiffDecoder_1.default(data); |
@@ -78,5 +78,5 @@ "use strict"; | ||
exports.tagsByName = tagsByName; | ||
for (let i in tagsById) { | ||
for (const i in tagsById) { | ||
tagsByName[tagsById[i]] = Number(i); | ||
} | ||
//# sourceMappingURL=exif.js.map |
@@ -41,5 +41,5 @@ "use strict"; | ||
exports.tagsByName = tagsByName; | ||
for (let i in tagsById) { | ||
for (const i in tagsById) { | ||
tagsByName[tagsById[i]] = Number(i); | ||
} | ||
//# sourceMappingURL=gps.js.map |
@@ -181,5 +181,5 @@ "use strict"; | ||
exports.tagsByName = tagsByName; | ||
for (let i in tagsById) { | ||
for (const i in tagsById) { | ||
tagsByName[tagsById[i]] = Number(i); | ||
} | ||
//# sourceMappingURL=standard.js.map |
import { IOBuffer } from 'iobuffer'; | ||
import TiffIfd from './tiffIfd'; | ||
import { BufferType, IDecodeOptions } from './types'; | ||
import { BufferType, DecodeOptions } from './types'; | ||
export default class TIFFDecoder extends IOBuffer { | ||
@@ -9,3 +9,3 @@ private _nextIFD; | ||
get pageCount(): number; | ||
decode(options?: IDecodeOptions): TiffIfd[]; | ||
decode(options?: DecodeOptions): TiffIfd[]; | ||
private decodeHeader; | ||
@@ -12,0 +12,0 @@ private decodeIFD; |
@@ -16,3 +16,2 @@ "use strict"; | ||
ignoreImageData: false, | ||
onlyFirst: false, | ||
}; | ||
@@ -52,11 +51,29 @@ class TIFFDecoder extends iobuffer_1.IOBuffer { | ||
decode(options = {}) { | ||
options = Object.assign({}, defaultOptions, options); | ||
const { pages } = options; | ||
checkPages(pages); | ||
const maxIndex = pages ? Math.max(...pages) : Infinity; | ||
options = { ...defaultOptions, ...options }; | ||
const result = []; | ||
this.decodeHeader(); | ||
let index = 0; | ||
while (this._nextIFD) { | ||
result.push(this.decodeIFD(options, true)); | ||
if (options.onlyFirst) { | ||
return [result[0]]; | ||
if (pages) { | ||
if (pages.includes(index)) { | ||
result.push(this.decodeIFD(options, true)); | ||
} | ||
else { | ||
this.decodeIFD({ ignoreImageData: true }, true); | ||
} | ||
if (index === maxIndex) { | ||
break; | ||
} | ||
} | ||
else { | ||
result.push(this.decodeIFD(options, true)); | ||
} | ||
index++; | ||
} | ||
if (index < maxIndex && maxIndex !== Infinity) { | ||
throw new RangeError(`Index ${maxIndex} is out of bounds. The stack only contains ${index} images.`); | ||
} | ||
return result; | ||
@@ -125,3 +142,3 @@ } | ||
if (tag === 0x8769 || tag === 0x8825) { | ||
let currentOffset = this.offset; | ||
const currentOffset = this.offset; | ||
let kind = 'exif'; | ||
@@ -165,3 +182,3 @@ if (tag === 0x8769) { | ||
const bitDepth = ifd.bitsPerSample; | ||
const maxValue = Math.pow(2, bitDepth) - 1; | ||
const maxValue = 2 ** bitDepth - 1; | ||
for (let i = 0; i < ifd.data.length; i++) { | ||
@@ -186,5 +203,5 @@ ifd.data[i] = maxValue - ifd.data[i]; | ||
for (let i = 0; i < stripOffsets.length; i++) { | ||
let stripData = new DataView(this.buffer, this.byteOffset + stripOffsets[i], stripByteCounts[i]); | ||
const stripData = new DataView(this.buffer, this.byteOffset + stripOffsets[i], stripByteCounts[i]); | ||
// Last strip can be smaller | ||
let length = remainingPixels > maxPixels ? maxPixels : remainingPixels; | ||
const length = remainingPixels > maxPixels ? maxPixels : remainingPixels; | ||
remainingPixels -= length; | ||
@@ -303,2 +320,11 @@ let dataToFill = stripData; | ||
} | ||
function checkPages(pages) { | ||
if (pages) { | ||
for (const page of pages) { | ||
if (page < 0 || !Number.isInteger(page)) { | ||
throw new RangeError(`Index ${page} is invalid. Must be a positive integer.`); | ||
} | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=tiffDecoder.js.map |
@@ -37,3 +37,3 @@ import Ifd from './ifd'; | ||
get sMaxSampleValue(): number; | ||
get palette(): [number, number, number][] | undefined; | ||
get palette(): Array<[number, number, number]> | undefined; | ||
} |
@@ -27,4 +27,4 @@ "use strict"; | ||
get date() { | ||
let date = new Date(); | ||
let result = dateTimeRegex.exec(this.dateTime); | ||
const date = new Date(); | ||
const result = dateTimeRegex.exec(this.dateTime); | ||
if (result === null) { | ||
@@ -103,3 +103,3 @@ throw new Error(`invalid dateTime: ${this.dateTime}`); | ||
get maxSampleValue() { | ||
return this.get('MaxSampleValue') || Math.pow(2, this.bitsPerSample) - 1; | ||
return this.get('MaxSampleValue') || 2 ** this.bitsPerSample - 1; | ||
} | ||
@@ -125,3 +125,4 @@ get xResolution() { | ||
get sampleFormat() { | ||
return this.get('SampleFormat') || 1; | ||
const data = alwaysArray(this.get('SampleFormat') || 1); | ||
return data[0]; | ||
} | ||
@@ -128,0 +129,0 @@ get sMinSampleValue() { |
/// <reference types="node" /> | ||
import { IOBuffer } from 'iobuffer'; | ||
export declare type BufferType = ArrayBufferLike | ArrayBufferView | IOBuffer | Buffer; | ||
export interface IDecodeOptions { | ||
export type BufferType = ArrayBufferLike | ArrayBufferView | IOBuffer | Buffer; | ||
export interface DecodeOptions { | ||
ignoreImageData?: boolean; | ||
onlyFirst?: boolean; | ||
/** | ||
* Specify the indices of the pages to decode in case of a multi-page TIFF. | ||
*/ | ||
pages?: number[]; | ||
} | ||
export declare type IFDKind = 'standard' | 'exif' | 'gps'; | ||
export declare type DataArray = Uint8Array | Uint16Array | Float32Array; | ||
export type IFDKind = 'standard' | 'exif' | 'gps'; | ||
export type DataArray = Uint8Array | Uint16Array | Float32Array; |
{ | ||
"name": "tiff", | ||
"version": "5.0.3", | ||
"version": "6.0.0", | ||
"description": "TIFF image decoder written entirely in JavaScript", | ||
@@ -42,16 +42,16 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"iobuffer": "^5.0.4", | ||
"pako": "^2.0.4" | ||
"iobuffer": "^5.3.2", | ||
"pako": "^2.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^27.0.2", | ||
"@types/node": "^16.11.6", | ||
"@types/pako": "^1.0.2", | ||
"eslint": "^8.1.0", | ||
"eslint-config-cheminfo-typescript": "^10.2.2", | ||
"jest": "^27.3.1", | ||
"prettier": "^2.4.1", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^27.0.7", | ||
"typescript": "^4.4.4" | ||
"@types/jest": "^29.5.11", | ||
"@types/node": "^20.10.5", | ||
"@types/pako": "^2.0.3", | ||
"eslint": "^8.56.0", | ||
"eslint-config-cheminfo-typescript": "^12.1.0", | ||
"jest": "^29.7.0", | ||
"prettier": "^3.1.1", | ||
"rimraf": "^5.0.5", | ||
"ts-jest": "^29.1.1", | ||
"typescript": "^5.3.3" | ||
}, | ||
@@ -58,0 +58,0 @@ "prettier": { |
@@ -15,5 +15,5 @@ # tiff | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][ci-image]][ci-url] | ||
[![npm download][download-image]][download-url] | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][ci-image]][ci-url] | ||
[![npm download][download-image]][download-url] | ||
@@ -82,3 +82,3 @@ </h3> | ||
[npm-url]: https://www.npmjs.com/package/tiff | ||
[ci-image]: https://github.com/image-js/tiff/workflows/Node.js%20CI/badge.svg?branch=master | ||
[ci-image]: https://github.com/image-js/tiff/workflows/Node.js%20CI/badge.svg?branch=main | ||
[ci-url]: https://github.com/image-js/tiff/actions?query=workflow%3A%22Node.js+CI%22 | ||
@@ -85,0 +85,0 @@ [codecov-image]: https://img.shields.io/codecov/c/github/image-js/tiff.svg |
@@ -46,3 +46,3 @@ import * as exif from './tags/exif'; | ||
const taglist = tags[this.kind].tagsById; | ||
for (let key of this.fields.keys()) { | ||
for (const key of this.fields.keys()) { | ||
if (taglist[key]) { | ||
@@ -49,0 +49,0 @@ this._map[taglist[key]] = this.fields.get(key); |
import TIFFDecoder from './tiffDecoder'; | ||
let types = new Map< | ||
const types = new Map< | ||
number, | ||
@@ -39,3 +39,3 @@ [number, (decoder: TIFFDecoder, count: number) => any] | ||
if (count === 1) return decoder.readUint8(); | ||
let array = new Uint8Array(count); | ||
const array = new Uint8Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -48,6 +48,6 @@ array[i] = decoder.readUint8(); | ||
function readASCII(decoder: TIFFDecoder, count: number): string | string[] { | ||
let strings = []; | ||
const strings = []; | ||
let currentString = ''; | ||
for (let i = 0; i < count; i++) { | ||
let char = String.fromCharCode(decoder.readUint8()); | ||
const char = String.fromCharCode(decoder.readUint8()); | ||
if (char === '\0') { | ||
@@ -69,3 +69,3 @@ strings.push(currentString); | ||
if (count === 1) return decoder.readUint16(); | ||
let array = new Uint16Array(count); | ||
const array = new Uint16Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -79,3 +79,3 @@ array[i] = decoder.readUint16(); | ||
if (count === 1) return decoder.readUint32(); | ||
let array = new Uint32Array(count); | ||
const array = new Uint32Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -91,3 +91,3 @@ array[i] = decoder.readUint32(); | ||
} | ||
let rationals = new Array(count); | ||
const rationals = new Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -101,3 +101,3 @@ rationals[i] = decoder.readUint32() / decoder.readUint32(); | ||
if (count === 1) return decoder.readInt8(); | ||
let array = new Int8Array(count); | ||
const array = new Int8Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -111,3 +111,3 @@ array[i] = decoder.readInt8(); | ||
if (count === 1) return decoder.readInt16(); | ||
let array = new Int16Array(count); | ||
const array = new Int16Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -121,3 +121,3 @@ array[i] = decoder.readInt16(); | ||
if (count === 1) return decoder.readInt32(); | ||
let array = new Int32Array(count); | ||
const array = new Int32Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -133,3 +133,3 @@ array[i] = decoder.readInt32(); | ||
} | ||
let rationals = new Array(count); | ||
const rationals = new Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -143,3 +143,3 @@ rationals[i] = decoder.readInt32() / decoder.readInt32(); | ||
if (count === 1) return decoder.readFloat32(); | ||
let array = new Float32Array(count); | ||
const array = new Float32Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -156,3 +156,3 @@ array[i] = decoder.readFloat32(); | ||
if (count === 1) return decoder.readFloat64(); | ||
let array = new Float64Array(count); | ||
const array = new Float64Array(count); | ||
for (let i = 0; i < count; i++) { | ||
@@ -159,0 +159,0 @@ array[i] = decoder.readFloat64(); |
import TIFFDecoder from './tiffDecoder'; | ||
import TiffIfd from './tiffIfd'; | ||
import { BufferType, IDecodeOptions } from './types'; | ||
import { BufferType, DecodeOptions } from './types'; | ||
function decodeTIFF(data: BufferType, options?: IDecodeOptions): TiffIfd[] { | ||
function decodeTIFF(data: BufferType, options?: DecodeOptions): TiffIfd[] { | ||
const decoder = new TIFFDecoder(data); | ||
@@ -20,2 +20,2 @@ return decoder.decode(options); | ||
export { decodeTIFF as decode, isMultiPage, pageCount, IDecodeOptions }; | ||
export { decodeTIFF as decode, isMultiPage, pageCount, DecodeOptions, TiffIfd }; |
@@ -74,3 +74,3 @@ const tagsById: Record<number, string> = { | ||
const tagsByName: Record<string, number> = {}; | ||
for (let i in tagsById) { | ||
for (const i in tagsById) { | ||
tagsByName[tagsById[i]] = Number(i); | ||
@@ -77,0 +77,0 @@ } |
@@ -37,3 +37,3 @@ const tagsById: Record<number, string> = { | ||
const tagsByName: Record<string, number> = {}; | ||
for (let i in tagsById) { | ||
for (const i in tagsById) { | ||
tagsByName[tagsById[i]] = Number(i); | ||
@@ -40,0 +40,0 @@ } |
@@ -179,3 +179,3 @@ const tagsById: Record<number, string> = { | ||
const tagsByName: Record<string, number> = {}; | ||
for (let i in tagsById) { | ||
for (const i in tagsById) { | ||
tagsByName[tagsById[i]] = Number(i); | ||
@@ -182,0 +182,0 @@ } |
@@ -12,11 +12,10 @@ import { IOBuffer } from 'iobuffer'; | ||
import TiffIfd from './tiffIfd'; | ||
import { BufferType, IDecodeOptions, IFDKind, DataArray } from './types'; | ||
import { BufferType, DecodeOptions, IFDKind, DataArray } from './types'; | ||
import { decompressZlib } from './zlib'; | ||
const defaultOptions: IDecodeOptions = { | ||
const defaultOptions: DecodeOptions = { | ||
ignoreImageData: false, | ||
onlyFirst: false, | ||
}; | ||
interface IInternalOptions extends IDecodeOptions { | ||
interface InternalOptions extends DecodeOptions { | ||
kind?: IFDKind; | ||
@@ -62,12 +61,32 @@ } | ||
public decode(options: IDecodeOptions = {}): TiffIfd[] { | ||
options = Object.assign({}, defaultOptions, options); | ||
public decode(options: DecodeOptions = {}): TiffIfd[] { | ||
const { pages } = options; | ||
checkPages(pages); | ||
const maxIndex = pages ? Math.max(...pages) : Infinity; | ||
options = { ...defaultOptions, ...options }; | ||
const result = []; | ||
this.decodeHeader(); | ||
let index = 0; | ||
while (this._nextIFD) { | ||
result.push(this.decodeIFD(options, true)); | ||
if (options.onlyFirst) { | ||
return [result[0]]; | ||
if (pages) { | ||
if (pages.includes(index)) { | ||
result.push(this.decodeIFD(options, true)); | ||
} else { | ||
this.decodeIFD({ ignoreImageData: true }, true); | ||
} | ||
if (index === maxIndex) { | ||
break; | ||
} | ||
} else { | ||
result.push(this.decodeIFD(options, true)); | ||
} | ||
index++; | ||
} | ||
if (index < maxIndex && maxIndex !== Infinity) { | ||
throw new RangeError( | ||
`Index ${maxIndex} is out of bounds. The stack only contains ${index} images.`, | ||
); | ||
} | ||
return result; | ||
@@ -96,5 +115,5 @@ } | ||
private decodeIFD(options: IInternalOptions, tiff: true): TiffIfd; | ||
private decodeIFD(options: IInternalOptions, tiff: false): IFD; | ||
private decodeIFD(options: IInternalOptions, tiff: boolean): TiffIfd | IFD { | ||
private decodeIFD(options: InternalOptions, tiff: true): TiffIfd; | ||
private decodeIFD(options: InternalOptions, tiff: false): IFD; | ||
private decodeIFD(options: InternalOptions, tiff: boolean): TiffIfd | IFD { | ||
this.seek(this._nextIFD); | ||
@@ -147,3 +166,3 @@ | ||
if (tag === 0x8769 || tag === 0x8825) { | ||
let currentOffset = this.offset; | ||
const currentOffset = this.offset; | ||
let kind: IFDKind = 'exif'; | ||
@@ -191,3 +210,3 @@ if (tag === 0x8769) { | ||
const bitDepth = ifd.bitsPerSample; | ||
const maxValue = Math.pow(2, bitDepth) - 1; | ||
const maxValue = 2 ** bitDepth - 1; | ||
for (let i = 0; i < ifd.data.length; i++) { | ||
@@ -216,3 +235,3 @@ ifd.data[i] = maxValue - ifd.data[i]; | ||
for (let i = 0; i < stripOffsets.length; i++) { | ||
let stripData = new DataView( | ||
const stripData = new DataView( | ||
this.buffer, | ||
@@ -224,3 +243,3 @@ this.byteOffset + stripOffsets[i], | ||
// Last strip can be smaller | ||
let length = remainingPixels > maxPixels ? maxPixels : remainingPixels; | ||
const length = remainingPixels > maxPixels ? maxPixels : remainingPixels; | ||
remainingPixels -= length; | ||
@@ -390,1 +409,12 @@ | ||
} | ||
function checkPages(pages: number[] | undefined) { | ||
if (pages) { | ||
for (const page of pages) { | ||
if (page < 0 || !Number.isInteger(page)) { | ||
throw new RangeError( | ||
`Index ${page} is invalid. Must be a positive integer.`, | ||
); | ||
} | ||
} | ||
} | ||
} |
@@ -25,4 +25,4 @@ import Ifd from './ifd'; | ||
public get date(): Date { | ||
let date = new Date(); | ||
let result = dateTimeRegex.exec(this.dateTime); | ||
const date = new Date(); | ||
const result = dateTimeRegex.exec(this.dateTime); | ||
if (result === null) { | ||
@@ -104,3 +104,3 @@ throw new Error(`invalid dateTime: ${this.dateTime}`); | ||
public get maxSampleValue(): number { | ||
return this.get('MaxSampleValue') || Math.pow(2, this.bitsPerSample) - 1; | ||
return this.get('MaxSampleValue') || 2 ** this.bitsPerSample - 1; | ||
} | ||
@@ -126,3 +126,4 @@ public get xResolution(): number { | ||
public get sampleFormat(): number { | ||
return this.get('SampleFormat') || 1; | ||
const data = alwaysArray(this.get('SampleFormat') || 1); | ||
return data[0]; | ||
} | ||
@@ -135,3 +136,3 @@ public get sMinSampleValue(): number { | ||
} | ||
public get palette(): [number, number, number][] | undefined { | ||
public get palette(): Array<[number, number, number]> | undefined { | ||
const totalColors = 2 ** this.bitsPerSample; | ||
@@ -143,3 +144,3 @@ const colorMap: number[] = this.get('ColorMap'); | ||
} | ||
const palette: [number, number, number][] = []; | ||
const palette: Array<[number, number, number]> = []; | ||
for (let i = 0; i < totalColors; i++) { | ||
@@ -146,0 +147,0 @@ palette.push([ |
@@ -5,5 +5,8 @@ import { IOBuffer } from 'iobuffer'; | ||
export interface IDecodeOptions { | ||
export interface DecodeOptions { | ||
ignoreImageData?: boolean; | ||
onlyFirst?: boolean; | ||
/** | ||
* Specify the indices of the pages to decode in case of a multi-page TIFF. | ||
*/ | ||
pages?: number[]; | ||
} | ||
@@ -10,0 +13,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
189468
3617
Updatediobuffer@^5.3.2
Updatedpako@^2.1.0