Last active
May 24, 2017 02:45
-
-
Save opparco/f569b4cbf31504d56d9a9f65860b8393 to your computer and use it in GitHub Desktop.
read PNG magic.
This file contains 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
/** | |
* read PNG file. | |
*/ | |
import * as fs from 'fs'; | |
function putUsage(): void | |
{ | |
const usage = "Usage: node pngreader.ts source.png"; | |
console.log(usage); | |
} | |
if (process.argv.length < 3) { | |
putUsage(); | |
process.exit(0); | |
} | |
const sourcePath = process.argv[2]; | |
fs.open(sourcePath, 'r', function (err, fd) { | |
let buffer = new Buffer(4); | |
fs.read(fd, buffer, 0, 4, null, function (err, bytesRead, buffer) { | |
console.log(buffer); | |
let png_p: boolean = ( | |
buffer[0] == 0x89 && | |
buffer[1] == 0x50 && // P | |
buffer[2] == 0x4e && // N | |
buffer[3] == 0x47 ); // G | |
console.log('This is ' + (png_p ? '' : 'not ') + 'PNG file!'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment