Jerome Wu
6 years ago
4 changed files with 131 additions and 116 deletions
@ -1,89 +1,96 @@
@@ -1,89 +1,96 @@
|
||||
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; |
||||
} |
||||
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, (err, buffer) => { |
||||
if (err) throw err; |
||||
loadImage(buffer, cb); |
||||
}); |
||||
} |
||||
return; |
||||
} |
||||
if (image instanceof Buffer) { |
||||
const { mime } = fileType(image); |
||||
|
||||
exports.terminateWorker = function(instance){ |
||||
instance.worker.kill(); |
||||
} |
||||
if (mime === 'image/png') { |
||||
const reader = new PNGReader(image); |
||||
reader.parse((err, png) => { |
||||
if (err) throw err; |
||||
|
||||
exports.sendPacket = function sendPacket(instance, packet){ |
||||
loadImage(packet.payload.image, img => { |
||||
packet.payload.image = img; |
||||
instance.worker.send(packet); |
||||
}); |
||||
} |
||||
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); |
||||
|
||||
function loadImage(image, cb){ |
||||
|
||||
if(typeof image === 'string'){ |
||||
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){ |
||||
if (err) throw err; |
||||
loadImage(buffer, cb); |
||||
Array(4).fill(0).forEach((v, idx) => { |
||||
data[offset + idx] = pix[idx]; |
||||
}); |
||||
} |
||||
} |
||||
return; |
||||
} else if (image instanceof Buffer){ |
||||
var mime = require('file-type')(image).mime |
||||
loadImage({ width, height, data }, cb); |
||||
}); |
||||
return; |
||||
} |
||||
if (mime === 'image/jpeg') { |
||||
loadImage(JPGReader.decode(image), cb); |
||||
return; |
||||
} |
||||
|
||||
if(mime === 'image/png'){ |
||||
var PNGReader = require('png.js'); |
||||
var reader = new PNGReader(image); |
||||
reader.parse(function(err, png){ |
||||
if (err) throw err; |
||||
// TODO: support for TIFF, NetPBM, BMP, etc.
|
||||
} |
||||
|
||||
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); |
||||
// 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)) { |
||||
loadImage({ ...image, data: Array.from(image.data) }, cb); |
||||
return; |
||||
} |
||||
cb(image); |
||||
} |
||||
|
||||
image.data[offset] = pix[0]; |
||||
image.data[offset + 1] = pix[1]; |
||||
image.data[offset + 2] = pix[2]; |
||||
image.data[offset + 3] = pix[3]; |
||||
} |
||||
} |
||||
loadImage(image, cb); |
||||
}); |
||||
return; |
||||
} else if (mime === 'image/jpeg'){ |
||||
loadImage(require('jpeg-js').decode(image), cb); |
||||
return; |
||||
} |
||||
exports.defaultOptions = { |
||||
workerPath: path.join(__dirname, 'worker.js'), |
||||
langPath: 'https://cdn.jsdelivr.net/gh/naptha/tessdata@gh-pages/4.0.0/', |
||||
}; |
||||
|
||||
// TODO: support for TIFF, NetPBM, BMP, etc.
|
||||
} |
||||
exports.spawnWorker = (instance, { workerPath }) => { |
||||
const cp = fork(workerPath); |
||||
cp.on('message', (packet) => { |
||||
instance._recv(packet); |
||||
}); |
||||
return cp; |
||||
}; |
||||
|
||||
// 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) |
||||
} |
||||
cb(image); |
||||
} |
||||
exports.terminateWorker = (instance) => { |
||||
instance.worker.kill(); |
||||
}; |
||||
|
||||
exports.sendPacket = (instance, packet) => { |
||||
loadImage(packet.payload.image, (img) => { |
||||
instance.worker.send({ |
||||
...packet, |
||||
payload: { |
||||
...packet.payload, |
||||
image: img, |
||||
}, |
||||
}); |
||||
}); |
||||
}; |
||||
|
Loading…
Reference in new issue