-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Investigate if we can safely remove the use of Uint8clampedArray when it's useless #14849
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
Comments
I suppose that it's possible that we currently use the |
For example it seems to be useless here: Line 358 in 694cb31
and even here (which is almost the previous version of the code above): Line 303 in 694cb31
or here: Lines 714 to 738 in 694cb31
or: Lines 742 to 757 in 694cb31
Maybe we could remove the Lines 715 to 726 in 694cb31
Just a sort of Copy-on-Write wrapper... something like that.And the clamped version is only useful when some data are potentially out of the 0-255 range, but in case we do stuff like buffer[i] ^= 0xFF it isn't required.
The problem I see with the |
I'd suggest that we simply remove the
What you're suggesting, assuming I'm understanding you correctly, is changing the return type of Edit: While you're technically correct about this case, note that it doesn't actually matter in practice since the JPEG decoder only returns |
I'm not crazy ;) Lines 616 to 619 in b72a448
My feeling is that having a CoW wrapper would help for performance but for code readability too. To give you some context, in order to move some image stuff on the worker instead of the main thread in using some OffscreenCanvas, I was wondering for example if could directly create a |
Oh, that's the missing piece of context required to understand what you meant here :-) Basically you want to limit the previous |
@Snuffleupagus, if you agree that it'd make things better, would mind to fix this bug ? |
Maybe, but I'm not sure exactly what you have in mind for the diff --git a/src/core/image.js b/src/core/image.js
index af0bf6f4a..6985024c1 100644
--- a/src/core/image.js
+++ b/src/core/image.js
@@ -872,7 +872,16 @@ class PDFImage {
this.image.drawWidth = drawWidth || this.width;
this.image.drawHeight = drawHeight || this.height;
this.image.forceRGB = !!forceRGB;
- return this.image.getBytes(length, /* forceClamped = */ true);
+
+ const imageBytes = this.image.getBytes(length);
+ return {
+ raw: imageBytes,
+ clamped: () => {
+ return imageBytes instanceof Uint8ClampedArray
+ ? imageBytes
+ : new Uint8ClampedArray(imageBytes);
+ },
+ };
}
} |
Yep and if possible the return object could handle the |
After taking a brief look, and running all tests with a quickly thrown together patch, I'm not actually sure that we need any of the In the following cases we're returning the data without any
In the following cases the image-data is only used internally, and again the exact TypedArray used shouldn't matter:
|
For example here:
pdf.js/src/core/decode_stream.js
Line 101 in 694cb31
we use the ctor with a typedArray argument:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray/Uint8ClampedArray#typedarray
and ergo data are copied.
And as far as I understand,
Uint8ClampedArray
is especially useful when some data are written in the array but in general (afaik) there are no difference with anUint8Array
.Those
Uint8ClampedArray
have been added in #9802.and it clearly makes sense for color stuff, for image.js::undoPreblend and maybe few other cases I overlooked but in general my feeling is that they're useless and their use induces a performance penalty.
@Snuffleupagus, any opinion about that ?
The text was updated successfully, but these errors were encountered: