[utif] Add types

This commit is contained in:
Jan Pesa 2019-02-07 13:44:32 +01:00
parent 0b417416fe
commit bfa57b661f
4 changed files with 110 additions and 0 deletions

71
types/utif/index.d.ts vendored Normal file
View File

@ -0,0 +1,71 @@
// Type definitions for utif 2.0
// Project: https://github.com/photopea/UTIF.js
// Definitions by: Jan Pesa <https://github.com/smajl>
// Naveen Kumar Sangi <https://github.com/nkprince007>
// 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;

23
types/utif/tsconfig.json Normal file
View File

@ -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"
]
}

1
types/utif/tslint.json Normal file
View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

15
types/utif/utif-tests.ts Normal file
View File

@ -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();