node-png
node-png copied to clipboard
Create png from pixel array
I have an buffer with number values from 0 to 255, I use rgba so the first number is the r value, the second the g value, .. and the last the alpha value.
The size of my image is 16_16 pixel. The size of my buffer is 16_16*4.
This is my important Code:
res.type('png'); //for expressjs
var tex = ...
var rgba = new Buffer(tex.length);
for (var i = 0; i < tex.length; i++) {
rgba[i] = tex.copy_pixel(i);
process.stdout.write(rgba[i]+" ");
};
var png = new Png(rgba, tex.width, tex.height, 'rgba');
var png_image = png.encodeSync();
res.send(png_image); //for expressjs
The output of process.stdout.write are 1024 numbers with a value 0 to 255.
An idea why my program does not work?
what I do is:
// pixelArray here is an array containing the values for R, G, B, and A (alpha) channels
var rgba = new Buffer(pixelArray.length);
for ( var index = 0; index < pixelArray.length; index++ ) {
rgba.writeUInt8( pixelArray[index], index )
}
var imgData = new Png(rgba, width, height, 'rgba');
var pngImage = imgData.encodeSync();