Skip to content

Instantly share code, notes, and snippets.

@inflammable
Last active February 27, 2021 01:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save inflammable/10981335 to your computer and use it in GitHub Desktop.
Save inflammable/10981335 to your computer and use it in GitHub Desktop.
GDepth:Data PNG Depth Map extractor
// A very crude tool to extract GDepth:Data chunks from the XMP data in images
// that use the Lens Blur feature in Google's Android Camera App
// Usage: node gdepth-extract.js <image name.jpg>
// Outputs <image name.png>
// Requires Buffer Tools for buffer.indexOf(): npm install buffertools
require('buffertools').extend();
var fs = require('fs'),
image = (process.argv)[2],
outFilename = image.slice(0,image.lastIndexOf('.')) + '.png',
data,
stringMarkers = {
start: 'GDepth:Data="',
end: '"'
},
findData = function() {
var xmpBuffer = new Buffer(data.toString('utf8'), 'base64');
fs.writeFileSync(outFilename, xmpBuffer, {encoding: 'utf8'});
},
getFileContents = function() {
var tempData = fs.readFileSync(image),
startMarker = tempData.indexOf(stringMarkers.start),
endMarker = tempData.indexOf(stringMarkers.end, startMarker + stringMarkers.start.length);
dataBuffer = tempData.slice((startMarker + stringMarkers.start.length), (endMarker));
var bufferLength = dataBuffer.length + 1,
bufferPos = 0,
fillBuffer = new Buffer(dataBuffer.length),
fillBufferPos = 0,
skipLength = 78; // remove the 78 bytes of binary crud - see: http://lunokhod.org/?p=1486
while(bufferLength--) {
if(bufferLength < 1) {
return;
} else {
bufferPos = dataBuffer.length - bufferLength;
}
if(dataBuffer[bufferPos] == 255) { // this is very crude
bufferLength -= skipLength;
} else {
fillBuffer.writeUInt8(dataBuffer.readUInt8(bufferPos), fillBufferPos);
fillBufferPos++;
}
data = fillBuffer.slice(0, fillBufferPos);
}
},
init = function() {
getFileContents();
findData();
};
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment