image-js 1.1.0 → 1.2.0

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 (74) hide show
  1. package/dist/image-js.esm.js +176 -48
  2. package/dist/image-js.esm.js.map +1 -1
  3. package/dist/image-js.esm.min.js +2 -2
  4. package/dist/image-js.esm.min.js.map +1 -1
  5. package/dist/image-js.umd.js +144 -14
  6. package/dist/image-js.umd.js.map +1 -1
  7. package/dist/image-js.umd.min.js +2 -2
  8. package/dist/image-js.umd.min.js.map +1 -1
  9. package/dist-types/image-js.d.ts +2 -2
  10. package/lib/draw/drawLineOnImage.d.ts.map +1 -1
  11. package/lib/draw/drawLineOnImage.js.map +1 -1
  12. package/lib/extra/draw/draw_text.d.ts +47 -0
  13. package/lib/extra/draw/draw_text.d.ts.map +1 -0
  14. package/lib/extra/draw/draw_text.js +108 -0
  15. package/lib/extra/draw/draw_text.js.map +1 -0
  16. package/lib/extra/draw/index.d.ts +3 -0
  17. package/lib/extra/draw/index.d.ts.map +1 -0
  18. package/lib/extra/draw/index.js +3 -0
  19. package/lib/extra/draw/index.js.map +1 -0
  20. package/lib/{ssim → extra/ssim}/compute_dssim.d.ts +2 -2
  21. package/lib/extra/ssim/compute_dssim.d.ts.map +1 -0
  22. package/lib/{ssim → extra/ssim}/compute_dssim.js +1 -1
  23. package/lib/extra/ssim/compute_dssim.js.map +1 -0
  24. package/lib/{ssim → extra/ssim}/compute_ssim.d.ts +1 -1
  25. package/lib/extra/ssim/compute_ssim.d.ts.map +1 -0
  26. package/lib/{ssim → extra/ssim}/compute_ssim.js +2 -2
  27. package/lib/extra/ssim/compute_ssim.js.map +1 -0
  28. package/lib/extra/ssim/index.d.ts +3 -0
  29. package/lib/extra/ssim/index.d.ts.map +1 -0
  30. package/lib/extra/ssim/index.js +3 -0
  31. package/lib/extra/ssim/index.js.map +1 -0
  32. package/lib/index_browser.d.ts +3 -0
  33. package/lib/index_browser.d.ts.map +1 -0
  34. package/lib/index_browser.js +5 -0
  35. package/lib/index_browser.js.map +1 -0
  36. package/lib/index_full.d.ts +4 -0
  37. package/lib/index_full.d.ts.map +1 -0
  38. package/lib/index_full.js +5 -0
  39. package/lib/index_full.js.map +1 -0
  40. package/lib/load/read.d.ts +1 -1
  41. package/lib/load/read.js +1 -1
  42. package/lib/save/write.d.ts +1 -1
  43. package/lib/save/write.js +1 -1
  44. package/lib/utils/cross_platform.d.ts +8 -0
  45. package/lib/utils/cross_platform.d.ts.map +1 -1
  46. package/lib/utils/cross_platform.js +34 -2
  47. package/lib/utils/cross_platform.js.map +1 -1
  48. package/lib/utils/setBlendedPixel.d.ts +1 -1
  49. package/lib/utils/setBlendedPixel.js +1 -1
  50. package/lib/utils/setBlendedVisiblePixel.d.ts +1 -1
  51. package/lib/utils/setBlendedVisiblePixel.js +1 -1
  52. package/package.json +17 -3
  53. package/src/draw/drawLineOnImage.ts +0 -1
  54. package/src/extra/draw/draw_text.ts +194 -0
  55. package/src/extra/draw/index.ts +2 -0
  56. package/src/{ssim → extra/ssim}/compute_dssim.ts +3 -3
  57. package/src/{ssim → extra/ssim}/compute_ssim.ts +3 -3
  58. package/src/extra/ssim/index.ts +2 -0
  59. package/src/index_browser.ts +4 -0
  60. package/src/index_full.ts +4 -0
  61. package/src/load/read.ts +1 -1
  62. package/src/save/write.ts +1 -1
  63. package/src/utils/cross_platform.ts +47 -4
  64. package/src/utils/setBlendedPixel.ts +1 -1
  65. package/src/utils/setBlendedVisiblePixel.ts +1 -1
  66. package/lib/ssim/compute_dssim.d.ts.map +0 -1
  67. package/lib/ssim/compute_dssim.js.map +0 -1
  68. package/lib/ssim/compute_ssim.d.ts.map +0 -1
  69. package/lib/ssim/compute_ssim.js.map +0 -1
  70. package/lib/ssim/index.d.ts +0 -3
  71. package/lib/ssim/index.d.ts.map +0 -1
  72. package/lib/ssim/index.js +0 -3
  73. package/lib/ssim/index.js.map +0 -1
  74. package/src/ssim/index.ts +0 -2
