image-js 1.5.0 → 1.6.1

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 (56) hide show
  1. package/dist/image-js.esm.js +337 -18
  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 +339 -22
  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 +23 -2
  10. package/lib/Mask.d.ts +8 -0
  11. package/lib/Mask.d.ts.map +1 -1
  12. package/lib/Mask.js +10 -0
  13. package/lib/Mask.js.map +1 -1
  14. package/lib/load/decodePng.d.ts.map +1 -1
  15. package/lib/load/decodePng.js +26 -15
  16. package/lib/load/decodePng.js.map +1 -1
  17. package/lib/maskAnalysis/getContourMoore.d.ts +10 -0
  18. package/lib/maskAnalysis/getContourMoore.d.ts.map +1 -0
  19. package/lib/maskAnalysis/getContourMoore.js +107 -0
  20. package/lib/maskAnalysis/getContourMoore.js.map +1 -0
  21. package/lib/maskAnalysis/getContourPavlidis.d.ts +11 -0
  22. package/lib/maskAnalysis/getContourPavlidis.d.ts.map +1 -0
  23. package/lib/maskAnalysis/getContourPavlidis.js +90 -0
  24. package/lib/maskAnalysis/getContourPavlidis.js.map +1 -0
  25. package/lib/maskAnalysis/getExternalContour.d.ts +11 -0
  26. package/lib/maskAnalysis/getExternalContour.d.ts.map +1 -0
  27. package/lib/maskAnalysis/getExternalContour.js +18 -0
  28. package/lib/maskAnalysis/getExternalContour.js.map +1 -0
  29. package/lib/maskAnalysis/maskAnalysis.types.d.ts +8 -2
  30. package/lib/maskAnalysis/maskAnalysis.types.d.ts.map +1 -1
  31. package/lib/maskAnalysis/utils/getExtendedBorderPoints.d.ts.map +1 -1
  32. package/lib/maskAnalysis/utils/getExtendedBorderPoints.js +2 -4
  33. package/lib/maskAnalysis/utils/getExtendedBorderPoints.js.map +1 -1
  34. package/lib/maskAnalysis/utils/getExternalContourUtils.d.ts +17 -0
  35. package/lib/maskAnalysis/utils/getExternalContourUtils.d.ts.map +1 -0
  36. package/lib/maskAnalysis/utils/getExternalContourUtils.js +32 -0
  37. package/lib/maskAnalysis/utils/getExternalContourUtils.js.map +1 -0
  38. package/lib/roi/Roi.d.ts +7 -0
  39. package/lib/roi/Roi.d.ts.map +1 -1
  40. package/lib/roi/Roi.js +9 -0
  41. package/lib/roi/Roi.js.map +1 -1
  42. package/lib/roi/getExternalContour.d.ts +10 -0
  43. package/lib/roi/getExternalContour.d.ts.map +1 -0
  44. package/lib/roi/getExternalContour.js +11 -0
  45. package/lib/roi/getExternalContour.js.map +1 -0
  46. package/package.json +8 -5
  47. package/src/Mask.ts +12 -0
  48. package/src/load/decodePng.ts +25 -17
  49. package/src/maskAnalysis/getContourMoore.ts +143 -0
  50. package/src/maskAnalysis/getContourPavlidis.ts +102 -0
  51. package/src/maskAnalysis/getExternalContour.ts +27 -0
  52. package/src/maskAnalysis/maskAnalysis.types.ts +8 -2
  53. package/src/maskAnalysis/utils/getExtendedBorderPoints.ts +2 -4
  54. package/src/maskAnalysis/utils/getExternalContourUtils.ts +33 -0
  55. package/src/roi/Roi.ts +11 -0
  56. package/src/roi/getExternalContour.ts +17 -0
@@ -1,15 +1,15 @@
1
1
  /*
2
- * image-js v1.5.0
2
+ * image-js v1.6.1
3
3
  * Image processing and manipulation in JavaScript
4
4
  * https://github.com/image-js/image-js#readme
5
5
  *
6
6
  * Licensed under the MIT license.
7
7
  */
