Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support reading QR codes from ImageData #15

Merged
merged 4 commits into from Dec 3, 2020

Conversation

eventualbuddha
Copy link
Contributor

Rather than requiring that the data be in PNG or JPEG format, allow it to be an ImageData object with raw pixel data. This is useful if, for example, you are looking for a QR code in a portion of an image you've already loaded and cropped.

@kaworu
Copy link
Owner

kaworu commented Nov 17, 2020

Sorry I haven't been able to take a close look at this PR yet. The feature seems very useful, thank you! I'll try my best to review this week.

@kaworu kaworu self-requested a review November 17, 2020 19:33
Copy link
Owner

@kaworu kaworu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this PR! I have a few requests (mainly cosmetic, documentation, and about the width/height handling) but overall the diff looks good!

index.js Outdated Show resolved Hide resolved
src/node_quirc_decode.c Show resolved Hide resolved
src/node-quirc.cc Outdated Show resolved Hide resolved
index.js Outdated
});
}
} else if (img && typeof img === "object") {
const channels = img.data.length / img.width / img.height;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid rounding error here, and probably check for negative and zero img.width and img.height so that the addon code may rely on the fact that theses values are sane.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll sanity check img.width and img.height. As for avoiding rounding, it does check that channels is exactly 1, 3, or 4. Is that good enough or are you imagining a scenario where it's not one of those values but is extremely close and somehow still passes for equal?

src/node_quirc_decode.c Outdated Show resolved Hide resolved
src/node-quirc.cc Show resolved Hide resolved
src/node_quirc_decode.c Outdated Show resolved Hide resolved
src/node_quirc_decode.c Outdated Show resolved Hide resolved
src/node_quirc_decode.c Show resolved Hide resolved
src/node_quirc_decode.c Outdated Show resolved Hide resolved
@kaworu kaworu self-assigned this Nov 17, 2020
@kaworu kaworu added the feature label Nov 17, 2020
Rather than requiring that the data be in PNG or JPEG format, allow it to be an ImageData object with raw pixel data. This is useful if, for example, you are looking for a QR code in a portion of an image you've already loaded and cropped.
There is actually one small difference in this version: when previously a validation error would throw synchronously even when a promise was expected, now it returns a rejected promise. The `throw` behavior is still the same when a callback is provided, which is probably still not great as it means you need two handlers: one for anything thrown by the call to `decode` and another for the `err` in the callback.
@kaworu
Copy link
Owner

kaworu commented Nov 24, 2020

@eventualbuddha thanks for addressing my concerns, I'll do a second round of review this week.

return (
typeof number === 'number' &&
number > 0 &&
(number | 0) === number &&
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we trying to floor here? If yes, why the bitwise OR trick rather than Math.floor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Math.floor works fine. I think editing C files put me in a C mood 😛

expect(function () {
quirc.decode();
}).to.throw(Error, "img must be a Buffer");
it("should return a rejected Promise when no arguments are given", function (done) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This make sense, thanks.

@kaworu kaworu closed this Dec 3, 2020
@kaworu kaworu reopened this Dec 3, 2020
@kaworu kaworu merged commit ed5a170 into kaworu:master Dec 3, 2020
@eventualbuddha eventualbuddha deleted the feat/raw branch December 3, 2020 22:06
@eventualbuddha
Copy link
Contributor Author

Thanks, @kaworu! Looking forward to pulling this into https://github.com/votingworks/vxsuite.

@kaworu
Copy link
Owner

kaworu commented Dec 5, 2020

Of course it's time for a release! I just want to take the time to setup GitHub action to build & push to npm. At that point, quirc might have bumped to 1.1 (see this PR) and we can sync right before we release 2.3.

eventualbuddha added a commit to votingworks/vxsuite that referenced this pull request Jan 12, 2021
My PR was merged (kaworu/node-quirc#15), so now we can pass an `ImageData` directly to be decoded without encoding as a PNG first.
eventualbuddha added a commit to votingworks/vxsuite that referenced this pull request Jan 12, 2021
My PR was merged (kaworu/node-quirc#15), so now we can pass an `ImageData` directly to be decoded without encoding as a PNG first.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants