image-js 1.2.0 → 1.3.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.
@@ -4,6 +4,7 @@ import type { BitDepth } from '../Image.js';
4
4
  import { Image } from '../Image.js';
5
5
 
6
6
  import { getMetadata } from './getMetadata.js';
7
+ import type { Resolution } from './load.types.ts';
7
8
 
8
9
  type TiffIfd = ReturnType<typeof decode>[number];
9
10
 
@@ -26,7 +27,7 @@ export function getImageFromIFD(ifd: TiffIfd): Image {
26
27
  if (ifd.data instanceof Float32Array || ifd.data instanceof Float64Array) {
27
28
  throw new Error('Float TIFF data is not supported.');
28
29
  }
29
-
30
+ const resolution = getTiffResolution(ifd);
30
31
  if (ifd.type === 3) {
31
32
  const hasAlpha = ifd.samplesPerPixel === 2;
32
33
  const pixelLength = hasAlpha ? 4 : 3;
@@ -66,6 +67,7 @@ export function getImageFromIFD(ifd: TiffIfd): Image {
66
67
  colorModel: hasAlpha ? 'RGBA' : 'RGB',
67
68
  bitDepth: 16,
68
69
  meta: getMetadata(ifd),
70
+ resolution,
69
71
  });
70
72
  } else if (ifd.type === 1 || ifd.type === 0) {
71
73
  if (ifd.bitsPerSample !== 1) {
@@ -74,6 +76,7 @@ export function getImageFromIFD(ifd: TiffIfd): Image {
74
76
  bitDepth: ifd.bitsPerSample as BitDepth,
75
77
  colorModel: ifd.alpha ? 'GREYA' : 'GREY',
76
78
  meta: getMetadata(ifd),
79
+ resolution,
77
80
  });
78
81
  } else {
79
82
  return new Image(ifd.width, ifd.height, {
@@ -81,6 +84,7 @@ export function getImageFromIFD(ifd: TiffIfd): Image {
81
84
  bitDepth: 8 as BitDepth,
82
85
  colorModel: 'GREY',
83
86
  meta: getMetadata(ifd),
87
+ resolution,
84
88
  });
85
89
  }
86
90
  } else {
@@ -89,6 +93,39 @@ export function getImageFromIFD(ifd: TiffIfd): Image {
89
93
  bitDepth: ifd.bitsPerSample as BitDepth,
90
94
  colorModel: ifd.alpha ? 'RGBA' : 'RGB',
91
95
  meta: getMetadata(ifd),
96
+ resolution,
92
97
  });
93
98
  }
94
99
  }
100
+
101
+ /**
102
+ * Gets image resolution from its metadata and converts it into Pixels per meter, when it's possible. Also keeps original resolution values and units.
103
+ * @param ifd - Tiff metadata.
104
+ * @returns Resolution object.
105
+ */
106
+ function getTiffResolution(ifd: TiffIfd): Resolution | undefined {
107
+ if (!ifd.xResolution || !ifd.yResolution) {
108
+ return undefined;
109
+ }
110
+
111
+ switch (ifd.resolutionUnit) {
112
+ case 1:
113
+ return {
114
+ x: ifd.xResolution,
115
+ y: ifd.yResolution,
116
+ unit: 'unknown',
117
+ };
118
+ case 3:
119
+ return {
120
+ x: ifd.xResolution,
121
+ y: ifd.yResolution,
122
+ unit: 'centimeter',
123
+ };
124
+ default:
125
+ return {
126
+ x: ifd.xResolution,
127
+ y: ifd.yResolution,
128
+ unit: 'inch',
129
+ };
130
+ }
131
+ }
@@ -5,3 +5,11 @@ export interface ImageMetadata {
5
5
  };
6
6
  exif: Record<string, unknown>;
7
7
  }
8
+
9
+ export interface Resolution {
10
+ x: number;
11
+ y: number;
12
+ unit: ResolutionUnit;
13
+ }
14
+
15
+ export type ResolutionUnit = 'inch' | 'centimeter' | 'meter' | 'unknown';