Installations
npm install pngjs-nozlib
Score
99.8
Supply Chain
100
Quality
75
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Developer
lukeapage
Developer Guide
Module System
CommonJS
Min. Node Version
>=0.10.0
Typescript Support
No
Node Version
0.12.2
NPM Version
2.13.1
Statistics
687 Stars
259 Commits
97 Forks
7 Watching
11 Branches
35 Contributors
Updated on 08 Nov 2024
Bundle Size
23.63 kB
Minified
7.04 kB
Minified + Gzipped
Languages
JavaScript (69.86%)
HTML (30.14%)
Total Downloads
Cumulative downloads
Total Downloads
30,697,943
Last day
-9.8%
13,687
Compared to previous day
Last week
1.8%
81,192
Compared to previous week
Last month
6.7%
370,753
Compared to previous month
Last year
-12.8%
5,531,589
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
pngjs
Simple PNG encoder/decoder for Node.js with no dependencies.
Based on the original pngjs with the follow enhancements.
- 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)
Table of Contents
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
Tested using PNG Suite. We read every file into pngjs, output it in standard 8bit colour, synchronously and asynchronously, then compare the original with the newly saved images.
To run the tests, fetch the repo (tests are not distributed via npm) and install with npm i
, run npm test
.
The only thing not converted is gamma correction - this is because multiple vendors will do gamma correction differently, so the tests will have different results on different browsers.
Installation
$ npm install pngjs --save
Browser
The package has been build with a Browserify version (npm run browserify
) and you can use the browser version by including in your code:
import { PNG } from 'pngjs/browser';
Example
1var fs = require("fs"), 2 PNG = require("pngjs").PNG; 3 4fs.createReadStream("in.png") 5 .pipe( 6 new PNG({ 7 filterType: 4, 8 }) 9 ) 10 .on("parsed", function () { 11 for (var y = 0; y < this.height; y++) { 12 for (var x = 0; x < this.width; x++) { 13 var idx = (this.width * y + x) << 2; 14 15 // invert color 16 this.data[idx] = 255 - this.data[idx]; 17 this.data[idx + 1] = 255 - this.data[idx + 1]; 18 this.data[idx + 2] = 255 - this.data[idx + 2]; 19 20 // and reduce opacity 21 this.data[idx + 3] = this.data[idx + 3] >> 1; 22 } 23 } 24 25 this.pack().pipe(fs.createWriteStream("out.png")); 26 });
For more examples see examples
folder.
Async API
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
width
- use this withheight
if you want to create png from scratchheight
- as abovecheckCRC
- 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)deflateLevel
- compression level for deflate (default: 9)deflateStrategy
- compression strategy for deflate (default: 3)deflateFactory
- deflate stream factory (default:zlib.createDeflate
)filterType
- png filtering method for scanlines (default: -1 => auto, accepts array of numbers 0-4)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.inputColorType
- the input colorType - see constants. Default is 6 (RGBA)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)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)
Event "metadata"
function(metadata) { }
Image's header has been parsed, metadata contains this information:
width
image size in pixelsheight
image size in pixelspalette
image is palettedcolor
image is not grayscalealpha
image contains alpha channelinterlace
image is interlaced
Event: "parsed"
function(data) { }
Input image has been completely parsed, data
is complete and ready for modification.
Event: "error"
function(error) { }
png.parse(data, [callback])
Parses PNG file data. Can be String
or Buffer
. Alternatively you can stream data to instance of PNG.
Optional callback
is once called on error
or parsed
. The callback gets
two arguments (err, data)
.
Returns this
for method chaining.
Example
1new PNG({ filterType: 4 }).parse(imageData, function (error, data) { 2 console.log(error, data); 3});
png.pack()
Starts converting data to PNG file Stream.
Returns this
for method chaining.
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
).
Returns this
for method chaining.
For example, the following code copies the top-left 100x50 px of in.png
into dst and writes it to out.png
:
1var dst = new PNG({ width: 100, height: 50 }); 2fs.createReadStream("in.png") 3 .pipe(new PNG()) 4 .on("parsed", function () { 5 this.bitblt(dst, 0, 0, 100, 50, 0, 0); 6 dst.pack().pipe(fs.createWriteStream("out.png")); 7 });
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.
In tests against PNG suite it compared 100% with chrome on all 8 bit and below images. On IE there were some differences.
The following example reads a file, adjusts the gamma (which sets the gamma to 0) and writes it out again, effectively removing any gamma correction from the image.
1fs.createReadStream("in.png") 2 .pipe(new PNG()) 3 .on("parsed", function () { 4 this.adjustGamma(); 5 this.pack().pipe(fs.createWriteStream("out.png")); 6 });
Property: width
Width of image in pixels
Property: height
Height of image in pixels
Property: data
Buffer of image pixel data. Every pixel consists 4 bytes: R, G, B, A (opacity).
Property: gamma
Gamma of image (0 if not specified)
Packing a PNG and removing alpha (RGBA to RGB)
When removing the alpha channel from an image, there needs to be a background color to correctly convert each pixel's transparency to the appropriate RGB value. By default, pngjs will flatten the image against a white background. You can override this in the options:
1var fs = require("fs"), 2 PNG = require("pngjs").PNG; 3 4fs.createReadStream("in.png") 5 .pipe( 6 new PNG({ 7 colorType: 2, 8 bgColor: { 9 red: 0, 10 green: 255, 11 blue: 0, 12 }, 13 }) 14 ) 15 .on("parsed", function () { 16 this.pack().pipe(fs.createWriteStream("out.png")); 17 });
Sync API
PNG.sync
PNG.sync.read(buffer)
Take a buffer and returns a PNG image. The properties on the image include the meta data and data
as per the async API above.
var data = fs.readFileSync('in.png');
var png = PNG.sync.read(data);
PNG.sync.write(png)
Take a PNG image and returns a buffer. The properties on the image include the meta data and data
as per the async API above.
var data = fs.readFileSync('in.png');
var png = PNG.sync.read(data);
var options = { colorType: 6 };
var buffer = PNG.sync.write(png, options);
fs.writeFileSync('out.png', buffer);
PNG.adjustGamma(src)
Adjusts the gamma of a sync image. See the async adjustGamma.
var data = fs.readFileSync('in.png');
var png = PNG.sync.read(data);
PNG.adjustGamma(png);
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
GitHub workflow tokens follow principle of least privilege
Details
- Info: topLevel 'contents' permission set to 'read': .github/workflows/ci.yml:9
- Info: no jobLevel write permissions found
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
Found 4/11 approved changesets -- score normalized to 3
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/pngjs/pngjs/ci.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/pngjs/pngjs/ci.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:41: update your workflow using https://app.stepsecurity.io/secureworkflow/pngjs/pngjs/ci.yml/main?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 26 are checked with a SAST tool
Reason
14 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-w5p7-h5w8-2hfq
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
4.1
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More