imageChannel.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ImageChannel = void 0;
  6. var _colorUtils = require("./colorUtils");
  7. /**
  8. * Copyright (c) Microsoft Corporation.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the 'License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. class ImageChannel {
  23. static intoRGB(width, height, data, options = {}) {
  24. const {
  25. paddingSize = 0,
  26. paddingColorOdd = [255, 0, 255],
  27. paddingColorEven = [0, 255, 0]
  28. } = options;
  29. const newWidth = width + 2 * paddingSize;
  30. const newHeight = height + 2 * paddingSize;
  31. const r = new Uint8Array(newWidth * newHeight);
  32. const g = new Uint8Array(newWidth * newHeight);
  33. const b = new Uint8Array(newWidth * newHeight);
  34. for (let y = 0; y < newHeight; ++y) {
  35. for (let x = 0; x < newWidth; ++x) {
  36. const index = y * newWidth + x;
  37. if (y >= paddingSize && y < newHeight - paddingSize && x >= paddingSize && x < newWidth - paddingSize) {
  38. const offset = ((y - paddingSize) * width + (x - paddingSize)) * 4;
  39. const alpha = data[offset + 3] === 255 ? 1 : data[offset + 3] / 255;
  40. r[index] = (0, _colorUtils.blendWithWhite)(data[offset], alpha);
  41. g[index] = (0, _colorUtils.blendWithWhite)(data[offset + 1], alpha);
  42. b[index] = (0, _colorUtils.blendWithWhite)(data[offset + 2], alpha);
  43. } else {
  44. const color = (y + x) % 2 === 0 ? paddingColorEven : paddingColorOdd;
  45. r[index] = color[0];
  46. g[index] = color[1];
  47. b[index] = color[2];
  48. }
  49. }
  50. }
  51. return [new ImageChannel(newWidth, newHeight, r), new ImageChannel(newWidth, newHeight, g), new ImageChannel(newWidth, newHeight, b)];
  52. }
  53. constructor(width, height, data) {
  54. this.data = void 0;
  55. this.width = void 0;
  56. this.height = void 0;
  57. this.data = data;
  58. this.width = width;
  59. this.height = height;
  60. }
  61. get(x, y) {
  62. return this.data[y * this.width + x];
  63. }
  64. boundXY(x, y) {
  65. return [Math.min(Math.max(x, 0), this.width - 1), Math.min(Math.max(y, 0), this.height - 1)];
  66. }
  67. }
  68. exports.ImageChannel = ImageChannel;