image_utils.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.applyMaskImageData = applyMaskImageData;
  27. var _util = require("./util.js");
  28. function applyMaskImageData({
  29. src,
  30. srcPos = 0,
  31. dest,
  32. destPos = 0,
  33. width,
  34. height,
  35. inverseDecode = false
  36. }) {
  37. const opaque = _util.FeatureTest.isLittleEndian ? 0xff000000 : 0x000000ff;
  38. const [zeroMapping, oneMapping] = !inverseDecode ? [opaque, 0] : [0, opaque];
  39. const widthInSource = width >> 3;
  40. const widthRemainder = width & 7;
  41. const srcLength = src.length;
  42. dest = new Uint32Array(dest.buffer);
  43. for (let i = 0; i < height; i++) {
  44. for (const max = srcPos + widthInSource; srcPos < max; srcPos++) {
  45. const elem = srcPos < srcLength ? src[srcPos] : 255;
  46. dest[destPos++] = elem & 0b10000000 ? oneMapping : zeroMapping;
  47. dest[destPos++] = elem & 0b1000000 ? oneMapping : zeroMapping;
  48. dest[destPos++] = elem & 0b100000 ? oneMapping : zeroMapping;
  49. dest[destPos++] = elem & 0b10000 ? oneMapping : zeroMapping;
  50. dest[destPos++] = elem & 0b1000 ? oneMapping : zeroMapping;
  51. dest[destPos++] = elem & 0b100 ? oneMapping : zeroMapping;
  52. dest[destPos++] = elem & 0b10 ? oneMapping : zeroMapping;
  53. dest[destPos++] = elem & 0b1 ? oneMapping : zeroMapping;
  54. }
  55. if (widthRemainder === 0) {
  56. continue;
  57. }
  58. const elem = srcPos < srcLength ? src[srcPos++] : 255;
  59. for (let j = 0; j < widthRemainder; j++) {
  60. dest[destPos++] = elem & 1 << 7 - j ? oneMapping : zeroMapping;
  61. }
  62. }
  63. return {
  64. srcPos,
  65. destPos
  66. };
  67. }