image-js 1.0.0 → 1.1.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 (76) hide show
  1. package/dist/image-js.esm.js +866 -198
  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 +866 -198
  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 -3
  10. package/lib/Mask.d.ts +1 -1
  11. package/lib/Mask.js +1 -1
  12. package/lib/featureMatching/matching/filterEuclideanDistance.d.ts.map +1 -1
  13. package/lib/featureMatching/matching/filterEuclideanDistance.js +6 -7
  14. package/lib/featureMatching/matching/filterEuclideanDistance.js.map +1 -1
  15. package/lib/featureMatching/matching/filterSmallestDistanceMatches.d.ts.map +1 -1
  16. package/lib/featureMatching/matching/filterSmallestDistanceMatches.js +6 -3
  17. package/lib/featureMatching/matching/filterSmallestDistanceMatches.js.map +1 -1
  18. package/lib/featureMatching/utils/sortByDistance.d.ts.map +1 -1
  19. package/lib/featureMatching/utils/sortByDistance.js +3 -1
  20. package/lib/featureMatching/utils/sortByDistance.js.map +1 -1
  21. package/lib/featureMatching/utils/sortBySourceDest.d.ts.map +1 -1
  22. package/lib/featureMatching/utils/sortBySourceDest.js +6 -2
  23. package/lib/featureMatching/utils/sortBySourceDest.js.map +1 -1
  24. package/lib/load/decode.d.ts.map +1 -1
  25. package/lib/load/decode.js +2 -0
  26. package/lib/load/decode.js.map +1 -1
  27. package/lib/load/decodeBmp.d.ts +8 -0
  28. package/lib/load/decodeBmp.d.ts.map +1 -0
  29. package/lib/load/decodeBmp.js +36 -0
  30. package/lib/load/decodeBmp.js.map +1 -0
  31. package/lib/load/decodePng.d.ts.map +1 -1
  32. package/lib/load/decodePng.js +29 -5
  33. package/lib/load/decodePng.js.map +1 -1
  34. package/lib/load/decodeTiff.d.ts.map +1 -1
  35. package/lib/load/decodeTiff.js +44 -21
  36. package/lib/load/decodeTiff.js.map +1 -1
  37. package/lib/maskAnalysis/utils/getMbrAngle.d.ts.map +1 -1
  38. package/lib/maskAnalysis/utils/getMbrAngle.js +2 -1
  39. package/lib/maskAnalysis/utils/getMbrAngle.js.map +1 -1
  40. package/lib/maskAnalysis/utils/monotoneChainConvexHull.d.ts.map +1 -1
  41. package/lib/maskAnalysis/utils/monotoneChainConvexHull.js +2 -1
  42. package/lib/maskAnalysis/utils/monotoneChainConvexHull.js.map +1 -1
  43. package/lib/save/encodeBmp.d.ts +4 -4
  44. package/lib/save/encodeBmp.d.ts.map +1 -1
  45. package/lib/save/encodeBmp.js +22 -14
  46. package/lib/save/encodeBmp.js.map +1 -1
  47. package/lib/stack/load/decodeApng.d.ts +8 -0
  48. package/lib/stack/load/decodeApng.d.ts.map +1 -0
  49. package/lib/stack/load/decodeApng.js +60 -0
  50. package/lib/stack/load/decodeApng.js.map +1 -0
  51. package/lib/stack/load/decodeStack.d.ts.map +1 -1
  52. package/lib/stack/load/decodeStack.js +2 -0
  53. package/lib/stack/load/decodeStack.js.map +1 -1
  54. package/lib/utils/geometry/points.d.ts.map +1 -1
  55. package/lib/utils/geometry/points.js +3 -1
  56. package/lib/utils/geometry/points.js.map +1 -1
  57. package/lib/utils/geometry/removeClosePoints.d.ts.map +1 -1
  58. package/lib/utils/geometry/removeClosePoints.js +2 -1
  59. package/lib/utils/geometry/removeClosePoints.js.map +1 -1
  60. package/package.json +27 -27
  61. package/src/Mask.ts +1 -1
  62. package/src/featureMatching/matching/filterEuclideanDistance.ts +6 -13
  63. package/src/featureMatching/matching/filterSmallestDistanceMatches.ts +6 -9
  64. package/src/featureMatching/utils/sortByDistance.ts +3 -1
  65. package/src/featureMatching/utils/sortBySourceDest.ts +6 -2
  66. package/src/load/decode.ts +2 -1
  67. package/src/load/decodeBmp.ts +37 -0
  68. package/src/load/decodePng.ts +39 -7
  69. package/src/load/decodeTiff.ts +53 -22
  70. package/src/maskAnalysis/utils/getMbrAngle.ts +2 -1
  71. package/src/maskAnalysis/utils/monotoneChainConvexHull.ts +2 -1
  72. package/src/save/encodeBmp.ts +22 -15
  73. package/src/stack/load/decodeApng.ts +71 -0
  74. package/src/stack/load/decodeStack.ts +2 -0
  75. package/src/utils/geometry/points.ts +3 -1
  76. package/src/utils/geometry/removeClosePoints.ts +2 -1
