Skip to content

Commit 0ab5204

Browse files
committed
Optimize RGBA to grayscale conversion
1 parent 0fe6e78 commit 0ab5204

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@undecaf/zbar-wasm",
3-
"version": "0.9.9",
3+
"version": "0.9.10",
44
"description": "A WebAssembly build of the C/C++ ZBar barcode reader",
55
"type": "module",
66
"main": "./dist/main.cjs",

src/ZBarImage.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ export class ZBarImage extends CppObject {
4242
if (len * 4 !== data.byteLength) {
4343
throw Error('dataBuf does not match width and height');
4444
}
45-
const buf = inst._malloc(len);
46-
for (let i = 0; i < len; ++i) {
47-
const r = data[i * 4];
48-
const g = data[i * 4 + 1];
49-
const b = data[i * 4 + 2];
50-
heap[buf + i] = (r * 19595 + g * 38469 + b * 7472) >> 16;
45+
const
46+
buf = inst._malloc(len),
47+
bufEnd = buf + len;
48+
for (let i = buf, j = 0; i < bufEnd; i++, j += 4) {
49+
heap[i] = (
50+
data[j] * 19595 +
51+
data[j + 1] * 38469 +
52+
data[j + 2] * 7472
53+
) >> 16;
5154
}
5255
const ptr = inst._Image_create(
5356
width,

0 commit comments

Comments
 (0)