Browse Source

Rewrite part of src/common/work.js

pull/265/head
Jerome Wu 6 years ago
parent
commit
2b3221fbfe
  1. 14
      src/common/worker.js
  2. 87
      src/node/index.js

14
src/common/worker.js

@ -1,3 +1,4 @@ @@ -1,3 +1,4 @@
const readImage = require('tesseract.js-core/src/utils/readImage');
var latestJob,
Module,
base,
@ -69,14 +70,11 @@ function handleInit(req, res){ @@ -69,14 +70,11 @@ function handleInit(req, res){
}
function setImage(Module, base, image) {
const imgbin = desaturate(image);
const { width, height } = image;
const ptr = Module._malloc(imgbin.length, Uint8Array.BYTES_PER_ELEMENT);
Module.HEAPU8.set(imgbin, ptr);
base.SetImage(ptr, width, height, Uint8Array.BYTES_PER_ELEMENT, width);
base.SetRectangle(0, 0, width, height);
return ptr;
const { w, h, data } = readImage(Module, Array.from(image));
base.SetImage(data);
base.SetRectangle(0, 0, w, h);
return data;
}
function loadLanguage(req, res, cb){

87
src/node/index.js

@ -1,70 +1,19 @@ @@ -1,70 +1,19 @@
const check = require('check-types');
const util = require('util');
const fs = require('fs');
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');
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);
if (mime === 'image/png') {
const reader = new PNGReader(image);
reader.parse((err, png) => {
if (err) throw err;
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);
const readFile = util.promisify(fs.readFile);
Array(4).fill(0).forEach((v, idx) => {
data[offset + idx] = pix[idx];
});
}
}
loadImage({ width, height, data }, cb);
});
return;
}
if (mime === 'image/jpeg') {
loadImage(JPGReader.decode(image), cb);
return;
}
// TODO: support for TIFF, NetPBM, BMP, etc.
}
// 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;
const loadImage = (imageURI) => {
if (isURL(imageURI)) {
return fetch(imageURI)
.then(resp => resp.buffer());
}
cb(image);
}
return readFile(imageURI);
};
exports.defaultOptions = {
workerPath: path.join(__dirname, 'worker.js'),
@ -84,13 +33,15 @@ exports.terminateWorker = (instance) => { @@ -84,13 +33,15 @@ exports.terminateWorker = (instance) => {
};
exports.sendPacket = (instance, packet) => {
loadImage(packet.payload.image, (img) => {
instance.worker.send({
...packet,
payload: {
...packet.payload,
image: img,
},
loadImage(packet.payload.image)
.then(buf => new Uint8Array(buf))
.then((img) => {
instance.worker.send({
...packet,
payload: {
...packet.payload,
image: Array.from(img),
},
});
});
});
};

Loading…
Cancel
Save