From f93f841a4f9290a5a1bc1dd408294a7068e0fb27 Mon Sep 17 00:00:00 2001 From: Jason Cheatham Date: Thu, 7 Sep 2017 22:37:52 -0400 Subject: [PATCH 1/3] Add types for pngjs 3.3 --- types/pngjs/index.d.ts | 100 +++++++++++++++++++++++++++++++++++++ types/pngjs/pngjs-tests.ts | 64 ++++++++++++++++++++++++ types/pngjs/tsconfig.json | 22 ++++++++ types/pngjs/tslint.json | 1 + 4 files changed, 187 insertions(+) create mode 100644 types/pngjs/index.d.ts create mode 100644 types/pngjs/pngjs-tests.ts create mode 100644 types/pngjs/tsconfig.json create mode 100644 types/pngjs/tslint.json diff --git a/types/pngjs/index.d.ts b/types/pngjs/index.d.ts new file mode 100644 index 0000000000..ece3434de1 --- /dev/null +++ b/types/pngjs/index.d.ts @@ -0,0 +1,100 @@ +// Type definitions for pngjs 3.3 +// Project: https://github.com/lukeapage/pngjs +// Definitions by: Jason Cheatham +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import { Duplex } from 'stream'; +import { createDeflate } from 'zlib'; + +export class PNG extends Duplex { + static adjustGamma(src: PNG): void; + + static bitblt( + src: PNG, + dst: PNG, + srcX?: number, + srcY?: number, + width?: number, + height?: number, + deltaX?: number, + deltaY?: number + ): void; + + static sync: { + read(buffer: Buffer, options?: ParserOptions): PNG; + write(buffer: Buffer, options?: PackerOptions): PNG; + }; + + constructor(options?: PNGOptions); + + data: Buffer; + gamma: number; + height: number; + width: number; + + adjustGamma(): void; + + bitblt( + dst: PNG, + srcX?: number, + srcY?: number, + width?: number, + height?: number, + deltaX?: number, + deltaY?: number + ): PNG; + + on(event: 'metadata', callback: (metadata: Metadata) => void): this; + on(event: 'parsed', callback: (data: Buffer) => void): this; + on(event: 'error', callback: (error: Error) => void): this; + on(event: string, callback: (...args: any[]) => void): this; + + pack(): PNG; + + parse(data: string | Buffer, callback?: (error: Error, data: PNG) => void): PNG; +} + +export interface BaseOptions { + width?: number; + height?: number; + fill?: boolean; +} + +export interface ParserOptions { + checkCRC?: boolean; +} + +export interface PackerOptions { + deflateChunkSize?: number; + deflateLevel?: number; + deflateStrategy?: number; + deflateFactory?: typeof createDeflate; + colorType?: ColorType; + bitDepth?: 8 | 16; + bgColor?: { + red: number; + green: number; + blue: number; + }; + inputHasAlpha?: boolean; + inputColorType?: ColorType; + filterType?: number | number[]; +} + +export type PNGOptions = BaseOptions & ParserOptions & PackerOptions; + +export type ColorType = 0 | 1 | 2 | 4; + +export interface Metadata { + width: number; + height: number; + depth: 1 | 2 | 4 | 8 | 16; + interlace: boolean; + palette: boolean; + color: boolean; + alpha: boolean; + bpp: 1 | 2 | 3 | 4; + colorType: ColorType; +} diff --git a/types/pngjs/pngjs-tests.ts b/types/pngjs/pngjs-tests.ts new file mode 100644 index 0000000000..e8afb1e27a --- /dev/null +++ b/types/pngjs/pngjs-tests.ts @@ -0,0 +1,64 @@ +import { PNG } from 'pngjs'; +import { createDeflate } from 'zlib'; + +const pngs = [ + new PNG(), + new PNG({}), + new PNG({ width: 1 }), + new PNG({ checkCRC: false }), + new PNG({ deflateChunkSize: 3 }), + new PNG({ + width: 1, + height: 1, + fill: false, + checkCRC: true, + deflateChunkSize: 1, + deflateLevel: 1, + deflateStrategy: 1, + deflateFactory: createDeflate, + colorType: 4, + bitDepth: 8, + inputHasAlpha: false, + filterType: 4 + }), + new PNG({ filterType: [1, 2, 3] }) +]; + +const png = pngs[0]; + +png.readable === true; +png.writable === true; +png.width === 1; +png.height === 1; +png.gamma === 1; +png.adjustGamma(); + +png.bitblt(pngs[1]); +png.bitblt(pngs[1], 1); +png.bitblt(pngs[1], 1, 1); +png.bitblt(pngs[1], 1, 1, 1, 1, 1, 1); + +png.on('metadata', metadata => { + metadata.bpp === 1; +}); +png.on('parsed', data => { + data.byteLength === 1; +}); +png.on('error', error => { + error === new Error('testing'); +}); +png.on('foo', () => {}); + +png.pack().adjustGamma(); + +png.parse('foo').adjustGamma(); +png.parse(Buffer.from('foo')).adjustGamma(); +png.parse('foo', (error, data) => { + error.stack; + data.adjustGamma(); +}).adjustGamma(); + +PNG.adjustGamma(png); + +PNG.bitblt(png, pngs[1]); +PNG.bitblt(png, pngs[1], 1, 1, 1, 1, 1, 1); diff --git a/types/pngjs/tsconfig.json b/types/pngjs/tsconfig.json new file mode 100644 index 0000000000..bdce83e9a2 --- /dev/null +++ b/types/pngjs/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "pngjs-tests.ts" + ] +} diff --git a/types/pngjs/tslint.json b/types/pngjs/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/pngjs/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 8c734c2da521aada690c4c54f956d93b97b900fc Mon Sep 17 00:00:00 2001 From: Jason Cheatham Date: Thu, 7 Sep 2017 23:40:08 -0400 Subject: [PATCH 2/3] Fix pngjs test lint failure --- types/pngjs/pngjs-tests.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/types/pngjs/pngjs-tests.ts b/types/pngjs/pngjs-tests.ts index e8afb1e27a..c85bbd5d3e 100644 --- a/types/pngjs/pngjs-tests.ts +++ b/types/pngjs/pngjs-tests.ts @@ -26,8 +26,12 @@ const pngs = [ const png = pngs[0]; -png.readable === true; -png.writable === true; +if (png.readable === true) { + console.log('readable'); +} +if (png.writable === true) { + console.log('writable'); +} png.width === 1; png.height === 1; png.gamma === 1; From aa4ee82744b39b6f61e8ba672fe762fa2d48264c Mon Sep 17 00:00:00 2001 From: Jason Cheatham Date: Thu, 7 Sep 2017 23:57:58 -0400 Subject: [PATCH 3/3] Fix pngjs test lint failure (again) --- types/pngjs/pngjs-tests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/pngjs/pngjs-tests.ts b/types/pngjs/pngjs-tests.ts index c85bbd5d3e..28b1efd733 100644 --- a/types/pngjs/pngjs-tests.ts +++ b/types/pngjs/pngjs-tests.ts @@ -26,10 +26,10 @@ const pngs = [ const png = pngs[0]; -if (png.readable === true) { +if (png.readable) { console.log('readable'); } -if (png.writable === true) { +if (png.writable) { console.log('writable'); } png.width === 1;