-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba415b3
commit 8dd0714
Showing
13 changed files
with
221 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import type { IImage } from './interface' | ||
import { toUTF8String, readInt32LE, readUInt32LE } from './utils' | ||
import type { IImage } from "./interface"; | ||
import { toUTF8String, readInt32LE, readUInt32LE } from "./utils"; | ||
|
||
export const BMP: IImage = { | ||
validate: (input) => toUTF8String(input, 0, 2) === 'BM', | ||
validate: (input) => toUTF8String(input, 0, 2) === "BM", | ||
|
||
calculate: (input) => ({ | ||
height: Math.abs(readInt32LE(input, 22)), | ||
width: readUInt32LE(input, 18), | ||
}), | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
import type { IImage } from './interface' | ||
import { ICO } from './ico' | ||
import { readUInt16LE } from './utils' | ||
import type { IImage } from "./interface"; | ||
import { ICO } from "./ico"; | ||
import { readUInt16LE } from "./utils"; | ||
|
||
const TYPE_CURSOR = 2 | ||
const TYPE_CURSOR = 2; | ||
export const CUR: IImage = { | ||
validate(input) { | ||
const reserved = readUInt16LE(input, 0) | ||
const imageCount = readUInt16LE(input, 4) | ||
if (reserved !== 0 || imageCount === 0) { return false } | ||
const reserved = readUInt16LE(input, 0); | ||
const imageCount = readUInt16LE(input, 4); | ||
if (reserved !== 0 || imageCount === 0) { | ||
return false; | ||
} | ||
|
||
const imageType = readUInt16LE(input, 2) | ||
return imageType === TYPE_CURSOR | ||
const imageType = readUInt16LE(input, 2); | ||
return imageType === TYPE_CURSOR; | ||
}, | ||
|
||
calculate: (input) => ICO.calculate(input), | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import type { IImage } from './interface' | ||
import { toUTF8String, readUInt16LE } from './utils' | ||
import type { IImage } from "./interface"; | ||
import { toUTF8String, readUInt16LE } from "./utils"; | ||
|
||
const gifRegexp = /^GIF8[79]a/ | ||
const gifRegexp = /^GIF8[79]a/; | ||
export const GIF: IImage = { | ||
validate: (input) => gifRegexp.test(toUTF8String(input, 0, 6)), | ||
|
||
calculate: (input) => ({ | ||
height: readUInt16LE(input, 8), | ||
width: readUInt16LE(input, 6), | ||
}), | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,70 @@ | ||
import type { IImage, ISize } from './interface' | ||
import { toHexString, toUTF8String, readUInt32BE, readUInt16BE } from './utils' | ||
import type { IImage, ISize } from "./interface"; | ||
import { toHexString, toUTF8String, readUInt32BE, readUInt16BE } from "./utils"; | ||
|
||
const BoxTypes = { | ||
ftyp: '66747970', | ||
ihdr: '69686472', | ||
jp2h: '6a703268', | ||
jp__: '6a502020', | ||
rreq: '72726571', | ||
xml_: '786d6c20', | ||
} | ||
ftyp: "66747970", | ||
ihdr: "69686472", | ||
jp2h: "6a703268", | ||
jp__: "6a502020", | ||
rreq: "72726571", | ||
xml_: "786d6c20", | ||
}; | ||
|
||
const calculateRREQLength = (box: Uint8Array): number => { | ||
const unit = box[0] | ||
let offset = 1 + 2 * unit | ||
const numStdFlags = readUInt16BE(box, offset) | ||
const flagsLength = numStdFlags * (2 + unit) | ||
offset = offset + 2 + flagsLength | ||
const numVendorFeatures = readUInt16BE(box, offset) | ||
const featuresLength = numVendorFeatures * (16 + unit) | ||
return offset + 2 + featuresLength | ||
} | ||
const unit = box[0]; | ||
let offset = 1 + 2 * unit; | ||
const numStdFlags = readUInt16BE(box, offset); | ||
const flagsLength = numStdFlags * (2 + unit); | ||
offset = offset + 2 + flagsLength; | ||
const numVendorFeatures = readUInt16BE(box, offset); | ||
const featuresLength = numVendorFeatures * (16 + unit); | ||
return offset + 2 + featuresLength; | ||
}; | ||
|
||
const parseIHDR = (box: Uint8Array): ISize => { | ||
return { | ||
height: readUInt32BE(box, 4), | ||
width: readUInt32BE(box, 8), | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
export const JP2: IImage = { | ||
validate(input) { | ||
const signature = toHexString(input, 4, 8) | ||
const signatureLength = readUInt32BE(input, 0) | ||
const signature = toHexString(input, 4, 8); | ||
const signatureLength = readUInt32BE(input, 0); | ||
if (signature !== BoxTypes.jp__ || signatureLength < 1) { | ||
return false | ||
return false; | ||
} | ||
|
||
const ftypeBoxStart = signatureLength + 4 | ||
const ftypBoxLength = readUInt32BE(input, signatureLength) | ||
const ftypBox = input.slice(ftypeBoxStart, ftypeBoxStart + ftypBoxLength) | ||
return toHexString(ftypBox, 0, 4) === BoxTypes.ftyp | ||
const ftypeBoxStart = signatureLength + 4; | ||
const ftypBoxLength = readUInt32BE(input, signatureLength); | ||
const ftypBox = input.slice(ftypeBoxStart, ftypeBoxStart + ftypBoxLength); | ||
return toHexString(ftypBox, 0, 4) === BoxTypes.ftyp; | ||
}, | ||
|
||
calculate(input) { | ||
const signatureLength = readUInt32BE(input, 0) | ||
const ftypBoxLength = readUInt16BE(input, signatureLength + 2) | ||
let offset = signatureLength + 4 + ftypBoxLength | ||
const nextBoxType = toHexString(input, offset, offset + 4) | ||
const signatureLength = readUInt32BE(input, 0); | ||
const ftypBoxLength = readUInt16BE(input, signatureLength + 2); | ||
let offset = signatureLength + 4 + ftypBoxLength; | ||
const nextBoxType = toHexString(input, offset, offset + 4); | ||
switch (nextBoxType) { | ||
case BoxTypes.rreq: { | ||
// WHAT ARE THESE 4 BYTES????? | ||
// eslint-disable-next-line no-case-declarations | ||
const MAGIC = 4 | ||
offset = | ||
offset + 4 + MAGIC + calculateRREQLength(input.slice(offset + 4)) | ||
return parseIHDR(input.slice(offset + 8, offset + 24)) | ||
} | ||
case BoxTypes.jp2h: { | ||
return parseIHDR(input.slice(offset + 8, offset + 24)) | ||
} | ||
default: { | ||
throw new TypeError( | ||
'Unsupported header found: ' + toUTF8String(input, offset, offset + 4) | ||
) | ||
} | ||
case BoxTypes.rreq: { | ||
// WHAT ARE THESE 4 BYTES????? | ||
// eslint-disable-next-line no-case-declarations | ||
const MAGIC = 4; | ||
offset = | ||
offset + 4 + MAGIC + calculateRREQLength(input.slice(offset + 4)); | ||
return parseIHDR(input.slice(offset + 8, offset + 24)); | ||
} | ||
case BoxTypes.jp2h: { | ||
return parseIHDR(input.slice(offset + 8, offset + 24)); | ||
} | ||
default: { | ||
throw new TypeError( | ||
"Unsupported header found: " + | ||
toUTF8String(input, offset, offset + 4), | ||
); | ||
} | ||
} | ||
}, | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.