Skip to content

Instantly share code, notes, and snippets.

@energizer91
Created March 1, 2019 10:06
Show Gist options
  • Save energizer91/6a36bf093381fedd613056a8fc54e06e to your computer and use it in GitHub Desktop.
Save energizer91/6a36bf093381fedd613056a8fc54e06e to your computer and use it in GitHub Desktop.
Get diff from 2 PNGs and recreate them
const PNG = require('pngjs').PNG;
const fs = require('fs');
const img1 = fs.createReadStream('./4a.png').pipe(new PNG()).on('parsed', doneReading);
const img2 = fs.createReadStream('./4b.png').pipe(new PNG()).on('parsed', doneReading);
let filesRead = 0;
function applyPixels(data1, data2, width, height, result) {
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const pos = (y * width + x) * 4;
xorPixels(data1, data2, pos, result);
}
}
}
function xorPixels(data1, data2, pos, result) {
result[pos + 0] = data1[pos + 0] ^ data2[pos + 0];
result[pos + 1] = data1[pos + 1] ^ data2[pos + 1];
result[pos + 2] = data1[pos + 2] ^ data2[pos + 2];
result[pos + 3] = data1[pos + 3] ^ data2[pos + 3];
}
function doneReading() {
if (++filesRead < 2) return;
const {width, height} = img1;
const result = new Buffer(height * width * 4); // 4 because R, G, B and alpha
// get diff buffer from two images
applyPixels(img1.data, img2.data, width, height, result);
// now we have result matrix. and we can recreate both images
// recreate image1 from image2
const png1FromPng2 = new PNG({width, height});
applyPixels(img2.data, result, width, height, png1FromPng2.data);
png1FromPng2.pack().pipe(fs.createWriteStream('diff1from2.png'));
// recreate image2 from image1
const png2FromPng1 = new PNG({width, height});
applyPixels(img1.data, result, width, height, png2FromPng1.data);
png2FromPng1.pack().pipe(fs.createWriteStream('diff2from1.png'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment