UNPKG

2.37 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * JavaScript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.applyMaskImageData = applyMaskImageData;
28
29var _util = require("./util.js");
30
31function applyMaskImageData({
32 src,
33 srcPos = 0,
34 dest,
35 destPos = 0,
36 width,
37 height,
38 inverseDecode = false
39}) {
40 const opaque = _util.FeatureTest.isLittleEndian ? 0xff000000 : 0x000000ff;
41 const [zeroMapping, oneMapping] = !inverseDecode ? [opaque, 0] : [0, opaque];
42 const widthInSource = width >> 3;
43 const widthRemainder = width & 7;
44 const srcLength = src.length;
45 dest = new Uint32Array(dest.buffer);
46
47 for (let i = 0; i < height; i++) {
48 for (const max = srcPos + widthInSource; srcPos < max; srcPos++) {
49 const elem = srcPos < srcLength ? src[srcPos] : 255;
50 dest[destPos++] = elem & 0b10000000 ? oneMapping : zeroMapping;
51 dest[destPos++] = elem & 0b1000000 ? oneMapping : zeroMapping;
52 dest[destPos++] = elem & 0b100000 ? oneMapping : zeroMapping;
53 dest[destPos++] = elem & 0b10000 ? oneMapping : zeroMapping;
54 dest[destPos++] = elem & 0b1000 ? oneMapping : zeroMapping;
55 dest[destPos++] = elem & 0b100 ? oneMapping : zeroMapping;
56 dest[destPos++] = elem & 0b10 ? oneMapping : zeroMapping;
57 dest[destPos++] = elem & 0b1 ? oneMapping : zeroMapping;
58 }
59
60 if (widthRemainder === 0) {
61 continue;
62 }
63
64 const elem = srcPos < srcLength ? src[srcPos++] : 255;
65
66 for (let j = 0; j < widthRemainder; j++) {
67 dest[destPos++] = elem & 1 << 7 - j ? oneMapping : zeroMapping;
68 }
69 }
70
71 return {
72 srcPos,
73 destPos
74 };
75}
\No newline at end of file