-
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
Showing
5 changed files
with
134 additions
and
1 deletion.
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,7 +1,6 @@ | ||
node_modules | ||
coverage | ||
dist | ||
types | ||
.vscode | ||
.DS_Store | ||
.eslintcache | ||
|
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// load all available handlers explicitely for browserify support | ||
import { BMP } from "./bmp"; | ||
import { CUR } from "./cur"; | ||
import { DDS } from "./dds"; | ||
import { GIF } from "./gif"; | ||
import { ICNS } from "./icns"; | ||
import { ICO } from "./ico"; | ||
import { J2C } from "./j2c"; | ||
import { JP2 } from "./jp2"; | ||
import { JPG } from "./jpg"; | ||
import { KTX } from "./ktx"; | ||
import { PNG } from "./png"; | ||
import { PNM } from "./pnm"; | ||
import { PSD } from "./psd"; | ||
import { SVG } from "./svg"; | ||
import { TGA } from "./tga"; | ||
import { TIFF } from "./tiff"; | ||
import { WEBP } from "./webp"; | ||
|
||
export const typeHandlers = { | ||
bmp: BMP, | ||
cur: CUR, | ||
dds: DDS, | ||
gif: GIF, | ||
icns: ICNS, | ||
ico: ICO, | ||
j2c: J2C, | ||
jp2: JP2, | ||
jpg: JPG, | ||
ktx: KTX, | ||
png: PNG, | ||
pnm: PNM, | ||
psd: PSD, | ||
svg: SVG, | ||
tga: TGA, | ||
tiff: TIFF, | ||
webp: WEBP, | ||
}; | ||
|
||
export type ImageType = keyof typeof typeHandlers; |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { IImage } from './interface' | ||
import { readUInt16LE } from './utils' | ||
|
||
export const TGA: IImage = { | ||
validate(input) { | ||
return readUInt16LE(input, 0) === 0 && readUInt16LE(input, 4) === 0 | ||
}, | ||
|
||
calculate(input) { | ||
return { | ||
height: readUInt16LE(input, 14), | ||
width: readUInt16LE(input, 12), | ||
} | ||
}, | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// based on http://www.compix.com/fileformattif.htm | ||
import type { IImage } from "./interface"; | ||
import { toHexString } from "./utils"; | ||
|
||
const signatures = new Set([ | ||
"492049", // ? | ||
"49492a00", // Little endian | ||
"4d4d002a", // Big Endian | ||
"4d4d002a", // BigTIFF > 4GB. currently not supported | ||
]); | ||
|
||
export const TIFF: IImage = { | ||
validate: (input) => signatures.has(toHexString(input, 0, 4)), | ||
calculate: () => ({ width: undefined, height: undefined }), | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const decoder = new TextDecoder() | ||
export const toUTF8String = ( | ||
input: Uint8Array, | ||
start = 0, | ||
end = input.length | ||
) => decoder.decode(input.slice(start, end)) | ||
|
||
export const toHexString = (input: Uint8Array, start = 0, end = input.length) => | ||
input | ||
.slice(start, end) | ||
.reduce((memo, i) => memo + ('0' + i.toString(16)).slice(-2), '') | ||
|
||
export const readInt16LE = (input: Uint8Array, offset = 0) => { | ||
const val = input[offset] + input[offset + 1] * 2 ** 8 | ||
return val | ((val & (2 ** 15)) * 0x1_ff_fe) | ||
} | ||
|
||
export const readUInt16BE = (input: Uint8Array, offset = 0) => | ||
input[offset] * 2 ** 8 + input[offset + 1] | ||
|
||
export const readUInt16LE = (input: Uint8Array, offset = 0) => | ||
input[offset] + input[offset + 1] * 2 ** 8 | ||
|
||
export const readUInt24LE = (input: Uint8Array, offset = 0) => | ||
input[offset] + input[offset + 1] * 2 ** 8 + input[offset + 2] * 2 ** 16 | ||
|
||
export const readInt32LE = (input: Uint8Array, offset = 0) => | ||
input[offset] + | ||
input[offset + 1] * 2 ** 8 + | ||
input[offset + 2] * 2 ** 16 + | ||
(input[offset + 3] << 24) | ||
|
||
export const readUInt32BE = (input: Uint8Array, offset = 0) => | ||
input[offset] * 2 ** 24 + | ||
input[offset + 1] * 2 ** 16 + | ||
input[offset + 2] * 2 ** 8 + | ||
input[offset + 3] | ||
|
||
export const readUInt32LE = (input: Uint8Array, offset = 0) => | ||
input[offset] + | ||
input[offset + 1] * 2 ** 8 + | ||
input[offset + 2] * 2 ** 16 + | ||
input[offset + 3] * 2 ** 24 | ||
|
||
// Abstract reading multi-byte unsigned integers | ||
const methods = { | ||
readUInt16BE, | ||
readUInt16LE, | ||
readUInt32BE, | ||
readUInt32LE, | ||
} as const | ||
|
||
type MethodName = keyof typeof methods | ||
export function readUInt( | ||
input: Uint8Array, | ||
bits: 16 | 32, | ||
offset: number, | ||
isBigEndian: boolean | ||
): number { | ||
offset = offset || 0 | ||
const endian = isBigEndian ? 'BE' : 'LE' | ||
const methodName: MethodName = ('readUInt' + bits + endian) as MethodName | ||
return methods[methodName](input, offset) | ||
} |