From b843f08705fb6eb76ee7daa6a5806836d922066a Mon Sep 17 00:00:00 2001 From: Jerome Wu Date: Fri, 23 Nov 2018 09:55:24 +0800 Subject: [PATCH] Minor refactor --- src/common/{job.js => TesseractJob.js} | 6 +- src/common/TesseractWorker.js | 89 +++++++++++++++++++++++++ src/common/desaturate.js | 30 --------- src/index.js | 90 +------------------------- 4 files changed, 96 insertions(+), 119 deletions(-) rename src/common/{job.js => TesseractJob.js} (97%) create mode 100644 src/common/TesseractWorker.js delete mode 100644 src/common/desaturate.js diff --git a/src/common/job.js b/src/common/TesseractJob.js similarity index 97% rename from src/common/job.js rename to src/common/TesseractJob.js index d691fad..843a41c 100644 --- a/src/common/job.js +++ b/src/common/TesseractJob.js @@ -2,7 +2,7 @@ const adapter = require('../node/'); let jobCounter = 0; -module.exports = class TesseractJob { +class TesseractJob { constructor(instance) { jobCounter += 1; this.id = `Job-${jobCounter}-${Math.random().toString(16).slice(3, 8)}`; @@ -83,4 +83,6 @@ module.exports = class TesseractJob { this._finally.forEach(fn => fn(data)); } } -}; +} + +module.exports = TesseractJob; diff --git a/src/common/TesseractWorker.js b/src/common/TesseractWorker.js new file mode 100644 index 0000000..063f44f --- /dev/null +++ b/src/common/TesseractWorker.js @@ -0,0 +1,89 @@ +const check = require('check-types'); +const adapter = require('../node'); +const circularize = require('./circularize'); +const TesseractJob = require('./TesseractJob'); + +class TesseractWorker { + constructor(workerOptions = {}) { + this.worker = null; + this.workerOptions = Object.assign({}, adapter.defaultOptions, workerOptions); + this._currentJob = null; + this._queue = []; + } + + recognize(image, options = { lang: 'eng' }) { + return this._delay((job) => { + job._send( + 'recognize', + { + image, + options: check.string(options) + ? { lang: options || 'eng' } + : options, + workerOptions: this.workerOptions, + }, + ); + }); + } + + detect(image, options = {}) { + return this._delay((job) => { + job._send( + 'detect', + { + image, + options, + workerOptions: this.workerOptions, + }, + ); + }); + } + + terminate() { + if (this.worker) { + adapter.terminateWorker(this); + } + this.worker = null; + this._currentJob = null; + this._queue = []; + } + + _delay(fn) { + if (check.null(this.worker)) { + this.worker = adapter.spawnWorker(this, this.workerOptions); + } + + const job = new TesseractJob(this); + this._queue.push(() => { + this._queue.shift(); + this._currentJob = job; + fn(job); + }); + if (check.null(this._currentJob)) { + this._dequeue(); + } + return job; + } + + _dequeue() { + this._currentJob = null; + if (this._queue.length) { + this._queue[0](); + } + } + + _recv(packet) { + if (this._currentJob.id === packet.jobId) { + this._currentJob._handle({ + ...packet, + data: packet.status === 'resolve' && packet.action === 'recognize' + ? circularize(packet.data) + : packet.data, + }); + } else { + console.warn(`Job ID ${packet.jobId} not known.`); + } + } +} + +module.exports = TesseractWorker; diff --git a/src/common/desaturate.js b/src/common/desaturate.js deleted file mode 100644 index 0136ab9..0000000 --- a/src/common/desaturate.js +++ /dev/null @@ -1,30 +0,0 @@ -/* eslint-disable no-bitwise */ -/* eslint-disable max-len */ - -// This converts an image to grayscale -module.exports = (image) => { - if (image.data) { - const src = image.data; - const { width, height } = image; - const dst = new Uint8Array(width * height); - const srcLength = src.length | 0; - const srcLength16 = (srcLength - 16) | 0; - let i = 0; - let j = 0; - - for (; i <= srcLength16; i += 16, j += 4) { - // convert to grayscale 4 pixels at a time; eveything with alpha gets put in front of 50% gray - dst[j] = (((src[i] * 77 + src[i + 1] * 151 + src[i + 2] * 28) * src[i + 3]) + ((255 - src[i + 3]) << 15) + 32768) >> 16; - dst[j + 1] = (((src[i + 4] * 77 + src[i + 5] * 151 + src[i + 6] * 28) * src[i + 7]) + ((255 - src[i + 7]) << 15) + 32768) >> 16; - dst[j + 2] = (((src[i + 8] * 77 + src[i + 9] * 151 + src[i + 10] * 28) * src[i + 11]) + ((255 - src[i + 11]) << 15) + 32768) >> 16; - dst[j + 3] = (((src[i + 12] * 77 + src[i + 13] * 151 + src[i + 14] * 28) * src[i + 15]) + ((255 - src[i + 15]) << 15) + 32768) >> 16; - } - // finish up - for (; i < srcLength; i += 4, j += 1) { - dst[j] = (((src[i] * 77 + src[i + 1] * 151 + src[i + 2] * 28) * src[i + 3]) + ((255 - src[i + 3]) << 15) + 32768) >> 16; - } - return dst; - } - return null; - // throw { err: 'Invalid ImageData' }; -}; diff --git a/src/index.js b/src/index.js index 6911dc1..4db7640 100644 --- a/src/index.js +++ b/src/index.js @@ -1,91 +1,7 @@ -const check = require('check-types'); -const adapter = require('./node'); -const circularize = require('./common/circularize'); -const TesseractJob = require('./common/job'); - -class TesseractWorker { - constructor(workerOptions = {}) { - this.worker = null; - this.workerOptions = Object.assign({}, adapter.defaultOptions, workerOptions); - this._currentJob = null; - this._queue = []; - } - - recognize(image, options = { lang: 'eng' }) { - return this._delay((job) => { - job._send( - 'recognize', - { - image, - options: check.string(options) - ? { lang: options || 'eng' } - : options, - workerOptions: this.workerOptions, - }, - ); - }); - } - - detect(image, options = {}) { - return this._delay((job) => { - job._send( - 'detect', - { - image, - options, - workerOptions: this.workerOptions, - }, - ); - }); - } - - terminate() { - if (this.worker) { - adapter.terminateWorker(this); - } - this.worker = null; - this._currentJob = null; - this._queue = []; - } - - _delay(fn) { - if (check.null(this.worker)) { - this.worker = adapter.spawnWorker(this, this.workerOptions); - } - - const job = new TesseractJob(this); - this._queue.push(() => { - this._queue.shift(); - this._currentJob = job; - fn(job); - }); - if (check.null(this._currentJob)) { - this._dequeue(); - } - return job; - } - - _dequeue() { - this._currentJob = null; - if (this._queue.length) { - this._queue[0](); - } - } - - _recv(packet) { - if (this._currentJob.id === packet.jobId) { - this._currentJob._handle({ - ...packet, - data: packet.status === 'resolve' && packet.action === 'recognize' - ? circularize(packet.data) - : packet.data, - }); - } else { - console.warn(`Job ID ${packet.jobId} not known.`); - } - } -} +const utils = require('tesseract.js-utils'); +const TesseractWorker = require('./common/TesseractWorker'); module.exports = { TesseractWorker, + utils, };