|
|
|
@ -1,78 +1,56 @@
@@ -1,78 +1,56 @@
|
|
|
|
|
const fetch = require('isomorphic-fetch'), |
|
|
|
|
isURL = require('is-url'), |
|
|
|
|
fork = require('child_process').fork, |
|
|
|
|
fs = require('fs'); |
|
|
|
|
const check = require('check-types'); |
|
|
|
|
const fetch = require('isomorphic-fetch'); |
|
|
|
|
const isURL = require('is-url'); |
|
|
|
|
const { fork } = require('child_process'); |
|
|
|
|
const fs = require('fs'); |
|
|
|
|
const path = require('path'); |
|
|
|
|
const fileType = require('file-type'); |
|
|
|
|
const PNGReader = require('png.js'); |
|
|
|
|
const JPGReader = require('jpeg-js'); |
|
|
|
|
|
|
|
|
|
exports.defaultOptions = { |
|
|
|
|
workerPath: require('path').join(__dirname, 'worker.js'), |
|
|
|
|
langPath: 'https://cdn.jsdelivr.net/gh/naptha/tessdata@gh-pages/4.0.0/', |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
exports.spawnWorker = function spawnWorker(instance, workerOptions){ |
|
|
|
|
var cp = fork(workerOptions.workerPath); |
|
|
|
|
cp.on('message', packet => { |
|
|
|
|
instance._recv(packet); |
|
|
|
|
}); |
|
|
|
|
return cp; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
exports.terminateWorker = function(instance){ |
|
|
|
|
instance.worker.kill(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
exports.sendPacket = function sendPacket(instance, packet){ |
|
|
|
|
loadImage(packet.payload.image, img => { |
|
|
|
|
packet.payload.image = img; |
|
|
|
|
instance.worker.send(packet); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function loadImage(image, cb){ |
|
|
|
|
|
|
|
|
|
if(typeof image === 'string'){ |
|
|
|
|
function loadImage(image, cb) { |
|
|
|
|
if (check.string(image)) { |
|
|
|
|
if (isURL(image)) { |
|
|
|
|
fetch(image) |
|
|
|
|
.then(resp => resp.buffer()) |
|
|
|
|
.then(buffer => loadImage(buffer, cb)) |
|
|
|
|
.catch(err => console.error(err)); |
|
|
|
|
} else { |
|
|
|
|
fs.readFile(image, function(err, buffer){ |
|
|
|
|
fs.readFile(image, (err, buffer) => { |
|
|
|
|
if (err) throw err; |
|
|
|
|
loadImage(buffer, cb); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} else if (image instanceof Buffer){ |
|
|
|
|
var mime = require('file-type')(image).mime |
|
|
|
|
} |
|
|
|
|
if (image instanceof Buffer) { |
|
|
|
|
const { mime } = fileType(image); |
|
|
|
|
|
|
|
|
|
if(mime === 'image/png'){ |
|
|
|
|
var PNGReader = require('png.js'); |
|
|
|
|
var reader = new PNGReader(image); |
|
|
|
|
reader.parse(function(err, png){ |
|
|
|
|
if (mime === 'image/png') { |
|
|
|
|
const reader = new PNGReader(image); |
|
|
|
|
reader.parse((err, png) => { |
|
|
|
|
if (err) throw err; |
|
|
|
|
|
|
|
|
|
var image = { |
|
|
|
|
width: png.getWidth(), |
|
|
|
|
height: png.getHeight() |
|
|
|
|
} |
|
|
|
|
image.data = new Uint8Array(image.width * image.height * 4) |
|
|
|
|
for(var j = 0; j < image.height; j++){ |
|
|
|
|
for(var i = 0; i < image.width; i++){ |
|
|
|
|
var offset = 4 * (i + j * image.width), |
|
|
|
|
pix = png.getPixel(i, j); |
|
|
|
|
const width = png.getWidth(); |
|
|
|
|
const height = png.getHeight(); |
|
|
|
|
const data = new Uint8Array(width * height * 4); |
|
|
|
|
|
|
|
|
|
for (let j = 0; j < height; j += 1) { |
|
|
|
|
for (let i = 0; i < width; i += 1) { |
|
|
|
|
const offset = 4 * (i + (j * width)); |
|
|
|
|
const pix = png.getPixel(i, j); |
|
|
|
|
|
|
|
|
|
image.data[offset] = pix[0]; |
|
|
|
|
image.data[offset + 1] = pix[1]; |
|
|
|
|
image.data[offset + 2] = pix[2]; |
|
|
|
|
image.data[offset + 3] = pix[3]; |
|
|
|
|
Array(4).fill(0).forEach((v, idx) => { |
|
|
|
|
data[offset + idx] = pix[idx]; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
loadImage(image, cb); |
|
|
|
|
loadImage({ width, height, data }, cb); |
|
|
|
|
}); |
|
|
|
|
return; |
|
|
|
|
} else if (mime === 'image/jpeg'){ |
|
|
|
|
loadImage(require('jpeg-js').decode(image), cb); |
|
|
|
|
} |
|
|
|
|
if (mime === 'image/jpeg') { |
|
|
|
|
loadImage(JPGReader.decode(image), cb); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -81,9 +59,38 @@ function loadImage(image, cb){
@@ -81,9 +59,38 @@ function loadImage(image, cb){
|
|
|
|
|
|
|
|
|
|
// node uses json.stringify for ipc which means we need to turn
|
|
|
|
|
// fancy arrays into raw arrays
|
|
|
|
|
if(image && image.data && image.data.length && !Array.isArray(image.data)){ |
|
|
|
|
image.data = Array.from(image.data); |
|
|
|
|
return loadImage(image, cb) |
|
|
|
|
if (image && image.data && image.data.length && !Array.isArray(image.data)) { |
|
|
|
|
loadImage({ ...image, data: Array.from(image.data) }, cb); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
cb(image); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
exports.defaultOptions = { |
|
|
|
|
workerPath: path.join(__dirname, 'worker.js'), |
|
|
|
|
langPath: 'https://cdn.jsdelivr.net/gh/naptha/tessdata@gh-pages/4.0.0/', |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
exports.spawnWorker = (instance, { workerPath }) => { |
|
|
|
|
const cp = fork(workerPath); |
|
|
|
|
cp.on('message', (packet) => { |
|
|
|
|
instance._recv(packet); |
|
|
|
|
}); |
|
|
|
|
return cp; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
exports.terminateWorker = (instance) => { |
|
|
|
|
instance.worker.kill(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
exports.sendPacket = (instance, packet) => { |
|
|
|
|
loadImage(packet.payload.image, (img) => { |
|
|
|
|
instance.worker.send({ |
|
|
|
|
...packet, |
|
|
|
|
payload: { |
|
|
|
|
...packet.payload, |
|
|
|
|
image: img, |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|