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
  *
@@ -16818,6 +16818,10 @@ ${indent}columns: ${matrix.columns}
16818
16818
  * Origin of the image relative to a the parent image.
16819
16819
  */
16820
16820
  origin;
16821
+ /**
16822
+ * Original image resolution.
16823
+ */
16824
+ originalResolution;
16821
16825
  meta;
16822
16826
  /**
16823
16827
  * Typed array holding the image data.
@@ -16838,7 +16842,8 @@ ${indent}columns: ${matrix.columns}
16838
16842
  row: 0,
16839
16843
  column: 0
16840
16844
  },
16841
- meta
16845
+ meta,
16846
+ resolution
16842
16847
  } = options;
16843
16848
  if (width < 1 || !Number.isInteger(width)) {
16844
16849
  throw new RangeError(`width must be an integer and at least 1. Received ${width}`);
@@ -16853,6 +16858,7 @@ ${indent}columns: ${matrix.columns}
16853
16858
  this.colorModel = colorModel;
16854
16859
  this.origin = origin;
16855
16860
  this.meta = meta;
16861
+ this.originalResolution = resolution;
16856
16862
  const colorModelDef = colorModels[colorModel];
16857
16863
  this.components = colorModelDef.components;
16858
16864
  this.alpha = colorModelDef.alpha;
@@ -16873,6 +16879,38 @@ ${indent}columns: ${matrix.columns}
16873
16879
  this.data = data;
16874
16880
  }
16875
16881
  }
16882
+ /**
16883
+ * Returns normalized resolution in pixels per centimeter. If resolution unit is unknown, return null.
16884
+ * @returns Object with x and y resolutions in pixel/cm.
16885
+ */
16886
+ get normalizedResolution() {
16887
+ if (!this.originalResolution) {
16888
+ return undefined;
16889
+ }
16890
+ const centimetersPerInch = 2.54;
16891
+ const centimetersPerMeter = 100;
16892
+ switch (this.originalResolution.unit) {
16893
+ case 'inch':
16894
+ return {
16895
+ x: this.originalResolution.x / centimetersPerInch,
16896
+ y: this.originalResolution.y / centimetersPerInch
16897
+ };
16898
+ case 'centimeter':
16899
+ return {
16900
+ x: this.originalResolution.x,
16901
+ y: this.originalResolution.y
16902
+ };
16903
+ case 'meter':
16904
+ return {
16905
+ x: this.originalResolution.x / centimetersPerMeter,
16906
+ y: this.originalResolution.y / centimetersPerMeter
16907
+ };
16908
+ case 'unknown':
16909
+ return null;
16910
+ default:
16911
+ throw new Error('Unknown resolution unit.');
16912
+ }
16913
+ }
16876
16914
  /**
16877
16915
  * Create a new Image based on the properties of an existing one.
16878
16916
  * @param other - Reference image.
@@ -31547,10 +31585,23 @@ ${indent}columns: ${matrix.columns}
31547
31585
  default:
31548
31586
  throw new RangeError(`invalid number of channels: ${png.channels}`);
31549
31587
  }
31588
+ let resolution;
31589
+ if (png.resolution) {
31590
+ resolution = png.resolution.unit === 1 ? /*If the resolution unit is meters*/{
31591
+ x: png.resolution.x,
31592
+ y: png.resolution.y,
31593
+ unit: 'meter'
31594
+ } : /*If resolution unit is unknown */{
31595
+ x: png.resolution.x,
31596
+ y: png.resolution.y,
31597
+ unit: 'unknown'
31598
+ };
31599
+ }
31550
31600
  return new Image(png.width, png.height, {
31551
31601
  colorModel,
31552
31602
  bitDepth,
31553
- data: png.data
31603
+ data: png.data,
31604
+ resolution
31554
31605
  });
31555
31606
  }
31556
31607
  /**
@@ -31620,6 +31671,7 @@ ${indent}columns: ${matrix.columns}
31620
31671
  if (ifd.data instanceof Float32Array || ifd.data instanceof Float64Array) {
31621
31672
  throw new Error('Float TIFF data is not supported.');
31622
31673
  }
31674
+ const resolution = getTiffResolution(ifd);
31623
31675
  if (ifd.type === 3) {
31624
31676
  const hasAlpha = ifd.samplesPerPixel === 2;
31625
31677
  const pixelLength = hasAlpha ? 4 : 3;
@@ -31647,7 +31699,8 @@ ${indent}columns: ${matrix.columns}
31647
31699
  data,
31648
31700
  colorModel: hasAlpha ? 'RGBA' : 'RGB',
31649
31701
  bitDepth: 16,
31650
- meta: getMetadata(ifd)
31702
+ meta: getMetadata(ifd),
31703
+ resolution
31651
31704
  });
31652
31705
  } else if (ifd.type === 1 || ifd.type === 0) {
31653
31706
  if (ifd.bitsPerSample !== 1) {
@@ -31655,14 +31708,16 @@ ${indent}columns: ${matrix.columns}
31655
31708
  data: ifd.data,
31656
31709
  bitDepth: ifd.bitsPerSample,
31657
31710
  colorModel: ifd.alpha ? 'GREYA' : 'GREY',
31658
- meta: getMetadata(ifd)
31711
+ meta: getMetadata(ifd),
31712
+ resolution
31659
31713
  });
31660
31714
  } else {
31661
31715
  return new Image(ifd.width, ifd.height, {
31662
31716
  data: ifd.data.map(pixel => pixel * 255),
31663
31717
  bitDepth: 8,
31664
31718
  colorModel: 'GREY',
31665
- meta: getMetadata(ifd)
31719
+ meta: getMetadata(ifd),
31720
+ resolution
31666
31721
  });
31667
31722
  }
31668
31723
  } else {
@@ -31670,10 +31725,41 @@ ${indent}columns: ${matrix.columns}
31670
31725
  data: ifd.data,
31671
31726
  bitDepth: ifd.bitsPerSample,
31672
31727
  colorModel: ifd.alpha ? 'RGBA' : 'RGB',
31673
- meta: getMetadata(ifd)
31728
+ meta: getMetadata(ifd),
31729
+ resolution
31674
31730
  });
31675
31731
  }
31676
31732
  }
31733
+ /**
31734
+ * Gets image resolution from its metadata and converts it into Pixels per meter, when it's possible. Also keeps original resolution values and units.
31735
+ * @param ifd - Tiff metadata.
31736
+ * @returns Resolution object.
31737
+ */
31738
+ function getTiffResolution(ifd) {
31739
+ if (!ifd.xResolution || !ifd.yResolution) {
31740
+ return undefined;
31741
+ }
31742
+ switch (ifd.resolutionUnit) {
31743
+ case 1:
31744
+ return {
31745
+ x: ifd.xResolution,
31746
+ y: ifd.yResolution,
31747
+ unit: 'unknown'
31748
+ };
31749
+ case 3:
31750
+ return {
31751
+ x: ifd.xResolution,
31752
+ y: ifd.yResolution,
31753
+ unit: 'centimeter'
31754
+ };
31755
+ default:
31756
+ return {
31757
+ x: ifd.xResolution,
31758
+ y: ifd.yResolution,
31759
+ unit: 'inch'
31760
+ };
31761
+ }
31762
+ }
31677
31763
 
31678
31764
  /**
31679
31765
  * Decode input data. Data format is automatically detected.