2022-04-06 22:34:08 +09:00
|
|
|
/* Copyright 2022 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-04-10 03:18:29 +09:00
|
|
|
import { FeatureTest } from "./util.js";
|
|
|
|
|
2022-04-06 22:34:08 +09:00
|
|
|
function applyMaskImageData({
|
|
|
|
src,
|
|
|
|
srcPos = 0,
|
|
|
|
dest,
|
2022-04-10 03:18:29 +09:00
|
|
|
destPos = 0,
|
2022-04-06 22:34:08 +09:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
inverseDecode = false,
|
|
|
|
}) {
|
2022-04-10 03:18:29 +09:00
|
|
|
const opaque = FeatureTest.isLittleEndian ? 0xff000000 : 0x000000ff;
|
|
|
|
const [zeroMapping, oneMapping] = !inverseDecode ? [opaque, 0] : [0, opaque];
|
|
|
|
const widthInSource = width >> 3;
|
|
|
|
const widthRemainder = width & 7;
|
|
|
|
const srcLength = src.length;
|
|
|
|
dest = new Uint32Array(dest.buffer);
|
2022-04-06 22:34:08 +09:00
|
|
|
|
2022-04-10 03:18:29 +09:00
|
|
|
for (let i = 0; i < height; i++) {
|
|
|
|
for (const max = srcPos + widthInSource; srcPos < max; srcPos++) {
|
|
|
|
const elem = srcPos < srcLength ? src[srcPos] : 255;
|
|
|
|
dest[destPos++] = elem & 0b10000000 ? oneMapping : zeroMapping;
|
|
|
|
dest[destPos++] = elem & 0b1000000 ? oneMapping : zeroMapping;
|
|
|
|
dest[destPos++] = elem & 0b100000 ? oneMapping : zeroMapping;
|
|
|
|
dest[destPos++] = elem & 0b10000 ? oneMapping : zeroMapping;
|
|
|
|
dest[destPos++] = elem & 0b1000 ? oneMapping : zeroMapping;
|
|
|
|
dest[destPos++] = elem & 0b100 ? oneMapping : zeroMapping;
|
|
|
|
dest[destPos++] = elem & 0b10 ? oneMapping : zeroMapping;
|
|
|
|
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;
|
2022-04-06 22:34:08 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { srcPos, destPos };
|
|
|
|
}
|
|
|
|
|
|
|
|
export { applyMaskImageData };
|