Last active
February 27, 2021 01:15
-
-
Save inflammable/10981335 to your computer and use it in GitHub Desktop.
GDepth:Data PNG Depth Map extractor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
See: http://googleresearch.blogspot.co.nz/2014/04/lens-blur-in-new-google-camera-app.html