Skip to content

Commit

Permalink
Merge pull request #11557 from Snuffleupagus/_getLinearizedBlockData-…
Browse files Browse the repository at this point in the history
…xScaleBlockOffset

Avoid re-calculating the `xScaleBlockOffset` when not necessary in `JpegImage._getLinearizedBlockData`
  • Loading branch information
timvandermeij authored Feb 9, 2020
2 parents 7948faf + a4440a1 commit f178805
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/core/jpg.js
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,7 @@ var JpegImage = (function JpegImageClosure() {
var data = new Uint8ClampedArray(dataLength);
var xScaleBlockOffset = new Uint32Array(width);
var mask3LSB = 0xfffffff8; // used to clear the 3 LSBs
let lastComponentScaleX;

for (i = 0; i < numComponents; i++) {
component = this.components[i];
Expand All @@ -1113,10 +1114,14 @@ var JpegImage = (function JpegImageClosure() {
offset = i;
output = component.output;
blocksPerScanline = (component.blocksPerLine + 1) << 3;
// precalculate the xScaleBlockOffset
for (x = 0; x < width; x++) {
j = 0 | (x * componentScaleX);
xScaleBlockOffset[x] = ((j & mask3LSB) << 3) | (j & 7);
// Precalculate the `xScaleBlockOffset`. Since it doesn't depend on the
// component data, that's only necessary when `componentScaleX` changes.
if (componentScaleX !== lastComponentScaleX) {
for (x = 0; x < width; x++) {
j = 0 | (x * componentScaleX);
xScaleBlockOffset[x] = ((j & mask3LSB) << 3) | (j & 7);
}
lastComponentScaleX = componentScaleX;
}
// linearize the blocks of the component
for (y = 0; y < height; y++) {
Expand Down

0 comments on commit f178805

Please sign in to comment.