@@ -21,7 +21,8 @@ export function monotoneChainConvexHull(
21
21
  ): Point[] {
22
22
  const { sorted = false } = options;
23
23
  if (!sorted) {
24
- points = points.slice().sort(byXThenY);
24
+ points = points.slice();
25
+ points.sort(byXThenY);
25
26
  }
26
27
 
27
28
  const n = points.length;
@@ -1,24 +1,31 @@
1
1
  import * as bmp from 'fast-bmp';
2
2
 
3
- import type { Image } from '../Image.js';
4
- import { Mask } from '../Mask.js';
3
+ import { Image } from '../Image.js';
4
+ import type { Mask } from '../Mask.js';
5
5
 
6
6
  /**
7
7
  * Creates a BMP buffer from a mask.
8
- * @param mask - The mask instance.
8
+ * @param image - The mask instance.
9
9
  * @returns The buffer.
10
10
  */
11
- export function encodeBmp(mask: Image | Mask) {
12
- if (!(mask instanceof Mask)) {
13
- throw new TypeError('Image bmp encoding is not implemented.');
11
+ export function encodeBmp(image: Image | Mask) {
12
+ if (image instanceof Image) {
13
+ return bmp.encode({
14
+ width: image.width,
15
+ height: image.height,
16
+ components: image.components,
17
+ bitsPerPixel: image.channels * image.bitDepth,
18
+ channels: image.channels,
19
+ data: image.getRawImage().data,
20
+ });
21
+ } else {
22
+ return bmp.encode({
23
+ width: image.width,
24
+ height: image.height,
25
+ components: 1,
26
+ bitsPerPixel: 1,
27
+ channels: 1,
28
+ data: image.getRawImage().data,
29
+ });
14
30
  }
15
- const imageData = mask.getRawImage();
16
- return bmp.encode({
17
- width: imageData.width,
18
- height: imageData.height,
19
- components: 1,
20
- bitsPerPixel: 1,
21
- channels: 1,
22
- data: imageData.data,
23
- });
24
31
  }
@@ -0,0 +1,71 @@
1
+ import type { IndexedColors } from 'fast-png';
2
+ import { decodeApng } from 'fast-png';
3
+
4
+ import { Image } from '../../Image.ts';
5
+ import { Stack } from '../../Stack.ts';
6
+ import type { ImageColorModel } from '../../utils/constants/colorModels.ts';
7
+
8
+ /**
9
+ * Decodes APNG image into a Stack
10
+ * @param data - APNG data.
11
+ * @returns stack of frames.
12
+ */
13
+ export function decodeStackFromApng(data: Uint8Array) {
14
+ const decodedApng = decodeApng(data);
15
+ const images: Image[] = [];
16
+ let colorModel: ImageColorModel;
17
+ switch (decodedApng.channels) {
18
+ case 1:
19
+ if (decodedApng.palette) {
20
+ colorModel = decodedApng.palette[0].length === 3 ? 'RGB' : 'RGBA';
21
+ } else {
22
+ colorModel = 'GREY';
23
+ }
24
+ break;
25
+ case 2:
26
+ colorModel = 'GREYA';
27
+ break;
28
+ case 3:
29
+ colorModel = 'RGB';
30
+ break;
31
+ default:
32
+ colorModel = 'RGBA';
33
+ break;
34
+ }
35
+ if (decodedApng.palette) {
36
+ for (const image of decodedApng.frames) {
37
+ images.push(
38
+ new Image(decodedApng.width, decodedApng.height, {
39
+ data: convertIndexedData(
40
+ image.data as Uint8Array,
41
+ decodedApng.palette,
42
+ ),
43
+ colorModel,
44
+ }),
45
+ );
46
+ }
47
+ } else {
48
+ for (const image of decodedApng.frames) {
49
+ images.push(
50
+ new Image(decodedApng.width, decodedApng.height, {
51
+ data: image.data,
52
+ colorModel,
53
+ }),
54
+ );
55
+ }
56
+ }
57
+
58
+ const stack = new Stack(images);
59
+
60
+ return stack;
61
+ }
62
+
63
+ function convertIndexedData(data: Uint8Array, palette: IndexedColors) {
64
+ const result = new Uint8Array(data.length * palette[0].length);
65
+ for (let i = 0; i < data.length; i++) {
66
+ for (let channel = 0; channel < palette[0].length; channel++) {
67
+ result[i * palette[0].length + channel] = palette[data[i]][channel];
68
+ }
69
+ }
70
+ return result;
71
+ }
@@ -3,6 +3,7 @@ import { match } from 'ts-pattern';
3
3
 
4
4
  import type { Stack } from '../../Stack.js';
5
5
 
6
+ import { decodeStackFromApng } from './decodeApng.ts';
6
7
  import { decodeStackFromTiff } from './decodeTiff.js';
7
8
 
8
9
  /**
@@ -20,6 +21,7 @@ export function decodeStack(data: ArrayBufferView): Stack {
20
21
  const type = imageType(typedArray);
21
22
  return match(type)
22
23
  .with({ mime: 'image/tiff' }, () => decodeStackFromTiff(typedArray))
24
+ .with({ mime: 'image/png' }, () => decodeStackFromApng(typedArray))
23
25
  .otherwise(() => {
24
26
  throw new RangeError(`invalid data format: ${type?.mime}`);
25
27
  });
@@ -94,9 +94,11 @@ export function round(point: Point): Point {
94
94
  * @returns Sorted points.
95
95
  */
96
96
  export function sortByColumnRow(points: Point[]): Point[] {
97
- return points.slice().sort((point1, point2) => {
97
+ const sortedPoints = points.slice();
98
+ sortedPoints.sort((point1, point2) => {
98
99
  if (point1.column < point2.column) return -1;
99
100
  if (point1.column > point2.column) return 1;
100
101
  return point1.row - point2.row;
101
102
  });
103
+ return sortedPoints;
102
104
  }
@@ -42,7 +42,8 @@ export function removeClosePoints(
42
42
  const channel = options?.channel || 0;
43
43
  const isMax = kind === 'maximum';
44
44
 
45
- const sortedPoints = points.slice().sort(getSort(image, channel, isMax));
45
+ const sortedPoints = points.slice();
46
+ sortedPoints.sort(getSort(image, channel, isMax));
46
47
 
47
48
  if (distance > 0) {
48
49
  for (let i = 0; i < sortedPoints.length; i++) {