image-js 0.35.2 → 0.35.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/index.d.ts +2 -9
  2. package/lib/browser/environment.js +0 -6
  3. package/lib/browser/worker/extend.js +0 -3
  4. package/lib/browser/worker/process/background.js +0 -12
  5. package/lib/browser/worker/worker.js +0 -17
  6. package/lib/image/Image.js +18 -51
  7. package/lib/image/compute/colorHistogram.js +0 -8
  8. package/lib/image/compute/countAlphaPixels.js +0 -3
  9. package/lib/image/compute/extendedPoints.js +2 -7
  10. package/lib/image/compute/histogram.js +6 -25
  11. package/lib/image/compute/localMaxima.js +2 -10
  12. package/lib/image/compute/max.js +0 -5
  13. package/lib/image/compute/mean.js +0 -4
  14. package/lib/image/compute/median.js +0 -4
  15. package/lib/image/compute/min.js +0 -5
  16. package/lib/image/compute/minimalBoundingRectangle.js +5 -18
  17. package/lib/image/compute/moment.js +0 -3
  18. package/lib/image/compute/monotoneChainConvexHull.js +0 -3
  19. package/lib/image/compute/points.js +0 -3
  20. package/lib/image/compute/relativePosition.js +2 -8
  21. package/lib/image/compute/sum.js +0 -5
  22. package/lib/image/core/bitMethods.js +0 -11
  23. package/lib/image/core/checkProcessable.js +0 -13
  24. package/lib/image/core/environment.js +0 -8
  25. package/lib/image/core/export.js +0 -38
  26. package/lib/image/core/extend.js +0 -13
  27. package/lib/image/core/getRGBAData.js +0 -15
  28. package/lib/image/core/kind.js +4 -28
  29. package/lib/image/core/kindNames.js +1 -0
  30. package/lib/image/core/load.js +1 -53
  31. package/lib/image/core/mediaTypes.js +0 -2
  32. package/lib/image/core/valueMethods.js +0 -13
  33. package/lib/image/extend.js +7 -83
  34. package/lib/image/filter/abs.js +0 -3
  35. package/lib/image/filter/add.js +2 -8
  36. package/lib/image/filter/background.js +0 -9
  37. package/lib/image/filter/blurFilter.js +0 -6
  38. package/lib/image/filter/divide.js +0 -8
  39. package/lib/image/filter/flipX.js +0 -5
  40. package/lib/image/filter/flipY.js +0 -4
  41. package/lib/image/filter/gaussianFilter.js +0 -5
  42. package/lib/image/filter/gradientFilter.js +0 -10
  43. package/lib/image/filter/hypotenuse.js +0 -13
  44. package/lib/image/filter/invert.js +0 -9
  45. package/lib/image/filter/level.js +1 -19
  46. package/lib/image/filter/medianFilter.js +0 -15
  47. package/lib/image/filter/multiply.js +0 -10
  48. package/lib/image/filter/scharrFilter.js +0 -4
  49. package/lib/image/filter/sobelFilter.js +0 -4
  50. package/lib/image/filter/subtract.js +0 -8
  51. package/lib/image/filter/subtractImage.js +0 -10
  52. package/lib/image/internal/checks.js +1 -9
  53. package/lib/image/internal/clamp.js +0 -1
  54. package/lib/image/internal/copy.js +0 -2
  55. package/lib/image/internal/copyAlphaChannel.js +0 -1
  56. package/lib/image/internal/getImageParameters.js +0 -1
  57. package/lib/image/internal/getOutputImage.js +1 -13
  58. package/lib/image/model/model.js +1 -1
  59. package/lib/image/morphology/blackHat.js +0 -5
  60. package/lib/image/morphology/close.js +0 -5
  61. package/lib/image/morphology/dilate.js +0 -40
  62. package/lib/image/morphology/erode.js +0 -40
  63. package/lib/image/morphology/morphologicalGradient.js +0 -5
  64. package/lib/image/morphology/open.js +0 -5
  65. package/lib/image/morphology/topHat.js +0 -5
  66. package/lib/image/operator/cannyEdge.js +0 -3
  67. package/lib/image/operator/convolution.js +9 -34
  68. package/lib/image/operator/convolutionSeparable.js +0 -10
  69. package/lib/image/operator/extract.js +4 -13
  70. package/lib/image/operator/floodFill.js +2 -15
  71. package/lib/image/operator/getSeparatedKernel.js +0 -2
  72. package/lib/image/operator/paintLabels.js +2 -13
  73. package/lib/image/operator/paintMasks.js +2 -12
  74. package/lib/image/operator/paintPoints.js +0 -9
  75. package/lib/image/operator/paintPolygon.js +0 -14
  76. package/lib/image/operator/paintPolygons.js +0 -4
  77. package/lib/image/operator/paintPolyline.js +0 -7
  78. package/lib/image/operator/paintPolylines.js +0 -4
  79. package/lib/image/roi/Roi.js +27 -207
  80. package/lib/image/roi/RoiLayer.js +1 -22
  81. package/lib/image/roi/RoiMap.js +5 -38
  82. package/lib/image/roi/creator/fromMask.js +4 -22
  83. package/lib/image/roi/creator/fromMaskConnectedComponentLabelingAlgorithm.js +2 -20
  84. package/lib/image/roi/creator/fromMaxima.js +10 -43
  85. package/lib/image/roi/creator/fromPoints.js +2 -10
  86. package/lib/image/roi/creator/fromWaterShed.js +6 -17
  87. package/lib/image/roi/feretDiameters.js +9 -21
  88. package/lib/image/roi/manager.js +30 -119
  89. package/lib/image/roi/util/commonBorderLength.js +5 -16
  90. package/lib/image/roi/util/mergeRoi.js +2 -21
  91. package/lib/image/transform/cmyk.js +0 -9
  92. package/lib/image/transform/colorDepth.js +0 -12
  93. package/lib/image/transform/crop.js +0 -20
  94. package/lib/image/transform/cropAlpha.js +0 -11
  95. package/lib/image/transform/grey.js +0 -16
  96. package/lib/image/transform/greyAlgorithms.js +1 -24
  97. package/lib/image/transform/hsl.js +0 -16
  98. package/lib/image/transform/hsv.js +0 -15
  99. package/lib/image/transform/insert.js +0 -6
  100. package/lib/image/transform/mask/huang.js +3 -18
  101. package/lib/image/transform/mask/intermodes.js +3 -14
  102. package/lib/image/transform/mask/isodata.js +0 -14
  103. package/lib/image/transform/mask/li.js +12 -42
  104. package/lib/image/transform/mask/mask.js +1 -13
  105. package/lib/image/transform/mask/maxEntropy.js +8 -25
  106. package/lib/image/transform/mask/mean.js +1 -3
  107. package/lib/image/transform/mask/minError.js +10 -21
  108. package/lib/image/transform/mask/minimum.js +0 -20
  109. package/lib/image/transform/mask/moments.js +4 -17
  110. package/lib/image/transform/mask/otsu.js +1 -7
  111. package/lib/image/transform/mask/percentile.js +0 -8
  112. package/lib/image/transform/mask/renyiEntropy.js +8 -34
  113. package/lib/image/transform/mask/shanbhag.js +8 -25
  114. package/lib/image/transform/mask/thresholdAlgorithms.js +1 -17
  115. package/lib/image/transform/mask/triangle.js +13 -29
  116. package/lib/image/transform/mask/yen.js +2 -14
  117. package/lib/image/transform/pad.js +0 -20
  118. package/lib/image/transform/resize/nearestNeighbor.js +0 -6
  119. package/lib/image/transform/resize/resize.js +0 -16
  120. package/lib/image/transform/rgba.js +0 -9
  121. package/lib/image/transform/rgba8.js +0 -3
  122. package/lib/image/transform/rotate.js +0 -29
  123. package/lib/image/transform/rotateFree.js +0 -31
  124. package/lib/image/transform/warping.js +2 -35
  125. package/lib/image/utility/combineChannels.js +0 -11
  126. package/lib/image/utility/getBestMatch.js +2 -13
  127. package/lib/image/utility/getChannel.js +0 -10
  128. package/lib/image/utility/getClosestCommonParent.js +6 -23
  129. package/lib/image/utility/getColumn.js +0 -4
  130. package/lib/image/utility/getIntersection.js +2 -18
  131. package/lib/image/utility/getMatrix.js +0 -7
  132. package/lib/image/utility/getPixelsArray.js +0 -5
  133. package/lib/image/utility/getPixelsGrid.js +0 -11
  134. package/lib/image/utility/getRow.js +0 -4
  135. package/lib/image/utility/getSimilarity.js +3 -17
  136. package/lib/image/utility/getThreshold.js +0 -4
  137. package/lib/image/utility/setBorder.js +0 -16
  138. package/lib/image/utility/setChannel.js +0 -6
  139. package/lib/image/utility/setMatrix.js +0 -6
  140. package/lib/image/utility/split.js +2 -15
  141. package/lib/index.js +0 -12
  142. package/lib/kernel/kernel.js +0 -3
  143. package/lib/kernel/laplacianOfGaussian.js +1 -7
  144. package/lib/node_modules/test/binary.js +0 -3
  145. package/lib/node_modules/test/oneRoi.js +0 -2
  146. package/lib/stack/Stack.js +2 -35
  147. package/lib/stack/compute/histogram.js +0 -4
  148. package/lib/stack/compute/histograms.js +0 -4
  149. package/lib/stack/compute/max.js +0 -3
  150. package/lib/stack/compute/median.js +0 -4
  151. package/lib/stack/compute/min.js +0 -3
  152. package/lib/stack/extend.js +1 -11
  153. package/lib/stack/transform/matchAndCrop.js +6 -19
  154. package/lib/stack/utility/averageImage.js +0 -9
  155. package/lib/stack/utility/maxImage.js +0 -7
  156. package/lib/stack/utility/minImage.js +0 -7
  157. package/lib/util/Shape.js +3 -40
  158. package/lib/util/base64.js +3 -14
  159. package/lib/util/channel.js +1 -35
  160. package/lib/util/color.js +1 -25
  161. package/lib/util/converter.js +1 -13
  162. package/lib/util/deepValue.js +0 -3
  163. package/lib/util/histogram.js +1 -12
  164. package/lib/util/kernel.js +2 -12
  165. package/lib/util/matrix.js +0 -13
  166. package/lib/util/points.js +12 -35
  167. package/lib/util/value.js +0 -7
  168. package/lib/util/zerosMatrix.js +0 -3
  169. package/package.json +1 -1
