
Security News
Vite+ Joins the Push to Consolidate JavaScript Tooling
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
@egoroof/upng
Advanced tools
This is a fork with two changes:
zlib
(which is faster)filter
A small, fast and advanced PNG / APNG encoder and decoder. It is the main PNG engine for Photopea image editor.
Download and include the UPNG.js
file in your code, or get it from NPM:
npm install @egoroof/upng
UPNG.js supports APNG and the interface expects "frames". Regular PNG is just a single-frame animation (single-item array).
UPNG.encode(imgs, w, h, cnum, [dels])
imgs
: array of frames. A frame is an ArrayBuffer containing the pixel data (RGBA, 8 bits per channel)w
, h
: width and height of the imagecnum
: number of colors in the result; 0: all colors (lossless PNG)dels
: array of delays for each frame (only when 2 or more frames)UPNG.js can do a lossy minification of PNG files, similar to TinyPNG and other tools. It performed quantization with k-means algorithm in the past, but now we use K-d trees.
Lossy compression is allowed by the last parameter cnum
. Set it to zero for a lossless compression, or write the number of allowed colors in the image. Smaller values produce smaller files. Or just use 0 for lossless / 256 for lossy.
// Read RGBA from canvas and encode with UPNG
var dta = ctx.getImageData(0,0,200,300).data; // ctx is Context2D of a Canvas
// dta = new Uint8Array(200 * 300 * 4); // or generate pixels manually
var png = UPNG.encode([dta.buffer], 200, 300, 0); console.log(new Uint8Array(png));
UPNG.encodeLL(imgs, w, h, cc, ac, depth, filter, [dels])
- low-level encodeimgs
: array of frames. A frame is an ArrayBuffer containing the pixel data (corresponding to following parameters)w
, h
: width and height of the imagecc
, ac
: number of color channels (1 or 3) and alpha channels (0 or 1)depth
: bit depth of pixel data (1, 2, 4, 8, 16)filter
: filter type https://www.w3.org/TR/PNG/#9Filters (-1 (find best), 0, 1, 2, 3, 4)dels
: array of delays for each frame (only when 2 or more frames)This function does not do any optimizations, it just stores what you give it. There are two cases when it is useful:
Supports all color types (including Grayscale and Palettes), all channel depths (1, 2, 4, 8, 16), interlaced images etc. Opens PNGs which other libraries can not open (tested with PngSuite).
UPNG.decode(buffer)
buffer
: ArrayBuffer containing the PNG filewidth
: the width of the imageheight
: the height of the imagedepth
: number of bits per channelctype
: color type of the file (Truecolor, Grayscale, Palette ...)frames
: additional info about frames (frame delays etc.)tabs
: additional chunks of the PNG filedata
: pixel data of the imagePNG files may have a various number of channels and a various color depth. The interpretation of data
depends on the current color type and color depth (see the PNG specification).
UPNG.toRGBA8(img)
img
: PNG image object (returned by UPNG.decode())var img = UPNG.decode(buff); // put ArrayBuffer of the PNG file into UPNG.decode
var rgba = UPNG.toRGBA8(img)[0]; // UPNG.toRGBA8 returns array of frames, size: width * height * 4 bytes.
PNG format uses the Inflate algorithm. Right now, UPNG.js calls Pako.js for the Inflate and Deflate method.
UPNG.js contains a very good Quantizer of 4-component 8-bit vectors (i.e. pixels). It can be used to generate nice color palettes (e.g. Photopea uses UPNG.js to make palettes for GIF images).
Quantization consists of two important steps: Finding a nice palette and Finding the closest color in the palette for each sample (non-trivial for large palettes). UPNG perfroms both steps.
var res = UPNG.quantize(data, psize);
data
: ArrayBuffer of samples (byte length is a multiple of four)psize
: Palette size (how many colors you want to have)The result object "res" has following properties:
abuf
: ArrayBuffer corresponding to data
, where colors are remapped by a paletteinds
: Uint8Array : the index of a color for each sample (only when psize
<=256)plte
: Array : the Palette - a list of colors, plte[i].est.q
and plte[i].est.rgba
is the color valuedata
.FAQs
Small, fast and advanced PNG / APNG encoder and decoder
We found that @egoroof/upng 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
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
Security News
Ruby Central’s incident report on the RubyGems.org access dispute sparks backlash from former maintainers and renewed debate over project governance.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.