imagelogger
Version:
Shows an image in console.
39 lines (30 loc) • 859 B
text/typescript
import { readFileSync } from 'fs';
import log, { Pixel, Image } from './main'
import { splitEvery } from './utils';
const fileName = process.argv[2];
if (!fileName) {
console.error('You need to pass a filename.');
process.exit(1);
}
try {
readFileSync(fileName);
} catch (e) {
console.error(`File could not be read, error follows.`);
console.error(e);
process.exit(1);
}
const image = readFileSync(fileName, 'utf8');
const rows = image.split(/\r?\n/);
const pixels = rows.map(r => splitEvery(r, 6)).map(r => {
return r.map(chars => {
return {
red: parseInt(chars[0] + chars[1], 16),
green: parseInt(chars[2] + chars[3], 16),
blue: parseInt(chars[4] + chars[5], 16),
} as Pixel
});
});
const img = new Image(pixels[0].length, pixels.length);
img.buffer = pixels;
log(img);