devongovett / png-stream

A streaming PNG encoder and decoder
29 stars 4 forks source link

Help encoding indexed png to file #5

Open tslater opened 7 years ago

tslater commented 7 years ago

I tried encoding a basic png, using the test file as an example

var PNGEncoder = require('png-stream/encoder');
var fs = require('fs');

var palette = new Buffer([ 256 ]);
var pixels = new Buffer(10 * 10);
pixels.fill(1);

var enc = new PNGEncoder(10, 10, { colorSpace: 'indexed', palette: palette })
   .pipe(fs.createWriteStream('out.png'))
   .end(pixels);

My os says that the file is corrupted though. Where am I going wrong?

devongovett commented 7 years ago

In this example, the .end(pixels) writes to the file (pipe returns the destination stream, not the source stream). So try something like this:

var enc = new PNGEncoder(10, 10, { colorSpace: 'indexed', palette: palette })
   .pipe(fs.createWriteStream('out.png'));

enc.end(pixels)
tslater commented 7 years ago

I'm trying that, but have the same result:

var PNGEncoder = require('png-stream/encoder');
var fs = require('fs');

var palette = new Buffer([ 256 ]);
var pixels = new Buffer(10 * 10);
pixels.fill(1);

var enc = new PNGEncoder(10, 10, { colorSpace: 'indexed', palette: palette })
   .pipe(fs.createWriteStream('out.png'));

enc.end(pixels);

Thanks for your help by the way!