Skip to content

Instantly share code, notes, and snippets.

@mickm
Created April 16, 2015 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mickm/4161975cfe98d8d30179 to your computer and use it in GitHub Desktop.
Save mickm/4161975cfe98d8d30179 to your computer and use it in GitHub Desktop.
rgba -> png example
#!/usr/bin/env node
var Png = require('png').Png; // https://github.com/pkrumins/node-png
var fs = require('fs');
var size = 256;
var rgba = new Buffer(size*size*4);
var x, y;
var png;
for (y = 0; y < size; y++) {
for (x = 0; x < size; x++) {
rgba.writeUInt8(y&x, (y*size+x)*4 + 0);
rgba.writeUInt8(y&x, (y*size+x)*4 + 1);
rgba.writeUInt8(y&x, (y*size+x)*4 + 2);
rgba.writeUInt8(y&x, (y*size+x)*4 + 3);
}
}
png = new Png(rgba, size, size, 'rgba');
png.encode(function(data,err) {
if (err) {
console.error(err);
process.exit(1);
}
fs.writeFileSync('wat.png', data.toString('binary'), 'binary');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment