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.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * image-js v1.2.0
2
+ * image-js v1.3.0
3
3
  * Image processing and manipulation in JavaScript
4
4
  * https://github.com/image-js/image-js#readme
5
5
  *
@@ -16811,6 +16811,10 @@ class Image {
16811
16811
  * Origin of the image relative to a the parent image.
16812
16812
  */
16813
16813
  origin;
16814
+ /**
16815
+ * Original image resolution.
16816
+ */
16817
+ originalResolution;
16814
16818
  meta;
16815
16819
  /**
16816
16820
  * Typed array holding the image data.
@@ -16831,7 +16835,8 @@ class Image {
16831
16835
  row: 0,
16832
16836
  column: 0
16833
16837
  },
16834
- meta
16838
+ meta,
16839
+ resolution
16835
16840
  } = options;
16836
16841
  if (width < 1 || !Number.isInteger(width)) {
16837
16842
  throw new RangeError(`width must be an integer and at least 1. Received ${width}`);
@@ -16846,6 +16851,7 @@ class Image {
16846
16851
  this.colorModel = colorModel;
16847
16852
  this.origin = origin;
16848
16853
  this.meta = meta;
16854
+ this.originalResolution = resolution;
16849
16855
  const colorModelDef = colorModels[colorModel];
16850
16856
  this.components = colorModelDef.components;
16851
16857
  this.alpha = colorModelDef.alpha;
@@ -16866,6 +16872,38 @@ class Image {
16866
16872
  this.data = data;
16867
16873
  }
16868
16874
  }
16875
+ /**
16876
+ * Returns normalized resolution in pixels per centimeter. If resolution unit is unknown, return null.
16877
+ * @returns Object with x and y resolutions in pixel/cm.
16878
+ */
16879
+ get normalizedResolution() {
16880
+ if (!this.originalResolution) {
16881
+ return undefined;
16882
+ }
16883
+ const centimetersPerInch = 2.54;
16884
+ const centimetersPerMeter = 100;
16885
+ switch (this.originalResolution.unit) {
16886
+ case 'inch':
16887
+ return {
16888
+ x: this.originalResolution.x / centimetersPerInch,
16889
+ y: this.originalResolution.y / centimetersPerInch
16890
+ };
16891
+ case 'centimeter':
16892
+ return {
16893
+ x: this.originalResolution.x,
16894
+ y: this.originalResolution.y
16895
+ };
16896
+ case 'meter':
16897
+ return {
16898
+ x: this.originalResolution.x / centimetersPerMeter,
16899
+ y: this.originalResolution.y / centimetersPerMeter
16900
+ };
16901
+ case 'unknown':
16902
+ return null;
16903
+ default:
16904
+ throw new Error('Unknown resolution unit.');
16905
+ }
16906
+ }
16869
16907
  /**
16870
16908
  * Create a new Image based on the properties of an existing one.
16871
16909
  * @param other - Reference image.
@@ -31540,10 +31578,23 @@ function decodePng(buffer) {
31540
31578
  default:
31541
31579
  throw new RangeError(`invalid number of channels: ${png.channels}`);
31542
31580
  }
31581
+ let resolution;
31582
+ if (png.resolution) {
31583
+ resolution = png.resolution.unit === 1 ? /*If the resolution unit is meters*/{
31584
+ x: png.resolution.x,
31585
+ y: png.resolution.y,
31586
+ unit: 'meter'
31587
+ } : /*If resolution unit is unknown */{
31588
+ x: png.resolution.x,
31589
+ y: png.resolution.y,
31590
+ unit: 'unknown'
31591
+ };
31592
+ }
31543
31593
  return new Image(png.width, png.height, {
31544
31594
  colorModel,
31545
31595
  bitDepth,
31546
- data: png.data
31596
+ data: png.data,
31597
+ resolution
31547
31598
  });
31548
31599
  }
31549
31600
  /**
@@ -31613,6 +31664,7 @@ function getImageFromIFD(ifd) {
31613
31664
  if (ifd.data instanceof Float32Array || ifd.data instanceof Float64Array) {
31614
31665
  throw new Error('Float TIFF data is not supported.');
31615
31666
  }
31667
+ const resolution = getTiffResolution(ifd);
31616
31668
  if (ifd.type === 3) {
31617
31669
  const hasAlpha = ifd.samplesPerPixel === 2;
31618
31670
  const pixelLength = hasAlpha ? 4 : 3;
@@ -31640,7 +31692,8 @@ function getImageFromIFD(ifd) {
31640
31692
  data,
31641
31693
  colorModel: hasAlpha ? 'RGBA' : 'RGB',
31642
31694
  bitDepth: 16,
31643
- meta: getMetadata(ifd)
31695
+ meta: getMetadata(ifd),
31696
+ resolution
31644
31697
  });
31645
31698
  } else if (ifd.type === 1 || ifd.type === 0) {
31646
31699
  if (ifd.bitsPerSample !== 1) {
@@ -31648,14 +31701,16 @@ function getImageFromIFD(ifd) {
31648
31701
  data: ifd.data,
31649
31702
  bitDepth: ifd.bitsPerSample,
31650
31703
  colorModel: ifd.alpha ? 'GREYA' : 'GREY',
31651
- meta: getMetadata(ifd)
31704
+ meta: getMetadata(ifd),
31705
+ resolution
31652
31706
  });
31653
31707
  } else {
31654
31708
  return new Image(ifd.width, ifd.height, {
31655
31709
  data: ifd.data.map(pixel => pixel * 255),
31656
31710
  bitDepth: 8,
31657
31711
  colorModel: 'GREY',
31658
- meta: getMetadata(ifd)
31712
+ meta: getMetadata(ifd),
31713
+ resolution
31659
31714
  });
31660
31715
  }
31661
31716
  } else {
@@ -31663,10 +31718,41 @@ function getImageFromIFD(ifd) {
31663
31718
  data: ifd.data,
31664
31719
  bitDepth: ifd.bitsPerSample,
31665
31720
  colorModel: ifd.alpha ? 'RGBA' : 'RGB',
31666
- meta: getMetadata(ifd)
31721
+ meta: getMetadata(ifd),
31722
+ resolution
31667
31723
  });
31668
31724
  }
31669
31725
  }
31726
+ /**
31727
+ * Gets image resolution from its metadata and converts it into Pixels per meter, when it's possible. Also keeps original resolution values and units.
31728
+ * @param ifd - Tiff metadata.
31729
+ * @returns Resolution object.
31730
+ */
31731
+ function getTiffResolution(ifd) {
31732
+ if (!ifd.xResolution || !ifd.yResolution) {
31733
+ return undefined;
31734
+ }
31735
+ switch (ifd.resolutionUnit) {
31736
+ case 1:
31737
+ return {
31738
+ x: ifd.xResolution,
31739
+ y: ifd.yResolution,
31740
+ unit: 'unknown'
31741
+ };
31742
+ case 3:
31743
+ return {
31744
+ x: ifd.xResolution,
31745
+ y: ifd.yResolution,
31746
+ unit: 'centimeter'
31747
+ };
31748
+ default:
31749
+ return {
31750
+ x: ifd.xResolution,
31751
+ y: ifd.yResolution,
31752
+ unit: 'inch'
31753
+ };
31754
+ }
31755
+ }
31670
31756
 
31671
31757
  /**
31672
31758
  * Decode input data. Data format is automatically detected.