alphafix
v1.0.2
Published
Fix colors in invisible pixels for good texture sampling
Readme
Alpha Fix
Photoshop writes pixels with 0 alpha but a bright white color (or other random patterns), which causes interpolation errors - instead, we want to spread the nearest non-alpha color. In this extended version, continue spreading until all alpha pixels are full to fix blending even at low mipmap levels.
See https://www.dashingstrike.com/ImageProcessor/ - "Photoshop has an alpha problem" for details and examples.
Works with pngjs images or any width+height+rgba data.
API usage:
const { alphafix } = require('alphafix');
let imgdata = Buffer.from(new Uint32Array([
0x00000000, 0x00000000, 0x00000000,
0xFF00007f, 0xFF0000ff, 0x00000000,
0x00000000, 0x00000000, 0x00000000,
]).buffer);
alphafix({
alpha_channel: 1, // bitmask, 1=first channel (e.g. ABGR), 8=last (e.g. RGBA), etc
image: {
width: 3,
height: 3,
data: imgdata,
},
});
// imgdata now contains:
// 0xFF000000, 0xFF000000, 0xFF000000,
// 0xFF00007f, 0xFF0000ff, 0xFF000000,
// 0xFF000000, 0xFF000000, 0xFF000000,
Note: pre-build binares for Win32/Linux included, if they do not work it falls back to a pure-JS implementation.
Additionally native PNG read/write functions are exposed (falls back to pngjs where not available):
const { pngRead, pngWrite } = require('alphafix');
// Read an image in an object like { width, height, bpp (1-4), data (Buffer) }
let img = pngRead(fs.readFileSync('file.png'));
// Force image to be in 4-bytes-per-pixel format, regardless of source format
let img = pngRead(fs.readFileSync('file.png'), { bpp: 4 });
// Write an image from a { width, height, data } object
fs.writeFileSync('out.png', pngWrite(img));