move color type constants to constants. Revert change requiring default options on null.

This commit is contained in:
Luke Page 2015-09-13 18:40:53 +01:00
parent 780a796de3
commit 159221c115
5 changed files with 43 additions and 36 deletions

View file

@ -12,9 +12,23 @@ module.exports = {
TYPE_tRNS: 0x74524e53, // eslint-disable-line camelcase TYPE_tRNS: 0x74524e53, // eslint-disable-line camelcase
TYPE_gAMA: 0x67414d41, // eslint-disable-line camelcase TYPE_gAMA: 0x67414d41, // eslint-disable-line camelcase
COLOR_PALETTE: 1, // color-type bits
COLOR_COLOR: 2, COLORTYPE_GRAYSCALE: 0,
COLOR_ALPHA: 4, COLORTYPE_PALETTE: 1,
COLORTYPE_COLOR: 2,
COLORTYPE_ALPHA: 4, // e.g. grayscale and alpha
// color-type combinations
COLORTYPE_PALETTE_COLOR: 3,
COLORTYPE_COLOR_ALPHA: 6,
COLORTYPE_TO_BPP_MAP: {
0: 1,
2: 3,
3: 1,
4: 2,
6: 4
},
GAMMA_DIVISION: 100000 GAMMA_DIVISION: 100000
}; };

View file

@ -1,6 +1,5 @@
'use strict'; 'use strict';
var util = require('util'); var util = require('util');
var Stream = require('stream'); var Stream = require('stream');
var zlib = require('zlib'); var zlib = require('zlib');
@ -8,20 +7,24 @@ var filter = require('./filter-pack');
var CrcStream = require('./crc'); var CrcStream = require('./crc');
var constants = require('./constants'); var constants = require('./constants');
var COLORTYPE_TRUECOLOR = 2;
var COLORTYPE_TRUECOLOR_ALPHA = 6;
var Packer = module.exports = function(options) { var Packer = module.exports = function(options) {
Stream.call(this); Stream.call(this);
this._options = options; this._options = options;
options.deflateChunkSize = options.deflateChunkSize || 32 * 1024; options.deflateChunkSize = options.deflateChunkSize || 32 * 1024;
options.deflateLevel = options.deflateLevel !== null ? options.deflateLevel : 9; options.deflateLevel = options.deflateLevel != null ? options.deflateLevel : 9;
options.deflateStrategy = options.deflateStrategy !== null ? options.deflateStrategy : 3; options.deflateStrategy = options.deflateStrategy != null ? options.deflateStrategy : 3;
options.deflateFactory = options.deflateFactory || zlib.createDeflate; options.deflateFactory = options.deflateFactory || zlib.createDeflate;
options.bitDepth = options.bitDepth || 8; options.bitDepth = options.bitDepth || 8;
options.colorType = (typeof options.colorType === 'number') ? options.colorType : COLORTYPE_TRUECOLOR_ALPHA; options.colorType = (typeof options.colorType === 'number') ? options.colorType : constants.COLORTYPE_COLOR_ALPHA;
if (options.colorType !== constants.COLORTYPE_COLOR && options.colorType !== constants.COLORTYPE_COLOR_ALPHA) {
throw new Error('option color type:' + options.colorType + ' is not supported at present');
}
if (options.bitDepth !== 8) {
throw new Error('option bit depth:' + options.bitDepth + ' is not supported at present');
}
this.readable = true; this.readable = true;
}; };
@ -38,10 +41,7 @@ Packer.prototype.pack = function(data, width, height, gamma) {
} }
// filter pixel data // filter pixel data
var bpp = 4; var bpp = constants.COLORTYPE_TO_BPP_MAP[this._options.colorType];
if (this._options.colorType === COLORTYPE_TRUECOLOR) {
bpp = 3;
}
var filteredData = filter(data, width, height, this._options, bpp); var filteredData = filter(data, width, height, this._options, bpp);
// compress it // compress it

View file

@ -36,14 +36,6 @@ var Parser = module.exports = function(options, dependencies) {
this.finished = dependencies.finished; this.finished = dependencies.finished;
}; };
var colorTypeToBppMap = {
0: 1,
2: 3,
3: 1,
4: 2,
6: 4
};
Parser.prototype.start = function() { Parser.prototype.start = function() {
this.read(constants.PNG_SIGNATURE.length, this.read(constants.PNG_SIGNATURE.length,
this._parseSignature.bind(this) this._parseSignature.bind(this)
@ -150,7 +142,7 @@ Parser.prototype._parseIHDR = function(data) {
this.error(new Error('Unsupported bit depth ' + depth)); this.error(new Error('Unsupported bit depth ' + depth));
return; return;
} }
if (!(colorType in colorTypeToBppMap)) { if (!(colorType in constants.COLORTYPE_TO_BPP_MAP)) {
this.error(new Error('Unsupported color type')); this.error(new Error('Unsupported color type'));
return; return;
} }
@ -169,7 +161,7 @@ Parser.prototype._parseIHDR = function(data) {
this._colorType = colorType; this._colorType = colorType;
var bpp = colorTypeToBppMap[this._colorType]; var bpp = constants.COLORTYPE_TO_BPP_MAP[this._colorType];
this._hasIHDR = true; this._hasIHDR = true;
@ -178,9 +170,9 @@ Parser.prototype._parseIHDR = function(data) {
height: height, height: height,
depth: depth, depth: depth,
interlace: Boolean(interlace), interlace: Boolean(interlace),
palette: Boolean(colorType & constants.COLOR_PALETTE), palette: Boolean(colorType & constants.COLORTYPE_PALETTE),
color: Boolean(colorType & constants.COLOR_COLOR), color: Boolean(colorType & constants.COLORTYPE_COLOR),
alpha: Boolean(colorType & constants.COLOR_ALPHA), alpha: Boolean(colorType & constants.COLORTYPE_ALPHA),
bpp: bpp, bpp: bpp,
colorType: colorType colorType: colorType
}); });
@ -221,7 +213,7 @@ Parser.prototype._parseTRNS = function(data) {
this._crc.write(data); this._crc.write(data);
// palette // palette
if (this._colorType === 3) { if (this._colorType === constants.COLORTYPE_PALETTE_COLOR) {
if (this._palette.length === 0) { if (this._palette.length === 0) {
this.error(new Error('Transparency chunk must be after palette')); this.error(new Error('Transparency chunk must be after palette'));
return; return;
@ -238,11 +230,11 @@ Parser.prototype._parseTRNS = function(data) {
// for colorType 0 (grayscale) and 2 (rgb) // for colorType 0 (grayscale) and 2 (rgb)
// there might be one gray/color defined as transparent // there might be one gray/color defined as transparent
if (this._colorType === 0) { if (this._colorType === constants.COLORTYPE_GRAYSCALE) {
// grey, 2 bytes // grey, 2 bytes
this.transColor([data.readUInt16BE(0)]); this.transColor([data.readUInt16BE(0)]);
} }
if (this._colorType === 2) { if (this._colorType === constants.COLORTYPE_COLOR) {
this.transColor([data.readUInt16BE(0), data.readUInt16BE(2), data.readUInt16BE(4)]); this.transColor([data.readUInt16BE(0), data.readUInt16BE(2), data.readUInt16BE(4)]);
} }
@ -267,7 +259,7 @@ Parser.prototype._parseIDAT = function(length, data) {
this._crc.write(data); this._crc.write(data);
if (this._colorType === 3 && this._palette.length === 0) { if (this._colorType === constants.COLORTYPE_PALETTE_COLOR && this._palette.length === 0) {
throw new Error('Expected palette not found'); throw new Error('Expected palette not found');
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 108 B

After

Width:  |  Height:  |  Size: 119 B

View file

@ -55,6 +55,7 @@ test("should correctly parse an 1-bit colormap png", function (t) {
if (getPixel(png, x, y) !== 0x000000FF) { if (getPixel(png, x, y) !== 0x000000FF) {
t.fail("pixel does not match - " + getPixel(png, x, y) + " !== 0x000000FF"); t.fail("pixel does not match - " + getPixel(png, x, y) + " !== 0x000000FF");
isOk = false; isOk = false;
break;
} }
} }
t.ok(isOk, "The pixels should all be black"); t.ok(isOk, "The pixels should all be black");
@ -82,6 +83,7 @@ test("should correctly parse an 8-bit grayscale png", function (t) {
if (getPixel(png, x, y) !== (x ^ y) * 286331136 + 255) { if (getPixel(png, x, y) !== (x ^ y) * 286331136 + 255) {
t.fail("pixel does not match - " + getPixel(png, x, y) + " !== " + ((x ^ y) * 286331136 + 255)); t.fail("pixel does not match - " + getPixel(png, x, y) + " !== " + ((x ^ y) * 286331136 + 255));
isOk = false; isOk = false;
break;
} }
} }
} }
@ -110,6 +112,7 @@ test("should correctly parse an 8-bit truecolor png", function (t) {
if (getPixel(png, x, y) !== x * 285212672 + y * 1114112 + (x ^ y) * 4352 + 255) { if (getPixel(png, x, y) !== x * 285212672 + y * 1114112 + (x ^ y) * 4352 + 255) {
t.fail("pixel does not match - " + getPixel(png, x, y) + " !== " + (x * 285212672 + y * 1114112 + (x ^ y) * 4352 + 255)); t.fail("pixel does not match - " + getPixel(png, x, y) + " !== " + (x * 285212672 + y * 1114112 + (x ^ y) * 4352 + 255));
isOk = false; isOk = false;
break;
} }
} }
} }
@ -138,12 +141,8 @@ test("should correctly parse an 8-bit truecolor png with alpha", function (t) {
if (getPixel(png, x, y) !== x * 285212672 + y * 1114112 + (x ^ y) * 17) { if (getPixel(png, x, y) !== x * 285212672 + y * 1114112 + (x ^ y) * 17) {
t.fail("pixel does not match - " + getPixel(png, x, y) + " !== " + (x * 285212672 + y * 1114112 + (x ^ y) * 17)); t.fail("pixel does not match - " + getPixel(png, x, y) + " !== " + (x * 285212672 + y * 1114112 + (x ^ y) * 17));
isOk = false; isOk = false;
break;
} }
t.equal(
getPixel(png, x, y),
x * 285212672 + y * 1114112 + (x ^ y) * 17
)
} }
} }
t.ok(isOk, "The pixels should match"); t.ok(isOk, "The pixels should match");
@ -205,6 +204,7 @@ test("should correctly read an indexed color image", function (t) {
if (getPixel(png, x, y) !== expected) { if (getPixel(png, x, y) !== expected) {
t.fail("pixel does not match - " + getPixel(png, x, y) + " !== " + expected); t.fail("pixel does not match - " + getPixel(png, x, y) + " !== " + expected);
isOk = false; isOk = false;
break;
} }
} }
} }
@ -249,6 +249,7 @@ test("should correctly read an indexed color image with alpha", function (t) {
if (getPixel(png, x, y) !== expected) { if (getPixel(png, x, y) !== expected) {
t.fail("pixel does not match - " + getPixel(png, x, y) + " !== " + expected); t.fail("pixel does not match - " + getPixel(png, x, y) + " !== " + expected);
isOk = false; isOk = false;
break;
} }
} }
} }