Comparing version 3.1.0 to 3.2.0
@@ -6,11 +6,15 @@ 'use strict'; | ||
module.exports = function(data, width, height, options) { | ||
var outHasAlpha = options.colorType === constants.COLORTYPE_COLOR_ALPHA; | ||
if (options.inputHasAlpha && outHasAlpha) { | ||
return data; | ||
var outHasAlpha = [constants.COLORTYPE_COLOR_ALPHA, constants.COLORTYPE_ALPHA].indexOf(options.colorType) !== -1; | ||
if ([constants.COLORTYPE_GRAYSCALE, constants.COLORTYPE_ALPHA].indexOf(options.colorType) === -1) { | ||
// If no need to convert to grayscale and alpha is present/absent in both, take a fast route | ||
if (options.inputHasAlpha && outHasAlpha) { | ||
return data; | ||
} | ||
if (!options.inputHasAlpha && !outHasAlpha) { | ||
return data; | ||
} | ||
} | ||
if (!options.inputHasAlpha && !outHasAlpha) { | ||
return data; | ||
} | ||
var outBpp = outHasAlpha ? 4 : 3; | ||
var outBpp = constants.COLORTYPE_TO_BPP_MAP[options.colorType]; | ||
var outData = new Buffer(width * height * outBpp); | ||
@@ -52,7 +56,21 @@ var inBpp = options.inputHasAlpha ? 4 : 3; | ||
outData[outIndex] = red; | ||
outData[outIndex + 1] = green; | ||
outData[outIndex + 2] = blue; | ||
if (outHasAlpha) { | ||
outData[outIndex + 3] = alpha; | ||
switch (options.colorType) { | ||
case constants.COLORTYPE_COLOR_ALPHA: | ||
case constants.COLORTYPE_COLOR: | ||
outData[outIndex] = red; | ||
outData[outIndex + 1] = green; | ||
outData[outIndex + 2] = blue; | ||
if (outHasAlpha) { | ||
outData[outIndex + 3] = alpha; | ||
} | ||
break; | ||
case constants.COLORTYPE_ALPHA: | ||
case constants.COLORTYPE_GRAYSCALE: | ||
// Convert to grayscale and alpha | ||
var grayscale = (red + green + blue) / 3; | ||
outData[outIndex] = grayscale; | ||
if (outHasAlpha) { | ||
outData[outIndex + 1] = alpha; | ||
} | ||
break; | ||
} | ||
@@ -59,0 +77,0 @@ |
@@ -18,5 +18,12 @@ 'use strict'; | ||
options.bitDepth = options.bitDepth || 8; | ||
// This is outputColorType | ||
options.colorType = (typeof options.colorType === 'number') ? options.colorType : constants.COLORTYPE_COLOR_ALPHA; | ||
options.inputColorType = options.inputColorType || constants.COLORTYPE_COLOR_ALPHA; | ||
if (options.colorType !== constants.COLORTYPE_COLOR && options.colorType !== 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'); | ||
@@ -78,3 +85,3 @@ } | ||
buf.writeUInt32BE(height, 4); | ||
buf[8] = this._options.bitDepth; // Bit depth | ||
buf[8] = this._options.bitDepth; // Bit depth | ||
buf[9] = this._options.colorType; // colorType | ||
@@ -94,2 +101,2 @@ buf[10] = 0; // compression | ||
return this._packChunk(constants.TYPE_IEND, null); | ||
}; | ||
}; |
@@ -13,5 +13,5 @@ 'use strict'; | ||
exports.write = function(png) { | ||
exports.write = function(png, options) { | ||
return pack(png); | ||
return pack(png, options); | ||
}; |
{ | ||
"name": "pngjs", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "PNG encoder/decoder in pure JS, supporting any bit size & interlace, async & sync with full test suite.", | ||
@@ -16,3 +16,4 @@ "contributors": [ | ||
"Xin-Xin Wang", | ||
"toriningen" | ||
"toriningen", | ||
"Eugene Kulabuhov" | ||
], | ||
@@ -19,0 +20,0 @@ "homepage": "https://github.com/lukeapage/pngjs", |
@@ -14,3 +14,3 @@ [![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) | ||
* Support for reading `tTRNS` transparent colours | ||
* Support for writing colortype 2 (RGB) and colortype 6 (RGBA) | ||
* Support for writing colortype 0 (grayscale), colortype 2 (RGB), colortype 4 (grayscale alpha) and colortype 6 (RGBA) | ||
* Sync interface as well as async | ||
@@ -22,4 +22,14 @@ * API compatible with pngjs and node-pngjs | ||
* Extended PNG e.g. Animation | ||
* Writing in different formats, colortype 0 (greyscale), colortype 3 (indexed color), colortype 4 (greyscale with alpha) | ||
* Synchronous write | ||
* Writing in colortype 3 (indexed color) | ||
# Table of Contents | ||
* [Requirements](#requirements) | ||
* [Comparison Table](#comparison-table) | ||
* [Tests](#tests) | ||
* [Installation](#installation) | ||
* [Browserify](#browserify) | ||
* [Example](#example) | ||
* [Async API](#async-api) | ||
* [Sync API](#sync-api) | ||
* [Changelog](#changelog) | ||
@@ -68,2 +78,10 @@ Requirements | ||
Browserify | ||
=========== | ||
Use [Browserify](browserify.org) to build a version of library that can run client-side: | ||
``` | ||
$ browserify lib/png.js --standalone png > bundle.js | ||
``` | ||
Then depending on your module system you can either `require('bundle.js')` or if you're not using module loader you can use plain old `<script src="bundle.js"></script>` tag and the library will be available under `window.PNG` or just `PNG`. | ||
Example | ||
@@ -118,3 +136,3 @@ ========== | ||
- `filterType` - png filtering method for scanlines (default: -1 => auto, accepts array of numbers 0-4) | ||
- `colorType` - the output colorType - see constants. 2 = color, no alpha, 6 = color & alpha. Default currently 6, but in the future may calculate best mode. | ||
- `colorType` - the output colorType - see constants. 0 = grayscale, no alpha, 2 = color, no alpha, 4 = grayscale & alpha, 6 = color & alpha. Default currently 6, but in the future may calculate best mode. | ||
- `inputHasAlpha` - whether the input bitmap has 4 bits per pixel (rgb and alpha) or 3 (rgb - no alpha). | ||
@@ -258,3 +276,4 @@ - `bgColor` - an object containing red, green, and blue values between 0 and 255 | ||
var png = PNG.sync.read(data); | ||
var buffer = PNG.sync.write(png); | ||
var options = { colorType: 6 }; | ||
var buffer = PNG.sync.write(png, options); | ||
fs.writeFileSync('out.png', buffer); | ||
@@ -277,2 +296,6 @@ ``` | ||
### 3.2.0 - 30/04/2017 | ||
- Support for encoding 8-bit grayscale images | ||
### 3.1.0 - 30/04/2017 | ||
@@ -279,0 +302,0 @@ - Support for pngs with zlib chunks that are malformed after valid data |
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
132932
28
1981
372