UNPKG

image-js

Version:

Image processing and manipulation in JavaScript

25 lines (19 loc) 557 B
'use strict'; const { Image } =require( './common'); module.exports = function binary([str]) { let lines=str.split(/\r?\n/).map(line=>line.trim()).filter(line => line); let width=lines[0].length; let height=lines.length; const trimmed = lines.join(''); const data = new Uint8Array(Math.ceil(trimmed.length / 8)); for (let i = 0; i < trimmed.length; i += 8) { const index = i / 8; data[index] = parseInt(trimmed.substr(i, 8).padEnd(8, '0'), 2); } return new Image( width, height, data, { kind: 'BINARY' }, ); };