4 changed files with 319 additions and 298 deletions
@ -1,24 +1,30 @@ |
|||||||
// This converts an image to grayscale
|
/* eslint-disable no-bitwise */ |
||||||
|
/* eslint-disable max-len */ |
||||||
|
|
||||||
module.exports = function desaturate(image){ |
// This converts an image to grayscale
|
||||||
var width, height; |
module.exports = (image) => { |
||||||
if (image.data) { |
if (image.data) { |
||||||
var src = image.data; |
const src = image.data; |
||||||
width = image.width, |
const { width, height } = image; |
||||||
height = image.height; |
const dst = new Uint8Array(width * height); |
||||||
var dst = new Uint8Array(width * height); |
const srcLength = src.length | 0; |
||||||
var srcLength = src.length | 0, srcLength_16 = (srcLength - 16) | 0; |
const srcLength16 = (srcLength - 16) | 0; |
||||||
|
let i = 0; |
||||||
|
let j = 0; |
||||||
|
|
||||||
for (var i = 0, j = 0; i <= srcLength_16; i += 16, j += 4) { |
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
|
// 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] = (((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 + 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 + 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 |
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; |
||||||
} |
} |
||||||
for (; i < srcLength; i += 4, ++j) //finish up
|
return dst; |
||||||
dst[j] = (((src[i] * 77 + src[i+1] * 151 + src[i+2] * 28) * src[i+3]) + ((255-src[i+3]) << 15) + 32768) >> 16 |
|
||||||
image = dst; |
|
||||||
} else { throw 'Invalid ImageData' } |
|
||||||
return image |
|
||||||
} |
} |
||||||
|
return null; |
||||||
|
// throw { err: 'Invalid ImageData' };
|
||||||
|
}; |
||||||
|
@ -1,81 +1,86 @@ |
|||||||
const adapter = require('../node/index.js') |
const adapter = require('../node/'); |
||||||
|
|
||||||
let jobCounter = 0; |
let jobCounter = 0; |
||||||
|
|
||||||
module.exports = class TesseractJob { |
module.exports = class TesseractJob { |
||||||
constructor(instance) { |
constructor(instance) { |
||||||
this.id = 'Job-' + (++jobCounter) + '-' + Math.random().toString(16).slice(3, 8) |
jobCounter += 1; |
||||||
|
this.id = `Job-${jobCounter}-${Math.random().toString(16).slice(3, 8)}`; |
||||||
|
|
||||||
this._instance = instance; |
this._instance = instance; |
||||||
this._resolve = [] |
this._resolve = []; |
||||||
this._reject = [] |
this._reject = []; |
||||||
this._progress = [] |
this._progress = []; |
||||||
this._finally = [] |
this._finally = []; |
||||||
} |
} |
||||||
|
|
||||||
then(resolve, reject) { |
then(resolve, reject) { |
||||||
if (this._resolve.push) { |
if (this._resolve.push) { |
||||||
this._resolve.push(resolve) |
this._resolve.push(resolve); |
||||||
} else { |
} else { |
||||||
resolve(this._resolve) |
resolve(this._resolve); |
||||||
} |
} |
||||||
|
|
||||||
if (reject) this.catch(reject); |
if (reject) this.catch(reject); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
catch(reject) { |
catch(reject) { |
||||||
if (this._reject.push) { |
if (this._reject.push) { |
||||||
this._reject.push(reject) |
this._reject.push(reject); |
||||||
} else { |
} else { |
||||||
reject(this._reject) |
reject(this._reject); |
||||||
} |
} |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
progress(fn) { |
progress(fn) { |
||||||
this._progress.push(fn) |
this._progress.push(fn); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
finally(fn) { |
finally(fn) { |
||||||
this._finally.push(fn) |
this._finally.push(fn); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
_send(action, payload) { |
_send(action, payload) { |
||||||
adapter.sendPacket(this._instance, { |
adapter.sendPacket(this._instance, { |
||||||
jobId: this.id, |
jobId: this.id, |
||||||
action: action, |
action, |
||||||
payload: payload |
payload, |
||||||
}) |
}); |
||||||
} |
} |
||||||
|
|
||||||
_handle(packet) { |
_handle(packet) { |
||||||
var data = packet.data; |
const { data } = packet; |
||||||
let runFinallyCbs = false; |
let runFinallyCbs = false; |
||||||
|
|
||||||
if (packet.status === 'resolve') { |
if (packet.status === 'resolve') { |
||||||
if (this._resolve.length === 0) console.log(data); |
if (this._resolve.length === 0) console.log(data); |
||||||
this._resolve.forEach(fn => { |
this._resolve.forEach((fn) => { |
||||||
var ret = fn(data); |
const ret = fn(data); |
||||||
if(ret && typeof ret.then == 'function'){ |
if (ret && typeof ret.then === 'function') { |
||||||
console.warn('TesseractJob instances do not chain like ES6 Promises. To convert it into a real promise, use Promise.resolve.') |
console.warn('TesseractJob instances do not chain like ES6 Promises. To convert it into a real promise, use Promise.resolve.'); |
||||||
} |
} |
||||||
}) |
}); |
||||||
this._resolve = data; |
this._resolve = data; |
||||||
this._instance._dequeue() |
this._instance._dequeue(); |
||||||
runFinallyCbs = true; |
runFinallyCbs = true; |
||||||
} else if (packet.status === 'reject') { |
} else if (packet.status === 'reject') { |
||||||
if (this._reject.length === 0) console.error(data); |
if (this._reject.length === 0) console.error(data); |
||||||
this._reject.forEach(fn => fn(data)) |
this._reject.forEach(fn => fn(data)); |
||||||
this._reject = data; |
this._reject = data; |
||||||
this._instance._dequeue() |
this._instance._dequeue(); |
||||||
runFinallyCbs = true; |
runFinallyCbs = true; |
||||||
} else if (packet.status === 'progress') { |
} else if (packet.status === 'progress') { |
||||||
this._progress.forEach(fn => fn(data)) |
this._progress.forEach(fn => fn(data)); |
||||||
} else { |
} else { |
||||||
console.warn('Message type unknown', packet.status) |
console.warn('Message type unknown', packet.status); |
||||||
} |
} |
||||||
|
|
||||||
if (runFinallyCbs) { |
if (runFinallyCbs) { |
||||||
this._finally.forEach(fn => fn(data)); |
this._finally.forEach(fn => fn(data)); |
||||||
} |
} |
||||||
} |
} |
||||||
} |
}; |
||||||
|
Loading…
Reference in new issue