From bfa57b661f5da696100751cb19cdac599213ad42 Mon Sep 17 00:00:00 2001 From: Jan Pesa Date: Thu, 7 Feb 2019 13:44:32 +0100 Subject: [PATCH] [utif] Add types --- types/utif/index.d.ts | 71 ++++++++++++++++++++++++++++++++++++++++ types/utif/tsconfig.json | 23 +++++++++++++ types/utif/tslint.json | 1 + types/utif/utif-tests.ts | 15 +++++++++ 4 files changed, 110 insertions(+) create mode 100644 types/utif/index.d.ts create mode 100644 types/utif/tsconfig.json create mode 100644 types/utif/tslint.json create mode 100644 types/utif/utif-tests.ts diff --git a/types/utif/index.d.ts b/types/utif/index.d.ts new file mode 100644 index 0000000000..569dfd949c --- /dev/null +++ b/types/utif/index.d.ts @@ -0,0 +1,71 @@ +// Type definitions for utif 2.0 +// Project: https://github.com/photopea/UTIF.js +// Definitions by: Jan Pesa +// Naveen Kumar Sangi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import 'node'; + +export as namespace UTIF; + +export type TiffTag = string[] | number[]; + +/** + * Each IFD is an object, keys are "tXYZ" (XYZ is a TIFF tag number), values are values of these tags. + * You can get the the dimension (and other properties, "metadata") of the image without decompressing pixel data. + * For more information on what each tag means, refer https://github.com/photopea/UTIF.js/blob/master/UTIF.js#L742 or TIFF 6 specification. + */ +// tslint:disable-next-line:interface-name +export interface IFD { + [property: string]: TiffTag | number | Uint8Array; + data: Uint8Array; + width: number; + height: number; +} + +/** + * Returns an array of "IFDs" (image file directories). + * + * @param buffer A Buffer or ArrayBuffer containing TIFF or EXIF data. + */ +export function decode(buffer: Buffer | ArrayBuffer): IFD[]; + +/** + * Loops through each IFD. If there is an image inside it, it is decoded and three new properties are added to the IFD: width, height and data. + * Note: TIFF files may have various number of channels and various color depth. The interpretation of data depends on many tags (see the TIFF 6 specification). + * + * @param buffer A Buffer or ArrayBuffer containing TIFF or EXIF data + * @param ifds An array of image file directories parsed via UTIF.decode() + */ +export function decodeImages(buffer: Buffer | ArrayBuffer, ifds: IFD[]): void; + +/** + * Returns Uint8Array of the image in RGBA format, 8 bits per channel (ready to use in context2d.putImageData() etc.) + * + * @param ifd An image file directory + */ +export function toRGBA8(ifd: IFD): Uint8Array; + +/** + * Returns an ArrayBuffer of the binary TIFF file. + * Note: No compression available right now. + * + * @param rgba A Uint8Array containing RGBA pixel data. + * @param w Width of the image. + * @param h Height of the image. + * @param metadata [optional] The image file directory which should be encoded. + */ +export function encodeImage(rgba: Uint8Array, w: number, h: number, metadata?: IFD): ArrayBuffer; + +/** + * Returns ArrayBuffer of binary data which can be used to encode EXIF data. + * + * @param ifds The array of IFDs (image file directories) to be encoded. + */ +export function encode(ifds: IFD[]): ArrayBuffer; + +/** + * Replaces all Image elements in the document with Canvas elements. + * The attributes "id", "class" and "style" will be copied from the original Image to the new Canvas. + */ +export function replaceIMG(): void; diff --git a/types/utif/tsconfig.json b/types/utif/tsconfig.json new file mode 100644 index 0000000000..cd4c8aa8fe --- /dev/null +++ b/types/utif/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "utif-tests.ts" + ] +} diff --git a/types/utif/tslint.json b/types/utif/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/utif/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/utif/utif-tests.ts b/types/utif/utif-tests.ts new file mode 100644 index 0000000000..66455e2f4a --- /dev/null +++ b/types/utif/utif-tests.ts @@ -0,0 +1,15 @@ +import * as UTIF from "utif"; + +// $ExpectType IFD[] +const IFDs = UTIF.decode(new ArrayBuffer(64)); +// $ExpectType Uint8Array +const rgba = UTIF.toRGBA8(IFDs[0]); + +// $ExpectType ArrayBuffer +UTIF.encodeImage(rgba, 8, 8); +// $ExpectType ArrayBuffer +UTIF.encode(IFDs); +// $ExpectType void +UTIF.decodeImages(new ArrayBuffer(64), IFDs); +// $ExpectType void +UTIF.replaceIMG();