Begin to lay out a symetric packer api for synchronous usage.

This commit is contained in:
Gusts Kaksis 2015-11-25 14:36:22 +02:00
parent a04ccc330a
commit 42fd49f0b6
6 changed files with 50 additions and 9 deletions

View file

@ -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.

19
examples/sync.js Normal file
View file

@ -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);

View file

@ -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);
};

5
lib/packer-sync.js Normal file
View file

@ -0,0 +1,5 @@
'use strict';
module.exports = function(buffer, options) {
return null;
};

View file

@ -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);
};

View file

@ -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');