@@ -1,5 +1,5 @@
1
1
  /*
2
- * image-js v1.1.0
2
+ * image-js v1.2.0
3
3
  * Image processing and manipulation in JavaScript
4
4
  * https://github.com/image-js/image-js#readme
5
5
  *
@@ -5109,11 +5109,11 @@ matrix.solve = solve;
5109
5109
  matrix.wrap = wrap;
5110
5110
 
5111
5111
  const EigenvalueDecomposition = EigenvalueDecomposition_1;
5112
- const Matrix = Matrix_1;
5112
+ const Matrix$1 = Matrix_1;
5113
5113
  const SVD = SVD$1;
5114
5114
  const SingularValueDecomposition = SingularValueDecomposition_1;
5115
5115
  const WrapperMatrix1D = WrapperMatrix1D_1;
5116
- var Matrix$1 = _default.Matrix ? _default.Matrix : Matrix_1;
5116
+ var Matrix = _default.Matrix ? _default.Matrix : Matrix_1;
5117
5117
  const determinant = determinant_1;
5118
5118
  const inverse = inverse_1;
5119
5119
  const pseudoInverse = pseudoInverse_1;
@@ -5125,7 +5125,7 @@ const pseudoInverse = pseudoInverse_1;
5125
5125
  * @returns The centroid.
5126
5126
  */
