Merge pull request #6207 from elisee/png2js

pngjs2 definition and tests
This commit is contained in:
Horiuchi_H
2015-10-09 16:29:21 +09:00
2 changed files with 90 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
/// <reference path="pngjs2.d.ts" />
import * as fs from "fs";
import { PNG } from "pngjs2";
let png = new PNG({ filterType: 4 });
fs.createReadStream("in.png")
.pipe(png)
.on("parsed", () => {
for (let y = 0; y < png.height; y++) {
for (let x = 0; x < png.width; x++) {
let idx = (png.width * y + x) << 2;
// invert color
png.data[idx] = 255 - png.data[idx];
png.data[idx+1] = 255 - png.data[idx+1];
png.data[idx+2] = 255 - png.data[idx+2];
// and reduce opacity
png.data[idx+3] = png.data[idx+3] >> 1;
}
}
png.pack().pipe(fs.createWriteStream('out.png'));
});
+64
View File
@@ -0,0 +1,64 @@
// Type definitions for pngjs2 2.0.0
// Project: https://www.npmjs.com/package/pngjs2
// Definitions by: Elisée Maurer <https://sparklinlabs.com/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../node/node.d.ts" />
declare module "pngjs2" {
import fs = require("fs");
import events = require("events");
import stream = require("stream");
interface PNGOptions {
width?: number;
height?: number;
checkCRC?: boolean;
deflateChunkSize?: number;
deflateLevel?: number;
deflateStrategy?: number;
deflateFactory?: any;
filterType?: number|number[];
colorType?: number;
inputHasAlpha?: boolean;
}
interface PNGMetadata {
width: number;
height: number;
palette: boolean;
color: boolean;
alpha: boolean;
interlace: boolean;
}
export class PNG extends stream.Writable {
constructor(options?: PNGOptions);
width: number;
height: number;
data: Buffer;
gamma: number;
on(event: string, callback: Function): PNG;
on(event: "metadata", callback: (metadata: PNGMetadata) => void): PNG;
on(event: "parsed", callback: (data: Buffer) => void): PNG;
on(event: "error", callback: (err: Error) => void): PNG;
parse(data: string|Buffer, callback?: (err: Error, data: Buffer) => void): PNG;
pack(): PNG;
pipe(destination: fs.WriteStream): PNG;
static bitblt(src: PNG, dst: PNG, srcX: number, srcY: number,
width: number, height: number, deltaX: number, deltaY: number): void;
bitblt(dst: PNG, srcX: number, srcY: number,
width: number, height: number, deltaX: number, deltaY: number): PNG;
}
export namespace PNG {
namespace sync {
function read(buffer: string|Buffer, options?: PNGOptions): PNG;
}
}
}