diff --git a/types/pica/index.d.ts b/types/pica/index.d.ts index 687bcec350..a5434d7d26 100644 --- a/types/pica/index.d.ts +++ b/types/pica/index.d.ts @@ -1,95 +1,101 @@ // Type definitions for pica 5.1 // Project: https://github.com/nodeca/pica // Definitions by: Hamit YILMAZ +// Piotr Błażejewicz // 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; +declare namespace pica { + 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; + } - /** - * Convenience method, similar to canvas.toBlob(), but with promise interface & polyfill for old browsers. - */ - toBlob( - canvas: HTMLCanvasElement, - mimeType: string, - quality?: number - ): Promise; + 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; + } - /** - * 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 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; + } + + interface Pica { + /** + * 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 PicaStatic { + new (config?: PicaOptions): Pica; + (config?: PicaOptions): Pica; + } } -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; -} +declare const pica: pica.PicaStatic; +export as namespace pica; -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; -} +export = pica; diff --git a/types/pica/test/pica-tests.cjs.ts b/types/pica/test/pica-tests.cjs.ts new file mode 100644 index 0000000000..ad5895e041 --- /dev/null +++ b/types/pica/test/pica-tests.cjs.ts @@ -0,0 +1,45 @@ +import pica = require('pica'); + +const resizer = pica(); + +const picaOptions = { features: ['js', 'wasm', 'ww', 'cib'] }; +const resizerWithOptions: pica.Pica = pica(picaOptions); + +const image: HTMLImageElement = document.createElement('img'); +image.width = 100; +image.height = 100; + +const 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 +const resizeOptions: pica.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 +const resizeBufferSrc: number[] = [21, 31]; +const resizeBufferOptions: pica.PicaResizeBufferOptions = { + src: resizeBufferSrc, + width: 100, + height: 100, + toWidth: 50, + toHeigh: 50, +}; +resizer.resizeBuffer(resizeBufferOptions); +resizerWithOptions.resizeBuffer(resizeBufferOptions); diff --git a/types/pica/pica-tests.ts b/types/pica/test/pica-tests.global.ts similarity index 61% rename from types/pica/pica-tests.ts rename to types/pica/test/pica-tests.global.ts index 8e49a72658..ecfbf10467 100644 --- a/types/pica/pica-tests.ts +++ b/types/pica/test/pica-tests.global.ts @@ -1,13 +1,12 @@ -let resizer: Pica = new Pica(); +const resizer = pica(); +const picaOptions: pica.PicaOptions = { features: ['js', 'wasm', 'ww', 'cib'] }; +const resizerWithOptions: pica.Pica = pica(picaOptions); -let picaOptions: PicaOptions = { features: ['js', 'wasm', 'ww', 'cib'] }; -let resizerWithOptions: Pica = new Pica(picaOptions); - -let image: HTMLImageElement = document.createElement('img'); +const image: HTMLImageElement = document.createElement('img'); image.width = 100; image.height = 100; -let canvas: HTMLCanvasElement = document.createElement('canvas'); +const canvas: HTMLCanvasElement = document.createElement('canvas'); canvas.width = 100; canvas.height = 100; @@ -16,8 +15,8 @@ resizer.resize(image, canvas); resizerWithOptions.resize(image, canvas); // Resize image with options -let resizeOptions: PicaResizeOptions = { - quality: 9 +const resizeOptions: pica.PicaResizeOptions = { + quality: 9, }; resizer.resize(image, canvas, resizeOptions); resizerWithOptions.resize(image, canvas, resizeOptions); @@ -31,8 +30,8 @@ resizer.toBlob(canvas, 'image/png', 9); resizerWithOptions.toBlob(canvas, 'image/png', 9); // Resize buffer -let resizeBufferSrc: number[] = [21, 31]; -let resizeBufferOptions: PicaResizeBufferOptions = { +const resizeBufferSrc: number[] = [21, 31]; +const resizeBufferOptions: pica.PicaResizeBufferOptions = { src: resizeBufferSrc, width: 100, height: 100, diff --git a/types/pica/tsconfig.json b/types/pica/tsconfig.json index d6b2dd44dd..114b9de0ff 100644 --- a/types/pica/tsconfig.json +++ b/types/pica/tsconfig.json @@ -19,6 +19,7 @@ }, "files": [ "index.d.ts", - "pica-tests.ts" + "test/pica-tests.cjs.ts", + "test/pica-tests.global.ts" ] }