Skip to content

Instantly share code, notes, and snippets.

@kbinani
Last active March 7, 2017 08:20
Show Gist options
  • Save kbinani/8e7476725d002731be120eefce71ce5a to your computer and use it in GitHub Desktop.
Save kbinani/8e7476725d002731be120eefce71ce5a to your computer and use it in GitHub Desktop.
// https://github.com/gemini-testing/png-img
'use strict;'
var PngImg = require('png-img');
function save(pixel_data, width, height, file_path) {
// init with 1x1 PNG buffer.
var tmp = new PngImg(new Buffer([137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 8, 2, 0, 0, 0, 144, 119, 83, 222, 0, 0, 0, 1, 115, 82, 71, 66, 0, 174, 206, 28, 233, 0, 0, 0, 12, 73, 68, 65, 84, 8, 29, 99, 96, 96, 96, 0, 0, 0, 4, 0, 1, 100, 50, 83, 254, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130]));
tmp.setSize(width, height);
var idx = 0;
var color = {};
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
color.r = pixel_data[idx++];
color.g = pixel_data[idx++];
color.b = pixel_data[idx++];
color.a = pixel_data[idx++];
tmp.set(x, y, color);
}
}
tmp.save(file_path, null);
}
function load(file_path) {
var data = fs.readFileSync(file_path);
var tmp = new PngImg(data);
var size = tmp.size();
var width = size.width;
var height = size.height;
var pixel_data = new Buffer(width * height * 4);
var idx = 0;
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
var color = tmp.get(x, y);
pixel_data[idx++] = color.r;
pixel_data[idx++] = color.g;
pixel_data[idx++] = color.b;
pixel_data[idx++] = color.a;
}
}
return {
width: width,
height: height,
pixel_data: pixel_data
};
}
// https://github.com/baslr/png-light
'use strict;'
var PNG = require('png-light'),
fs = require('fs');
function save(pixel_data, width, height, file_path) {
var tmp = new PNG({width: width, height: height});
pixel_data.copy(tmp.data);
var buffer = tmp.pack();
fs.writeFileSync(file_path, buffer);
}
function load(file_path) {
var buffer = fs.readFileSync(file_path);
var tmp = new PNG({buffer: buffer});
var pixel_data = new Buffer(tmp.width * tmp.height * 4);
tmp.data.copy(pixel_data);
return {
width: tmp.width,
height: tmp.height,
pixel_data: pixel_data
};
}
// https://github.com/lukeapage/pngjs
'use strict;'
var png = require('pngjs'),
fs = require('fs');
function save(pixel_data, width, height, file_path) {
var tmp = new png.PNG({width: width, height: height});
pixel_data.copy(tmp.data);
var buffer = png.PNG.sync.write(tmp);
fs.writeFileSync(file_path, buffer);
}
function load(file_path) {
var data = fs.readFileSync(file_path);
var tmp = png.PNG.sync.read(data);
var pixel_data = new Buffer(tmp.width * tmp.height * 4);
tmp.data.copy(pixel_data);
return {
width: tmp.width,
height: tmp.height,
pixel_data: pixel_data
};
}
// https://github.com/GeorgeChan/pnglib
'use strict;'
var pnglib = require('pnglib'),
fs = require('fs');
function save(pixel_data, width, height, file_path) {
var tmp = new pnglib(width, height, 24);
var idx = 0;
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
var r = pixel_data[idx++];
var g = pixel_data[idx++];
var b = pixel_data[idx++];
var a = pixel_data[idx++];
var index = tmp.index(x, y);
tmp.buffer[index] = tmp.color(r, g, b, a);
}
}
var b64 = tmp.getBase64();
var buf = new Buffer(b64, 'base64');
fs.writeFileSync(file_path, buf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment