Comparing version 4.0.1 to 5.0.0
{ | ||
"rules": { | ||
"no-alert": "error", | ||
"no-array-constructor": "off", | ||
"no-bitwise": "off", | ||
"no-caller": "error", | ||
"no-catch-shadow": "error", | ||
"no-class-assign": "error", | ||
"no-cond-assign": "error", | ||
"no-console": "error", | ||
"no-const-assign": "error", | ||
"no-constant-condition": "error", | ||
"no-continue": "off", | ||
"no-control-regex": "error", | ||
"no-debugger": "error", | ||
"no-delete-var": "error", | ||
"no-div-regex": "off", | ||
"no-dupe-keys": "error", | ||
"no-dupe-args": "error", | ||
"no-duplicate-case": "error", | ||
"no-else-return": "error", | ||
"no-empty": "error", | ||
"no-empty-character-class": "error", | ||
"no-eq-null": "off", | ||
"no-eval": "error", | ||
"no-ex-assign": "error", | ||
"no-extend-native": "error", | ||
"no-extra-bind": "error", | ||
"no-extra-boolean-cast": "error", | ||
"no-extra-parens": "off", | ||
"no-extra-semi": "error", | ||
"no-fallthrough": "error", | ||
"no-floating-decimal": "error", | ||
"no-func-assign": "error", | ||
"no-implicit-coercion": "error", | ||
"no-implied-eval": "error", | ||
"no-inline-comments": "off", | ||
"no-inner-declarations": ["error", "functions"], | ||
"no-invalid-regexp": "error", | ||
"no-invalid-this": "error", | ||
"no-irregular-whitespace": "error", | ||
"no-iterator": "error", | ||
"no-label-var": "error", | ||
"no-labels": "error", | ||
"no-lone-blocks": "error", | ||
"no-lonely-if": "error", | ||
"no-loop-func": "error", | ||
"no-mixed-requires": ["error", false], | ||
"no-mixed-spaces-and-tabs": ["error", false], | ||
"linebreak-style": ["off", "unix"], | ||
"no-multi-spaces": "error", | ||
"no-multi-str": "off", | ||
"no-multiple-empty-lines": ["error", {"max": 2}], | ||
"no-native-reassign": "error", | ||
"no-negated-in-lhs": "error", | ||
"no-nested-ternary": "error", | ||
"no-new": "error", | ||
"no-new-func": "error", | ||
"no-new-object": "error", | ||
"no-new-require": "error", | ||
"no-new-wrappers": "error", | ||
"no-obj-calls": "error", | ||
"no-octal": "error", | ||
"no-octal-escape": "off", | ||
"no-param-reassign": "off", | ||
"no-path-concat": "off", | ||
"no-plusplus": "off", | ||
"no-process-env": "off", | ||
"no-process-exit": "off", | ||
"no-proto": "off", | ||
"no-redeclare": "error", | ||
"no-regex-spaces": "error", | ||
"no-restricted-modules": "off", | ||
"no-return-assign": "error", | ||
"no-script-url": "off", | ||
"no-self-compare": "error", | ||
"no-sequences": "error", | ||
"no-shadow": "error", | ||
"no-shadow-restricted-names": "error", | ||
"no-spaced-func": "error", | ||
"no-sparse-arrays": "error", | ||
"no-sync": "off", | ||
"no-ternary": "off", | ||
"no-trailing-spaces": "error", | ||
"no-this-before-super": "error", | ||
"no-throw-literal": "error", | ||
"no-undef": "error", | ||
"no-undef-init": "error", | ||
"no-undefined": "off", | ||
"no-unexpected-multiline": "error", | ||
"no-underscore-dangle": "off", | ||
"no-unneeded-ternary": "off", | ||
"no-unreachable": "error", | ||
"no-unused-expressions": "error", | ||
"no-unused-vars": ["error", {"vars": "all", "args": "after-used"}], | ||
"no-use-before-define": "error", | ||
"no-useless-call": "error", | ||
"no-void": "off", | ||
"no-var": "off", | ||
"no-warning-comments": ["error", { "terms": ["todo", "fixme", "xxx"], "location": "start" }], | ||
"no-with": "error", | ||
"array-bracket-spacing": ["error", "never"], | ||
"arrow-parens": "error", | ||
"arrow-spacing": "error", | ||
"accessor-pairs": "error", | ||
"block-scoped-var": "off", | ||
"brace-style": ["error", "stroustrup"], | ||
"callback-return": ["error", ["callback", "cb", "next"]], | ||
"camelcase": ["error", {"properties": "always"}], | ||
"comma-dangle": ["error", "never"], | ||
"comma-spacing": "error", | ||
"comma-style": "error", | ||
"complexity": "off", | ||
"computed-property-spacing": ["off", "never"], | ||
"consistent-return": "off", | ||
"consistent-this": ["off", "that"], | ||
"constructor-super": "error", | ||
"curly": ["error", "all"], | ||
"default-case": "error", | ||
"dot-location": ["error", "property"], | ||
"dot-notation": ["error", { "allowKeywords": true }], | ||
"eol-last": "off", | ||
"eqeqeq": ["error", "smart"], | ||
"func-names": "off", | ||
"func-style": ["off", "declaration"], | ||
"generator-star-spacing": "off", | ||
"guard-for-in": "error", | ||
"handle-callback-err": "error", | ||
"id-length": ["error", {"min": 3, "max": 25, "exceptions":["x", "y", "i", "j", "ex", "up"]}], | ||
"indent": ["error", 2, {"SwitchCase": 1}], | ||
"init-declarations": "off", | ||
"key-spacing": ["error", { "beforeColon": false, "afterColon": true }], | ||
"keyword-spacing": "error", | ||
"lines-around-comment": "off", | ||
"max-depth": ["error", 6], | ||
"max-len": ["error", { "code": 160 }], | ||
"max-nested-callbacks": ["error", 1], | ||
"max-params": ["error", 6], | ||
"max-statements": "off", | ||
"new-cap": "error", | ||
"new-parens": "error", | ||
"newline-after-var": "off", | ||
"object-curly-spacing": ["error", "always"], | ||
"object-shorthand": "off", | ||
"one-var": ["error", { | ||
"initialized": "never" | ||
}], | ||
"operator-assignment": ["off", "always"], | ||
"operator-linebreak": ["error", "after"], | ||
"padded-blocks": "off", | ||
"prefer-const": "off", | ||
"prefer-spread": "off", | ||
"prefer-reflect": "off", | ||
"quote-props": "off", | ||
"quotes": ["error", "single"], | ||
"radix": "off", | ||
"id-match": "off", | ||
"require-yield": "off", | ||
"semi": ["error", "always"], | ||
"semi-spacing": ["error", {"before": false, "after": true}], | ||
"sort-vars": "off", | ||
"space-before-blocks": ["error", "always"], | ||
"space-before-function-paren": ["error", "never"], | ||
"space-in-parens": ["error", "never"], | ||
"space-infix-ops": "error", | ||
"space-unary-ops": ["error", { "words": true, "nonwords": false }], | ||
"spaced-comment": "off", | ||
"strict": ["error", "global"], | ||
"use-isnan": "error", | ||
"valid-jsdoc": "off", | ||
"valid-typeof": "error", | ||
"vars-on-top": "off", | ||
"wrap-iife": "off", | ||
"wrap-regex": "off", | ||
"yoda": ["off", "never"] | ||
}, | ||
"env": { | ||
"node": true, | ||
"es6": true | ||
"parserOptions": { "ecmaVersion": 2018 }, | ||
"extends": ["eslint:recommended", "prettier"], | ||
"env": { | ||
"node": true, | ||
"es6": true | ||
}, | ||
"rules": { | ||
"no-var": "error" | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": ["test/*.js", "examples/*.js"], | ||
"rules": { | ||
"no-console": "off" | ||
} | ||
} | ||
] | ||
} |
@@ -1,17 +0,17 @@ | ||
'use strict'; | ||
"use strict"; | ||
var interlaceUtils = require('./interlace'); | ||
let interlaceUtils = require("./interlace"); | ||
var pixelBppMapper = [ | ||
let pixelBppMapper = [ | ||
// 0 - dummy entry | ||
function() {}, | ||
function () {}, | ||
// 1 - L | ||
// 0: 0, 1: 0, 2: 0, 3: 0xff | ||
function(pxData, data, pxPos, rawPos) { | ||
function (pxData, data, pxPos, rawPos) { | ||
if (rawPos === data.length) { | ||
throw new Error('Ran out of data'); | ||
throw new Error("Ran out of data"); | ||
} | ||
var pixel = data[rawPos]; | ||
let pixel = data[rawPos]; | ||
pxData[pxPos] = pixel; | ||
@@ -25,8 +25,8 @@ pxData[pxPos + 1] = pixel; | ||
// 0: 0, 1: 0, 2: 0, 3: 1 | ||
function(pxData, data, pxPos, rawPos) { | ||
function (pxData, data, pxPos, rawPos) { | ||
if (rawPos + 1 >= data.length) { | ||
throw new Error('Ran out of data'); | ||
throw new Error("Ran out of data"); | ||
} | ||
var pixel = data[rawPos]; | ||
let pixel = data[rawPos]; | ||
pxData[pxPos] = pixel; | ||
@@ -40,5 +40,5 @@ pxData[pxPos + 1] = pixel; | ||
// 0: 0, 1: 1, 2: 2, 3: 0xff | ||
function(pxData, data, pxPos, rawPos) { | ||
function (pxData, data, pxPos, rawPos) { | ||
if (rawPos + 2 >= data.length) { | ||
throw new Error('Ran out of data'); | ||
throw new Error("Ran out of data"); | ||
} | ||
@@ -54,5 +54,5 @@ | ||
// 0: 0, 1: 1, 2: 2, 3: 3 | ||
function(pxData, data, pxPos, rawPos) { | ||
function (pxData, data, pxPos, rawPos) { | ||
if (rawPos + 3 >= data.length) { | ||
throw new Error('Ran out of data'); | ||
throw new Error("Ran out of data"); | ||
} | ||
@@ -64,13 +64,13 @@ | ||
pxData[pxPos + 3] = data[rawPos + 3]; | ||
} | ||
}, | ||
]; | ||
var pixelBppCustomMapper = [ | ||
let pixelBppCustomMapper = [ | ||
// 0 - dummy entry | ||
function() {}, | ||
function () {}, | ||
// 1 - L | ||
// 0: 0, 1: 0, 2: 0, 3: 0xff | ||
function(pxData, pixelData, pxPos, maxBit) { | ||
var pixel = pixelData[0]; | ||
function (pxData, pixelData, pxPos, maxBit) { | ||
let pixel = pixelData[0]; | ||
pxData[pxPos] = pixel; | ||
@@ -84,4 +84,4 @@ pxData[pxPos + 1] = pixel; | ||
// 0: 0, 1: 0, 2: 0, 3: 1 | ||
function(pxData, pixelData, pxPos) { | ||
var pixel = pixelData[0]; | ||
function (pxData, pixelData, pxPos) { | ||
let pixel = pixelData[0]; | ||
pxData[pxPos] = pixel; | ||
@@ -95,3 +95,3 @@ pxData[pxPos + 1] = pixel; | ||
// 0: 0, 1: 1, 2: 2, 3: 0xff | ||
function(pxData, pixelData, pxPos, maxBit) { | ||
function (pxData, pixelData, pxPos, maxBit) { | ||
pxData[pxPos] = pixelData[0]; | ||
@@ -105,3 +105,3 @@ pxData[pxPos + 1] = pixelData[1]; | ||
// 0: 0, 1: 1, 2: 2, 3: 3 | ||
function(pxData, pixelData, pxPos) { | ||
function (pxData, pixelData, pxPos) { | ||
pxData[pxPos] = pixelData[0]; | ||
@@ -111,24 +111,23 @@ pxData[pxPos + 1] = pixelData[1]; | ||
pxData[pxPos + 3] = pixelData[3]; | ||
} | ||
}, | ||
]; | ||
function bitRetriever(data, depth) { | ||
let leftOver = []; | ||
let i = 0; | ||
var leftOver = []; | ||
var i = 0; | ||
function split() { | ||
if (i === data.length) { | ||
throw new Error('Ran out of data'); | ||
throw new Error("Ran out of data"); | ||
} | ||
var byte = data[i]; | ||
let byte = data[i]; | ||
i++; | ||
var byte8, byte7, byte6, byte5, byte4, byte3, byte2, byte1; | ||
let byte8, byte7, byte6, byte5, byte4, byte3, byte2, byte1; | ||
switch (depth) { | ||
default: | ||
throw new Error('unrecognised depth'); | ||
throw new Error("unrecognised depth"); | ||
case 16: | ||
byte2 = data[i]; | ||
i++; | ||
leftOver.push(((byte << 8) + byte2)); | ||
leftOver.push((byte << 8) + byte2); | ||
break; | ||
@@ -142,5 +141,5 @@ case 4: | ||
byte4 = byte & 3; | ||
byte3 = byte >> 2 & 3; | ||
byte2 = byte >> 4 & 3; | ||
byte1 = byte >> 6 & 3; | ||
byte3 = (byte >> 2) & 3; | ||
byte2 = (byte >> 4) & 3; | ||
byte1 = (byte >> 6) & 3; | ||
leftOver.push(byte1, byte2, byte3, byte4); | ||
@@ -150,9 +149,9 @@ break; | ||
byte8 = byte & 1; | ||
byte7 = byte >> 1 & 1; | ||
byte6 = byte >> 2 & 1; | ||
byte5 = byte >> 3 & 1; | ||
byte4 = byte >> 4 & 1; | ||
byte3 = byte >> 5 & 1; | ||
byte2 = byte >> 6 & 1; | ||
byte1 = byte >> 7 & 1; | ||
byte7 = (byte >> 1) & 1; | ||
byte6 = (byte >> 2) & 1; | ||
byte5 = (byte >> 3) & 1; | ||
byte4 = (byte >> 4) & 1; | ||
byte3 = (byte >> 5) & 1; | ||
byte2 = (byte >> 6) & 1; | ||
byte1 = (byte >> 7) & 1; | ||
leftOver.push(byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8); | ||
@@ -164,28 +163,29 @@ break; | ||
return { | ||
get: function(count) { | ||
get: function (count) { | ||
while (leftOver.length < count) { | ||
split(); | ||
} | ||
var returner = leftOver.slice(0, count); | ||
let returner = leftOver.slice(0, count); | ||
leftOver = leftOver.slice(count); | ||
return returner; | ||
}, | ||
resetAfterLine: function() { | ||
resetAfterLine: function () { | ||
leftOver.length = 0; | ||
}, | ||
end: function() { | ||
end: function () { | ||
if (i !== data.length) { | ||
throw new Error('extra data found'); | ||
throw new Error("extra data found"); | ||
} | ||
} | ||
}, | ||
}; | ||
} | ||
function mapImage8Bit(image, pxData, getPxPos, bpp, data, rawPos) { // eslint-disable-line max-params | ||
var imageWidth = image.width; | ||
var imageHeight = image.height; | ||
var imagePass = image.index; | ||
for (var y = 0; y < imageHeight; y++) { | ||
for (var x = 0; x < imageWidth; x++) { | ||
var pxPos = getPxPos(x, y, imagePass); | ||
function mapImage8Bit(image, pxData, getPxPos, bpp, data, rawPos) { | ||
// eslint-disable-line max-params | ||
let imageWidth = image.width; | ||
let imageHeight = image.height; | ||
let imagePass = image.index; | ||
for (let y = 0; y < imageHeight; y++) { | ||
for (let x = 0; x < imageWidth; x++) { | ||
let pxPos = getPxPos(x, y, imagePass); | ||
pixelBppMapper[bpp](pxData, data, pxPos, rawPos); | ||
@@ -198,10 +198,11 @@ rawPos += bpp; //eslint-disable-line no-param-reassign | ||
function mapImageCustomBit(image, pxData, getPxPos, bpp, bits, maxBit) { // eslint-disable-line max-params | ||
var imageWidth = image.width; | ||
var imageHeight = image.height; | ||
var imagePass = image.index; | ||
for (var y = 0; y < imageHeight; y++) { | ||
for (var x = 0; x < imageWidth; x++) { | ||
var pixelData = bits.get(bpp); | ||
var pxPos = getPxPos(x, y, imagePass); | ||
function mapImageCustomBit(image, pxData, getPxPos, bpp, bits, maxBit) { | ||
// eslint-disable-line max-params | ||
let imageWidth = image.width; | ||
let imageHeight = image.height; | ||
let imagePass = image.index; | ||
for (let y = 0; y < imageHeight; y++) { | ||
for (let x = 0; x < imageWidth; x++) { | ||
let pixelData = bits.get(bpp); | ||
let pxPos = getPxPos(x, y, imagePass); | ||
pixelBppCustomMapper[bpp](pxData, pixelData, pxPos, maxBit); | ||
@@ -213,24 +214,23 @@ } | ||
exports.dataToBitMap = function(data, bitmapInfo) { | ||
exports.dataToBitMap = function (data, bitmapInfo) { | ||
let width = bitmapInfo.width; | ||
let height = bitmapInfo.height; | ||
let depth = bitmapInfo.depth; | ||
let bpp = bitmapInfo.bpp; | ||
let interlace = bitmapInfo.interlace; | ||
let bits; | ||
var width = bitmapInfo.width; | ||
var height = bitmapInfo.height; | ||
var depth = bitmapInfo.depth; | ||
var bpp = bitmapInfo.bpp; | ||
var interlace = bitmapInfo.interlace; | ||
if (depth !== 8) { | ||
var bits = bitRetriever(data, depth); | ||
bits = bitRetriever(data, depth); | ||
} | ||
var pxData; | ||
let pxData; | ||
if (depth <= 8) { | ||
pxData = Buffer.alloc(width * height * 4); | ||
} | ||
else { | ||
} else { | ||
pxData = new Uint16Array(width * height * 4); | ||
} | ||
var maxBit = Math.pow(2, depth) - 1; | ||
var rawPos = 0; | ||
var images; | ||
var getPxPos; | ||
let maxBit = Math.pow(2, depth) - 1; | ||
let rawPos = 0; | ||
let images; | ||
let getPxPos; | ||
@@ -240,7 +240,6 @@ if (interlace) { | ||
getPxPos = interlaceUtils.getInterlaceIterator(width, height); | ||
} | ||
else { | ||
var nonInterlacedPxPos = 0; | ||
getPxPos = function() { | ||
var returner = nonInterlacedPxPos; | ||
} else { | ||
let nonInterlacedPxPos = 0; | ||
getPxPos = function () { | ||
let returner = nonInterlacedPxPos; | ||
nonInterlacedPxPos += 4; | ||
@@ -252,16 +251,28 @@ return returner; | ||
for (var imageIndex = 0; imageIndex < images.length; imageIndex++) { | ||
for (let imageIndex = 0; imageIndex < images.length; imageIndex++) { | ||
if (depth === 8) { | ||
rawPos = mapImage8Bit(images[imageIndex], pxData, getPxPos, bpp, data, rawPos); | ||
rawPos = mapImage8Bit( | ||
images[imageIndex], | ||
pxData, | ||
getPxPos, | ||
bpp, | ||
data, | ||
rawPos | ||
); | ||
} else { | ||
mapImageCustomBit( | ||
images[imageIndex], | ||
pxData, | ||
getPxPos, | ||
bpp, | ||
bits, | ||
maxBit | ||
); | ||
} | ||
else { | ||
mapImageCustomBit(images[imageIndex], pxData, getPxPos, bpp, bits, maxBit); | ||
} | ||
} | ||
if (depth === 8) { | ||
if (rawPos !== data.length) { | ||
throw new Error('extra data found'); | ||
throw new Error("extra data found"); | ||
} | ||
} | ||
else { | ||
} else { | ||
bits.end(); | ||
@@ -268,0 +279,0 @@ } |
@@ -1,10 +0,13 @@ | ||
'use strict'; | ||
"use strict"; | ||
var constants = require('./constants'); | ||
let constants = require("./constants"); | ||
module.exports = function(dataIn, width, height, options) { | ||
var outHasAlpha = [constants.COLORTYPE_COLOR_ALPHA, constants.COLORTYPE_ALPHA].indexOf(options.colorType) !== -1; | ||
module.exports = function (dataIn, width, height, options) { | ||
let outHasAlpha = | ||
[constants.COLORTYPE_COLOR_ALPHA, constants.COLORTYPE_ALPHA].indexOf( | ||
options.colorType | ||
) !== -1; | ||
if (options.colorType === options.inputColorType) { | ||
var bigEndian = (function() { | ||
var buffer = new ArrayBuffer(2); | ||
let bigEndian = (function () { | ||
let buffer = new ArrayBuffer(2); | ||
new DataView(buffer).setInt16(0, 256, true /* littleEndian */); | ||
@@ -21,10 +24,10 @@ // Int16Array uses the platform's endianness. | ||
// map to a UInt16 array if data is 16bit, fix endianness below | ||
var data = options.bitDepth !== 16 ? dataIn : new Uint16Array(dataIn.buffer); | ||
let data = options.bitDepth !== 16 ? dataIn : new Uint16Array(dataIn.buffer); | ||
var maxValue = 255; | ||
var inBpp = constants.COLORTYPE_TO_BPP_MAP[options.inputColorType]; | ||
let maxValue = 255; | ||
let inBpp = constants.COLORTYPE_TO_BPP_MAP[options.inputColorType]; | ||
if (inBpp === 4 && !options.inputHasAlpha) { | ||
inBpp = 3; | ||
} | ||
var outBpp = constants.COLORTYPE_TO_BPP_MAP[options.colorType]; | ||
let outBpp = constants.COLORTYPE_TO_BPP_MAP[options.colorType]; | ||
if (options.bitDepth === 16) { | ||
@@ -34,8 +37,8 @@ maxValue = 65535; | ||
} | ||
var outData = Buffer.alloc(width * height * outBpp); | ||
let outData = Buffer.alloc(width * height * outBpp); | ||
var inIndex = 0; | ||
var outIndex = 0; | ||
let inIndex = 0; | ||
let outIndex = 0; | ||
var bgColor = options.bgColor || {}; | ||
let bgColor = options.bgColor || {}; | ||
if (bgColor.red === undefined) { | ||
@@ -52,6 +55,6 @@ bgColor.red = maxValue; | ||
function getRGBA() { | ||
var red; | ||
var green; | ||
var blue; | ||
var alpha = maxValue; | ||
let red; | ||
let green; | ||
let blue; | ||
let alpha = maxValue; | ||
switch (options.inputColorType) { | ||
@@ -81,3 +84,7 @@ case constants.COLORTYPE_COLOR_ALPHA: | ||
default: | ||
throw new Error('input color type:' + options.inputColorType + ' is not supported at present'); | ||
throw new Error( | ||
"input color type:" + | ||
options.inputColorType + | ||
" is not supported at present" | ||
); | ||
} | ||
@@ -88,5 +95,14 @@ | ||
alpha /= maxValue; | ||
red = Math.min(Math.max(Math.round((1 - alpha) * bgColor.red + alpha * red), 0), maxValue); | ||
green = Math.min(Math.max(Math.round((1 - alpha) * bgColor.green + alpha * green), 0), maxValue); | ||
blue = Math.min(Math.max(Math.round((1 - alpha) * bgColor.blue + alpha * blue), 0), maxValue); | ||
red = Math.min( | ||
Math.max(Math.round((1 - alpha) * bgColor.red + alpha * red), 0), | ||
maxValue | ||
); | ||
green = Math.min( | ||
Math.max(Math.round((1 - alpha) * bgColor.green + alpha * green), 0), | ||
maxValue | ||
); | ||
blue = Math.min( | ||
Math.max(Math.round((1 - alpha) * bgColor.blue + alpha * blue), 0), | ||
maxValue | ||
); | ||
} | ||
@@ -97,5 +113,5 @@ } | ||
for (var y = 0; y < height; y++) { | ||
for (var x = 0; x < width; x++) { | ||
var rgba = getRGBA(data, inIndex); | ||
for (let y = 0; y < height; y++) { | ||
for (let x = 0; x < width; x++) { | ||
let rgba = getRGBA(data, inIndex); | ||
@@ -112,4 +128,3 @@ switch (options.colorType) { | ||
} | ||
} | ||
else { | ||
} else { | ||
outData.writeUInt16BE(rgba.red, outIndex); | ||
@@ -124,5 +139,5 @@ outData.writeUInt16BE(rgba.green, outIndex + 2); | ||
case constants.COLORTYPE_ALPHA: | ||
case constants.COLORTYPE_GRAYSCALE: | ||
case constants.COLORTYPE_GRAYSCALE: { | ||
// Convert to grayscale and alpha | ||
var grayscale = (rgba.red + rgba.green + rgba.blue) / 3; | ||
let grayscale = (rgba.red + rgba.green + rgba.blue) / 3; | ||
if (options.bitDepth === 8) { | ||
@@ -133,4 +148,3 @@ outData[outIndex] = grayscale; | ||
} | ||
} | ||
else { | ||
} else { | ||
outData.writeUInt16BE(grayscale, outIndex); | ||
@@ -142,4 +156,5 @@ if (outHasAlpha) { | ||
break; | ||
} | ||
default: | ||
throw new Error('unrecognised color Type ' + options.colorType); | ||
throw new Error("unrecognised color Type " + options.colorType); | ||
} | ||
@@ -146,0 +161,0 @@ |
@@ -1,9 +0,7 @@ | ||
'use strict'; | ||
"use strict"; | ||
let util = require("util"); | ||
let Stream = require("stream"); | ||
var util = require('util'); | ||
var Stream = require('stream'); | ||
var ChunkStream = module.exports = function() { | ||
let ChunkStream = (module.exports = function () { | ||
Stream.call(this); | ||
@@ -17,40 +15,38 @@ | ||
this._encoding = 'utf8'; | ||
this._encoding = "utf8"; | ||
this.writable = true; | ||
}; | ||
}); | ||
util.inherits(ChunkStream, Stream); | ||
ChunkStream.prototype.read = function(length, callback) { | ||
ChunkStream.prototype.read = function (length, callback) { | ||
this._reads.push({ | ||
length: Math.abs(length), // if length < 0 then at most this length | ||
allowLess: length < 0, | ||
func: callback | ||
func: callback, | ||
}); | ||
process.nextTick(function() { | ||
this._process(); | ||
process.nextTick( | ||
function () { | ||
this._process(); | ||
// its paused and there is not enought data then ask for more | ||
if (this._paused && this._reads && this._reads.length > 0) { | ||
this._paused = false; | ||
// its paused and there is not enought data then ask for more | ||
if (this._paused && this._reads && this._reads.length > 0) { | ||
this._paused = false; | ||
this.emit('drain'); | ||
} | ||
}.bind(this)); | ||
this.emit("drain"); | ||
} | ||
}.bind(this) | ||
); | ||
}; | ||
ChunkStream.prototype.write = function(data, encoding) { | ||
ChunkStream.prototype.write = function (data, encoding) { | ||
if (!this.writable) { | ||
this.emit('error', new Error('Stream not writable')); | ||
this.emit("error", new Error("Stream not writable")); | ||
return false; | ||
} | ||
var dataBuffer; | ||
let dataBuffer; | ||
if (Buffer.isBuffer(data)) { | ||
dataBuffer = data; | ||
} | ||
else { | ||
} else { | ||
dataBuffer = Buffer.from(data, encoding || this._encoding); | ||
@@ -72,4 +68,3 @@ } | ||
ChunkStream.prototype.end = function(data, encoding) { | ||
ChunkStream.prototype.end = function (data, encoding) { | ||
if (data) { | ||
@@ -89,4 +84,3 @@ this.write(data, encoding); | ||
this._end(); | ||
} | ||
else { | ||
} else { | ||
this._buffers.push(null); | ||
@@ -99,8 +93,5 @@ this._process(); | ||
ChunkStream.prototype._end = function() { | ||
ChunkStream.prototype._end = function () { | ||
if (this._reads.length > 0) { | ||
this.emit('error', | ||
new Error('Unexpected end of input') | ||
); | ||
this.emit("error", new Error("Unexpected end of input")); | ||
} | ||
@@ -111,4 +102,3 @@ | ||
ChunkStream.prototype.destroy = function() { | ||
ChunkStream.prototype.destroy = function () { | ||
if (!this._buffers) { | ||
@@ -122,6 +112,6 @@ return; | ||
this.emit('close'); | ||
this.emit("close"); | ||
}; | ||
ChunkStream.prototype._processReadAllowingLess = function(read) { | ||
ChunkStream.prototype._processReadAllowingLess = function (read) { | ||
// ok there is any data so that we can satisfy this request | ||
@@ -131,7 +121,6 @@ this._reads.shift(); // == read | ||
// first we need to peek into first buffer | ||
var smallerBuf = this._buffers[0]; | ||
let smallerBuf = this._buffers[0]; | ||
// ok there is more data than we need | ||
if (smallerBuf.length > read.length) { | ||
this._buffered -= read.length; | ||
@@ -141,5 +130,3 @@ this._buffers[0] = smallerBuf.slice(read.length); | ||
read.func.call(this, smallerBuf.slice(0, read.length)); | ||
} | ||
else { | ||
} else { | ||
// ok this is less than maximum length so use it all | ||
@@ -153,15 +140,14 @@ this._buffered -= smallerBuf.length; | ||
ChunkStream.prototype._processRead = function(read) { | ||
ChunkStream.prototype._processRead = function (read) { | ||
this._reads.shift(); // == read | ||
var pos = 0; | ||
var count = 0; | ||
var data = Buffer.alloc(read.length); | ||
let pos = 0; | ||
let count = 0; | ||
let data = Buffer.alloc(read.length); | ||
// create buffer for all data | ||
while (pos < read.length) { | ||
let buf = this._buffers[count++]; | ||
let len = Math.min(buf.length, read.length - pos); | ||
var buf = this._buffers[count++]; | ||
var len = Math.min(buf.length, read.length - pos); | ||
buf.copy(data, pos, 0, len); | ||
@@ -186,21 +172,16 @@ pos += len; | ||
ChunkStream.prototype._process = function() { | ||
ChunkStream.prototype._process = function () { | ||
try { | ||
// as long as there is any data and read requests | ||
while (this._buffered > 0 && this._reads && this._reads.length > 0) { | ||
let read = this._reads[0]; | ||
var read = this._reads[0]; | ||
// read any data (but no more than length) | ||
if (read.allowLess) { | ||
this._processReadAllowingLess(read); | ||
} | ||
else if (this._buffered >= read.length) { | ||
} else if (this._buffered >= read.length) { | ||
// ok we can meet some expectations | ||
this._processRead(read); | ||
} | ||
else { | ||
} else { | ||
// not enought data to satisfy first request in queue | ||
@@ -215,6 +196,5 @@ // so we need to wait for more | ||
} | ||
} catch (ex) { | ||
this.emit("error", ex); | ||
} | ||
catch (ex) { | ||
this.emit('error', ex); | ||
} | ||
}; |
@@ -1,6 +0,4 @@ | ||
'use strict'; | ||
"use strict"; | ||
module.exports = { | ||
PNG_SIGNATURE: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a], | ||
@@ -30,6 +28,6 @@ | ||
4: 2, | ||
6: 4 | ||
6: 4, | ||
}, | ||
GAMMA_DIVISION: 100000 | ||
GAMMA_DIVISION: 100000, | ||
}; |
@@ -1,13 +0,12 @@ | ||
'use strict'; | ||
"use strict"; | ||
var crcTable = []; | ||
let crcTable = []; | ||
(function() { | ||
for (var i = 0; i < 256; i++) { | ||
var currentCrc = i; | ||
for (var j = 0; j < 8; j++) { | ||
(function () { | ||
for (let i = 0; i < 256; i++) { | ||
let currentCrc = i; | ||
for (let j = 0; j < 8; j++) { | ||
if (currentCrc & 1) { | ||
currentCrc = 0xedb88320 ^ (currentCrc >>> 1); | ||
} | ||
else { | ||
} else { | ||
currentCrc = currentCrc >>> 1; | ||
@@ -18,11 +17,10 @@ } | ||
} | ||
}()); | ||
})(); | ||
var CrcCalculator = module.exports = function() { | ||
let CrcCalculator = (module.exports = function () { | ||
this._crc = -1; | ||
}; | ||
}); | ||
CrcCalculator.prototype.write = function(data) { | ||
for (var i = 0; i < data.length; i++) { | ||
CrcCalculator.prototype.write = function (data) { | ||
for (let i = 0; i < data.length; i++) { | ||
this._crc = crcTable[(this._crc ^ data[i]) & 0xff] ^ (this._crc >>> 8); | ||
@@ -33,11 +31,9 @@ } | ||
CrcCalculator.prototype.crc32 = function() { | ||
CrcCalculator.prototype.crc32 = function () { | ||
return this._crc ^ -1; | ||
}; | ||
CrcCalculator.crc32 = function(buf) { | ||
var crc = -1; | ||
for (var i = 0; i < buf.length; i++) { | ||
CrcCalculator.crc32 = function (buf) { | ||
let crc = -1; | ||
for (let i = 0; i < buf.length; i++) { | ||
crc = crcTable[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8); | ||
@@ -44,0 +40,0 @@ } |
@@ -1,8 +0,7 @@ | ||
'use strict'; | ||
"use strict"; | ||
var paethPredictor = require('./paeth-predictor'); | ||
let paethPredictor = require("./paeth-predictor"); | ||
function filterNone(pxData, pxPos, byteWidth, rawData, rawPos) { | ||
for (var x = 0; x < byteWidth; x++) { | ||
for (let x = 0; x < byteWidth; x++) { | ||
rawData[rawPos + x] = pxData[pxPos + x]; | ||
@@ -13,7 +12,6 @@ } | ||
function filterSumNone(pxData, pxPos, byteWidth) { | ||
let sum = 0; | ||
let length = pxPos + byteWidth; | ||
var sum = 0; | ||
var length = pxPos + byteWidth; | ||
for (var i = pxPos; i < length; i++) { | ||
for (let i = pxPos; i < length; i++) { | ||
sum += Math.abs(pxData[i]); | ||
@@ -25,8 +23,6 @@ } | ||
function filterSub(pxData, pxPos, byteWidth, rawData, rawPos, bpp) { | ||
for (let x = 0; x < byteWidth; x++) { | ||
let left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
let val = pxData[pxPos + x] - left; | ||
for (var x = 0; x < byteWidth; x++) { | ||
var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
var val = pxData[pxPos + x] - left; | ||
rawData[rawPos + x] = val; | ||
@@ -37,9 +33,7 @@ } | ||
function filterSumSub(pxData, pxPos, byteWidth, bpp) { | ||
let sum = 0; | ||
for (let x = 0; x < byteWidth; x++) { | ||
let left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
let val = pxData[pxPos + x] - left; | ||
var sum = 0; | ||
for (var x = 0; x < byteWidth; x++) { | ||
var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
var val = pxData[pxPos + x] - left; | ||
sum += Math.abs(val); | ||
@@ -52,8 +46,6 @@ } | ||
function filterUp(pxData, pxPos, byteWidth, rawData, rawPos) { | ||
for (let x = 0; x < byteWidth; x++) { | ||
let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
let val = pxData[pxPos + x] - up; | ||
for (var x = 0; x < byteWidth; x++) { | ||
var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
var val = pxData[pxPos + x] - up; | ||
rawData[rawPos + x] = val; | ||
@@ -64,10 +56,8 @@ } | ||
function filterSumUp(pxData, pxPos, byteWidth) { | ||
let sum = 0; | ||
let length = pxPos + byteWidth; | ||
for (let x = pxPos; x < length; x++) { | ||
let up = pxPos > 0 ? pxData[x - byteWidth] : 0; | ||
let val = pxData[x] - up; | ||
var sum = 0; | ||
var length = pxPos + byteWidth; | ||
for (var x = pxPos; x < length; x++) { | ||
var up = pxPos > 0 ? pxData[x - byteWidth] : 0; | ||
var val = pxData[x] - up; | ||
sum += Math.abs(val); | ||
@@ -80,9 +70,7 @@ } | ||
function filterAvg(pxData, pxPos, byteWidth, rawData, rawPos, bpp) { | ||
for (let x = 0; x < byteWidth; x++) { | ||
let left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
let val = pxData[pxPos + x] - ((left + up) >> 1); | ||
for (var x = 0; x < byteWidth; x++) { | ||
var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
var val = pxData[pxPos + x] - ((left + up) >> 1); | ||
rawData[rawPos + x] = val; | ||
@@ -93,10 +81,8 @@ } | ||
function filterSumAvg(pxData, pxPos, byteWidth, bpp) { | ||
let sum = 0; | ||
for (let x = 0; x < byteWidth; x++) { | ||
let left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
let val = pxData[pxPos + x] - ((left + up) >> 1); | ||
var sum = 0; | ||
for (var x = 0; x < byteWidth; x++) { | ||
var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
var val = pxData[pxPos + x] - ((left + up) >> 1); | ||
sum += Math.abs(val); | ||
@@ -109,10 +95,9 @@ } | ||
function filterPaeth(pxData, pxPos, byteWidth, rawData, rawPos, bpp) { | ||
for (let x = 0; x < byteWidth; x++) { | ||
let left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
let upleft = | ||
pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0; | ||
let val = pxData[pxPos + x] - paethPredictor(left, up, upleft); | ||
for (var x = 0; x < byteWidth; x++) { | ||
var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
var upleft = pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0; | ||
var val = pxData[pxPos + x] - paethPredictor(left, up, upleft); | ||
rawData[rawPos + x] = val; | ||
@@ -123,10 +108,10 @@ } | ||
function filterSumPaeth(pxData, pxPos, byteWidth, bpp) { | ||
var sum = 0; | ||
for (var x = 0; x < byteWidth; x++) { | ||
let sum = 0; | ||
for (let x = 0; x < byteWidth; x++) { | ||
let left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
let upleft = | ||
pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0; | ||
let val = pxData[pxPos + x] - paethPredictor(left, up, upleft); | ||
var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
var upleft = pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0; | ||
var val = pxData[pxPos + x] - paethPredictor(left, up, upleft); | ||
sum += Math.abs(val); | ||
@@ -138,3 +123,3 @@ } | ||
var filters = { | ||
let filters = { | ||
0: filterNone, | ||
@@ -144,6 +129,6 @@ 1: filterSub, | ||
3: filterAvg, | ||
4: filterPaeth | ||
4: filterPaeth, | ||
}; | ||
var filterSums = { | ||
let filterSums = { | ||
0: filterSumNone, | ||
@@ -153,17 +138,14 @@ 1: filterSumSub, | ||
3: filterSumAvg, | ||
4: filterSumPaeth | ||
4: filterSumPaeth, | ||
}; | ||
module.exports = function(pxData, width, height, options, bpp) { | ||
var filterTypes; | ||
if (!('filterType' in options) || options.filterType === -1) { | ||
module.exports = function (pxData, width, height, options, bpp) { | ||
let filterTypes; | ||
if (!("filterType" in options) || options.filterType === -1) { | ||
filterTypes = [0, 1, 2, 3, 4]; | ||
} | ||
else if (typeof options.filterType === 'number') { | ||
} else if (typeof options.filterType === "number") { | ||
filterTypes = [options.filterType]; | ||
} else { | ||
throw new Error("unrecognised filter types"); | ||
} | ||
else { | ||
throw new Error('unrecognised filter types'); | ||
} | ||
@@ -173,17 +155,16 @@ if (options.bitDepth === 16) { | ||
} | ||
var byteWidth = width * bpp; | ||
var rawPos = 0; | ||
var pxPos = 0; | ||
var rawData = Buffer.alloc((byteWidth + 1) * height); | ||
let byteWidth = width * bpp; | ||
let rawPos = 0; | ||
let pxPos = 0; | ||
let rawData = Buffer.alloc((byteWidth + 1) * height); | ||
var sel = filterTypes[0]; | ||
let sel = filterTypes[0]; | ||
for (var y = 0; y < height; y++) { | ||
for (let y = 0; y < height; y++) { | ||
if (filterTypes.length > 1) { | ||
// find best filter for this line (with lowest sum of values) | ||
var min = Infinity; | ||
let min = Infinity; | ||
for (var i = 0; i < filterTypes.length; i++) { | ||
var sum = filterSums[filterTypes[i]](pxData, pxPos, byteWidth, bpp); | ||
for (let i = 0; i < filterTypes.length; i++) { | ||
let sum = filterSums[filterTypes[i]](pxData, pxPos, byteWidth, bpp); | ||
if (sum < min) { | ||
@@ -190,0 +171,0 @@ sel = filterTypes[i]; |
@@ -1,25 +0,24 @@ | ||
'use strict'; | ||
"use strict"; | ||
var util = require('util'); | ||
var ChunkStream = require('./chunkstream'); | ||
var Filter = require('./filter-parse'); | ||
let util = require("util"); | ||
let ChunkStream = require("./chunkstream"); | ||
let Filter = require("./filter-parse"); | ||
var FilterAsync = module.exports = function(bitmapInfo) { | ||
let FilterAsync = (module.exports = function (bitmapInfo) { | ||
ChunkStream.call(this); | ||
var buffers = []; | ||
var that = this; | ||
let buffers = []; | ||
let that = this; | ||
this._filter = new Filter(bitmapInfo, { | ||
read: this.read.bind(this), | ||
write: function(buffer) { | ||
write: function (buffer) { | ||
buffers.push(buffer); | ||
}, | ||
complete: function() { | ||
that.emit('complete', Buffer.concat(buffers)); | ||
} | ||
complete: function () { | ||
that.emit("complete", Buffer.concat(buffers)); | ||
}, | ||
}); | ||
this._filter.start(); | ||
}; | ||
}); | ||
util.inherits(FilterAsync, ChunkStream); |
@@ -1,18 +0,15 @@ | ||
'use strict'; | ||
"use strict"; | ||
var SyncReader = require('./sync-reader'); | ||
var Filter = require('./filter-parse'); | ||
let SyncReader = require("./sync-reader"); | ||
let Filter = require("./filter-parse"); | ||
exports.process = function(inBuffer, bitmapInfo) { | ||
var outBuffers = []; | ||
var reader = new SyncReader(inBuffer); | ||
var filter = new Filter(bitmapInfo, { | ||
exports.process = function (inBuffer, bitmapInfo) { | ||
let outBuffers = []; | ||
let reader = new SyncReader(inBuffer); | ||
let filter = new Filter(bitmapInfo, { | ||
read: reader.read.bind(reader), | ||
write: function(bufferPart) { | ||
write: function (bufferPart) { | ||
outBuffers.push(bufferPart); | ||
}, | ||
complete: function() { | ||
} | ||
complete: function () {}, | ||
}); | ||
@@ -24,2 +21,2 @@ | ||
return Buffer.concat(outBuffers); | ||
}; | ||
}; |
@@ -1,8 +0,8 @@ | ||
'use strict'; | ||
"use strict"; | ||
var interlaceUtils = require('./interlace'); | ||
var paethPredictor = require('./paeth-predictor'); | ||
let interlaceUtils = require("./interlace"); | ||
let paethPredictor = require("./paeth-predictor"); | ||
function getByteWidth(width, bpp, depth) { | ||
var byteWidth = width * bpp; | ||
let byteWidth = width * bpp; | ||
if (depth !== 8) { | ||
@@ -14,10 +14,9 @@ byteWidth = Math.ceil(byteWidth / (8 / depth)); | ||
var Filter = module.exports = function(bitmapInfo, dependencies) { | ||
let Filter = (module.exports = function (bitmapInfo, dependencies) { | ||
let width = bitmapInfo.width; | ||
let height = bitmapInfo.height; | ||
let interlace = bitmapInfo.interlace; | ||
let bpp = bitmapInfo.bpp; | ||
let depth = bitmapInfo.depth; | ||
var width = bitmapInfo.width; | ||
var height = bitmapInfo.height; | ||
var interlace = bitmapInfo.interlace; | ||
var bpp = bitmapInfo.bpp; | ||
var depth = bitmapInfo.depth; | ||
this.read = dependencies.read; | ||
@@ -30,16 +29,15 @@ this.write = dependencies.write; | ||
if (interlace) { | ||
var passes = interlaceUtils.getImagePasses(width, height); | ||
for (var i = 0; i < passes.length; i++) { | ||
let passes = interlaceUtils.getImagePasses(width, height); | ||
for (let i = 0; i < passes.length; i++) { | ||
this._images.push({ | ||
byteWidth: getByteWidth(passes[i].width, bpp, depth), | ||
height: passes[i].height, | ||
lineIndex: 0 | ||
lineIndex: 0, | ||
}); | ||
} | ||
} | ||
else { | ||
} else { | ||
this._images.push({ | ||
byteWidth: getByteWidth(width, bpp, depth), | ||
height: height, | ||
lineIndex: 0 | ||
lineIndex: 0, | ||
}); | ||
@@ -54,23 +52,27 @@ } | ||
this._xComparison = bpp; | ||
} | ||
else if (depth === 16) { | ||
} else if (depth === 16) { | ||
this._xComparison = bpp * 2; | ||
} | ||
else { | ||
} else { | ||
this._xComparison = 1; | ||
} | ||
}; | ||
}); | ||
Filter.prototype.start = function() { | ||
this.read(this._images[this._imageIndex].byteWidth + 1, this._reverseFilterLine.bind(this)); | ||
Filter.prototype.start = function () { | ||
this.read( | ||
this._images[this._imageIndex].byteWidth + 1, | ||
this._reverseFilterLine.bind(this) | ||
); | ||
}; | ||
Filter.prototype._unFilterType1 = function(rawData, unfilteredLine, byteWidth) { | ||
Filter.prototype._unFilterType1 = function ( | ||
rawData, | ||
unfilteredLine, | ||
byteWidth | ||
) { | ||
let xComparison = this._xComparison; | ||
let xBiggerThan = xComparison - 1; | ||
var xComparison = this._xComparison; | ||
var xBiggerThan = xComparison - 1; | ||
for (var x = 0; x < byteWidth; x++) { | ||
var rawByte = rawData[1 + x]; | ||
var f1Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0; | ||
for (let x = 0; x < byteWidth; x++) { | ||
let rawByte = rawData[1 + x]; | ||
let f1Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0; | ||
unfilteredLine[x] = rawByte + f1Left; | ||
@@ -80,9 +82,12 @@ } | ||
Filter.prototype._unFilterType2 = function(rawData, unfilteredLine, byteWidth) { | ||
Filter.prototype._unFilterType2 = function ( | ||
rawData, | ||
unfilteredLine, | ||
byteWidth | ||
) { | ||
let lastLine = this._lastLine; | ||
var lastLine = this._lastLine; | ||
for (var x = 0; x < byteWidth; x++) { | ||
var rawByte = rawData[1 + x]; | ||
var f2Up = lastLine ? lastLine[x] : 0; | ||
for (let x = 0; x < byteWidth; x++) { | ||
let rawByte = rawData[1 + x]; | ||
let f2Up = lastLine ? lastLine[x] : 0; | ||
unfilteredLine[x] = rawByte + f2Up; | ||
@@ -92,13 +97,16 @@ } | ||
Filter.prototype._unFilterType3 = function(rawData, unfilteredLine, byteWidth) { | ||
Filter.prototype._unFilterType3 = function ( | ||
rawData, | ||
unfilteredLine, | ||
byteWidth | ||
) { | ||
let xComparison = this._xComparison; | ||
let xBiggerThan = xComparison - 1; | ||
let lastLine = this._lastLine; | ||
var xComparison = this._xComparison; | ||
var xBiggerThan = xComparison - 1; | ||
var lastLine = this._lastLine; | ||
for (var x = 0; x < byteWidth; x++) { | ||
var rawByte = rawData[1 + x]; | ||
var f3Up = lastLine ? lastLine[x] : 0; | ||
var f3Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0; | ||
var f3Add = Math.floor((f3Left + f3Up) / 2); | ||
for (let x = 0; x < byteWidth; x++) { | ||
let rawByte = rawData[1 + x]; | ||
let f3Up = lastLine ? lastLine[x] : 0; | ||
let f3Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0; | ||
let f3Add = Math.floor((f3Left + f3Up) / 2); | ||
unfilteredLine[x] = rawByte + f3Add; | ||
@@ -108,14 +116,17 @@ } | ||
Filter.prototype._unFilterType4 = function(rawData, unfilteredLine, byteWidth) { | ||
Filter.prototype._unFilterType4 = function ( | ||
rawData, | ||
unfilteredLine, | ||
byteWidth | ||
) { | ||
let xComparison = this._xComparison; | ||
let xBiggerThan = xComparison - 1; | ||
let lastLine = this._lastLine; | ||
var xComparison = this._xComparison; | ||
var xBiggerThan = xComparison - 1; | ||
var lastLine = this._lastLine; | ||
for (var x = 0; x < byteWidth; x++) { | ||
var rawByte = rawData[1 + x]; | ||
var f4Up = lastLine ? lastLine[x] : 0; | ||
var f4Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0; | ||
var f4UpLeft = x > xBiggerThan && lastLine ? lastLine[x - xComparison] : 0; | ||
var f4Add = paethPredictor(f4Left, f4Up, f4UpLeft); | ||
for (let x = 0; x < byteWidth; x++) { | ||
let rawByte = rawData[1 + x]; | ||
let f4Up = lastLine ? lastLine[x] : 0; | ||
let f4Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0; | ||
let f4UpLeft = x > xBiggerThan && lastLine ? lastLine[x - xComparison] : 0; | ||
let f4Add = paethPredictor(f4Left, f4Up, f4UpLeft); | ||
unfilteredLine[x] = rawByte + f4Add; | ||
@@ -125,14 +136,11 @@ } | ||
Filter.prototype._reverseFilterLine = function(rawData) { | ||
Filter.prototype._reverseFilterLine = function (rawData) { | ||
let filter = rawData[0]; | ||
let unfilteredLine; | ||
let currentImage = this._images[this._imageIndex]; | ||
let byteWidth = currentImage.byteWidth; | ||
var filter = rawData[0]; | ||
var unfilteredLine; | ||
var currentImage = this._images[this._imageIndex]; | ||
var byteWidth = currentImage.byteWidth; | ||
if (filter === 0) { | ||
unfilteredLine = rawData.slice(1, byteWidth + 1); | ||
} | ||
else { | ||
} else { | ||
unfilteredLine = Buffer.alloc(byteWidth); | ||
@@ -154,3 +162,3 @@ | ||
default: | ||
throw new Error('Unrecognised filter type - ' + filter); | ||
throw new Error("Unrecognised filter type - " + filter); | ||
} | ||
@@ -166,4 +174,3 @@ } | ||
currentImage = this._images[this._imageIndex]; | ||
} | ||
else { | ||
} else { | ||
this._lastLine = unfilteredLine; | ||
@@ -175,4 +182,3 @@ } | ||
this.read(currentImage.byteWidth + 1, this._reverseFilterLine.bind(this)); | ||
} | ||
else { | ||
} else { | ||
this._lastLine = null; | ||
@@ -179,0 +185,0 @@ this.complete(); |
@@ -1,15 +0,15 @@ | ||
'use strict'; | ||
"use strict"; | ||
function dePalette(indata, outdata, width, height, palette) { | ||
var pxPos = 0; | ||
let pxPos = 0; | ||
// use values from palette | ||
for (var y = 0; y < height; y++) { | ||
for (var x = 0; x < width; x++) { | ||
var color = palette[indata[pxPos]]; | ||
for (let y = 0; y < height; y++) { | ||
for (let x = 0; x < width; x++) { | ||
let color = palette[indata[pxPos]]; | ||
if (!color) { | ||
throw new Error('index ' + indata[pxPos] + ' not in palette'); | ||
throw new Error("index " + indata[pxPos] + " not in palette"); | ||
} | ||
for (var i = 0; i < 4; i++) { | ||
for (let i = 0; i < 4; i++) { | ||
outdata[pxPos + i] = color[i]; | ||
@@ -23,6 +23,6 @@ } | ||
function replaceTransparentColor(indata, outdata, width, height, transColor) { | ||
var pxPos = 0; | ||
for (var y = 0; y < height; y++) { | ||
for (var x = 0; x < width; x++) { | ||
var makeTrans = false; | ||
let pxPos = 0; | ||
for (let y = 0; y < height; y++) { | ||
for (let x = 0; x < width; x++) { | ||
let makeTrans = false; | ||
@@ -33,8 +33,11 @@ if (transColor.length === 1) { | ||
} | ||
} | ||
else if (transColor[0] === indata[pxPos] && transColor[1] === indata[pxPos + 1] && transColor[2] === indata[pxPos + 2]) { | ||
} else if ( | ||
transColor[0] === indata[pxPos] && | ||
transColor[1] === indata[pxPos + 1] && | ||
transColor[2] === indata[pxPos + 2] | ||
) { | ||
makeTrans = true; | ||
} | ||
if (makeTrans) { | ||
for (var i = 0; i < 4; i++) { | ||
for (let i = 0; i < 4; i++) { | ||
outdata[pxPos + i] = 0; | ||
@@ -49,10 +52,12 @@ } | ||
function scaleDepth(indata, outdata, width, height, depth) { | ||
var maxOutSample = 255; | ||
var maxInSample = Math.pow(2, depth) - 1; | ||
var pxPos = 0; | ||
let maxOutSample = 255; | ||
let maxInSample = Math.pow(2, depth) - 1; | ||
let pxPos = 0; | ||
for (var y = 0; y < height; y++) { | ||
for (var x = 0; x < width; x++) { | ||
for (var i = 0; i < 4; i++) { | ||
outdata[pxPos + i] = Math.floor((indata[pxPos + i] * maxOutSample) / maxInSample + 0.5); | ||
for (let y = 0; y < height; y++) { | ||
for (let x = 0; x < width; x++) { | ||
for (let i = 0; i < 4; i++) { | ||
outdata[pxPos + i] = Math.floor( | ||
(indata[pxPos + i] * maxOutSample) / maxInSample + 0.5 | ||
); | ||
} | ||
@@ -64,17 +69,16 @@ pxPos += 4; | ||
module.exports = function(indata, imageData) { | ||
module.exports = function (indata, imageData) { | ||
let depth = imageData.depth; | ||
let width = imageData.width; | ||
let height = imageData.height; | ||
let colorType = imageData.colorType; | ||
let transColor = imageData.transColor; | ||
let palette = imageData.palette; | ||
var depth = imageData.depth; | ||
var width = imageData.width; | ||
var height = imageData.height; | ||
var colorType = imageData.colorType; | ||
var transColor = imageData.transColor; | ||
var palette = imageData.palette; | ||
let outdata = indata; // only different for 16 bits | ||
var outdata = indata; // only different for 16 bits | ||
if (colorType === 3) { // paletted | ||
if (colorType === 3) { | ||
// paletted | ||
dePalette(indata, outdata, width, height, palette); | ||
} | ||
else { | ||
} else { | ||
if (transColor) { | ||
@@ -81,0 +85,0 @@ replaceTransparentColor(indata, outdata, width, height, transColor); |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -14,57 +14,61 @@ // Adam 7 | ||
var imagePasses = [ | ||
{ // pass 1 - 1px | ||
let imagePasses = [ | ||
{ | ||
// pass 1 - 1px | ||
x: [0], | ||
y: [0] | ||
y: [0], | ||
}, | ||
{ // pass 2 - 1px | ||
{ | ||
// pass 2 - 1px | ||
x: [4], | ||
y: [0] | ||
y: [0], | ||
}, | ||
{ // pass 3 - 2px | ||
{ | ||
// pass 3 - 2px | ||
x: [0, 4], | ||
y: [4] | ||
y: [4], | ||
}, | ||
{ // pass 4 - 4px | ||
{ | ||
// pass 4 - 4px | ||
x: [2, 6], | ||
y: [0, 4] | ||
y: [0, 4], | ||
}, | ||
{ // pass 5 - 8px | ||
{ | ||
// pass 5 - 8px | ||
x: [0, 2, 4, 6], | ||
y: [2, 6] | ||
y: [2, 6], | ||
}, | ||
{ // pass 6 - 16px | ||
{ | ||
// pass 6 - 16px | ||
x: [1, 3, 5, 7], | ||
y: [0, 2, 4, 6] | ||
y: [0, 2, 4, 6], | ||
}, | ||
{ // pass 7 - 32px | ||
{ | ||
// pass 7 - 32px | ||
x: [0, 1, 2, 3, 4, 5, 6, 7], | ||
y: [1, 3, 5, 7] | ||
} | ||
y: [1, 3, 5, 7], | ||
}, | ||
]; | ||
exports.getImagePasses = function(width, height) { | ||
var images = []; | ||
var xLeftOver = width % 8; | ||
var yLeftOver = height % 8; | ||
var xRepeats = (width - xLeftOver) / 8; | ||
var yRepeats = (height - yLeftOver) / 8; | ||
for (var i = 0; i < imagePasses.length; i++) { | ||
var pass = imagePasses[i]; | ||
var passWidth = xRepeats * pass.x.length; | ||
var passHeight = yRepeats * pass.y.length; | ||
for (var j = 0; j < pass.x.length; j++) { | ||
exports.getImagePasses = function (width, height) { | ||
let images = []; | ||
let xLeftOver = width % 8; | ||
let yLeftOver = height % 8; | ||
let xRepeats = (width - xLeftOver) / 8; | ||
let yRepeats = (height - yLeftOver) / 8; | ||
for (let i = 0; i < imagePasses.length; i++) { | ||
let pass = imagePasses[i]; | ||
let passWidth = xRepeats * pass.x.length; | ||
let passHeight = yRepeats * pass.y.length; | ||
for (let j = 0; j < pass.x.length; j++) { | ||
if (pass.x[j] < xLeftOver) { | ||
passWidth++; | ||
} | ||
else { | ||
} else { | ||
break; | ||
} | ||
} | ||
for (j = 0; j < pass.y.length; j++) { | ||
for (let j = 0; j < pass.y.length; j++) { | ||
if (pass.y[j] < yLeftOver) { | ||
passHeight++; | ||
} | ||
else { | ||
} else { | ||
break; | ||
@@ -80,10 +84,14 @@ } | ||
exports.getInterlaceIterator = function(width) { | ||
return function(x, y, pass) { | ||
var outerXLeftOver = x % imagePasses[pass].x.length; | ||
var outerX = (((x - outerXLeftOver) / imagePasses[pass].x.length) * 8) + imagePasses[pass].x[outerXLeftOver]; | ||
var outerYLeftOver = y % imagePasses[pass].y.length; | ||
var outerY = (((y - outerYLeftOver) / imagePasses[pass].y.length) * 8) + imagePasses[pass].y[outerYLeftOver]; | ||
return (outerX * 4) + (outerY * width * 4); | ||
exports.getInterlaceIterator = function (width) { | ||
return function (x, y, pass) { | ||
let outerXLeftOver = x % imagePasses[pass].x.length; | ||
let outerX = | ||
((x - outerXLeftOver) / imagePasses[pass].x.length) * 8 + | ||
imagePasses[pass].x[outerXLeftOver]; | ||
let outerYLeftOver = y % imagePasses[pass].y.length; | ||
let outerY = | ||
((y - outerYLeftOver) / imagePasses[pass].y.length) * 8 + | ||
imagePasses[pass].y[outerYLeftOver]; | ||
return outerX * 4 + outerY * width * 4; | ||
}; | ||
}; | ||
}; |
@@ -1,12 +0,12 @@ | ||
'use strict'; | ||
"use strict"; | ||
var util = require('util'); | ||
var Stream = require('stream'); | ||
var constants = require('./constants'); | ||
var Packer = require('./packer'); | ||
let util = require("util"); | ||
let Stream = require("stream"); | ||
let constants = require("./constants"); | ||
let Packer = require("./packer"); | ||
var PackerAsync = module.exports = function(opt) { | ||
let PackerAsync = (module.exports = function (opt) { | ||
Stream.call(this); | ||
var options = opt || {}; | ||
let options = opt || {}; | ||
@@ -17,30 +17,35 @@ this._packer = new Packer(options); | ||
this.readable = true; | ||
}; | ||
}); | ||
util.inherits(PackerAsync, Stream); | ||
PackerAsync.prototype.pack = function(data, width, height, gamma) { | ||
PackerAsync.prototype.pack = function (data, width, height, gamma) { | ||
// Signature | ||
this.emit('data', Buffer.from(constants.PNG_SIGNATURE)); | ||
this.emit('data', this._packer.packIHDR(width, height)); | ||
this.emit("data", Buffer.from(constants.PNG_SIGNATURE)); | ||
this.emit("data", this._packer.packIHDR(width, height)); | ||
if (gamma) { | ||
this.emit('data', this._packer.packGAMA(gamma)); | ||
this.emit("data", this._packer.packGAMA(gamma)); | ||
} | ||
var filteredData = this._packer.filterData(data, width, height); | ||
let filteredData = this._packer.filterData(data, width, height); | ||
// compress it | ||
this._deflate.on('error', this.emit.bind(this, 'error')); | ||
this._deflate.on("error", this.emit.bind(this, "error")); | ||
this._deflate.on('data', function(compressedData) { | ||
this.emit('data', this._packer.packIDAT(compressedData)); | ||
}.bind(this)); | ||
this._deflate.on( | ||
"data", | ||
function (compressedData) { | ||
this.emit("data", this._packer.packIDAT(compressedData)); | ||
}.bind(this) | ||
); | ||
this._deflate.on('end', function() { | ||
this.emit('data', this._packer.packIEND()); | ||
this.emit('end'); | ||
}.bind(this)); | ||
this._deflate.on( | ||
"end", | ||
function () { | ||
this.emit("data", this._packer.packIEND()); | ||
this.emit("end"); | ||
}.bind(this) | ||
); | ||
this._deflate.end(filteredData); | ||
}; |
@@ -1,22 +0,23 @@ | ||
'use strict'; | ||
"use strict"; | ||
var hasSyncZlib = true; | ||
var zlib = require('zlib'); | ||
let hasSyncZlib = true; | ||
let zlib = require("zlib"); | ||
if (!zlib.deflateSync) { | ||
hasSyncZlib = false; | ||
} | ||
var constants = require('./constants'); | ||
var Packer = require('./packer'); | ||
let constants = require("./constants"); | ||
let Packer = require("./packer"); | ||
module.exports = function(metaData, opt) { | ||
module.exports = function (metaData, opt) { | ||
if (!hasSyncZlib) { | ||
throw new Error('To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0'); | ||
throw new Error( | ||
"To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0" | ||
); | ||
} | ||
var options = opt || {}; | ||
let options = opt || {}; | ||
var packer = new Packer(options); | ||
let packer = new Packer(options); | ||
var chunks = []; | ||
let chunks = []; | ||
@@ -33,10 +34,17 @@ // Signature | ||
var filteredData = packer.filterData(metaData.data, metaData.width, metaData.height); | ||
let filteredData = packer.filterData( | ||
metaData.data, | ||
metaData.width, | ||
metaData.height | ||
); | ||
// compress it | ||
var compressedData = zlib.deflateSync(filteredData, packer.getDeflateOptions()); | ||
let compressedData = zlib.deflateSync( | ||
filteredData, | ||
packer.getDeflateOptions() | ||
); | ||
filteredData = null; | ||
if (!compressedData || !compressedData.length) { | ||
throw new Error('bad png - invalid compressed data response'); | ||
throw new Error("bad png - invalid compressed data response"); | ||
} | ||
@@ -43,0 +51,0 @@ chunks.push(packer.packIDAT(compressedData)); |
@@ -1,70 +0,90 @@ | ||
'use strict'; | ||
"use strict"; | ||
var constants = require('./constants'); | ||
var CrcStream = require('./crc'); | ||
var bitPacker = require('./bitpacker'); | ||
var filter = require('./filter-pack'); | ||
var zlib = require('zlib'); | ||
let constants = require("./constants"); | ||
let CrcStream = require("./crc"); | ||
let bitPacker = require("./bitpacker"); | ||
let filter = require("./filter-pack"); | ||
let zlib = require("zlib"); | ||
var Packer = module.exports = function(options) { | ||
let Packer = (module.exports = function (options) { | ||
this._options = options; | ||
options.deflateChunkSize = options.deflateChunkSize || 32 * 1024; | ||
options.deflateLevel = options.deflateLevel != null ? options.deflateLevel : 9; | ||
options.deflateStrategy = options.deflateStrategy != null ? options.deflateStrategy : 3; | ||
options.inputHasAlpha = options.inputHasAlpha != null ? options.inputHasAlpha : true; | ||
options.deflateLevel = | ||
options.deflateLevel != null ? options.deflateLevel : 9; | ||
options.deflateStrategy = | ||
options.deflateStrategy != null ? options.deflateStrategy : 3; | ||
options.inputHasAlpha = | ||
options.inputHasAlpha != null ? options.inputHasAlpha : true; | ||
options.deflateFactory = options.deflateFactory || zlib.createDeflate; | ||
options.bitDepth = options.bitDepth || 8; | ||
// This is outputColorType | ||
options.colorType = (typeof options.colorType === 'number') ? options.colorType : constants.COLORTYPE_COLOR_ALPHA; | ||
options.inputColorType = (typeof options.inputColorType === 'number') ? options.inputColorType : constants.COLORTYPE_COLOR_ALPHA; | ||
options.colorType = | ||
typeof options.colorType === "number" | ||
? options.colorType | ||
: constants.COLORTYPE_COLOR_ALPHA; | ||
options.inputColorType = | ||
typeof options.inputColorType === "number" | ||
? options.inputColorType | ||
: constants.COLORTYPE_COLOR_ALPHA; | ||
if ([ | ||
constants.COLORTYPE_GRAYSCALE, | ||
constants.COLORTYPE_COLOR, | ||
constants.COLORTYPE_COLOR_ALPHA, | ||
constants.COLORTYPE_ALPHA | ||
].indexOf(options.colorType) === -1) { | ||
throw new Error('option color type:' + options.colorType + ' is not supported at present'); | ||
if ( | ||
[ | ||
constants.COLORTYPE_GRAYSCALE, | ||
constants.COLORTYPE_COLOR, | ||
constants.COLORTYPE_COLOR_ALPHA, | ||
constants.COLORTYPE_ALPHA, | ||
].indexOf(options.colorType) === -1 | ||
) { | ||
throw new Error( | ||
"option color type:" + options.colorType + " is not supported at present" | ||
); | ||
} | ||
if ([ | ||
constants.COLORTYPE_GRAYSCALE, | ||
constants.COLORTYPE_COLOR, | ||
constants.COLORTYPE_COLOR_ALPHA, | ||
constants.COLORTYPE_ALPHA | ||
].indexOf(options.inputColorType) === -1) { | ||
throw new Error('option input color type:' + options.inputColorType + ' is not supported at present'); | ||
if ( | ||
[ | ||
constants.COLORTYPE_GRAYSCALE, | ||
constants.COLORTYPE_COLOR, | ||
constants.COLORTYPE_COLOR_ALPHA, | ||
constants.COLORTYPE_ALPHA, | ||
].indexOf(options.inputColorType) === -1 | ||
) { | ||
throw new Error( | ||
"option input color type:" + | ||
options.inputColorType + | ||
" is not supported at present" | ||
); | ||
} | ||
if (options.bitDepth !== 8 && options.bitDepth !== 16) { | ||
throw new Error('option bit depth:' + options.bitDepth + ' is not supported at present'); | ||
throw new Error( | ||
"option bit depth:" + options.bitDepth + " is not supported at present" | ||
); | ||
} | ||
}; | ||
}); | ||
Packer.prototype.getDeflateOptions = function() { | ||
Packer.prototype.getDeflateOptions = function () { | ||
return { | ||
chunkSize: this._options.deflateChunkSize, | ||
level: this._options.deflateLevel, | ||
strategy: this._options.deflateStrategy | ||
strategy: this._options.deflateStrategy, | ||
}; | ||
}; | ||
Packer.prototype.createDeflate = function() { | ||
Packer.prototype.createDeflate = function () { | ||
return this._options.deflateFactory(this.getDeflateOptions()); | ||
}; | ||
Packer.prototype.filterData = function(data, width, height) { | ||
Packer.prototype.filterData = function (data, width, height) { | ||
// convert to correct format for filtering (e.g. right bpp and bit depth) | ||
var packedData = bitPacker(data, width, height, this._options); | ||
let packedData = bitPacker(data, width, height, this._options); | ||
// filter pixel data | ||
var bpp = constants.COLORTYPE_TO_BPP_MAP[this._options.colorType]; | ||
var filteredData = filter(packedData, width, height, this._options, bpp); | ||
let bpp = constants.COLORTYPE_TO_BPP_MAP[this._options.colorType]; | ||
let filteredData = filter(packedData, width, height, this._options, bpp); | ||
return filteredData; | ||
}; | ||
Packer.prototype._packChunk = function(type, data) { | ||
Packer.prototype._packChunk = function (type, data) { | ||
let len = data ? data.length : 0; | ||
let buf = Buffer.alloc(len + 12); | ||
var len = (data ? data.length : 0); | ||
var buf = Buffer.alloc(len + 12); | ||
buf.writeUInt32BE(len, 0); | ||
@@ -77,8 +97,11 @@ buf.writeUInt32BE(type, 4); | ||
buf.writeInt32BE(CrcStream.crc32(buf.slice(4, buf.length - 4)), buf.length - 4); | ||
buf.writeInt32BE( | ||
CrcStream.crc32(buf.slice(4, buf.length - 4)), | ||
buf.length - 4 | ||
); | ||
return buf; | ||
}; | ||
Packer.prototype.packGAMA = function(gamma) { | ||
var buf = Buffer.alloc(4); | ||
Packer.prototype.packGAMA = function (gamma) { | ||
let buf = Buffer.alloc(4); | ||
buf.writeUInt32BE(Math.floor(gamma * constants.GAMMA_DIVISION), 0); | ||
@@ -88,5 +111,4 @@ return this._packChunk(constants.TYPE_gAMA, buf); | ||
Packer.prototype.packIHDR = function(width, height) { | ||
var buf = Buffer.alloc(13); | ||
Packer.prototype.packIHDR = function (width, height) { | ||
let buf = Buffer.alloc(13); | ||
buf.writeUInt32BE(width, 0); | ||
@@ -103,8 +125,8 @@ buf.writeUInt32BE(height, 4); | ||
Packer.prototype.packIDAT = function(data) { | ||
Packer.prototype.packIDAT = function (data) { | ||
return this._packChunk(constants.TYPE_IDAT, data); | ||
}; | ||
Packer.prototype.packIEND = function() { | ||
Packer.prototype.packIEND = function () { | ||
return this._packChunk(constants.TYPE_IEND, null); | ||
}; |
@@ -1,10 +0,9 @@ | ||
'use strict'; | ||
"use strict"; | ||
module.exports = function paethPredictor(left, above, upLeft) { | ||
let paeth = left + above - upLeft; | ||
let pLeft = Math.abs(paeth - left); | ||
let pAbove = Math.abs(paeth - above); | ||
let pUpLeft = Math.abs(paeth - upLeft); | ||
var paeth = left + above - upLeft; | ||
var pLeft = Math.abs(paeth - left); | ||
var pAbove = Math.abs(paeth - above); | ||
var pUpLeft = Math.abs(paeth - upLeft); | ||
if (pLeft <= pAbove && pLeft <= pUpLeft) { | ||
@@ -17,2 +16,2 @@ return left; | ||
return upLeft; | ||
}; | ||
}; |
@@ -1,12 +0,12 @@ | ||
'use strict'; | ||
"use strict"; | ||
var util = require('util'); | ||
var zlib = require('zlib'); | ||
var ChunkStream = require('./chunkstream'); | ||
var FilterAsync = require('./filter-parse-async'); | ||
var Parser = require('./parser'); | ||
var bitmapper = require('./bitmapper'); | ||
var formatNormaliser = require('./format-normaliser'); | ||
let util = require("util"); | ||
let zlib = require("zlib"); | ||
let ChunkStream = require("./chunkstream"); | ||
let FilterAsync = require("./filter-parse-async"); | ||
let Parser = require("./parser"); | ||
let bitmapper = require("./bitmapper"); | ||
let formatNormaliser = require("./format-normaliser"); | ||
var ParserAsync = module.exports = function(options) { | ||
let ParserAsync = (module.exports = function (options) { | ||
ChunkStream.call(this); | ||
@@ -18,3 +18,3 @@ | ||
metadata: this._handleMetaData.bind(this), | ||
gamma: this.emit.bind(this, 'gamma'), | ||
gamma: this.emit.bind(this, "gamma"), | ||
palette: this._handlePalette.bind(this), | ||
@@ -25,3 +25,3 @@ transColor: this._handleTransColor.bind(this), | ||
simpleTransparency: this._simpleTransparency.bind(this), | ||
headersFinished: this._headersFinished.bind(this) | ||
headersFinished: this._headersFinished.bind(this), | ||
}); | ||
@@ -32,10 +32,8 @@ this._options = options; | ||
this._parser.start(); | ||
}; | ||
}); | ||
util.inherits(ParserAsync, ChunkStream); | ||
ParserAsync.prototype._handleError = function (err) { | ||
this.emit("error", err); | ||
ParserAsync.prototype._handleError = function(err) { | ||
this.emit('error', err); | ||
this.writable = false; | ||
@@ -54,3 +52,3 @@ | ||
// it's destroy()'ed. | ||
this._filter.on('error', function() {}); | ||
this._filter.on("error", function () {}); | ||
} | ||
@@ -61,3 +59,3 @@ | ||
ParserAsync.prototype._inflateData = function(data) { | ||
ParserAsync.prototype._inflateData = function (data) { | ||
if (!this._inflate) { | ||
@@ -67,17 +65,22 @@ if (this._bitmapInfo.interlace) { | ||
this._inflate.on('error', this.emit.bind(this, 'error')); | ||
this._filter.on('complete', this._complete.bind(this)); | ||
this._inflate.on("error", this.emit.bind(this, "error")); | ||
this._filter.on("complete", this._complete.bind(this)); | ||
this._inflate.pipe(this._filter); | ||
} | ||
else { | ||
var rowSize = ((this._bitmapInfo.width * this._bitmapInfo.bpp * this._bitmapInfo.depth + 7) >> 3) + 1; | ||
var imageSize = rowSize * this._bitmapInfo.height; | ||
var chunkSize = Math.max(imageSize, zlib.Z_MIN_CHUNK); | ||
} else { | ||
let rowSize = | ||
((this._bitmapInfo.width * | ||
this._bitmapInfo.bpp * | ||
this._bitmapInfo.depth + | ||
7) >> | ||
3) + | ||
1; | ||
let imageSize = rowSize * this._bitmapInfo.height; | ||
let chunkSize = Math.max(imageSize, zlib.Z_MIN_CHUNK); | ||
this._inflate = zlib.createInflate({ chunkSize: chunkSize }); | ||
var leftToInflate = imageSize; | ||
let leftToInflate = imageSize; | ||
var emitError = this.emit.bind(this, 'error'); | ||
this._inflate.on('error', function(err) { | ||
let emitError = this.emit.bind(this, "error"); | ||
this._inflate.on("error", function (err) { | ||
if (!leftToInflate) { | ||
@@ -89,6 +92,6 @@ return; | ||
}); | ||
this._filter.on('complete', this._complete.bind(this)); | ||
this._filter.on("complete", this._complete.bind(this)); | ||
var filterWrite = this._filter.write.bind(this._filter); | ||
this._inflate.on('data', function(chunk) { | ||
let filterWrite = this._filter.write.bind(this._filter); | ||
this._inflate.on("data", function (chunk) { | ||
if (!leftToInflate) { | ||
@@ -107,3 +110,3 @@ return; | ||
this._inflate.on('end', this._filter.end.bind(this._filter)); | ||
this._inflate.on("end", this._filter.end.bind(this._filter)); | ||
} | ||
@@ -114,3 +117,3 @@ } | ||
ParserAsync.prototype._handleMetaData = function(metaData) { | ||
ParserAsync.prototype._handleMetaData = function (metaData) { | ||
this._metaData = metaData; | ||
@@ -122,21 +125,21 @@ this._bitmapInfo = Object.create(metaData); | ||
ParserAsync.prototype._handleTransColor = function(transColor) { | ||
ParserAsync.prototype._handleTransColor = function (transColor) { | ||
this._bitmapInfo.transColor = transColor; | ||
}; | ||
ParserAsync.prototype._handlePalette = function(palette) { | ||
ParserAsync.prototype._handlePalette = function (palette) { | ||
this._bitmapInfo.palette = palette; | ||
}; | ||
ParserAsync.prototype._simpleTransparency = function() { | ||
ParserAsync.prototype._simpleTransparency = function () { | ||
this._metaData.alpha = true; | ||
}; | ||
ParserAsync.prototype._headersFinished = function() { | ||
ParserAsync.prototype._headersFinished = function () { | ||
// Up until this point, we don't know if we have a tRNS chunk (alpha) | ||
// so we can't emit metadata any earlier | ||
this.emit('metadata', this._metaData); | ||
this.emit("metadata", this._metaData); | ||
}; | ||
ParserAsync.prototype._finished = function() { | ||
ParserAsync.prototype._finished = function () { | ||
if (this.errord) { | ||
@@ -147,5 +150,4 @@ return; | ||
if (!this._inflate) { | ||
this.emit('error', 'No Inflate block'); | ||
} | ||
else { | ||
this.emit("error", "No Inflate block"); | ||
} else { | ||
// no more data to inflate | ||
@@ -156,4 +158,3 @@ this._inflate.end(); | ||
ParserAsync.prototype._complete = function(filteredData) { | ||
ParserAsync.prototype._complete = function (filteredData) { | ||
if (this.errord) { | ||
@@ -163,9 +164,10 @@ return; | ||
let normalisedBitmapData; | ||
try { | ||
var bitmapData = bitmapper.dataToBitMap(filteredData, this._bitmapInfo); | ||
let bitmapData = bitmapper.dataToBitMap(filteredData, this._bitmapInfo); | ||
var normalisedBitmapData = formatNormaliser(bitmapData, this._bitmapInfo); | ||
normalisedBitmapData = formatNormaliser(bitmapData, this._bitmapInfo); | ||
bitmapData = null; | ||
} | ||
catch (ex) { | ||
} catch (ex) { | ||
this._handleError(ex); | ||
@@ -175,3 +177,3 @@ return; | ||
this.emit('parsed', normalisedBitmapData); | ||
this.emit("parsed", normalisedBitmapData); | ||
}; |
@@ -1,23 +0,23 @@ | ||
'use strict'; | ||
"use strict"; | ||
var hasSyncZlib = true; | ||
var zlib = require('zlib'); | ||
var inflateSync = require('./sync-inflate'); | ||
let hasSyncZlib = true; | ||
let zlib = require("zlib"); | ||
let inflateSync = require("./sync-inflate"); | ||
if (!zlib.deflateSync) { | ||
hasSyncZlib = false; | ||
} | ||
var SyncReader = require('./sync-reader'); | ||
var FilterSync = require('./filter-parse-sync'); | ||
var Parser = require('./parser'); | ||
var bitmapper = require('./bitmapper'); | ||
var formatNormaliser = require('./format-normaliser'); | ||
let SyncReader = require("./sync-reader"); | ||
let FilterSync = require("./filter-parse-sync"); | ||
let Parser = require("./parser"); | ||
let bitmapper = require("./bitmapper"); | ||
let formatNormaliser = require("./format-normaliser"); | ||
module.exports = function(buffer, options) { | ||
module.exports = function (buffer, options) { | ||
if (!hasSyncZlib) { | ||
throw new Error('To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0'); | ||
throw new Error( | ||
"To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0" | ||
); | ||
} | ||
var err; | ||
let err; | ||
function handleError(_err_) { | ||
@@ -27,3 +27,3 @@ err = _err_; | ||
var metaData; | ||
let metaData; | ||
function handleMetaData(_metaData_) { | ||
@@ -45,3 +45,3 @@ metaData = _metaData_; | ||
var gamma; | ||
let gamma; | ||
function handleGamma(_gamma_) { | ||
@@ -51,3 +51,3 @@ gamma = _gamma_; | ||
var inflateDataList = []; | ||
let inflateDataList = []; | ||
function handleInflateData(inflatedData) { | ||
@@ -57,5 +57,5 @@ inflateDataList.push(inflatedData); | ||
var reader = new SyncReader(buffer); | ||
let reader = new SyncReader(buffer); | ||
var parser = new Parser(options, { | ||
let parser = new Parser(options, { | ||
read: reader.read.bind(reader), | ||
@@ -68,3 +68,3 @@ error: handleError, | ||
inflateData: handleInflateData, | ||
simpleTransparency: handleSimpleTransparency | ||
simpleTransparency: handleSimpleTransparency, | ||
}); | ||
@@ -80,27 +80,30 @@ | ||
//join together the inflate datas | ||
var inflateData = Buffer.concat(inflateDataList); | ||
let inflateData = Buffer.concat(inflateDataList); | ||
inflateDataList.length = 0; | ||
var inflatedData; | ||
let inflatedData; | ||
if (metaData.interlace) { | ||
inflatedData = zlib.inflateSync(inflateData); | ||
} else { | ||
let rowSize = | ||
((metaData.width * metaData.bpp * metaData.depth + 7) >> 3) + 1; | ||
let imageSize = rowSize * metaData.height; | ||
inflatedData = inflateSync(inflateData, { | ||
chunkSize: imageSize, | ||
maxLength: imageSize, | ||
}); | ||
} | ||
else { | ||
var rowSize = ((metaData.width * metaData.bpp * metaData.depth + 7) >> 3) + 1; | ||
var imageSize = rowSize * metaData.height; | ||
inflatedData = inflateSync(inflateData, { chunkSize: imageSize, maxLength: imageSize }); | ||
} | ||
inflateData = null; | ||
if (!inflatedData || !inflatedData.length) { | ||
throw new Error('bad png - invalid inflate data response'); | ||
throw new Error("bad png - invalid inflate data response"); | ||
} | ||
var unfilteredData = FilterSync.process(inflatedData, metaData); | ||
let unfilteredData = FilterSync.process(inflatedData, metaData); | ||
inflateData = null; | ||
var bitmapData = bitmapper.dataToBitMap(unfilteredData, metaData); | ||
let bitmapData = bitmapper.dataToBitMap(unfilteredData, metaData); | ||
unfilteredData = null; | ||
var normalisedBitmapData = formatNormaliser(bitmapData, metaData); | ||
let normalisedBitmapData = formatNormaliser(bitmapData, metaData); | ||
@@ -107,0 +110,0 @@ metaData.data = normalisedBitmapData; |
@@ -1,9 +0,7 @@ | ||
'use strict'; | ||
"use strict"; | ||
var constants = require('./constants'); | ||
var CrcCalculator = require('./crc'); | ||
let constants = require("./constants"); | ||
let CrcCalculator = require("./crc"); | ||
var Parser = module.exports = function(options, dependencies) { | ||
let Parser = (module.exports = function (options, dependencies) { | ||
this._options = options; | ||
@@ -38,18 +36,15 @@ options.checkCRC = options.checkCRC !== false; | ||
this.simpleTransparency = dependencies.simpleTransparency; | ||
this.headersFinished = dependencies.headersFinished || function() {}; | ||
}; | ||
this.headersFinished = dependencies.headersFinished || function () {}; | ||
}); | ||
Parser.prototype.start = function() { | ||
this.read(constants.PNG_SIGNATURE.length, | ||
this._parseSignature.bind(this) | ||
); | ||
Parser.prototype.start = function () { | ||
this.read(constants.PNG_SIGNATURE.length, this._parseSignature.bind(this)); | ||
}; | ||
Parser.prototype._parseSignature = function(data) { | ||
Parser.prototype._parseSignature = function (data) { | ||
let signature = constants.PNG_SIGNATURE; | ||
var signature = constants.PNG_SIGNATURE; | ||
for (var i = 0; i < signature.length; i++) { | ||
for (let i = 0; i < signature.length; i++) { | ||
if (data[i] !== signature[i]) { | ||
this.error(new Error('Invalid file signature')); | ||
this.error(new Error("Invalid file signature")); | ||
return; | ||
@@ -61,11 +56,10 @@ } | ||
Parser.prototype._parseChunkBegin = function(data) { | ||
Parser.prototype._parseChunkBegin = function (data) { | ||
// chunk content length | ||
var length = data.readUInt32BE(0); | ||
let length = data.readUInt32BE(0); | ||
// chunk type | ||
var type = data.readUInt32BE(4); | ||
var name = ''; | ||
for (var i = 4; i < 8; i++) { | ||
let type = data.readUInt32BE(4); | ||
let name = ""; | ||
for (let i = 4; i < 8; i++) { | ||
name += String.fromCharCode(data[i]); | ||
@@ -77,3 +71,3 @@ } | ||
// chunk flags | ||
var ancillary = Boolean(data[4] & 0x20); // or critical | ||
let ancillary = Boolean(data[4] & 0x20); // or critical | ||
// priv = Boolean(data[5] & 0x20), // or public | ||
@@ -83,3 +77,3 @@ // safeToCopy = Boolean(data[7] & 0x20); // or unsafe | ||
if (!this._hasIHDR && type !== constants.TYPE_IHDR) { | ||
this.error(new Error('Expected IHDR on beggining')); | ||
this.error(new Error("Expected IHDR on beggining")); | ||
return; | ||
@@ -96,3 +90,3 @@ } | ||
if (!ancillary) { | ||
this.error(new Error('Unsupported critical chunk type ' + name)); | ||
this.error(new Error("Unsupported critical chunk type " + name)); | ||
return; | ||
@@ -104,18 +98,17 @@ } | ||
Parser.prototype._skipChunk = function(/*data*/) { | ||
Parser.prototype._skipChunk = function (/*data*/) { | ||
this.read(8, this._parseChunkBegin.bind(this)); | ||
}; | ||
Parser.prototype._handleChunkEnd = function() { | ||
Parser.prototype._handleChunkEnd = function () { | ||
this.read(4, this._parseChunkEnd.bind(this)); | ||
}; | ||
Parser.prototype._parseChunkEnd = function(data) { | ||
Parser.prototype._parseChunkEnd = function (data) { | ||
let fileCrc = data.readInt32BE(0); | ||
let calcCrc = this._crc.crc32(); | ||
var fileCrc = data.readInt32BE(0); | ||
var calcCrc = this._crc.crc32(); | ||
// check CRC | ||
if (this._options.checkCRC && calcCrc !== fileCrc) { | ||
this.error(new Error('Crc error - ' + fileCrc + ' - ' + calcCrc)); | ||
this.error(new Error("Crc error - " + fileCrc + " - " + calcCrc)); | ||
return; | ||
@@ -129,16 +122,15 @@ } | ||
Parser.prototype._handleIHDR = function(length) { | ||
Parser.prototype._handleIHDR = function (length) { | ||
this.read(length, this._parseIHDR.bind(this)); | ||
}; | ||
Parser.prototype._parseIHDR = function(data) { | ||
Parser.prototype._parseIHDR = function (data) { | ||
this._crc.write(data); | ||
var width = data.readUInt32BE(0); | ||
var height = data.readUInt32BE(4); | ||
var depth = data[8]; | ||
var colorType = data[9]; // bits: 1 palette, 2 color, 4 alpha | ||
var compr = data[10]; | ||
var filter = data[11]; | ||
var interlace = data[12]; | ||
let width = data.readUInt32BE(0); | ||
let height = data.readUInt32BE(4); | ||
let depth = data[8]; | ||
let colorType = data[9]; // bits: 1 palette, 2 color, 4 alpha | ||
let compr = data[10]; | ||
let filter = data[11]; | ||
let interlace = data[12]; | ||
@@ -150,20 +142,26 @@ // console.log(' width', width, 'height', height, | ||
if (depth !== 8 && depth !== 4 && depth !== 2 && depth !== 1 && depth !== 16) { | ||
this.error(new Error('Unsupported bit depth ' + depth)); | ||
if ( | ||
depth !== 8 && | ||
depth !== 4 && | ||
depth !== 2 && | ||
depth !== 1 && | ||
depth !== 16 | ||
) { | ||
this.error(new Error("Unsupported bit depth " + depth)); | ||
return; | ||
} | ||
if (!(colorType in constants.COLORTYPE_TO_BPP_MAP)) { | ||
this.error(new Error('Unsupported color type')); | ||
this.error(new Error("Unsupported color type")); | ||
return; | ||
} | ||
if (compr !== 0) { | ||
this.error(new Error('Unsupported compression method')); | ||
this.error(new Error("Unsupported compression method")); | ||
return; | ||
} | ||
if (filter !== 0) { | ||
this.error(new Error('Unsupported filter method')); | ||
this.error(new Error("Unsupported filter method")); | ||
return; | ||
} | ||
if (interlace !== 0 && interlace !== 1) { | ||
this.error(new Error('Unsupported interlace method')); | ||
this.error(new Error("Unsupported interlace method")); | ||
return; | ||
@@ -174,3 +172,3 @@ } | ||
var bpp = constants.COLORTYPE_TO_BPP_MAP[this._colorType]; | ||
let bpp = constants.COLORTYPE_TO_BPP_MAP[this._colorType]; | ||
@@ -188,3 +186,3 @@ this._hasIHDR = true; | ||
bpp: bpp, | ||
colorType: colorType | ||
colorType: colorType, | ||
}); | ||
@@ -195,20 +193,13 @@ | ||
Parser.prototype._handlePLTE = function(length) { | ||
Parser.prototype._handlePLTE = function (length) { | ||
this.read(length, this._parsePLTE.bind(this)); | ||
}; | ||
Parser.prototype._parsePLTE = function(data) { | ||
Parser.prototype._parsePLTE = function (data) { | ||
this._crc.write(data); | ||
var entries = Math.floor(data.length / 3); | ||
let entries = Math.floor(data.length / 3); | ||
// console.log('Palette:', entries); | ||
for (var i = 0; i < entries; i++) { | ||
this._palette.push([ | ||
data[i * 3], | ||
data[i * 3 + 1], | ||
data[i * 3 + 2], | ||
0xff | ||
]); | ||
for (let i = 0; i < entries; i++) { | ||
this._palette.push([data[i * 3], data[i * 3 + 1], data[i * 3 + 2], 0xff]); | ||
} | ||
@@ -221,8 +212,7 @@ | ||
Parser.prototype._handleTRNS = function(length) { | ||
Parser.prototype._handleTRNS = function (length) { | ||
this.simpleTransparency(); | ||
this.read(length, this._parseTRNS.bind(this)); | ||
}; | ||
Parser.prototype._parseTRNS = function(data) { | ||
Parser.prototype._parseTRNS = function (data) { | ||
this._crc.write(data); | ||
@@ -233,10 +223,10 @@ | ||
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; | ||
} | ||
if (data.length > this._palette.length) { | ||
this.error(new Error('More transparent colors than palette size')); | ||
this.error(new Error("More transparent colors than palette size")); | ||
return; | ||
} | ||
for (var i = 0; i < data.length; i++) { | ||
for (let i = 0; i < data.length; i++) { | ||
this._palette[i][3] = data[i]; | ||
@@ -254,3 +244,7 @@ } | ||
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), | ||
]); | ||
} | ||
@@ -261,7 +255,6 @@ | ||
Parser.prototype._handleGAMA = function(length) { | ||
Parser.prototype._handleGAMA = function (length) { | ||
this.read(length, this._parseGAMA.bind(this)); | ||
}; | ||
Parser.prototype._parseGAMA = function(data) { | ||
Parser.prototype._parseGAMA = function (data) { | ||
this._crc.write(data); | ||
@@ -273,3 +266,3 @@ this.gamma(data.readUInt32BE(0) / constants.GAMMA_DIVISION); | ||
Parser.prototype._handleIDAT = function(length) { | ||
Parser.prototype._handleIDAT = function (length) { | ||
if (!this._emittedHeadersFinished) { | ||
@@ -281,17 +274,18 @@ this._emittedHeadersFinished = true; | ||
}; | ||
Parser.prototype._parseIDAT = function(length, data) { | ||
Parser.prototype._parseIDAT = function (length, data) { | ||
this._crc.write(data); | ||
if (this._colorType === constants.COLORTYPE_PALETTE_COLOR && this._palette.length === 0) { | ||
throw new Error('Expected palette not found'); | ||
if ( | ||
this._colorType === constants.COLORTYPE_PALETTE_COLOR && | ||
this._palette.length === 0 | ||
) { | ||
throw new Error("Expected palette not found"); | ||
} | ||
this.inflateData(data); | ||
var leftOverLength = length - data.length; | ||
let leftOverLength = length - data.length; | ||
if (leftOverLength > 0) { | ||
this._handleIDAT(leftOverLength); | ||
} | ||
else { | ||
} else { | ||
this._handleChunkEnd(); | ||
@@ -301,7 +295,6 @@ } | ||
Parser.prototype._handleIEND = function(length) { | ||
Parser.prototype._handleIEND = function (length) { | ||
this.read(length, this._parseIEND.bind(this)); | ||
}; | ||
Parser.prototype._parseIEND = function(data) { | ||
Parser.prototype._parseIEND = function (data) { | ||
this._crc.write(data); | ||
@@ -308,0 +301,0 @@ |
@@ -1,16 +0,12 @@ | ||
'use strict'; | ||
"use strict"; | ||
let parse = require("./parser-sync"); | ||
let pack = require("./packer-sync"); | ||
var parse = require('./parser-sync'); | ||
var pack = require('./packer-sync'); | ||
exports.read = function(buffer, options) { | ||
exports.read = function (buffer, options) { | ||
return parse(buffer, options || {}); | ||
}; | ||
exports.write = function(png, options) { | ||
exports.write = function (png, options) { | ||
return pack(png, options); | ||
}; |
149
lib/png.js
@@ -1,11 +0,10 @@ | ||
'use strict'; | ||
"use strict"; | ||
var util = require('util'); | ||
var Stream = require('stream'); | ||
var Parser = require('./parser-async'); | ||
var Packer = require('./packer-async'); | ||
var PNGSync = require('./png-sync'); | ||
let util = require("util"); | ||
let Stream = require("stream"); | ||
let Parser = require("./parser-async"); | ||
let Packer = require("./packer-async"); | ||
let PNGSync = require("./png-sync"); | ||
var PNG = exports.PNG = function(options) { | ||
let PNG = (exports.PNG = function (options) { | ||
Stream.call(this); | ||
@@ -19,4 +18,6 @@ | ||
this.data = this.width > 0 && this.height > 0 ? | ||
Buffer.alloc(4 * this.width * this.height) : null; | ||
this.data = | ||
this.width > 0 && this.height > 0 | ||
? Buffer.alloc(4 * this.width * this.height) | ||
: null; | ||
@@ -32,18 +33,20 @@ if (options.fill && this.data) { | ||
this._parser.on('error', this.emit.bind(this, 'error')); | ||
this._parser.on('close', this._handleClose.bind(this)); | ||
this._parser.on('metadata', this._metadata.bind(this)); | ||
this._parser.on('gamma', this._gamma.bind(this)); | ||
this._parser.on('parsed', function(data) { | ||
this.data = data; | ||
this.emit('parsed', data); | ||
}.bind(this)); | ||
this._parser.on("error", this.emit.bind(this, "error")); | ||
this._parser.on("close", this._handleClose.bind(this)); | ||
this._parser.on("metadata", this._metadata.bind(this)); | ||
this._parser.on("gamma", this._gamma.bind(this)); | ||
this._parser.on( | ||
"parsed", | ||
function (data) { | ||
this.data = data; | ||
this.emit("parsed", data); | ||
}.bind(this) | ||
); | ||
this._packer = new Packer(options); | ||
this._packer.on('data', this.emit.bind(this, 'data')); | ||
this._packer.on('end', this.emit.bind(this, 'end')); | ||
this._parser.on('close', this._handleClose.bind(this)); | ||
this._packer.on('error', this.emit.bind(this, 'error')); | ||
}; | ||
this._packer.on("data", this.emit.bind(this, "data")); | ||
this._packer.on("end", this.emit.bind(this, "end")); | ||
this._parser.on("close", this._handleClose.bind(this)); | ||
this._packer.on("error", this.emit.bind(this, "error")); | ||
}); | ||
util.inherits(PNG, Stream); | ||
@@ -53,12 +56,13 @@ | ||
PNG.prototype.pack = function() { | ||
PNG.prototype.pack = function () { | ||
if (!this.data || !this.data.length) { | ||
this.emit('error', 'No data provided'); | ||
this.emit("error", "No data provided"); | ||
return this; | ||
} | ||
process.nextTick(function() { | ||
this._packer.pack(this.data, this.width, this.height, this.gamma); | ||
}.bind(this)); | ||
process.nextTick( | ||
function () { | ||
this._packer.pack(this.data, this.width, this.height, this.gamma); | ||
}.bind(this) | ||
); | ||
@@ -68,10 +72,8 @@ return this; | ||
PNG.prototype.parse = function(data, callback) { | ||
PNG.prototype.parse = function (data, callback) { | ||
if (callback) { | ||
var onParsed, onError; | ||
let onParsed, onError; | ||
onParsed = function(parsedData) { | ||
this.removeListener('error', onError); | ||
onParsed = function (parsedData) { | ||
this.removeListener("error", onError); | ||
@@ -82,4 +84,4 @@ this.data = parsedData; | ||
onError = function(err) { | ||
this.removeListener('parsed', onParsed); | ||
onError = function (err) { | ||
this.removeListener("parsed", onParsed); | ||
@@ -89,4 +91,4 @@ callback(err, null); | ||
this.once('parsed', onParsed); | ||
this.once('error', onError); | ||
this.once("parsed", onParsed); | ||
this.once("error", onError); | ||
} | ||
@@ -98,3 +100,3 @@ | ||
PNG.prototype.write = function(data) { | ||
PNG.prototype.write = function (data) { | ||
this._parser.write(data); | ||
@@ -104,25 +106,25 @@ return true; | ||
PNG.prototype.end = function(data) { | ||
PNG.prototype.end = function (data) { | ||
this._parser.end(data); | ||
}; | ||
PNG.prototype._metadata = function(metadata) { | ||
PNG.prototype._metadata = function (metadata) { | ||
this.width = metadata.width; | ||
this.height = metadata.height; | ||
this.emit('metadata', metadata); | ||
this.emit("metadata", metadata); | ||
}; | ||
PNG.prototype._gamma = function(gamma) { | ||
PNG.prototype._gamma = function (gamma) { | ||
this.gamma = gamma; | ||
}; | ||
PNG.prototype._handleClose = function() { | ||
PNG.prototype._handleClose = function () { | ||
if (!this._parser.writable && !this._packer.readable) { | ||
this.emit('close'); | ||
this.emit("close"); | ||
} | ||
}; | ||
PNG.bitblt = function(src, dst, srcX, srcY, width, height, deltaX, deltaY) { // eslint-disable-line max-params | ||
PNG.bitblt = function (src, dst, srcX, srcY, width, height, deltaX, deltaY) { | ||
// eslint-disable-line max-params | ||
// coerce pixel dimensions to integers (also coerces undefined -> 0): | ||
@@ -138,12 +140,23 @@ /* eslint-disable no-param-reassign */ | ||
if (srcX > src.width || srcY > src.height || srcX + width > src.width || srcY + height > src.height) { | ||
throw new Error('bitblt reading outside image'); | ||
if ( | ||
srcX > src.width || | ||
srcY > src.height || | ||
srcX + width > src.width || | ||
srcY + height > src.height | ||
) { | ||
throw new Error("bitblt reading outside image"); | ||
} | ||
if (deltaX > dst.width || deltaY > dst.height || deltaX + width > dst.width || deltaY + height > dst.height) { | ||
throw new Error('bitblt writing outside image'); | ||
if ( | ||
deltaX > dst.width || | ||
deltaY > dst.height || | ||
deltaX + width > dst.width || | ||
deltaY + height > dst.height | ||
) { | ||
throw new Error("bitblt writing outside image"); | ||
} | ||
for (var y = 0; y < height; y++) { | ||
src.data.copy(dst.data, | ||
for (let y = 0; y < height; y++) { | ||
src.data.copy( | ||
dst.data, | ||
((deltaY + y) * dst.width + deltaX) << 2, | ||
@@ -156,5 +169,13 @@ ((srcY + y) * src.width + srcX) << 2, | ||
PNG.prototype.bitblt = function ( | ||
dst, | ||
srcX, | ||
srcY, | ||
width, | ||
height, | ||
deltaX, | ||
deltaY | ||
) { | ||
// eslint-disable-line max-params | ||
PNG.prototype.bitblt = function(dst, srcX, srcY, width, height, deltaX, deltaY) { // eslint-disable-line max-params | ||
PNG.bitblt(this, dst, srcX, srcY, width, height, deltaX, deltaY); | ||
@@ -164,10 +185,10 @@ return this; | ||
PNG.adjustGamma = function(src) { | ||
PNG.adjustGamma = function (src) { | ||
if (src.gamma) { | ||
for (var y = 0; y < src.height; y++) { | ||
for (var x = 0; x < src.width; x++) { | ||
var idx = (src.width * y + x) << 2; | ||
for (let y = 0; y < src.height; y++) { | ||
for (let x = 0; x < src.width; x++) { | ||
let idx = (src.width * y + x) << 2; | ||
for (var i = 0; i < 3; i++) { | ||
var sample = src.data[idx + i] / 255; | ||
for (let i = 0; i < 3; i++) { | ||
let sample = src.data[idx + i] / 255; | ||
sample = Math.pow(sample, 1 / 2.2 / src.gamma); | ||
@@ -182,4 +203,4 @@ src.data[idx + i] = Math.round(sample * 255); | ||
PNG.prototype.adjustGamma = function() { | ||
PNG.prototype.adjustGamma = function () { | ||
PNG.adjustGamma(this); | ||
}; |
@@ -1,8 +0,8 @@ | ||
'use strict'; | ||
"use strict"; | ||
var assert = require('assert').ok; | ||
var zlib = require('zlib'); | ||
var util = require('util'); | ||
let assert = require("assert").ok; | ||
let zlib = require("zlib"); | ||
let util = require("util"); | ||
var kMaxLength = require('buffer').kMaxLength; | ||
let kMaxLength = require("buffer").kMaxLength; | ||
@@ -47,19 +47,19 @@ function Inflate(opts) { | ||
Inflate.prototype._processChunk = function(chunk, flushFlag, asyncCb) { | ||
if (typeof asyncCb === 'function') { | ||
Inflate.prototype._processChunk = function (chunk, flushFlag, asyncCb) { | ||
if (typeof asyncCb === "function") { | ||
return zlib.Inflate._processChunk.call(this, chunk, flushFlag, asyncCb); | ||
} | ||
var self = this; | ||
let self = this; | ||
var availInBefore = chunk && chunk.length; | ||
var availOutBefore = this._chunkSize - this._offset; | ||
var leftToInflate = this._maxLength; | ||
var inOff = 0; | ||
let availInBefore = chunk && chunk.length; | ||
let availOutBefore = this._chunkSize - this._offset; | ||
let leftToInflate = this._maxLength; | ||
let inOff = 0; | ||
var buffers = []; | ||
var nread = 0; | ||
let buffers = []; | ||
let nread = 0; | ||
var error; | ||
this.on('error', function(err) { | ||
let error; | ||
this.on("error", function (err) { | ||
error = err; | ||
@@ -73,7 +73,7 @@ }); | ||
var have = availOutBefore - availOutAfter; | ||
assert(have >= 0, 'have should not go down'); | ||
let have = availOutBefore - availOutAfter; | ||
assert(have >= 0, "have should not go down"); | ||
if (have > 0) { | ||
var out = self._buffer.slice(self._offset, self._offset + have); | ||
let out = self._buffer.slice(self._offset, self._offset + have); | ||
self._offset += have; | ||
@@ -101,3 +101,3 @@ | ||
if (availOutAfter === 0) { | ||
inOff += (availInBefore - availInAfter); | ||
inOff += availInBefore - availInAfter; | ||
availInBefore = availInAfter; | ||
@@ -111,5 +111,7 @@ | ||
assert(this._handle, 'zlib binding closed'); | ||
assert(this._handle, "zlib binding closed"); | ||
let res; | ||
do { | ||
var res = this._handle.writeSync(flushFlag, | ||
res = this._handle.writeSync( | ||
flushFlag, | ||
chunk, // in | ||
@@ -120,3 +122,4 @@ inOff, // in_off | ||
this._offset, //out_off | ||
availOutBefore); // out_len | ||
availOutBefore | ||
); // out_len | ||
// Node 8 --> 9 compatibility check | ||
@@ -132,6 +135,10 @@ res = res || this._writeState; | ||
_close(this); | ||
throw new RangeError('Cannot create final Buffer. It would be larger than 0x' + kMaxLength.toString(16) + ' bytes'); | ||
throw new RangeError( | ||
"Cannot create final Buffer. It would be larger than 0x" + | ||
kMaxLength.toString(16) + | ||
" bytes" | ||
); | ||
} | ||
var buf = Buffer.concat(buffers, nread); | ||
let buf = Buffer.concat(buffers, nread); | ||
_close(this); | ||
@@ -145,10 +152,10 @@ | ||
function zlibBufferSync(engine, buffer) { | ||
if (typeof buffer === 'string') { | ||
if (typeof buffer === "string") { | ||
buffer = Buffer.from(buffer); | ||
} | ||
if (!(buffer instanceof Buffer)) { | ||
throw new TypeError('Not a string or buffer'); | ||
throw new TypeError("Not a string or buffer"); | ||
} | ||
var flushFlag = engine._finishFlushFlag; | ||
let flushFlag = engine._finishFlushFlag; | ||
if (flushFlag == null) { | ||
@@ -155,0 +162,0 @@ flushFlag = zlib.Z_FINISH; |
@@ -1,31 +0,29 @@ | ||
'use strict'; | ||
"use strict"; | ||
var SyncReader = module.exports = function(buffer) { | ||
let SyncReader = (module.exports = function (buffer) { | ||
this._buffer = buffer; | ||
this._reads = []; | ||
}; | ||
}); | ||
SyncReader.prototype.read = function(length, callback) { | ||
SyncReader.prototype.read = function (length, callback) { | ||
this._reads.push({ | ||
length: Math.abs(length), // if length < 0 then at most this length | ||
allowLess: length < 0, | ||
func: callback | ||
func: callback, | ||
}); | ||
}; | ||
SyncReader.prototype.process = function() { | ||
SyncReader.prototype.process = function () { | ||
// as long as there is any data and read requests | ||
while (this._reads.length > 0 && this._buffer.length) { | ||
let read = this._reads[0]; | ||
var read = this._reads[0]; | ||
if (this._buffer.length && (this._buffer.length >= read.length || read.allowLess)) { | ||
if ( | ||
this._buffer.length && | ||
(this._buffer.length >= read.length || read.allowLess) | ||
) { | ||
// ok there is any data so that we can satisfy this request | ||
this._reads.shift(); // == read | ||
var buf = this._buffer; | ||
let buf = this._buffer; | ||
@@ -35,18 +33,14 @@ this._buffer = buf.slice(read.length); | ||
read.func.call(this, buf.slice(0, read.length)); | ||
} | ||
else { | ||
} else { | ||
break; | ||
} | ||
} | ||
if (this._reads.length > 0) { | ||
return new Error('There are some read requests waitng on finished stream'); | ||
return new Error("There are some read requests waitng on finished stream"); | ||
} | ||
if (this._buffer.length > 0) { | ||
return new Error('unrecognised content at end of stream'); | ||
return new Error("unrecognised content at end of stream"); | ||
} | ||
}; |
{ | ||
"name": "pngjs", | ||
"version": "4.0.1", | ||
"version": "5.0.0", | ||
"description": "PNG encoder/decoder in pure JS, supporting any bit size & interlace, async & sync with full test suite.", | ||
@@ -33,3 +33,3 @@ "contributors": [ | ||
"engines": { | ||
"node": ">=8.0.0" | ||
"node": ">=10.13.0" | ||
}, | ||
@@ -43,30 +43,33 @@ "main": "./lib/png.js", | ||
"scripts": { | ||
"build": "npm run prepublish", | ||
"prepublish": "npm run browserify", | ||
"build": "yarn prepublish", | ||
"prepublish": "yarn browserify", | ||
"browserify": "browserify lib/png.js --standalone png > browser.js", | ||
"coverage": "istanbul -- cover node_modules/tape/bin/tape test/*-spec.js nolarge", | ||
"coverage-report": "npm run coverage && istanbul report html", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls", | ||
"test": "npm run lint && tape test/*-spec.js | tap-dot && node test/run-compare", | ||
"lint": "eslint lib" | ||
"coverage": "nyc --reporter=lcov --reporter=text-summary tape test/*-spec.js nolarge", | ||
"test": "yarn lint && yarn prettier:check && tape test/*-spec.js | tap-dot && node test/run-compare", | ||
"lint": "eslint .", | ||
"prettier:write": "prettier --write .", | ||
"prettier:check": "prettier --check ." | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/lukeapage/pngjs2.git" | ||
"url": "git://github.com/lukeapage/pngjs.git" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/lukeapage/pngjs2/issues" | ||
"url": "https://github.com/lukeapage/pngjs/issues" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^14.5.0", | ||
"browserify": "16.5.1", | ||
"buffer-equal": "1.0.0", | ||
"connect": "^3.4.0", | ||
"eslint": "^5.15.2", | ||
"istanbul": "^0.4.4", | ||
"puppeteer": "1.20.0", | ||
"serve-static": "^1.10.0", | ||
"tap-dot": "^2.0.0", | ||
"tape": "^4.0.2" | ||
"codecov": "3.6.5", | ||
"connect": "3.7.0", | ||
"eslint": "6.8.0", | ||
"eslint-config-prettier": "6.10.1", | ||
"nyc": "15.0.1", | ||
"prettier": "2.0.4", | ||
"puppeteer": "2.1.1", | ||
"serve-static": "1.14.1", | ||
"tap-dot": "2.0.0", | ||
"tape": "4.13.2" | ||
} | ||
} |
304
README.md
@@ -1,7 +0,5 @@ | ||
[![Build Status](https://travis-ci.org/lukeapage/pngjs.svg?branch=master)](https://travis-ci.org/lukeapage/pngjs) [![Build status](https://ci.appveyor.com/api/projects/status/tb8418jql1trkntd/branch/master?svg=true)](https://ci.appveyor.com/project/lukeapage/pngjs2/branch/master) [![Coverage Status](https://coveralls.io/repos/lukeapage/pngjs2/badge.svg?branch=master&service=github)](https://coveralls.io/github/lukeapage/pngjs2?branch=master) [![npm version](https://badge.fury.io/js/pngjs.svg)](http://badge.fury.io/js/pngjs) | ||
[![Build Status](https://travis-ci.com/lukeapage/pngjs.svg?branch=master)](https://travis-ci.com/lukeapage/pngjs) [![Build status](https://ci.appveyor.com/api/projects/status/qo5x8ayutr028108/branch/master?svg=true)](https://ci.appveyor.com/project/lukeapage/pngjs/branch/master) [![codecov](https://codecov.io/gh/lukeapage/pngjs/branch/master/graph/badge.svg)](https://codecov.io/gh/lukeapage/pngjs) [![npm version](https://badge.fury.io/js/pngjs.svg)](http://badge.fury.io/js/pngjs) | ||
pngjs | ||
======== | ||
# pngjs | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/lukeapage/pngjs.svg)](https://greenkeeper.io/) | ||
Simple PNG encoder/decoder for Node.js with no dependencies. | ||
@@ -11,53 +9,51 @@ | ||
* Support for reading 1,2,4 & 16 bit files | ||
* Support for reading interlace files | ||
* Support for reading `tTRNS` transparent colours | ||
* Support for writing colortype 0 (grayscale), colortype 2 (RGB), colortype 4 (grayscale alpha) and colortype 6 (RGBA) | ||
* Sync interface as well as async | ||
* API compatible with pngjs and node-pngjs | ||
- Support for reading 1,2,4 & 16 bit files | ||
- Support for reading interlace files | ||
- Support for reading `tTRNS` transparent colours | ||
- Support for writing colortype 0 (grayscale), colortype 2 (RGB), colortype 4 (grayscale alpha) and colortype 6 (RGBA) | ||
- Sync interface as well as async | ||
- API compatible with pngjs and node-pngjs | ||
Known lack of support for: | ||
* Extended PNG e.g. Animation | ||
* Writing in colortype 3 (indexed color) | ||
- Extended PNG e.g. Animation | ||
- Writing in colortype 3 (indexed color) | ||
# Table of Contents | ||
* [Requirements](#requirements) | ||
* [Comparison Table](#comparison-table) | ||
* [Tests](#tests) | ||
* [Installation](#installation) | ||
* [Browser](#browser) | ||
* [Example](#example) | ||
* [Async API](#async-api) | ||
* [Sync API](#sync-api) | ||
* [Changelog](#changelog) | ||
Requirements | ||
============ | ||
- [Requirements](#requirements) | ||
- [Comparison Table](#comparison-table) | ||
- [Tests](#tests) | ||
- [Installation](#installation) | ||
- [Browser](#browser) | ||
- [Example](#example) | ||
- [Async API](#async-api) | ||
- [Sync API](#sync-api) | ||
- [Changelog](#changelog) | ||
* Node.js v4 (use older v2.3.0 for 0.10/0.12/iojs support) | ||
# Requirements | ||
Comparison Table | ||
================ | ||
- Node.js v4 (use older v2.3.0 for 0.10/0.12/iojs support) | ||
Name | Forked From | Sync | Async | 16 Bit | 1/2/4 Bit | Interlace | Gamma | Encodes | Tested | ||
---------|--------------|------|-------|--------|-----------|-----------|-------|---------|-------- | ||
pngjs | | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ||
node-png | pngjs | No | Yes | No | No | No | Hidden| Yes | Manual | ||
png-coder| pngjs | No | Yes | Yes | No | No | Hidden| Yes | Manual | ||
pngparse | | No | Yes | No | Yes | No | No | No | Yes | ||
pngparse-sync | pngparse| Yes | No | No | Yes | No | No | No | Yes | ||
png-async| | No | Yes | No | No | No | No | Yes | Yes | ||
png-js | | No | Yes | No | No | No | No | No | No | ||
# Comparison Table | ||
| Name | Forked From | Sync | Async | 16 Bit | 1/2/4 Bit | Interlace | Gamma | Encodes | Tested | | ||
| ------------- | ----------- | ---- | ----- | ------ | --------- | --------- | ------ | ------- | ------ | | ||
| pngjs | | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | | ||
| node-png | pngjs | No | Yes | No | No | No | Hidden | Yes | Manual | | ||
| png-coder | pngjs | No | Yes | Yes | No | No | Hidden | Yes | Manual | | ||
| pngparse | | No | Yes | No | Yes | No | No | No | Yes | | ||
| pngparse-sync | pngparse | Yes | No | No | Yes | No | No | No | Yes | | ||
| png-async | | No | Yes | No | No | No | No | Yes | Yes | | ||
| png-js | | No | Yes | No | No | No | No | No | No | | ||
Native C++ node decoders: | ||
* png | ||
* png-sync (sync version of above) | ||
* pixel-png | ||
* png-img | ||
Tests | ||
===== | ||
- png | ||
- png-sync (sync version of above) | ||
- pixel-png | ||
- png-img | ||
# Tests | ||
Tested using [PNG Suite](http://www.schaik.com/pngsuite/). We read every file into pngjs, output it in standard 8bit colour, synchronously and asynchronously, then compare the original with the newly saved images. | ||
@@ -71,4 +67,4 @@ | ||
Installation | ||
=============== | ||
# Installation | ||
``` | ||
@@ -78,4 +74,4 @@ $ npm install pngjs --save | ||
Browser | ||
=========== | ||
# Browser | ||
The package has been build with a [Browserify](browserify.org) version (`npm run browserify`) and you can use the browser version by including in your code: | ||
@@ -87,35 +83,36 @@ | ||
Example | ||
========== | ||
# Example | ||
```js | ||
var fs = require('fs'), | ||
PNG = require('pngjs').PNG; | ||
var fs = require("fs"), | ||
PNG = require("pngjs").PNG; | ||
fs.createReadStream('in.png') | ||
.pipe(new PNG({ | ||
filterType: 4 | ||
})) | ||
.on('parsed', function() { | ||
fs.createReadStream("in.png") | ||
.pipe( | ||
new PNG({ | ||
filterType: 4, | ||
}) | ||
) | ||
.on("parsed", function () { | ||
for (var y = 0; y < this.height; y++) { | ||
for (var x = 0; x < this.width; x++) { | ||
var idx = (this.width * y + x) << 2; | ||
for (var y = 0; y < this.height; y++) { | ||
for (var x = 0; x < this.width; x++) { | ||
var idx = (this.width * y + x) << 2; | ||
// invert color | ||
this.data[idx] = 255 - this.data[idx]; | ||
this.data[idx + 1] = 255 - this.data[idx + 1]; | ||
this.data[idx + 2] = 255 - this.data[idx + 2]; | ||
// invert color | ||
this.data[idx] = 255 - this.data[idx]; | ||
this.data[idx+1] = 255 - this.data[idx+1]; | ||
this.data[idx+2] = 255 - this.data[idx+2]; | ||
// and reduce opacity | ||
this.data[idx + 3] = this.data[idx + 3] >> 1; | ||
} | ||
} | ||
// and reduce opacity | ||
this.data[idx+3] = this.data[idx+3] >> 1; | ||
} | ||
} | ||
this.pack().pipe(fs.createWriteStream("out.png")); | ||
}); | ||
``` | ||
this.pack().pipe(fs.createWriteStream('out.png')); | ||
}); | ||
``` | ||
For more examples see `examples` folder. | ||
Async API | ||
================ | ||
# Async API | ||
@@ -125,10 +122,11 @@ As input any color type is accepted (grayscale, rgb, palette, grayscale with alpha, rgb with alpha) but 8 bit per sample (channel) is the only supported bit depth. Interlaced mode is not supported. | ||
## Class: PNG | ||
`PNG` is readable and writable `Stream`. | ||
### Options | ||
### Options | ||
- `width` - use this with `height` if you want to create png from scratch | ||
- `height` - as above | ||
- `checkCRC` - whether parser should be strict about checksums in source stream (default: `true`) | ||
- `deflateChunkSize` - chunk size used for deflating data chunks, this should be power of 2 and must not be less than 256 and more than 32*1024 (default: 32 kB) | ||
- `deflateChunkSize` - chunk size used for deflating data chunks, this should be power of 2 and must not be less than 256 and more than 32\*1024 (default: 32 kB) | ||
- `deflateLevel` - compression level for deflate (default: 9) | ||
@@ -141,11 +139,12 @@ - `deflateStrategy` - compression strategy for deflate (default: 3) | ||
- `bitDepth` - the bitDepth of the output, 8 or 16 bits. Input data is expected to have this bit depth. | ||
16 bit data is expected in the system endianness (Default: 8) | ||
16 bit data is expected in the system endianness (Default: 8) | ||
- `inputHasAlpha` - whether the input bitmap has 4 bytes per pixel (rgb and alpha) or 3 (rgb - no alpha). | ||
- `bgColor` - an object containing red, green, and blue values between 0 and 255 | ||
that is used when packing a PNG if alpha is not to be included (default: 255,255,255) | ||
that is used when packing a PNG if alpha is not to be included (default: 255,255,255) | ||
### Event "metadata" | ||
### Event "metadata" | ||
`function(metadata) { }` | ||
Image's header has been parsed, metadata contains this information: | ||
- `width` image size in pixels | ||
@@ -158,13 +157,13 @@ - `height` image size in pixels | ||
### Event: "parsed" | ||
### Event: "parsed" | ||
`function(data) { }` | ||
Input image has been completely parsed, `data` is complete and ready for modification. | ||
### Event: "error" | ||
### Event: "error" | ||
`function(error) { }` | ||
### png.parse(data, [callback]) | ||
### png.parse(data, [callback]) | ||
Parses PNG file data. Can be `String` or `Buffer`. Alternatively you can stream data to instance of PNG. | ||
@@ -178,6 +177,6 @@ | ||
#### Example | ||
```js | ||
new PNG({ filterType:4 }).parse( imageData, function(error, data) | ||
{ | ||
console.log(error, data) | ||
new PNG({ filterType: 4 }).parse(imageData, function (error, data) { | ||
console.log(error, data); | ||
}); | ||
@@ -187,2 +186,3 @@ ``` | ||
### png.pack() | ||
Starts converting data to PNG file Stream. | ||
@@ -192,4 +192,4 @@ | ||
### png.bitblt(dst, sx, sy, w, h, dx, dy) | ||
### png.bitblt(dst, sx, sy, w, h, dx, dy) | ||
Helper for image manipulation, copies a rectangle of pixels from current (i.e. the source) image (`sx`, `sy`, `w`, `h`) to `dst` image (at `dx`, `dy`). | ||
@@ -200,13 +200,15 @@ | ||
For example, the following code copies the top-left 100x50 px of `in.png` into dst and writes it to `out.png`: | ||
```js | ||
var dst = new PNG({width: 100, height: 50}); | ||
fs.createReadStream('in.png') | ||
.pipe(new PNG()) | ||
.on('parsed', function() { | ||
this.bitblt(dst, 0, 0, 100, 50, 0, 0); | ||
dst.pack().pipe(fs.createWriteStream('out.png')); | ||
}); | ||
var dst = new PNG({ width: 100, height: 50 }); | ||
fs.createReadStream("in.png") | ||
.pipe(new PNG()) | ||
.on("parsed", function () { | ||
this.bitblt(dst, 0, 0, 100, 50, 0, 0); | ||
dst.pack().pipe(fs.createWriteStream("out.png")); | ||
}); | ||
``` | ||
### Property: adjustGamma() | ||
Helper that takes data and adjusts it to be gamma corrected. Note that it is not 100% reliable with transparent colours because that requires knowing the background colour the bitmap is rendered on to. | ||
@@ -219,23 +221,24 @@ | ||
```js | ||
fs.createReadStream('in.png') | ||
.pipe(new PNG()) | ||
.on('parsed', function() { | ||
this.adjustGamma(); | ||
this.pack().pipe(fs.createWriteStream('out.png')); | ||
}); | ||
fs.createReadStream("in.png") | ||
.pipe(new PNG()) | ||
.on("parsed", function () { | ||
this.adjustGamma(); | ||
this.pack().pipe(fs.createWriteStream("out.png")); | ||
}); | ||
``` | ||
### Property: width | ||
Width of image in pixels | ||
### Property: height | ||
### Property: height | ||
Height of image in pixels | ||
### Property: data | ||
### Property: data | ||
Buffer of image pixel data. Every pixel consists 4 bytes: R, G, B, A (opacity). | ||
### Property: gamma | ||
### Property: gamma | ||
Gamma of image (0 if not specified) | ||
@@ -250,17 +253,19 @@ | ||
```js | ||
var fs = require('fs'), | ||
PNG = require('pngjs').PNG; | ||
var fs = require("fs"), | ||
PNG = require("pngjs").PNG; | ||
fs.createReadStream('in.png') | ||
.pipe(new PNG({ | ||
colorType: 2, | ||
bgColor: { | ||
red: 0, | ||
green: 255, | ||
blue: 0 | ||
} | ||
})) | ||
.on('parsed', function() { | ||
this.pack().pipe(fs.createWriteStream('out.png')); | ||
}); | ||
fs.createReadStream("in.png") | ||
.pipe( | ||
new PNG({ | ||
colorType: 2, | ||
bgColor: { | ||
red: 0, | ||
green: 255, | ||
blue: 0, | ||
}, | ||
}) | ||
) | ||
.on("parsed", function () { | ||
this.pack().pipe(fs.createWriteStream("out.png")); | ||
}); | ||
``` | ||
@@ -303,6 +308,9 @@ | ||
# Changelog | ||
Changelog | ||
============ | ||
### 5.0.0 - 15/04/2020 | ||
- Drop support for Node 8 | ||
- Browserified bundle may now contain ES20(15-20) code if the supported node version supports it. Please run the browserified version through babel if you need to support older browsers. | ||
### 4.0.1 - 15/04/2020 | ||
@@ -346,59 +354,73 @@ | ||
### 3.1.0 - 30/04/2017 | ||
- Support for pngs with zlib chunks that are malformed after valid data | ||
- Support for pngs with zlib chunks that are malformed after valid data | ||
### 3.0.1 - 16/02/2017 | ||
- Fix single pixel pngs | ||
- Fix single pixel pngs | ||
### 3.0.0 - 03/08/2016 | ||
- Drop support for node below v4 and iojs. Pin to 2.3.0 to use with old, unsupported or patched node versions. | ||
- Drop support for node below v4 and iojs. Pin to 2.3.0 to use with old, unsupported or patched node versions. | ||
### 2.3.0 - 22/04/2016 | ||
- Support for sync in node 0.10 | ||
- Support for sync in node 0.10 | ||
### 2.2.0 - 04/12/2015 | ||
- Add sync write api | ||
- Fix newfile example | ||
- Correct comparison table | ||
- Add sync write api | ||
- Fix newfile example | ||
- Correct comparison table | ||
### 2.1.0 - 28/10/2015 | ||
- rename package to pngjs | ||
- added 'bgColor' option | ||
- rename package to pngjs | ||
- added 'bgColor' option | ||
### 2.0.0 - 08/10/2015 | ||
- fixes to readme | ||
- *breaking change* - bitblt on the png prototype now doesn't take a unused, unnecessary src first argument | ||
- fixes to readme | ||
- _breaking change_ - bitblt on the png prototype now doesn't take a unused, unnecessary src first argument | ||
### 1.2.0 - 13/09/2015 | ||
- support passing colorType to write PNG's and writing bitmaps without alpha information | ||
- support passing colorType to write PNG's and writing bitmaps without alpha information | ||
### 1.1.0 - 07/09/2015 | ||
- support passing a deflate factory for controlled compression | ||
- support passing a deflate factory for controlled compression | ||
### 1.0.2 - 22/08/2015 | ||
- Expose all PNG creation info | ||
- Expose all PNG creation info | ||
### 1.0.1 - 21/08/2015 | ||
- Fix non square interlaced files | ||
- Fix non square interlaced files | ||
### 1.0.0 - 08/08/2015 | ||
- More tests | ||
- source linted | ||
- maintainability refactorings | ||
- async API - exceptions in reading now emit warnings | ||
- documentation improvement - sync api now documented, adjustGamma documented | ||
- breaking change - gamma chunk is now written. previously a read then write would destroy gamma information, now it is persisted. | ||
- More tests | ||
- source linted | ||
- maintainability refactorings | ||
- async API - exceptions in reading now emit warnings | ||
- documentation improvement - sync api now documented, adjustGamma documented | ||
- breaking change - gamma chunk is now written. previously a read then write would destroy gamma information, now it is persisted. | ||
### 0.0.3 - 03/08/2015 | ||
- Error handling fixes | ||
- ignore files for smaller npm footprint | ||
- Error handling fixes | ||
- ignore files for smaller npm footprint | ||
### 0.0.2 - 02/08/2015 | ||
- Bugfixes to interlacing, support for transparent colours | ||
- Bugfixes to interlacing, support for transparent colours | ||
### 0.0.1 - 02/08/2015 | ||
- Initial release, see pngjs for older changelog. | ||
License | ||
========= | ||
- Initial release, see pngjs for older changelog. | ||
# License | ||
(The MIT License) | ||
@@ -405,0 +427,0 @@ |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1021215
60
17586
430
12