From a44ea7e379190872c5d039a2026627a7946d98fd Mon Sep 17 00:00:00 2001 From: Hamit YILMAZ Date: Fri, 27 Dec 2019 18:28:45 +0300 Subject: [PATCH] Add pica library type definition files (#41210) * Add pica library type definition files * Fix pica library type definition files array and maximum line exceeds * Add pica library tests with parameterless constructor * Fix pica library pica resize buffer options src optional problem. - Add pica tests resizeBuffer with src options. --- types/pica/index.d.ts | 95 ++++++++++++++++++++++++++++++++++++++++ types/pica/pica-tests.ts | 43 ++++++++++++++++++ types/pica/tsconfig.json | 24 ++++++++++ types/pica/tslint.json | 1 + 4 files changed, 163 insertions(+) create mode 100644 types/pica/index.d.ts create mode 100644 types/pica/pica-tests.ts create mode 100644 types/pica/tsconfig.json create mode 100644 types/pica/tslint.json diff --git a/types/pica/index.d.ts b/types/pica/index.d.ts new file mode 100644 index 0000000000..687bcec350 --- /dev/null +++ b/types/pica/index.d.ts @@ -0,0 +1,95 @@ +// Type definitions for pica 5.1 +// Project: https://github.com/nodeca/pica +// Definitions by: Hamit YILMAZ +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare class Pica { + constructor(config?: PicaOptions); + /** + * Resize image from one canvas (or image) to another. + * Sizes are taken from source and destination objects. + * Result is Promise, resolved with to on success. + * (!) If you need to process multiple images, do it sequentially to optimize CPU & memory use. + * Pica already knows how to use multiple cores (if browser allows). + */ + resize( + from: HTMLCanvasElement | HTMLImageElement | File | Blob, + to: HTMLCanvasElement, + options?: PicaResizeOptions + ): Promise; + + /** + * Convenience method, similar to canvas.toBlob(), but with promise interface & polyfill for old browsers. + */ + toBlob( + canvas: HTMLCanvasElement, + mimeType: string, + quality?: number + ): Promise; + + /** + * Supplementary method, not recommended for direct use. + * Resize Uint8Array with raw RGBA bitmap (don't confuse with jpeg / png / ... binaries). + * It does not use tiles & webworkers. Left for special cases when you really need to process raw binary data (for example, if you decode jpeg files "manually"). + */ + resizeBuffer( + options: PicaResizeBufferOptions + ): Promise; +} + +interface PicaOptions { + // tile width/height. + // Images are processed by regions, to restrict peak memory use. Default 1024. + tile?: number; + // list of features to use. + // Default is [ 'js', 'wasm', 'ww' ]. Can be [ 'js', 'wasm', 'cib', 'ww' ] or [ 'all' ]. + // Note, resize via createImageBitmap() ('cib') disabled by default due problems with quality. + features?: string[]; + // cache timeout, ms. Webworkers create is not fast. + // This option allow reuse webworkers effectively. Default 2000. + idle?: number; + // max webworkers pool size. Default is autodetected CPU count, but not more than 4. + concurrency?: number; +} + +interface PicaResizeOptions { + // 0..3. Default = 3 (lanczos, win=3). + quality?: number; + // use alpha channel. Default = false. + alpha?: boolean; + // >=0, in percents. Default = 0 (off). Usually between 50 to 100 is good. + unsharpAmount?: number; + // 0.5..2.0. By default it's not set. Radius of Gaussian blur. + // If it is less than 0.5, Unsharp Mask is off. Big values are clamped to 2.0. + unsharpRadius?: number; + // 0..255. Default = 0. Threshold for applying unsharp mask. + unsharpThreshold?: number; + // Promise instance. If defined, current operation will be terminated on rejection. + cancelToken?: string; +} + +interface PicaResizeBufferOptions { + // Uint8Array with source data. + src: number[]; + // src image width. + width: number; + // src image height. + height: number; + // output width, >=0, in pixels. + toWidth: number; + // output height, >=0, in pixels. + toHeigh: number; + // 0..3. Default = 3 (lanczos, win=3). + quality?: number; + // use alpha channel. Default = false. + alpha?: boolean; + // >=0, in percents. Default = 0 (off). Usually between 50 to 100 is good. + unsharpAmount?: number; + // 0.5..2.0. Radius of Gaussian blur. + // If it is less than 0.5, Unsharp Mask is off. Big values are clamped to 2.0. + unsharpRadius?: number; + // 0..255. Default = 0. Threshold for applying unsharp mask. + unsharpThreshold?: number; + // Optional. Output buffer to write data, if you don't wish pica to create new one. + dest?: string; +} diff --git a/types/pica/pica-tests.ts b/types/pica/pica-tests.ts new file mode 100644 index 0000000000..8e49a72658 --- /dev/null +++ b/types/pica/pica-tests.ts @@ -0,0 +1,43 @@ +let resizer: Pica = new Pica(); + +let picaOptions: PicaOptions = { features: ['js', 'wasm', 'ww', 'cib'] }; +let resizerWithOptions: Pica = new Pica(picaOptions); + +let image: HTMLImageElement = document.createElement('img'); +image.width = 100; +image.height = 100; + +let canvas: HTMLCanvasElement = document.createElement('canvas'); +canvas.width = 100; +canvas.height = 100; + +// Resize image +resizer.resize(image, canvas); +resizerWithOptions.resize(image, canvas); + +// Resize image with options +let resizeOptions: PicaResizeOptions = { + quality: 9 +}; +resizer.resize(image, canvas, resizeOptions); +resizerWithOptions.resize(image, canvas, resizeOptions); + +// Blob canvas +resizer.toBlob(canvas, 'image/png'); +resizerWithOptions.toBlob(canvas, 'image/png'); + +// Blob canvas with quality +resizer.toBlob(canvas, 'image/png', 9); +resizerWithOptions.toBlob(canvas, 'image/png', 9); + +// Resize buffer +let resizeBufferSrc: number[] = [21, 31]; +let resizeBufferOptions: PicaResizeBufferOptions = { + src: resizeBufferSrc, + width: 100, + height: 100, + toWidth: 50, + toHeigh: 50, +}; +resizer.resizeBuffer(resizeBufferOptions); +resizerWithOptions.resizeBuffer(resizeBufferOptions); diff --git a/types/pica/tsconfig.json b/types/pica/tsconfig.json new file mode 100644 index 0000000000..d6b2dd44dd --- /dev/null +++ b/types/pica/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "pica-tests.ts" + ] +} diff --git a/types/pica/tslint.json b/types/pica/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/pica/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }