ImageData.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  7. class ImageData {
  8. get width() {
  9. return this._width;
  10. }
  11. get height() {
  12. return this._height;
  13. }
  14. get data() {
  15. return this._data;
  16. }
  17. constructor(arr, w, h) {
  18. _defineProperty(this, "_width", 0);
  19. _defineProperty(this, "_height", 0);
  20. _defineProperty(this, "_data", null);
  21. if (arguments.length === 2) {
  22. if (arr instanceof Uint8ClampedArray) {
  23. if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.');
  24. if (arr.length % 4 !== 0) throw new RangeError('Source length must be a positive multiple of 4.');
  25. if (!Number.isFinite(w)) throw new RangeError('The width is zero or not a number.');
  26. if (w === 0) throw new RangeError('The width is zero or not a number.');
  27. this._width = w;
  28. this._height = arr.length / 4 / w;
  29. this._data = arr;
  30. } else {
  31. const width = arr;
  32. const height = w;
  33. if (!Number.isFinite(height)) throw new RangeError('The height is zero or not a number.');
  34. if (height === 0) throw new RangeError('The height is zero or not a number.');
  35. if (!Number.isFinite(width)) throw new RangeError('The width is zero or not a number.');
  36. if (width === 0) throw new RangeError('The width is zero or not a number.');
  37. this._width = width;
  38. this._height = height;
  39. this._data = new Uint8ClampedArray(width * height * 4);
  40. }
  41. } else if (arguments.length === 3) {
  42. if (!(arr instanceof Uint8ClampedArray)) throw new TypeError('First argument must be a Uint8ClampedArray when using 3 arguments.');
  43. if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.');
  44. if (arr.length % 4 !== 0) throw new RangeError('Source length must be a positive multiple of 4.');
  45. if (!Number.isFinite(h)) throw new RangeError('The height is zero or not a number.');
  46. if (h === 0) throw new RangeError('The height is zero or not a number.');
  47. if (!Number.isFinite(w)) throw new RangeError('The width is zero or not a number.');
  48. if (w === 0) throw new RangeError('The width is zero or not a number.');
  49. if (arr.length !== w * h * 4) throw new RangeError('Source doesn\'n contain the exact number of pixels needed.');
  50. this._width = w;
  51. this._height = h;
  52. this._data = arr;
  53. } else {
  54. throw new TypeError('Wrong number of arguments provided.');
  55. }
  56. }
  57. }
  58. exports.default = ImageData;