@@ -4,21 +4,13 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = setExportMethods;
7
-
8
7
  var _blobUtil = require("blob-util");
9
-
10
8
  var _fastBmp = require("fast-bmp");
11
-
12
9
  var _fastPng = require("fast-png");
13
-
14
10
  var _jpegJs = require("jpeg-js");
15
-
16
11
  var _base = require("../../util/base64");
17
-
18
12
  var _environment = require("./environment");
19
-
20
13
  var _mediaTypes = require("./mediaTypes");
21
-
22
14
  function encodeJpeg(image, options = {}) {
23
15
  const data = {
24
16
  width: image.width,
@@ -27,7 +19,6 @@ function encodeJpeg(image, options = {}) {
27
19
  };
28
20
  return (0, _jpegJs.encode)(data, options.quality).data;
29
21
  }
30
-
31
22
  function encodePng(image, options) {
32
23
  const data = {
33
24
  width: image.width,
@@ -36,16 +27,13 @@ function encodePng(image, options) {
36
27
  depth: image.bitDepth,
37
28
  data: image.data
38
29
  };
39
-
40
30
  if (data.depth === 1 || data.depth === 32) {
41
31
  data.depth = 8;
42
32
  data.channels = 4;
43
33
  data.data = image.getRGBAData();
44
34
  }
45
-
46
35
  return (0, _fastPng.encode)(data, options);
47
36
  }
48
-
49
37
  const exportMethods = {
50
38
  /**
51
39
  * Save the image to disk (Node.js only)
@@ -66,23 +54,18 @@ const exportMethods = {
66
54
  let {
67
55
  format
68
56
  } = options;
69
-
70
57
  if (!format) {
71
58
  // try to infer format from filename
72
59
  const m = /\.(?<format>[a-zA-Z]+)$/.exec(path);
73
-
74
60
  if (m) {
75
61
  format = m.groups.format.toLowerCase();
76
62
  }
77
63
  }
78
-
79
64
  if (!format) {
80
65
  throw new Error('file format not provided');
81
66
  }
82
-
83
67
  return new Promise((resolve, reject) => {
84
68
  let stream, buffer;
85
-
86
69
  switch (format.toLowerCase()) {
87
70
  case 'png':
88
71
  {
@@ -91,10 +74,8 @@ const exportMethods = {
91
74
  } else {
92
75
  buffer = encodePng(this, encoderOptions);
93
76
  }
94
-
95
77
  break;
96
78
  }
97
-
98
79
  case 'jpg':
99
80
  case 'jpeg':
100
81
  if (useCanvas) {
@@ -102,17 +83,13 @@ const exportMethods = {
102
83
  } else {
103
84
  buffer = encodeJpeg(this, encoderOptions);
104
85
  }
105
-
106
86
  break;
107
-
108
87
  case 'bmp':
109
88
  buffer = (0, _fastBmp.encode)(this, encoderOptions);
110
89
  break;
111
-
112
90
  default:
113
91
  throw new RangeError(`invalid output format: ${format}`);
114
92
  }
115
-
116
93
  if (stream) {
117
94
  let out = (0, _environment.createWriteStream)(path);
118
95
  out.on('finish', resolve);
@@ -124,13 +101,11 @@ const exportMethods = {
124
101
  reject(err);
125
102
  return;
126
103
  }
127
-
128
104
  resolve();
129
105
  });
130
106
  }
131
107
  });
132
108
  },
133
-
134
109
  /**
135
110
  * Creates a dataURL string from the image.
136
111
  * @memberof Image
@@ -146,18 +121,15 @@ const exportMethods = {
146
121
  options = type;
147
122
  type = 'image/png';
148
123
  }
149
-
150
124
  const {
151
125
  useCanvas = false,
152
126
  encoder: encoderOptions = undefined
153
127
  } = options;
154
128
  type = (0, _mediaTypes.getType)(type);
155
-
156
129
  function dataUrl(encoder, ctx) {
157
130
  const u8 = encoder(ctx, encoderOptions);
158
131
  return (0, _base.toBase64URL)(u8, type);
159
132
  }
160
-
161
133
  if (type === 'image/bmp') {
162
134
  return dataUrl(_fastBmp.encode, this);
163
135
  } else if (type === 'image/png' && !useCanvas) {
@@ -168,7 +140,6 @@ const exportMethods = {
168
140
  return this.getCanvas().toDataURL(type);
169
141
  }
170
142
  },
171
-
172
143
  /**
173
144
  * Encodes the image and returns a buffer
174
145
  * @memberof Image
@@ -183,23 +154,18 @@ const exportMethods = {
183
154
  format = 'png',
184
155
  encoder: encoderOptions = undefined
185
156
  } = options;
186
-
187
157
  switch (format.toLowerCase()) {
188
158
  case 'png':
189
159
  return encodePng(this, encoderOptions);
190
-
191
160
  case 'jpeg':
192
161
  case 'jpg':
193
162
  return encodeJpeg(this, encoderOptions);
194
-
195
163
  case 'bmp':
196
164
  return (0, _fastBmp.encode)(this, encoderOptions);
197
-
198
165
  default:
199
166
  throw new RangeError(`invalid output format: ${format}`);
200
167
  }
201
168
  },
202
-
203
169
  /**
204
170
  * Creates a base64 string from the image.
205
171
  * @memberof Image
@@ -218,7 +184,6 @@ const exportMethods = {
218
184
  return dataURL.substring(dataURL.indexOf(',') + 1);
219
185
  }
220
186
  },
221
-
222
187
  /**
223
188
  * Creates a blob from the image and return a Promise.
224
189
  * This function is only available in the browser.
@@ -231,7 +196,6 @@ const exportMethods = {
231
196
  toBlob(type = 'image/png', quality = 0.8) {
232
197
  return (0, _blobUtil.canvasToBlob)(this.getCanvas(), type, quality);
233
198
  },
234
-
235
199
  /**
236
200
  * Creates a new canvas element and draw the image inside it
237
201
  * @memberof Image
@@ -247,9 +211,7 @@ const exportMethods = {
247
211
  ctx.putImageData(data, 0, 0);
248
212
  return canvas;
249
213
  }
250
-
251
214
  };
252
-
253
215
  function setExportMethods(Image) {
254
216
  for (const i in exportMethods) {
255
217
  Image.prototype[i] = exportMethods[i];
@@ -5,36 +5,28 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.extendMethod = extendMethod;
7
7
  exports.extendProperty = extendProperty;
8
-
9
8
  var _hasOwn = _interopRequireDefault(require("has-own"));
10
-
11
9
  var _Image = _interopRequireDefault(require("../Image"));
12
-
13
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
-
15
11
  let computedPropertyDescriptor = {
16
12
  configurable: true,
17
13
  enumerable: false,
18
14
  get: undefined
19
15
  };
20
-
21
16
  function extendMethod(name, method, options = {}) {
22
17
  let {
23
18
  inPlace = false,
24
19
  returnThis = true,
25
20
  partialArgs = []
26
21
  } = options;
27
-
28
22
  if (inPlace) {
29
23
  _Image.default.prototype[name] = function (...args) {
30
24
  // remove computed properties
31
25
  this.computed = null;
32
26
  let result = method.apply(this, [...partialArgs, ...args]);
33
-
34
27
  if (returnThis) {
35
28
  return this;
36
29
  }
37
-
38
30
  return result;
39
31
  };
40
32
  } else {
@@ -42,27 +34,22 @@ function extendMethod(name, method, options = {}) {
42
34
  return method.apply(this, [...partialArgs, ...args]);
43
35
  };
44
36
  }
45
-
46
37
  return _Image.default;
47
38
  }
48
-
49
39
  function extendProperty(name, method, options = {}) {
50
40
  let {
51
41
  partialArgs = []
52
42
  } = options;
53
-
54
43
  computedPropertyDescriptor.get = function () {
55
44
  if (this.computed === null) {
56
45
  this.computed = {};
57
46
  } else if ((0, _hasOwn.default)(name, this.computed)) {
58
47
  return this.computed[name];
59
48
  }
60
-
61
49
  let result = method.apply(this, partialArgs);
62
50
  this.computed[name] = result;
63
51
  return result;
64
52
  };
65
-
66
53
  Object.defineProperty(_Image.default.prototype, name, computedPropertyDescriptor);
67
54
  return _Image.default;
68
55
  }
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = getRGBAData;
7
-
8
7
  var _model = require("../model/model");
9
-
10
8
  /**
11
9
  * Retrieve the data of the current image as RGBA 8 bits
12
10
  * The source image may be:
@@ -29,14 +27,12 @@ function getRGBAData(options = {}) {
29
27
  });
30
28
  const arrayLength = this.width * this.height * 4;
31
29
  let newData = clamped ? new Uint8ClampedArray(arrayLength) : new Uint8Array(arrayLength);
32
-
33
30
  if (this.bitDepth === 1) {
34
31
  fillDataFromBinary(this, newData);
35
32
  } else if (this.bitDepth === 32) {
36
33
  this.checkProcessable('getRGBAData', {
37
34
  alpha: 0
38
35
  });
39
-
40
36
  if (this.components === 1) {
41
37
  fillDataFromGrey32(this, newData);
42
38
  } else if (this.components === 3) {
@@ -55,7 +51,6 @@ function getRGBAData(options = {}) {
55
51
  fillDataFromRGB(this, newData);
56
52
  }
57
53
  }
58
-
59
54
  if (this.alpha === 1) {
60
55
  this.checkProcessable('getRGBAData', {
61
56
  bitDepth: [8, 16]
@@ -64,10 +59,8 @@ function getRGBAData(options = {}) {
64
59
  } else {
65
60
  fillAlpha(this, newData);
66
61
  }
67
-
68
62
  return newData;
69
63
  }
70
-
71
64
  function fillDataFromBinary(image, newData) {
72
65
  for (let i = 0; i < image.size; i++) {
73
66
  const value = image.getBit(i);
@@ -76,12 +69,10 @@ function fillDataFromBinary(image, newData) {
76
69
  newData[i * 4 + 2] = value * 255;
77
70
  }
78
71
  }
79
-
80
72
  function fillDataFromGrey32(image, newData) {
81
73
  const min = image.min[0];
82
74
  const max = image.max[0];
83
75
  const range = max - min;
84
-
85
76
  for (let i = 0; i < image.size; i++) {
86
77
  const val = Math.floor(255 * (image.data[i] - min) / range);
87
78
  newData[i * 4] = val;
@@ -89,12 +80,10 @@ function fillDataFromGrey32(image, newData) {
89
80
  newData[i * 4 + 2] = val;
90
81
  }
91
82
  }
92
-
93
83
  function fillDataFromRGB32(image, newData) {
94
84
  const min = Math.min(...image.min);
95
85
  const max = Math.max(...image.max);
96
86
  const range = max - min;
97
-
98
87
  for (let i = 0; i < image.size; i++) {
99
88
  const val1 = Math.floor(255 * (image.data[i * 3] - min) / range);
100
89
  const val2 = Math.floor(255 * (image.data[i * 3 + 1] - min) / range);
@@ -104,7 +93,6 @@ function fillDataFromRGB32(image, newData) {
104
93
  newData[i * 4 + 2] = val3;
105
94
  }
106
95
  }
107
-
108
96
  function fillDataFromGrey(image, newData) {
109
97
  for (let i = 0; i < image.size; i++) {
110
98
  newData[i * 4] = image.data[i * image.channels] >>> image.bitDepth - 8;
@@ -112,7 +100,6 @@ function fillDataFromGrey(image, newData) {
112
100
  newData[i * 4 + 2] = image.data[i * image.channels] >>> image.bitDepth - 8;
113
101
  }
114
102
  }
115
-
116
103
  function fillDataFromRGB(image, newData) {
117
104
  for (let i = 0; i < image.size; i++) {
118
105
  newData[i * 4] = image.data[i * image.channels] >>> image.bitDepth - 8;
@@ -120,13 +107,11 @@ function fillDataFromRGB(image, newData) {
120
107
  newData[i * 4 + 2] = image.data[i * image.channels + 2] >>> image.bitDepth - 8;
121
108
  }
122
109
  }
123
-
124
110
  function copyAlpha(image, newData) {
125
111
  for (let i = 0; i < image.size; i++) {
126
112
  newData[i * 4 + 3] = image.data[i * image.channels + image.components] >> image.bitDepth - 8;
127
113
  }
128
114
  }
129
-
130
115
  function fillAlpha(image, newData) {
131
116
  for (let i = 0; i < image.size; i++) {
132
117
  newData[i * 4 + 3] = 255;
@@ -7,15 +7,10 @@ exports.createPixelArray = createPixelArray;
7
7
  exports.getKind = getKind;
8
8
  exports.getTheoreticalPixelArraySize = getTheoreticalPixelArraySize;
9
9
  exports.verifyKindDefinition = verifyKindDefinition;
10
-
11
10
  var ColorModel = _interopRequireWildcard(require("../model/model"));
12
-
13
11
  var Kind = _interopRequireWildcard(require("./kindNames"));
14
-
15
12
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
16
-
17
13
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
18
-
19
14
  const kinds = {};
20
15
  kinds[Kind.BINARY] = {
21
16
  components: 1,
@@ -59,19 +54,14 @@ kinds[Kind.CMYKA] = {
59
54
  bitDepth: 8,
60
55
  colorModel: ColorModel.CMYK
61
56
  };
62
-
63
57
  function getKind(kind) {
64
58
  const result = kinds[kind];
65
-
66
59
  if (!result) {
67
60
  throw new RangeError(`invalid image kind: ${kind}`);
68
61
  }
69
-
70
62
  return result;
71
63
  }
72
-
73
64
  const validBitDepth = [1, 8, 16, 32];
74
-
75
65
  function verifyKindDefinition(definition) {
76
66
  const {
77
67
  components,
@@ -79,66 +69,52 @@ function verifyKindDefinition(definition) {
79
69
  bitDepth,
80
70
  colorModel
81
71
  } = definition;
82
-
83
72
  if (!Number.isInteger(components) || components <= 0) {
84
73
  throw new RangeError(`invalid components: ${components}. Must be a positive integer`);
85
74
  }
86
-
87
75
  if (alpha !== 0 && alpha !== 1 && typeof alpha !== 'boolean') {
88
76
  throw new TypeError(`invalid alpha: ${alpha}: must be a boolean, 0 or 1`);
89
77
  }
90
-
91
78
  if (!validBitDepth.includes(bitDepth)) {
92
79
  throw new RangeError(`invalid bitDepth: ${bitDepth}. Must be one of ${validBitDepth.join(', ')}`);
93
- } // eslint-disable-next-line import/namespace
94
-
95
-
80
+ }
81
+ // eslint-disable-next-line import/namespace
96
82
  if (!ColorModel[colorModel]) {
97
83
  throw new RangeError(`invalid colorModel: ${colorModel}. Must be one of ${Object.keys(ColorModel).join(', ')}`);
98
84
  }
99
85
  }
100
-
101
86
  function getTheoreticalPixelArraySize(size, channels, bitDepth) {
102
87
  let length = channels * size;
103
-
104
88
  if (bitDepth === 1) {
105
89
  length = Math.ceil(length / 8);
106
90
  }
107
-
108
91
  return length;
109
92
  }
110
-
111
93
  function createPixelArray(size, components, alpha, channels, bitDepth, maxValue) {
112
94
  const length = channels * size;
113
95
  let arr;
114
-
115
96
  switch (bitDepth) {
116
97
  case 1:
117
98
  arr = new Uint8Array(Math.ceil(length / 8));
118
99
  break;
119
-
120
100
  case 8:
121
101
  arr = new Uint8Array(length);
122
102
  break;
123
-
124
103
  case 16:
125
104
  arr = new Uint16Array(length);
126
105
  break;
127
-
128
106
  case 32:
129
107
  arr = new Float32Array(length);
130
108
  break;
131
-
132
109
  default:
133
110
  throw new Error(`Cannot create pixel array for bit depth ${bitDepth}`);
134
- } // alpha channel is 100% by default
135
-
111
+ }
136
112
 
113
+ // alpha channel is 100% by default
137
114
  if (alpha) {
138
115
  for (let i = components; i < arr.length; i += channels) {
139
116
  arr[i] = maxValue;
140
117
  }
141
118
  }
142
-
143
119
  return arr;
144
120
  }
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.RGBA = exports.RGB = exports.GREYA = exports.GREY = exports.CMYKA = exports.CMYK = exports.BINARY = void 0;
7
7
  // Shortcuts for common image kinds
8
+
8
9
  const BINARY = 'BINARY';
9
10
  exports.BINARY = BINARY;
10
11
  const GREY = 'GREY';
@@ -4,30 +4,19 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = load;
7
-
8
7
  var _fastJpeg = require("fast-jpeg");
9
-
10
8
  var _fastPng = require("fast-png");
11
-
12
9
  var _imageType = _interopRequireDefault(require("image-type"));
13
-
14
10
  var _jpegJs = require("jpeg-js");
15
-
16
11
  var _tiff = require("tiff");
17
-
18
12
  var _Stack = _interopRequireDefault(require("../../stack/Stack"));
19
-
20
13
  var _base = require("../../util/base64");
21
-
22
14
  var _Image = _interopRequireDefault(require("../Image"));
23
-
24
15
  var _model = require("../model/model");
25
-
26
16
  var _environment = require("./environment");
27
-
28
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
-
30
18
  const isDataURL = /^data:[a-z]+\/(?:[a-z]+);base64,/;
19
+
31
20
  /**
32
21
  * Load an image
33
22
  * @memberof Image
@@ -42,7 +31,6 @@ const isDataURL = /^data:[a-z]+\/(?:[a-z]+);base64,/;
42
31
  * @example
43
32
  * const image = await Image.load('https://example.com/image.png');
44
33
  */
45
-
46
34
  function load(image, options) {
47
35
  if (typeof image === 'string') {
48
36
  return loadURL(image, options);
@@ -54,28 +42,21 @@ function load(image, options) {
54
42
  throw new Error('argument to "load" must be a string or buffer.');
55
43
  }
56
44
  }
57
-
58
45
  function loadBinary(image, base64Url, ignorePalette) {
59
46
  const type = (0, _imageType.default)(image);
60
-
61
47
  if (type) {
62
48
  switch (type.mime) {
63
49
  case 'image/png':
64
50
  return loadPNG(image);
65
-
66
51
  case 'image/jpeg':
67
52
  return loadJPEG(image);
68
-
69
53
  case 'image/tiff':
70
54
  return loadTIFF(image, ignorePalette);
71
-
72
55
  default:
73
56
  return loadGeneric(getBase64(type.mime));
74
57
  }
75
58
  }
76
-
77
59
  return loadGeneric(getBase64('application/octet-stream'));
78
-
79
60
  function getBase64(type) {
80
61
  if (base64Url) {
81
62
  return base64Url;
@@ -84,47 +65,39 @@ function loadBinary(image, base64Url, ignorePalette) {
84
65
  }
85
66
  }
86
67
  }
87
-
88
68
  function loadURL(url, options) {
89
69
  const dataURL = url.slice(0, 64).match(isDataURL);
90
70
  let binaryDataP;
91
-
92
71
  if (dataURL !== null) {
93
72
  binaryDataP = Promise.resolve((0, _base.decode)(url.slice(dataURL[0].length)));
94
73
  } else {
95
74
  binaryDataP = (0, _environment.fetchBinary)(url, options);
96
75
  }
97
-
98
76
  return binaryDataP.then(binaryData => {
99
77
  const uint8 = new Uint8Array(binaryData);
100
78
  return loadBinary(uint8, dataURL ? url : undefined, options && options.ignorePalette);
101
79
  });
102
80
  }
103
-
104
81
  function loadPNG(data) {
105
82
  const png = (0, _fastPng.decode)(data);
106
83
  let channels = png.channels;
107
84
  let components;
108
85
  let alpha = 0;
109
-
110
86
  if (channels === 2 || channels === 4) {
111
87
  components = channels - 1;
112
88
  alpha = 1;
113
89
  } else {
114
90
  components = channels;
115
91
  }
116
-
117
92
  if (png.palette) {
118
93
  return loadPNGFromPalette(png);
119
94
  }
120
-
121
95
  return new _Image.default(png.width, png.height, png.data, {
122
96
  components,
123
97
  alpha,
124
98
  bitDepth: png.depth
125
99
  });
126
100
  }
127
-
128
101
  function loadPNGFromPalette(png) {
129
102
  const pixels = png.width * png.height;
130
103
  const channels = png.palette[0].length;
@@ -134,40 +107,32 @@ function loadPNGFromPalette(png) {
134
107
  const mask = parseInt('1'.repeat(png.depth), 2);
135
108
  const hasAlpha = channels === 4;
136
109
  let dataIndex = 0;
137
-
138
110
  for (let i = 0; i < pixels; i++) {
139
111
  const index = Math.floor(i / factor);
140
112
  let value = png.data[index];
141
-
142
113
  if (png.depth < 8) {
143
114
  value = value >>> png.depth * (pixelsPerByte - 1 - i % pixelsPerByte) & mask;
144
115
  }
145
-
146
116
  const paletteValue = png.palette[value];
147
117
  data[dataIndex++] = paletteValue[0];
148
118
  data[dataIndex++] = paletteValue[1];
149
119
  data[dataIndex++] = paletteValue[2];
150
-
151
120
  if (hasAlpha) {
152
121
  data[dataIndex++] = paletteValue[3];
153
122
  }
154
123
  }
155
-
156
124
  return new _Image.default(png.width, png.height, data, {
157
125
  components: 3,
158
126
  alpha: hasAlpha,
159
127
  bitDepth: 8
160
128
  });
161
129
  }
162
-
163
130
  function loadJPEG(data) {
164
131
  const decodedExif = (0, _fastJpeg.decode)(data);
165
132
  let meta;
166
-
167
133
  if (decodedExif.exif) {
168
134
  meta = getMetadata(decodedExif.exif);
169
135
  }
170
-
171
136
  const jpeg = (0, _jpegJs.decode)(data, {
172
137
  useTArray: true,
173
138
  maxMemoryUsageInMB: 1024
@@ -175,10 +140,8 @@ function loadJPEG(data) {
175
140
  let image = new _Image.default(jpeg.width, jpeg.height, jpeg.data, {
176
141
  meta
177
142
  });
178
-
179
143
  if (meta && meta.tiff.tags.Orientation) {
180
144
  const orientation = meta.tiff.tags.Orientation;
181
-
182
145
  if (orientation > 2) {
183
146
  image = image.rotate({
184
147
  3: 180,
@@ -189,18 +152,14 @@ function loadJPEG(data) {
189
152
  8: 270
190
153
  }[orientation]);
191
154
  }
192
-
193
155
  if ([2, 4, 5, 7].includes(orientation)) {
194
156
  image = image.flipX();
195
157
  }
196
158
  }
197
-
198
159
  return image;
199
160
  }
200
-
201
161
  function loadTIFF(data, ignorePalette) {
202
162
  let result = (0, _tiff.decode)(data);
203
-
204
163
  if (result.length === 1) {
205
164
  return getImageFromIFD(result[0], ignorePalette);
206
165
  } else {
@@ -209,7 +168,6 @@ function loadTIFF(data, ignorePalette) {
209
168
  }));
210
169
  }
211
170
  }
212
-
213
171
  function getMetadata(image) {
214
172
  const metadata = {
215
173
  tiff: {
@@ -217,25 +175,20 @@ function getMetadata(image) {
217
175
  tags: image.map
218
176
  }
219
177
  };
220
-
221
178
  if (image.exif) {
222
179
  metadata.exif = image.exif;
223
180
  }
224
-
225
181
  if (image.gps) {
226
182
  metadata.gps = image.gps;
227
183
  }
228
-
229
184
  return metadata;
230
185
  }
231
-
232
186
  function getImageFromIFD(image, ignorePalette) {
233
187
  if (!ignorePalette && image.type === 3) {
234
188
  // Palette
235
189
  const data = new Uint16Array(3 * image.width * image.height);
236
190
  const palette = image.palette;
237
191
  let ptr = 0;
238
-
239
192
  for (let i = 0; i < image.data.length; i++) {
240
193
  const index = image.data[i];
241
194
  const color = palette[index];
@@ -243,7 +196,6 @@ function getImageFromIFD(image, ignorePalette) {
243
196
  data[ptr++] = color[1];
244
197
  data[ptr++] = color[2];
245
198
  }
246
-
247
199
  return new _Image.default(image.width, image.height, data, {
248
200
  components: 3,
249
201
  alpha: image.alpha,
@@ -261,12 +213,10 @@ function getImageFromIFD(image, ignorePalette) {
261
213
  });
262
214
  }
263
215
  }
264
-
265
216
  function loadGeneric(url, options) {
266
217
  options = options || {};
267
218
  return new Promise(function (resolve, reject) {
268
219
  let image = new _environment.DOMImage();
269
-
270
220
  image.onload = function () {
271
221
  let w = image.width;
272
222
  let h = image.height;
@@ -276,11 +226,9 @@ function loadGeneric(url, options) {
276
226
  let data = ctx.getImageData(0, 0, w, h).data;
277
227
  resolve(new _Image.default(w, h, data, options));
278
228
  };
279
-
280
229
  image.onerror = function () {
281
230
  reject(new Error(`Could not load ${url}`));
282
231
  };
283
-
284
232
  image.src = url;
285
233
  });
286
234
  }