From 42fd49f0b6906f14f1188677113c1c6a1a5f24b2 Mon Sep 17 00:00:00 2001 From: Gusts Kaksis Date: Wed, 25 Nov 2015 14:36:22 +0200 Subject: [PATCH] Begin to lay out a symetric packer api for synchronous usage. --- README.md | 11 +++++++++++ examples/sync.js | 19 +++++++++++++++++++ lib/{packer.js => packer-async.js} | 16 ++++++++-------- lib/packer-sync.js | 5 +++++ lib/png-sync.js | 6 ++++++ lib/png.js | 2 +- 6 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 examples/sync.js rename lib/{packer.js => packer-async.js} (87%) create mode 100644 lib/packer-sync.js diff --git a/README.md b/README.md index fa2edd2..b1191e8 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,17 @@ var data = fs.readFileSync('in.png'); var png = PNG.sync.read(data); ``` +### PNG.sync.write(png) + +Take a PNG image and returns a buffer. The properties on the image include the meta data and `data` as per the async API above. + +``` +var data = fs.readFileSync('in.png'); +var png = PNG.sync.read(data); +var buffer = PNG.sync.write(png); +fs.writeFileSync('out.png', buffer); +``` + ### PNG.adjustGamma(src) Adjusts the gamma of a sync image. See the async adjustGamma. diff --git a/examples/sync.js b/examples/sync.js new file mode 100644 index 0000000..98b964c --- /dev/null +++ b/examples/sync.js @@ -0,0 +1,19 @@ +#!/usr/bin/env node + +var fs = require('fs'), + PNG = require('../lib/png').PNG; + + +var srcFname = process.argv[2], + dstFname = process.argv[3] || 'out.png'; + +// Read a PNG file +var data = fs.readFileSync(srcFname); +// Parse it +var png = PNG.sync.read(data, { + filterType: -1 +}); +// Pack it back into a PNG data +var buff = PNG.sync.write(png); +// Write a PNG file +fs.writeFileSync(dstFname, buff); \ No newline at end of file diff --git a/lib/packer.js b/lib/packer-async.js similarity index 87% rename from lib/packer.js rename to lib/packer-async.js index 7318d21..7f029e5 100644 --- a/lib/packer.js +++ b/lib/packer-async.js @@ -8,7 +8,7 @@ var CrcStream = require('./crc'); var constants = require('./constants'); var bitPacker = require('./bitpacker'); -var Packer = module.exports = function(options) { +var PackerAsync = module.exports = function(options) { Stream.call(this); this._options = options; @@ -30,10 +30,10 @@ var Packer = module.exports = function(options) { this.readable = true; }; -util.inherits(Packer, Stream); +util.inherits(PackerAsync, Stream); -Packer.prototype.pack = function(data, width, height, gamma) { +PackerAsync.prototype.pack = function(data, width, height, gamma) { // Signature this.emit('data', new Buffer(constants.PNG_SIGNATURE)); this.emit('data', this._packIHDR(width, height, this._options.bitDepth, this._options.colorType)); @@ -69,7 +69,7 @@ Packer.prototype.pack = function(data, width, height, gamma) { deflate.end(filteredData); }; -Packer.prototype._packChunk = function(type, data) { +PackerAsync.prototype._packChunk = function(type, data) { var len = (data ? data.length : 0); var buf = new Buffer(len + 12); @@ -85,13 +85,13 @@ Packer.prototype._packChunk = function(type, data) { return buf; }; -Packer.prototype._packGAMA = function(gamma) { +PackerAsync.prototype._packGAMA = function(gamma) { var buf = new Buffer(4); buf.writeUInt32BE(Math.floor(gamma * constants.GAMMA_DIVISION), 0); return this._packChunk(constants.TYPE_gAMA, buf); }; -Packer.prototype._packIHDR = function(width, height, bitDepth, colorType) { +PackerAsync.prototype._packIHDR = function(width, height, bitDepth, colorType) { var buf = new Buffer(13); buf.writeUInt32BE(width, 0); @@ -105,10 +105,10 @@ Packer.prototype._packIHDR = function(width, height, bitDepth, colorType) { return this._packChunk(constants.TYPE_IHDR, buf); }; -Packer.prototype._packIDAT = function(data) { +PackerAsync.prototype._packIDAT = function(data) { return this._packChunk(constants.TYPE_IDAT, data); }; -Packer.prototype._packIEND = function() { +PackerAsync.prototype._packIEND = function() { return this._packChunk(constants.TYPE_IEND, null); }; diff --git a/lib/packer-sync.js b/lib/packer-sync.js new file mode 100644 index 0000000..d627a6a --- /dev/null +++ b/lib/packer-sync.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = function(buffer, options) { + return null; +}; diff --git a/lib/png-sync.js b/lib/png-sync.js index 21405e6..c225a37 100644 --- a/lib/png-sync.js +++ b/lib/png-sync.js @@ -2,9 +2,15 @@ var parse = require('./parser-sync'); +var pack = require('./packer-sync'); exports.read = function(buffer, options) { return parse(buffer, options || {}); }; + +exports.write = function(png) { + + return pack(png); +}; diff --git a/lib/png.js b/lib/png.js index f5147aa..f1c6808 100644 --- a/lib/png.js +++ b/lib/png.js @@ -3,7 +3,7 @@ var util = require('util'); var Stream = require('stream'); var Parser = require('./parser-async'); -var Packer = require('./packer'); +var Packer = require('./packer-async'); var PNGSync = require('./png-sync');