Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
The png-async npm package is a library for reading and writing PNG images asynchronously. It provides a simple API for manipulating PNG images, making it suitable for applications that require image processing without blocking the main thread.
Reading PNG files
This feature allows you to read PNG files asynchronously. The code sample demonstrates how to read a PNG file from the filesystem, parse it, and log its width, height, and pixel data.
const fs = require('fs');
const PNG = require('png-async');
fs.createReadStream('input.png')
.pipe(new PNG())
.on('parsed', function() {
console.log('Width:', this.width);
console.log('Height:', this.height);
console.log('Data:', this.data);
});
Writing PNG files
This feature allows you to write PNG files asynchronously. The code sample demonstrates how to create a new PNG image, manipulate its pixel data to fill it with red color, and save it to the filesystem.
const fs = require('fs');
const PNG = require('png-async');
const png = new PNG({ width: 100, height: 100 });
for (let y = 0; y < png.height; y++) {
for (let x = 0; x < png.width; x++) {
const idx = (png.width * y + x) << 2;
png.data[idx] = 255; // Red
png.data[idx + 1] = 0; // Green
png.data[idx + 2] = 0; // Blue
png.data[idx + 3] = 255; // Alpha
}
}
png.pack().pipe(fs.createWriteStream('output.png'));
Transforming PNG images
This feature allows you to transform PNG images asynchronously. The code sample demonstrates how to read a PNG file, invert its colors, and save the transformed image to the filesystem.
const fs = require('fs');
const PNG = require('png-async');
fs.createReadStream('input.png')
.pipe(new PNG())
.on('parsed', function() {
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
const idx = (this.width * y + x) << 2;
// Invert colors
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];
}
}
this.pack().pipe(fs.createWriteStream('output.png'));
});
pngjs is a pure JavaScript library for reading and writing PNG files. It provides a synchronous API, which can be simpler to use in some cases but may block the main thread during processing. It is suitable for applications where blocking operations are acceptable.
sharp is a high-performance image processing library that supports multiple image formats, including PNG. It provides both synchronous and asynchronous APIs and is optimized for speed and memory usage. It is suitable for applications that require fast and efficient image processing.
jimp is an image processing library written entirely in JavaScript for Node.js. It supports reading and writing PNG files, among other formats, and provides a wide range of image manipulation features. It is suitable for applications that require a comprehensive set of image processing tools.
A simple and non-blocking PNG encoder / decoder for Node.
forked from node-png.
$ npm install png-async --save
$ git clone https://github.com/kanreisa/node-png-async.git
$ cd node-png-async
$ npm install
$ npm run build
$ npm run test
var fs = require('fs');
var png = require('png-async');
fs.createReadStream('in.png')
.pipe(png.createImage({
filterType: 4
}))
.on('parsed', function () {
// Note: this is blocking. be careful.
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];
// and reduce opacity
this.data[idx+3] = this.data[idx+3] >> 1;
}
}
this.pack().pipe(fs.createWriteStream('out.png'));
});
For more examples see examples
folder.
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.
gAMA
- gamma,tRNS
- transparency (but only for paletted image)Image
is readable and writable Stream
.
width
- use this with height
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 delate (default: 9)deflateStrategy
- compression strategy for delate (default: 3)filterType
- png filtering method for scanlines (default: -1 => auto, accepts array of numbers 0-4)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 channelfunction(data) { }
Input image has been completly parsed, data
is complete and ready for modification.
function(error) { }
Parses PNG file data. 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.
Starts converting data to PNG file Stream.
Returns this
for method chaining.
Helper for image manipulation, copies rectangle of pixels from current image (sx
, sy
, w
, h
) to dst
image (at dx
, dy
).
Returns this
for method chaining.
Width of image in pixels
Height of image in pixels
Buffer of image pixel data. Every pixel consists 4 bytes: R, G, B, A (opacity).
Gamma of image (0 if not specified)
FAQs
A simple and non-blocking PNG encoder / decoder.
We found that png-async demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.