5127
5127
  function getCentroid(points) {
5128
- let result = new Matrix$1(3, 1);
5128
+ let result = new Matrix(3, 1);
5129
5129
  const nbPoints = points.columns;
5130
5130
  let xSum = 0;
5131
5131
  let ySum = 0;
@@ -5166,7 +5166,7 @@ function getAffineTransform$1(source, destination) {
5166
5166
  ratioSum += getDistanceToOrigin(destinationPoint) / getDistanceToOrigin(sourcePoint);
5167
5167
  }
5168
5168
  const scale = ratioSum / nbPoints;
5169
- const scaledSource = Matrix$1.mul(translatedSource, scale);
5169
+ const scaledSource = Matrix.mul(translatedSource, scale);
5170
5170
  // computing rotation
5171
5171
  const covarianceMatrix = scaledSource.mmul(translatedDestination.transpose());
5172
5172
  const svd = new SingularValueDecomposition(covarianceMatrix);
@@ -5182,7 +5182,7 @@ function getAffineTransform$1(source, destination) {
5182
5182
  angleDegrees = 180;
5183
5183
  }
5184
5184
  // computing translation
5185
- const translation = Matrix$1.sub(destinationCentroid, Matrix$1.mul(rotation.mmul(sourceCentroid), scale));
5185
+ const translation = Matrix.sub(destinationCentroid, Matrix.mul(rotation.mmul(sourceCentroid), scale));
5186
5186
  return {
5187
5187
  translation: {
5188
5188
  x: translation.get(0, 0),
@@ -6263,9 +6263,9 @@ function F(t) {
6263
6263
  var index_umd$1 = {exports: {}};
6264
6264
 
6265
6265
  var index_umd = index_umd$1.exports;
6266
- (function (module, exports) {
6266
+ (function (module, exports$1) {
6267
6267
  (function (N, J) {
6268
- J(exports) ;
6268
+ J(exports$1) ;
6269
6269
  })(index_umd, function (N) {
6270
6270
 
6271
6271
  function J(f, a, o, t, e) {
@@ -6949,7 +6949,7 @@ function getHarrisScore(image, origin, options = {}) {
6949
6949
  const xxSum = xx.sum();
6950
6950
  const xySum = xy.sum();
6951
6951
  const yySum = yy.sum();
6952
- const structureTensor = new Matrix([[xxSum, xySum], [xySum, yySum]]);
6952
+ const structureTensor = new Matrix$1([[xxSum, xySum], [xySum, yySum]]);
6953
6953
  const eigenValues = new EigenvalueDecomposition(structureTensor).realEigenvalues;
6954
6954
  return eigenValues[0] * eigenValues[1] - harrisConstant * (eigenValues[0] + eigenValues[1]) ** 2;
6955
6955
  }
@@ -8662,7 +8662,7 @@ function getDefaultColor(image) {
8662
8662
  * @param image - The image with which to blend.
8663
8663
  * @param column - Column of the target pixel.
8664
8664
  * @param row - Row of the target pixel.
8665
- * @param color - Color with which to blend the image pixel. @default `'Opaque black'`.
8665
+ * @param color - Color with which to blend the image pixel. Default: `Opaque black`.
8666
8666
  */
8667
8667
  function setBlendedPixel(image, column, row, color) {
8668
8668
  color = color ?? getDefaultColor(image);
@@ -8692,7 +8692,7 @@ function setBlendedPixel(image, column, row, color) {
8692
8692
  * @param image - The image with which to blend.
8693
8693
  * @param column - Column of the target pixel.
8694
8694
  * @param row - Row of the target pixel.
8695
- * @param color - Color with which to blend the image pixel. @default `'Opaque black'`.
8695
+ * @param color - Color with which to blend the image pixel. Default: `Opaque black`.
8696
8696
  */
8697
8697
  function setBlendedVisiblePixel(image, column, row, color) {
8698
8698
  if (column >= 0 && column < image.width && row >= 0 && row < image.height) {
@@ -9444,9 +9444,9 @@ function robustSubtract(e, f) {
9444
9444
  })(orientation);
9445
9445
  var orientationExports = orientation.exports;
9446
9446
 
9447
- var robustPnp = robustPointInPolygon;
9447
+ var robustPnp = robustPointInPolygon$1;
9448
9448
  var orient = orientationExports;
9449
- function robustPointInPolygon(vs, point) {
9449
+ function robustPointInPolygon$1(vs, point) {
9450
9450
  var x = point[0];
9451
9451
  var y = point[1];
9452
9452
  var n = vs.length;
@@ -9542,7 +9542,7 @@ function robustPointInPolygon(vs, point) {
9542
9542
  }
9543
9543
  return 2 * inside - 1;
9544
9544
  }
9545
- var robustPointInPolygon$1 = /*@__PURE__*/getDefaultExportFromCjs(robustPnp);
9545
+ var robustPointInPolygon = /*@__PURE__*/getDefaultExportFromCjs(robustPnp);
9546
9546
 
9547
9547
  /**
9548
9548
  * Convert object points into array points.
@@ -9590,7 +9590,7 @@ function drawPolygonOnImage(image, points, options = {}) {
9590
9590
  const arrayPoints = arrayPointsToObjects(filteredPoints);
9591
9591
  for (let row = 0; row < newImage.height; row++) {
9592
9592
  for (let column = 0; column < newImage.width; column++) {
9593
- if (robustPointInPolygon$1(arrayPoints, [column, row]) === -1) {
9593
+ if (robustPointInPolygon(arrayPoints, [column, row]) === -1) {
9594
9594
  setBlendedVisiblePixel(newImage, Math.round(origin.column) + column, Math.round(origin.row) + row, fillColor);
9595
9595
  }
9596
9596
  }
@@ -9631,7 +9631,7 @@ function drawPolygonOnMask(mask, points, options = {}) {
9631
9631
  const arrayPoints = arrayPointsToObjects(filteredPoints);
9632
9632
  for (let row = 0; row < newMask.height; row++) {
9633
9633
  for (let column = 0; column < newMask.width; column++) {
9634
- if (robustPointInPolygon$1(arrayPoints, [column, row]) === -1) {
9634
+ if (robustPointInPolygon(arrayPoints, [column, row]) === -1) {
9635
9635
  newMask.setBit(Math.round(origin.column) + column, Math.round(origin.row) + row, 1);
9636
9636
  }
9637
9637
  }
@@ -12606,7 +12606,7 @@ function transform(image, transformMatrix, options = {}) {
12606
12606
  height = Math.round(height);
12607
12607
  }
12608
12608
  if (!options.inverse) {
12609
- transformMatrix = inverse(new Matrix(transformMatrix)).to2DArray();
12609
+ transformMatrix = inverse(new Matrix$1(transformMatrix)).to2DArray();
12610
12610
  }
12611
12611
  const newImage = Image.createFrom(image, {
12612
12612
  width,
@@ -12845,8 +12845,8 @@ function getPerspectiveWarp(pts, options = {}) {
12845
12845
  const [x2, y2] = [widthRect - 1, 0];
12846
12846
  const [x3, y3] = [widthRect - 1, heightRect - 1];
12847
12847
  const [x4, y4] = [0, heightRect - 1];
12848
- const S = new Matrix([[x1, y1, 1, 0, 0, 0, -x1 * tl.column, -y1 * tl.column], [x2, y2, 1, 0, 0, 0, -x2 * tr.column, -y2 * tr.column], [x3, y3, 1, 0, 0, 0, -x3 * br.column, -y3 * br.column], [x4, y4, 1, 0, 0, 0, -x4 * bl.column, -y4 * bl.column], [0, 0, 0, x1, y1, 1, -x1 * tl.row, -y1 * tl.row], [0, 0, 0, x2, y2, 1, -x2 * tr.row, -y2 * tr.row], [0, 0, 0, x3, y3, 1, -x3 * br.row, -y3 * br.row], [0, 0, 0, x4, y4, 1, -x4 * bl.row, -y4 * bl.row]]);
12849
- const D = Matrix.columnVector([tl.column, tr.column, br.column, bl.column, tl.row, tr.row, br.row, bl.row]);
12848
+ const S = new Matrix$1([[x1, y1, 1, 0, 0, 0, -x1 * tl.column, -y1 * tl.column], [x2, y2, 1, 0, 0, 0, -x2 * tr.column, -y2 * tr.column], [x3, y3, 1, 0, 0, 0, -x3 * br.column, -y3 * br.column], [x4, y4, 1, 0, 0, 0, -x4 * bl.column, -y4 * bl.column], [0, 0, 0, x1, y1, 1, -x1 * tl.row, -y1 * tl.row], [0, 0, 0, x2, y2, 1, -x2 * tr.row, -y2 * tr.row], [0, 0, 0, x3, y3, 1, -x3 * br.row, -y3 * br.row], [0, 0, 0, x4, y4, 1, -x4 * bl.row, -y4 * bl.row]]);
12849
+ const D = Matrix$1.columnVector([tl.column, tr.column, br.column, bl.column, tl.row, tr.row, br.row, bl.row]);
12850
12850
  const svd = new SingularValueDecomposition(S);
12851
12851
  const T = svd.solve(D).to1DArray(); // solve S*T = D
12852
12852
  T.push(1);
@@ -13241,7 +13241,7 @@ class PolynomialRegression2D extends BaseRegression2D {
13241
13241
  // @ts-expect-error internal use only
13242
13242
  if (inputs === true) {
13243
13243
  // @ts-expect-error internal use only
13244
- this.coefficients = Matrix.columnVector(outputs.coefficients);
13244
+ this.coefficients = Matrix$1.columnVector(outputs.coefficients);
13245
13245
  // @ts-expect-error internal use only
13246
13246
  this.order = outputs.order;
13247
13247
  // @ts-expect-error internal use only
@@ -13356,8 +13356,8 @@ function powColVector(x, power) {
13356
13356
  * @param y - A vector of the prediction values.
13357
13357
  */
13358
13358
  function train(input, y, order) {
13359
- if (!Matrix.isMatrix(y)) y = Matrix.columnVector(y);
13360
- const x = new Matrix(y.rows, 2);
13359
+ if (!Matrix$1.isMatrix(y)) y = Matrix$1.columnVector(y);
13360
+ const x = new Matrix$1(y.rows, 2);
13361
13361
  x.setColumn(0, input.x);
13362
13362
  x.setColumn(1, input.y);
13363
13363
  if (y.rows !== x.rows) {
@@ -13382,7 +13382,7 @@ function train(input, y, order) {
13382
13382
  x1.mulColumn(0, scaleX1);
13383
13383
  x2.mulColumn(0, scaleX2);
13384
13384
  y.mulColumn(0, scaleY);
13385
- const A = new Matrix(examples, nbCoefficients);
13385
+ const A = new Matrix$1(examples, nbCoefficients);
13386
13386
  let col = 0;
13387
13387
  for (let i = 0; i <= order; ++i) {
13388
13388
  const limit = order - i;
@@ -13397,11 +13397,11 @@ function train(input, y, order) {
13397
13397
  computeRightSingularVectors: true,
13398
13398
  autoTranspose: false
13399
13399
  });
13400
- let qqs = Matrix.rowVector(svd.diagonal);
13400
+ let qqs = Matrix$1.rowVector(svd.diagonal);
13401
13401
  qqs = qqs.apply((i, j) => {
13402
13402
  if (qqs.get(i, j) >= 1e-15) qqs.set(i, j, 1 / qqs.get(i, j));else qqs.set(i, j, 0);
13403
13403
  });
13404
- const qqs1 = Matrix.zeros(examples, nbCoefficients);
13404
+ const qqs1 = Matrix$1.zeros(examples, nbCoefficients);
13405
13405
  for (let i = 0; i < nbCoefficients; ++i) {
13406
13406
  qqs1.set(i, i, qqs.get(0, i));
13407
13407
  }
@@ -16069,8 +16069,8 @@ class MultivariateLinearRegression {
16069
16069
  this.outputs = y.outputs;
16070
16070
  this.intercept = y.intercept;
16071
16071
  } else {
16072
- x = new Matrix(x);
16073
- y = new Matrix(y);
16072
+ x = new Matrix$1(x);
16073
+ y = new Matrix$1(y);
16074
16074
  if (intercept) {
16075
16075
  x.addColumn(new Array(x.rows).fill(1));
16076
16076
  }
@@ -16112,8 +16112,8 @@ class MultivariateLinearRegression {
16112
16112
  }
16113
16113
  return y;
16114
16114
  }
16115
- } else if (Matrix.isMatrix(x)) {
16116
- const y = new Matrix(x.rows, this.outputs);
16115
+ } else if (Matrix$1.isMatrix(x)) {
16116
+ const y = new Matrix$1(x.rows, this.outputs);
16117
16117
  for (let i = 0; i < x.rows; i++) {
16118
16118
  y.setRow(i, this._predict(x.getRow(i)));
16119
16119
  }
@@ -28673,7 +28673,7 @@ function encode(image, options = defaultPng) {
28673
28673
  * @returns - The `fs`, `path` and `url` Node.js modules.
28674
28674
  */
28675
28675
  function getNodeApiOrThrow(methodName) {
28676
- if (typeof process === 'undefined' || typeof process.getBuiltinModule !== 'function') {
28676
+ if (!isNode()) {
28677
28677
  throw new Error(`${methodName} is only implemented for Node.js`);
28678
28678
  }
28679
28679
  return {
@@ -28682,6 +28682,35 @@ function getNodeApiOrThrow(methodName) {
28682
28682
  url: process.getBuiltinModule('node:url')
28683
28683
  };
28684
28684
  }
28685
+ let CanvasCtorBrowser;
28686
+ let CanvasCtorNode;
28687
+ /**
28688
+ * Returns a 2D canvas context for rendering on the browser or Node.js.
28689
+ * @param width - Width of the canvas.
28690
+ * @param height - Height of the canvas.
28691
+ * @returns The initialised canvas context.
28692
+ */
28693
+ function getCanvasContext(width, height) {
28694
+ if (isNode()) {
28695
+ CanvasCtorNode ??= getRequireFn()('skia-canvas').Canvas;
28696
+ return new CanvasCtorNode(width, height).getContext('2d');
28697
+ } else {
28698
+ CanvasCtorBrowser ??= globalThis.OffscreenCanvas;
28699
+ const context = new CanvasCtorBrowser(width, height).getContext('2d');
28700
+ if (!context) {
28701
+ throw new Error('Failed to create canvas context');
28702
+ }
28703
+ return context;
28704
+ }
28705
+ }
28706
+ function isNode() {
28707
+ return typeof process !== 'undefined' && typeof process.getBuiltinModule === 'function';
28708
+ }
28709
+ let requireFn;
28710
+ function getRequireFn() {
28711
+ requireFn ??= process.getBuiltinModule('node:module').createRequire(import.meta.url);
28712
+ return requireFn;
28713
+ }
28685
28714
 
28686
28715
  /**
28687
28716
  * Asynchronously write an image to the disk.
@@ -28708,7 +28737,7 @@ async function write(path, image, options) {
28708
28737
  await nodeApi.fs.promises.writeFile(path, toWrite);
28709
28738
  }
28710
28739
  /**
28711
- * Synchronous version of @see {@link write}.
28740
+ * Synchronous version of {@link write}.
28712
28741
  * This method is only implemented for Node.js.
28713
28742
  * @param path - Path where the image should be written.
28714
28743
  * @param image - Image to save.
@@ -28858,7 +28887,7 @@ function encodeDataURL(image, options = defaultPng) {
28858
28887
  * @returns The matrix.
28859
28888
  */
28860
28889
  function getMatrixFromPoints(points) {
28861
- const matrix = new Matrix(3, points.length);
28890
+ const matrix = new Matrix$1(3, points.length);
28862
28891
  for (let i = 0; i < points.length; i++) {
28863
28892
  matrix.set(0, i, points[i].column);
28864
28893
  matrix.set(1, i, points[i].row);
@@ -30168,18 +30197,18 @@ var fileTypeExports = fileType$1.exports;
30168
30197
 
30169
30198
  const fileType = fileTypeExports;
30170
30199
  const imageExts = new Set(['jpg', 'png', 'gif', 'webp', 'flif', 'cr2', 'tif', 'bmp', 'jxr', 'psd', 'ico', 'bpg', 'jp2', 'jpm', 'jpx', 'heic', 'cur', 'dcm']);
30171
- const imageType = input => {
30200
+ const imageType$1 = input => {
30172
30201
  const ret = fileType(input);
30173
30202
  return imageExts.has(ret && ret.ext) ? ret : null;
30174
30203
  };
30175
- imageType$2.exports = imageType;
30204
+ imageType$2.exports = imageType$1;
30176
30205
  // TODO: Remove this for the next major release
30177
- imageType$2.exports.default = imageType;
30178
- Object.defineProperty(imageType, 'minimumBytes', {
30206
+ imageType$2.exports.default = imageType$1;
30207
+ Object.defineProperty(imageType$1, 'minimumBytes', {
30179
30208
  value: fileType.minimumBytes
30180
30209
  });
30181
30210
  var imageTypeExports = imageType$2.exports;
30182
- var imageType$1 = /*@__PURE__*/getDefaultExportFromCjs(imageTypeExports);
30211
+ var imageType = /*@__PURE__*/getDefaultExportFromCjs(imageTypeExports);
30183
30212
 
30184
30213
  /**
30185
30214
  * Decode a BMP. See the fast-bmp npm module.
@@ -31647,7 +31676,7 @@ function getImageFromIFD(ifd) {
31647
31676
  */
31648
31677
  function decode(data) {
31649
31678
  const typedArray = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
31650
- const type = imageType$1(typedArray);
31679
+ const type = imageType(typedArray);
31651
31680
  return M$2(type).with({
31652
31681
  mime: 'image/png'
31653
31682
  }, () => decodePng(typedArray)).with({
@@ -31674,7 +31703,7 @@ async function read(path) {
31674
31703
  return decode(data);
31675
31704
  }
31676
31705
  /**
31677
- * Synchronous version of @see {@link read}.
31706
+ * Synchronous version of {@link read}.
31678
31707
  * This method is only implemented for Node.js.
31679
31708
  * @param path - The path to the image.
31680
31709
  * @returns Image instance.
@@ -32747,7 +32776,7 @@ class RoiMapManager {
32747
32776
  * @returns The ROI map matrix.
32748
32777
  */
32749
32778
  getMapMatrix() {
32750
- return Matrix.from1DArray(this.map.height, this.map.width, this.map.data).to2DArray();
32779
+ return Matrix$1.from1DArray(this.map.height, this.map.width, this.map.data).to2DArray();
32751
32780
  }
32752
32781
  getRois(options = {}) {
32753
32782
  return getRois(this, options);
@@ -32887,7 +32916,7 @@ function commonjsRequire(path) {
32887
32916
 
32888
32917
  var priorityQueue = {exports: {}};
32889
32918
 
32890
- (function (module, exports) {
32919
+ (function (module, exports$1) {
32891
32920
  (function (f) {
32892
32921
  {
32893
32922
  module.exports = f();
@@ -32917,7 +32946,7 @@ var priorityQueue = {exports: {}};
32917
32946
  for (var o = 0; o < r.length; o++) s(r[o]);
32918
32947
  return s;
32919
32948
  }({
32920
- 1: [function (_dereq_, module, exports) {
32949
+ 1: [function (_dereq_, module, exports$1) {
32921
32950
  var AbstractPriorityQueue,
32922
32951
  ArrayStrategy,
32923
32952
  BHeapStrategy,
@@ -32962,7 +32991,7 @@ var priorityQueue = {exports: {}};
32962
32991
  "./PriorityQueue/BHeapStrategy": 4,
32963
32992
  "./PriorityQueue/BinaryHeapStrategy": 5
32964
32993
  }],
32965
- 2: [function (_dereq_, module, exports) {
32994
+ 2: [function (_dereq_, module, exports$1) {
32966
32995
  module.exports = function () {
32967
32996
  function AbstractPriorityQueue(options) {
32968
32997
  var ref;
@@ -33000,7 +33029,7 @@ var priorityQueue = {exports: {}};
33000
33029
  return AbstractPriorityQueue;
33001
33030
  }();
33002
33031
  }, {}],
33003
- 3: [function (_dereq_, module, exports) {
33032
+ 3: [function (_dereq_, module, exports$1) {
33004
33033
  var binarySearchForIndexReversed;
33005
33034
  binarySearchForIndexReversed = function (array, value, comparator) {
33006
33035
  var high, low, mid;
@@ -33043,7 +33072,7 @@ var priorityQueue = {exports: {}};
33043
33072
  return ArrayStrategy;
33044
33073
  }();
33045
33074
  }, {}],
33046
- 4: [function (_dereq_, module, exports) {
33075
+ 4: [function (_dereq_, module, exports$1) {
33047
33076
  module.exports = function () {
33048
33077
  function BHeapStrategy(options) {
33049
33078
  var arr, j, k, len, ref, ref1, shift, value;
@@ -33181,7 +33210,7 @@ var priorityQueue = {exports: {}};
33181
33210
  return BHeapStrategy;
33182
33211
  }();
33183
33212
  }, {}],
33184
- 5: [function (_dereq_, module, exports) {
33213
+ 5: [function (_dereq_, module, exports$1) {
33185
33214
  module.exports = function () {
33186
33215
  function BinaryHeapStrategy(options) {
33187
33216
  var ref;
@@ -33580,7 +33609,7 @@ function decodeStackFromTiff(buffer) {
33580
33609
  */
33581
33610
  function decodeStack(data) {
33582
33611
  const typedArray = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
33583
- const type = imageType$1(typedArray);
33612
+ const type = imageType(typedArray);
33584
33613
  return M$2(type).with({
33585
33614
  mime: 'image/tiff'
33586
33615
  }, () => decodeStackFromTiff(typedArray)).with({
@@ -33843,5 +33872,104 @@ const channelLabels = {
33843
33872
  BINARY: ['Mask']
33844
33873
  };
33845
33874
 
33846
- export { BorderType, DerivativeFilter, GreyAlgorithm, Image, ImageColorModel, ImageCoordinates, ImageFormat, InterpolationType, Mask, Montage, MontageDisposition, Roi, RoiKind, RoiMapManager, RoisColorMode, Stack, ThresholdAlgorithm, add, alignMinDifference, and, blur, bottomHat, bruteForceOneMatch, cannyEdgeDetector, channelLabels, clearBorder, close, colorModels, colorRois, computeConvolutionValue, computeMse, computePsnr, computeRmse, computeRois, computeThreshold, convertBinaryToGrey, convertBinaryToRgb, convertBitDepth, convertColor, copyAlpha, copyTo, correctBackground, correctColor, crop, cropAlpha, cropRectangle, crosscheck, decode, decodeJpeg, decodePng, decodeStack, decodeTiff, defaultPng, derivativeFilter, dilate, directConvolution, divide, drawCircleOnImage, drawKeypoints, drawLineOnImage, drawLineOnMask, drawMarker, drawMarkers, drawMatches, drawPoints, drawPolygonOnImage, drawPolygonOnMask, drawPolylineOnImage, drawPolylineOnMask, drawRectangle, encode, encodeDataURL, encodeJpeg, encodePng, erode, extendBorders, extract, fetchURL, flip, floodFill, fromMask, gaussianBlur, getAffineTransform, getBestKeypointsInRadius, getBorderPoints, getBriefDescriptors, getCrosscheckMatches, getDirection, getExtrema, getFastKeypoints, getImageFromIFD, getIntensityCentroid, getMask, getOrientedFastKeypoints, getPerspectiveWarp, getRegressionVariables, getRois, gradientFilter, grey, histogram$1 as histogram, hypotenuse, increaseContrast, invert, level, mean, median, medianFilter, merge, morphologicalGradient, multiply, open, or, order4Points, overlapImages, paintMaskOnImage, paintMaskOnMask, pixelate, rawDirectConvolution, read, readCanvas, readImg, readSync, removeClosePoints, resize, rotate, separableConvolution, solidFill, split, subtract, threshold, topHat, transform, transformRotate, variance, waterShed, write, writeCanvas, writeSync };
33875
+ /**
33876
+ * Draws text on an image.
33877
+ * @param image - Image to write text on.
33878
+ * @param text - Text to write on the image.
33879
+ * @param options - Out Options
33880
+ * @returns Image with drawn text.
33881
+ */
33882
+ function drawText(image, text, options) {
33883
+ if (Array.isArray(text) && text.length === 0) {
33884
+ throw new Error('At least one text element must be provided');
33885
+ }
33886
+ const newImage = getOutputImage(image, options, {
33887
+ clone: true
33888
+ });
33889
+ const defaultFont = options?.font ?? '12px Helvetica';
33890
+ const defaultColor = options?.fontColor ?? [255, 255, 255, 255];
33891
+ const ctx = getCanvasContext(image.width, image.height);
33892
+ if (!Array.isArray(text)) {
33893
+ drawTextElement(image, ctx, text, defaultFont, defaultColor);
33894
+ } else {
33895
+ for (const label of text) {
33896
+ drawTextElement(image, ctx, label, defaultFont, defaultColor);
33897
+ }
33898
+ }
33899
+ layerCanvas(newImage.getRawImage().data, ctx.getImageData(0, 0, image.width, image.height).data, image.channels, image.bitDepth);
33900
+ return newImage;
33901
+ }
33902
+ function drawTextElement(image, ctx, text, defaultFont, defaultColor) {
33903
+ ctx.font = text.font ?? defaultFont;
33904
+ const fontColor = text.fontColor ?? defaultColor;
33905
+ validateValues(fontColor, image);
33906
+ const alpha = fontColor[3] ? fontColor[3] / 255 : 1;
33907
+ const normalizedColor = [fontColor[0] ?? 0, fontColor[1] ?? 0, fontColor[2] ?? 0, alpha];
33908
+ ctx.fillStyle = `rgba(${normalizedColor.join(',')})`;
33909
+ ctx.fillText(String(text.content), text.position.column, text.position.row);
33910
+ }
33911
+ /**
33912
+ * Draws labels on the image data from canvas.
33913
+ * @param imageData - Image data to draw text on.
33914
+ * @param canvasData - Canvas data to draw on the image.
33915
+ * @param numberOfChannels - Number of channels of the initial image.
33916
+ * @param bitDepth - Bit depth of the initial image.
33917
+ */
33918
+ function layerCanvas(imageData, canvasData, numberOfChannels, bitDepth) {
33919
+ const config = CHANNEL_CONFIGS[numberOfChannels] || CHANNEL_CONFIGS[1];
33920
+ const pixelCount = canvasData.length >>> 2;
33921
+ const bitShift = bitDepth - 8;
33922
+ let imageIndex = 0;
33923
+ let canvasIndex = 0;
33924
+ for (let pixel = 0; pixel < pixelCount; pixel++) {
33925
+ const canvasAlpha = canvasData[canvasIndex + 3] / 255;
33926
+ // Skip transparent canvas pixels completely
33927
+ if (canvasAlpha === 0) {
33928
+ imageIndex += numberOfChannels;
33929
+ canvasIndex += 4;
33930
+ continue;
33931
+ }
33932
+ const invAlpha = 1 - canvasAlpha;
33933
+ for (const channel of config.channelOffsets) {
33934
+ const targetIndex = imageIndex + channel;
33935
+ imageData[targetIndex] = Math.round(canvasData[canvasIndex + channel] * canvasAlpha + imageData[targetIndex] * invAlpha) << bitShift;
33936
+ }
33937
+ if (config.hasAlpha) {
33938
+ const alphaIndex = imageIndex + config.alphaOffset;
33939
+ const imageAlpha = (imageData[alphaIndex] >>> bitShift) / 255;
33940
+ const newAlpha = canvasAlpha + imageAlpha * (1 - canvasAlpha);
33941
+ imageData[alphaIndex] = Math.round(newAlpha * 255) << bitShift;
33942
+ }
33943
+ imageIndex += numberOfChannels;
33944
+ canvasIndex += 4;
33945
+ }
33946
+ }
33947
+ const CHANNEL_CONFIGS = {
33948
+ // GREY
33949
+ 1: {
33950
+ channelOffsets: [0],
33951
+ hasAlpha: false,
33952
+ alphaOffset: undefined
33953
+ },
33954
+ // GREYA
33955
+ 2: {
33956
+ channelOffsets: [0],
33957
+ hasAlpha: true,
33958
+ alphaOffset: 1
33959
+ },
33960
+ 3: {
33961
+ // RGB
33962
+ channelOffsets: [0, 1, 2],
33963
+ hasAlpha: false,
33964
+ alphaOffset: undefined
33965
+ },
33966
+ 4: {
33967
+ // RGBA
33968
+ channelOffsets: [0, 1, 2],
33969
+ hasAlpha: true,
33970
+ alphaOffset: 3
33971
+ }
33972
+ };
33973
+
33974
+ export { BorderType, DerivativeFilter, GreyAlgorithm, Image, ImageColorModel, ImageCoordinates, ImageFormat, InterpolationType, Mask, Montage, MontageDisposition, Roi, RoiKind, RoiMapManager, RoisColorMode, Stack, ThresholdAlgorithm, add, alignMinDifference, and, blur, bottomHat, bruteForceOneMatch, cannyEdgeDetector, channelLabels, clearBorder, close, colorModels, colorRois, computeConvolutionValue, computeMse, computePsnr, computeRmse, computeRois, computeThreshold, convertBinaryToGrey, convertBinaryToRgb, convertBitDepth, convertColor, copyAlpha, copyTo, correctBackground, correctColor, crop, cropAlpha, cropRectangle, crosscheck, decode, decodeJpeg, decodePng, decodeStack, decodeTiff, defaultPng, derivativeFilter, dilate, directConvolution, divide, drawCircleOnImage, drawKeypoints, drawLineOnImage, drawLineOnMask, drawMarker, drawMarkers, drawMatches, drawPoints, drawPolygonOnImage, drawPolygonOnMask, drawPolylineOnImage, drawPolylineOnMask, drawRectangle, drawText, encode, encodeDataURL, encodeJpeg, encodePng, erode, extendBorders, extract, fetchURL, flip, floodFill, fromMask, gaussianBlur, getAffineTransform, getBestKeypointsInRadius, getBorderPoints, getBriefDescriptors, getCrosscheckMatches, getDirection, getExtrema, getFastKeypoints, getImageFromIFD, getIntensityCentroid, getMask, getOrientedFastKeypoints, getPerspectiveWarp, getRegressionVariables, getRois, gradientFilter, grey, histogram$1 as histogram, hypotenuse, increaseContrast, invert, level, mean, median, medianFilter, merge, morphologicalGradient, multiply, open, or, order4Points, overlapImages, paintMaskOnImage, paintMaskOnMask, pixelate, rawDirectConvolution, read, readCanvas, readImg, readSync, removeClosePoints, resize, rotate, separableConvolution, solidFill, split, subtract, threshold, topHat, transform, transformRotate, variance, waterShed, write, writeCanvas, writeSync };
33847
33975
  //# sourceMappingURL=image-js.esm.js.map