8
8
  (function (global, factory) {
9
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
10
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
11
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.IJS = {}));
12
- })(this, (function (exports) { 'use strict';
9
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('node:assert')) :
10
+ typeof define === 'function' && define.amd ? define(['exports', 'node:assert'], factory) :
11
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.IJS = {}, global.assert$1));
12
+ })(this, (function (exports, assert$1) { 'use strict';
13
13
 
14
14
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
15
15
  function getDefaultExportFromCjs (x) {
@@ -13966,6 +13966,287 @@ ${indent}columns: ${matrix.columns}
13966
13966
  return Math.abs(area / 2);
13967
13967
  }
13968
13968
 
13969
+ /**
13970
+ * Gets point's row and column from their index and mask width.
13971
+ * @param index - point's index.
13972
+ * @param maskWidth - mask width.
13973
+ * @returns array of point's row and column.
13974
+ */
13975
+ function getCoordsFromIndex(index, maskWidth) {
13976
+ return [Math.floor(index / maskWidth), index % maskWidth];
13977
+ }
13978
+ /**
13979
+ * Safely get the bit value at the given column and row in the mask, returning 0 if out of bounds.
13980
+ * @param mask - Mask to get the bit from.
13981
+ * @param col - Column index.
13982
+ * @param row - Row index.
13983
+ * @returns the bit value.
13984
+ */
13985
+ function getBitSafe(mask, col, row) {
13986
+ if (!isInBounds(mask, col, row)) return 0;
13987
+ return mask.getBit(col, row);
13988
+ }
13989
+ /**
13990
+ * Checks if the given column and row are within the bounds of the mask.
13991
+ * @param mask - Mask to check against.
13992
+ * @param col - Column index.
13993
+ * @param row - Row index.
13994
+ * @returns whether the given column and row are within the bounds of the mask.
13995
+ */
13996
+ function isInBounds(mask, col, row) {
13997
+ return col >= 0 && col < mask.width && row >= 0 && row < mask.height;
13998
+ }
13999
+
14000
+ /**
14001
+ * Possible directions.
14002
+ */
14003
+ const directions = [{
14004
+ dc: -1,
14005
+ dr: 0
14006
+ },
14007
+ // left
14008
+ {
14009
+ dc: -1,
14010
+ dr: -1
14011
+ },
14012
+ // up-left
14013
+ {
14014
+ dc: 0,
14015
+ dr: -1
14016
+ },
14017
+ // up
14018
+ {
14019
+ dc: 1,
14020
+ dr: -1
14021
+ },
14022
+ // up-right
14023
+ {
14024
+ dc: 1,
14025
+ dr: 0
14026
+ },
14027
+ // right
14028
+ {
14029
+ dc: 1,
14030
+ dr: 1
14031
+ },
14032
+ // down-right
14033
+ {
14034
+ dc: 0,
14035
+ dr: 1
14036
+ },
14037
+ // down
14038
+ {
14039
+ dc: -1,
14040
+ dr: 1
14041
+ } // down-left
14042
+ ];
14043
+ // Lookup table for directions' index.
14044
+ const directionIndexLookup = new Int8Array(9).fill(-1);
14045
+ for (let i = 0; i < directions.length; i++) {
14046
+ const {
14047
+ dc,
14048
+ dr
14049
+ } = directions[i];
14050
+ directionIndexLookup[(dr + 1) * 3 + (dc + 1)] = i;
14051
+ }
14052
+ /**
14053
+ * Return an array with the coordinates of the pixels that are on the border of the mask using Moore's tracing algorithm.
14054
+ * The reference is the top-left corner of the ROI.
14055
+ * @param mask - Mask to process.
14056
+ * @returns The array of border pixels.
14057
+ */
14058
+ function getContourMoore(mask) {
14059
+ let index = 0;
14060
+ const contour = [];
14061
+ const visited = new Uint8Array(mask.width * mask.height);
14062
+ while (mask.getBitByIndex(index) !== 1 && index !== mask.size) {
14063
+ index++;
14064
+ }
14065
+ if (index === mask.size) return contour;
14066
+ const [row, col] = getCoordsFromIndex(index, mask.width);
14067
+ const startingPoint = {
14068
+ column: col,
14069
+ row
14070
+ };
14071
+ let currentPoint = startingPoint;
14072
+ visited[index] = 1;
14073
+ contour.push(currentPoint);
14074
+ const firstNext = findNextPoint(mask, currentPoint, {
14075
+ column: col - 1,
14076
+ row
14077
+ });
14078
+ if (!firstNext) {
14079
+ return contour;
14080
+ }
14081
+ let startingBacktrackPoint = firstNext.prevPoint;
14082
+ let backtrackPoint = firstNext.prevPoint;
14083
+ currentPoint = firstNext.currPoint;
14084
+ while (true) {
14085
+ index = currentPoint.row * mask.width + currentPoint.column;
14086
+ if (!visited[index]) {
14087
+ visited[index] = 1;
14088
+ contour.push(currentPoint);
14089
+ }
14090
+ const next = findNextPoint(mask, currentPoint, backtrackPoint);
14091
+ assert$1.ok(next, 'Next border point is undefined.');
14092
+ backtrackPoint = next.prevPoint;
14093
+ currentPoint = next.currPoint;
14094
+ if (currentPoint.column === startingPoint.column && currentPoint.row === startingPoint.row) {
14095
+ if (backtrackPoint.column === startingBacktrackPoint.column && backtrackPoint.row === startingBacktrackPoint.row) {
14096
+ break;
14097
+ } else {
14098
+ startingBacktrackPoint = backtrackPoint;
14099
+ }
14100
+ }
14101
+ }
14102
+ return contour;
14103
+ }
14104
+ /**
14105
+ * Finds next point to trace.
14106
+ * @param mask - Mask in check.
14107
+ * @param current - Current border point.
14108
+ * @param previous - Last non-border point to start search from.
14109
+ * @returns Object with new border point and last non-border point.
14110
+ */
14111
+ function findNextPoint(mask, current, previous) {
14112
+ let prevPoint = previous;
14113
+ // Vector from current back to previous
14114
+ const backDc = previous.column - current.column;
14115
+ const backDr = previous.row - current.row;
14116
+ const startIndex = directionIndexLookup[(backDr + 1) * 3 + (backDc + 1)];
14117
+ // Scan clockwise starting from that index.
14118
+ for (let i = 0; i < directions.length; i++) {
14119
+ const {
14120
+ dc,
14121
+ dr
14122
+ } = directions.at((startIndex + i) % directions.length);
14123
+ const newCol = current.column + dc;
14124
+ const newRow = current.row + dr;
14125
+ if (getBitSafe(mask, newCol, newRow) === 1) {
14126
+ // Stores found border point and last non-border point that
14127
+ // was found. Will be used as a starting point in the next
14128
+ // iteration.
14129
+ return {
14130
+ prevPoint,
14131
+ currPoint: {
14132
+ column: newCol,
14133
+ row: newRow
14134
+ }
14135
+ };
14136
+ } else {
14137
+ prevPoint = {
14138
+ column: newCol,
14139
+ row: newRow
14140
+ };
14141
+ }
14142
+ }
14143
+ // Is triggered only if the point has no neighbours. So it is undefined
14144
+ // only if mask is made of one pixel.
14145
+ return undefined;
14146
+ }
14147
+
14148
+ const DR = [-1, 0, 1, 0];
14149
+ const DC = [0, 1, 0, -1];
14150
+ function turnLeft(d) {
14151
+ return (d + 3) % 4;
14152
+ }
14153
+ function turnRight(d) {
14154
+ return (d + 1) % 4;
14155
+ }
14156
+ /**
14157
+ * Return an array with the coordinates of the pixels that are on the
14158
+ * external border of the mask using Theo Pavlidis's tracing algorithm.
14159
+ * The reference is the top-left corner of the ROI.
14160
+ * @param mask - Mask to process.
14161
+ * @returns The array of external border pixels.
14162
+ */
14163
+ function getContourPavlidis(mask) {
14164
+ let index = 0;
14165
+ while (index < mask.size && mask.getBitByIndex(index) !== 1) {
14166
+ index++;
14167
+ }
14168
+ if (index === mask.size) return [];
14169
+ const [row, col] = getCoordsFromIndex(index, mask.width);
14170
+ return traceFrom(mask, col, row);
14171
+ }
14172
+ /**
14173
+ * Traces contour using Pavlidis algorithm.
14174
+ * @param mask - Mask in check.
14175
+ * @param startCol - Starting column.
14176
+ * @param startRow - Starting row.
14177
+ * @returns Array of contour points.
14178
+ */
14179
+ function traceFrom(mask, startCol, startRow) {
14180
+ let row = startRow;
14181
+ let column = startCol;
14182
+ let dir = 2;
14183
+ let inPlaceTurns = 0;
14184
+ const seen = new Uint8Array(mask.width * mask.height);
14185
+ const contour = [];
14186
+ let visits = 0;
14187
+ while (true) {
14188
+ const idx = row * mask.width + column;
14189
+ if (!seen[idx]) {
14190
+ seen[idx] = 1;
14191
+ contour.push({
14192
+ row,
14193
+ column
14194
+ });
14195
+ } else if (row === startRow && column === startCol) {
14196
+ if (visits === 2) {
14197
+ break;
14198
+ } else {
14199
+ visits++;
14200
+ }
14201
+ }
14202
+ const left = turnLeft(dir);
14203
+ const right = turnRight(dir);
14204
+ const c1r = row + DR[left];
14205
+ const c1c = column + DC[left];
14206
+ const c2r = row + DR[dir];
14207
+ const c2c = column + DC[dir];
14208
+ const c3r = row + DR[right];
14209
+ const c3c = column + DC[right];
14210
+ if (getBitSafe(mask, c1c, c1r) === 1) {
14211
+ dir = left;
14212
+ row = c1r;
14213
+ column = c1c;
14214
+ inPlaceTurns = 0;
14215
+ } else if (getBitSafe(mask, c2c, c2r) === 1) {
14216
+ row = c2r;
14217
+ column = c2c;
14218
+ inPlaceTurns = 0;
14219
+ } else if (getBitSafe(mask, c3c, c3r) === 1) {
14220
+ dir = right;
14221
+ row = c3r;
14222
+ column = c3c;
14223
+ inPlaceTurns = 0;
14224
+ } else {
14225
+ dir = right;
14226
+ inPlaceTurns++;
14227
+ if (inPlaceTurns >= 4) break;
14228
+ }
14229
+ }
14230
+ return contour;
14231
+ }
14232
+
14233
+ /**
14234
+ * Finds external contour of the mask.
14235
+ * @param mask - Mask to find contours from.
14236
+ * @param options - GetExternalContourOptions.
14237
+ * @returns Array of contour points.
14238
+ */
14239
+ function getExternalContour$1(mask, options = {}) {
14240
+ const {
14241
+ allowCorners = false
14242
+ } = options;
14243
+ if (allowCorners) {
14244
+ return getContourPavlidis(mask);
14245
+ } else {
14246
+ return getContourMoore(mask);
14247
+ }
14248
+ }
14249
+
13969
14250
  /**
13970
14251
  * Get the pixels that surround an ROI. The pixels include the top and left borders,
13971
14252
  * but extend the right and bottom one by one pixel.
@@ -13975,10 +14256,7 @@ ${indent}columns: ${matrix.columns}
13975
14256
  * @returns - The array of points.
13976
14257
  */
13977
14258
  function getExtendedBorderPoints(mask) {
13978
- const borderPoints = mask.getBorderPoints({
13979
- allowCorners: true,
13980
- innerBorders: false
13981
- });
14259
+ const borderPoints = getExternalContour$1(mask);
13982
14260
  const result = [];
13983
14261
  for (const point of borderPoints) {
13984
14262
  result.push(point, {
@@ -15608,6 +15886,15 @@ ${indent}columns: ${matrix.columns}
15608
15886
  getBorderPoints(options) {
15609
15887
  return getBorderPoints$1(this, options);
15610
15888
  }
15889
+ /**
15890
+ * Returns external contour of the mask. Unlike border points, returned points
15891
+ * follow the shape of the mask.
15892
+ * @param options - Get external contour options.
15893
+ * @returns Array of contour points.
15894
+ */
15895
+ getExternalContour(options) {
15896
+ return getExternalContour$1(this, options);
15897
+ }
15611
15898
  /**
15612
15899
  * Get the vertices of the convex Hull polygon of a mask.
15613
15900
  * @returns Array of the vertices of the convex Hull in clockwise order.
@@ -26074,18 +26361,7 @@ ${indent}columns: ${matrix.columns}
26074
26361
  default:
26075
26362
  throw new RangeError(`invalid number of channels: ${png.channels}`);
26076
26363
  }
26077
- let resolution;
26078
- if (png.resolution) {
26079
- resolution = png.resolution.unit === 1 ? /*If the resolution unit is meters*/{
26080
- x: png.resolution.x,
26081
- y: png.resolution.y,
26082
- unit: 'meter'
26083
- } : /*If resolution unit is unknown */{
26084
- x: png.resolution.x,
26085
- y: png.resolution.y,
26086
- unit: 'unknown'
26087
- };
26088
- }
26364
+ const resolution = getResolution(png);
26089
26365
  return new Image(png.width, png.height, {
26090
26366
  colorModel,
26091
26367
  bitDepth,
@@ -26117,9 +26393,11 @@ ${indent}columns: ${matrix.columns}
26117
26393
  data[dataIndex++] = paletteChannel;
26118
26394
  }
26119
26395
  }
26396
+ const resolution = getResolution(png);
26120
26397
  return new Image(png.width, png.height, {
26121
26398
  data,
26122
- colorModel: png.palette[0].length === 4 ? 'RGBA' : 'RGB'
26399
+ colorModel: png.palette[0].length === 4 ? 'RGBA' : 'RGB',
26400
+ resolution
26123
26401
  });
26124
26402
  }
26125
26403
  function decodeBinary(png) {
@@ -26139,6 +26417,26 @@ ${indent}columns: ${matrix.columns}
26139
26417
  }
26140
26418
  return result;
26141
26419
  }
26420
+ /**
26421
+ * Gets image's resolution from its parsed data.
26422
+ * @param png - Parsed .png image.
26423
+ * @returns Object with resolution data if exists.
26424
+ */
26425
+ function getResolution(png) {
26426
+ if (png.resolution) {
26427
+ return png.resolution.unit === 1 ? /*If the resolution unit is meters*/{
26428
+ x: png.resolution.x,
26429
+ y: png.resolution.y,
26430
+ unit: 'meter'
26431
+ } : /*If resolution unit is unknown */{
26432
+ x: png.resolution.x,
26433
+ y: png.resolution.y,
26434
+ unit: 'unknown'
26435
+ };
26436
+ } else {
26437
+ return undefined;
26438
+ }
26439
+ }
26142
26440
 
26143
26441
  /**
26144
26442
  * Decode a TIFF. See the tiff module.
@@ -26566,6 +26864,17 @@ ${indent}columns: ${matrix.columns}
26566
26864
  return mask.getBorderPoints(options);
26567
26865
  }
26568
26866
 
26867
+ /**
26868
+ * Finds external contour of the roi from its mask.
26869
+ * @param roi - roi to find contour from.
26870
+ * @param options - GetExternalContourOptions.
26871
+ * @returns Array of contour points.
26872
+ */
26873
+ function getExternalContour(roi, options) {
26874
+ const mask = roi.getMask();
26875
+ return mask.getExternalContour(options);
26876
+ }
26877
+
26569
26878
  /**
26570
26879
  * Generate a mask of an ROI.
26571
26880
  * @param roi - The ROI to generate a mask for.
@@ -26769,6 +27078,14 @@ ${indent}columns: ${matrix.columns}
26769
27078
  getBorderPoints(options) {
26770
27079
  return getBorderPoints(this, options);
26771
27080
  }
27081
+ /**
27082
+ * Finds external contour of the roi from its mask.
27083
+ * @param options - GetExternalContourOptions.
27084
+ * @returns Array of contour points.
27085
+ */
27086
+ getExternalContour(options) {
27087
+ return getExternalContour(this, options);
27088
+ }
26772
27089
  /**
26773
27090
  * Returns an array of ROIs IDs that are included in the current ROI.
26774
27091
  * This will be useful to know if there are some holes in the ROI.