Skip to content

Commit

Permalink
chore: include src/types
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 18, 2023
1 parent d74a2aa commit ba415b3
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
node_modules
coverage
dist
types
.vscode
.DS_Store
.eslintcache
Expand Down
40 changes: 40 additions & 0 deletions src/types/index.ts
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;
15 changes: 15 additions & 0 deletions src/types/tga.ts
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),
}
},
}
15 changes: 15 additions & 0 deletions src/types/tiff.ts
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 }),
};
64 changes: 64 additions & 0 deletions src/types/utils.ts
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)
}

0 comments on commit ba415b3

Please sign in to comment.