node-png icon indicating copy to clipboard operation
node-png copied to clipboard

Create png from pixel array

Open JumpLink opened this issue 12 years ago • 1 comments

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?

Here can found my full source code.

JumpLink avatar Aug 07 '12 00:08 JumpLink

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();

victor73 avatar Dec 26 '12 20:12 victor73