Merge pull request #14766 from calixteman/mask2

Improve performance of applyMaskImageData
This commit is contained in:
calixteman 2022-04-09 22:48:28 +02:00 committed by GitHub
commit 2c135b02e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,30 +13,42 @@
* limitations under the License. * limitations under the License.
*/ */
import { FeatureTest } from "./util.js";
function applyMaskImageData({ function applyMaskImageData({
src, src,
srcPos = 0, srcPos = 0,
dest, dest,
destPos = 3, destPos = 0,
width, width,
height, height,
inverseDecode = false, inverseDecode = false,
}) { }) {
const srcLength = src.byteLength; const opaque = FeatureTest.isLittleEndian ? 0xff000000 : 0x000000ff;
const zeroMapping = inverseDecode ? 0 : 255; const [zeroMapping, oneMapping] = !inverseDecode ? [opaque, 0] : [0, opaque];
const oneMapping = inverseDecode ? 255 : 0; const widthInSource = width >> 3;
const widthRemainder = width & 7;
const srcLength = src.length;
dest = new Uint32Array(dest.buffer);
for (let j = 0; j < height; j++) { for (let i = 0; i < height; i++) {
let elem, for (const max = srcPos + widthInSource; srcPos < max; srcPos++) {
mask = 0; const elem = srcPos < srcLength ? src[srcPos] : 255;
for (let k = 0; k < width; k++) { dest[destPos++] = elem & 0b10000000 ? oneMapping : zeroMapping;
if (mask === 0) { dest[destPos++] = elem & 0b1000000 ? oneMapping : zeroMapping;
elem = srcPos < srcLength ? src[srcPos++] : 255; dest[destPos++] = elem & 0b100000 ? oneMapping : zeroMapping;
mask = 128; dest[destPos++] = elem & 0b10000 ? oneMapping : zeroMapping;
} dest[destPos++] = elem & 0b1000 ? oneMapping : zeroMapping;
dest[destPos] = elem & mask ? oneMapping : zeroMapping; dest[destPos++] = elem & 0b100 ? oneMapping : zeroMapping;
destPos += 4; dest[destPos++] = elem & 0b10 ? oneMapping : zeroMapping;
mask >>= 1; dest[destPos++] = elem & 0b1 ? oneMapping : zeroMapping;
}
if (widthRemainder === 0) {
continue;
}
const elem = srcPos < srcLength ? src[srcPos++] : 255;
for (let j = 0; j < widthRemainder; j++) {
dest[destPos++] = elem & (1 << (7 - j)) ? oneMapping : zeroMapping;
} }
} }