dataslice.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. class DataSlice {
  4. constructor(arrayBuffer, sliceOffset, littleEndian, bigTiff) {
  5. this._dataView = new DataView(arrayBuffer);
  6. this._sliceOffset = sliceOffset;
  7. this._littleEndian = littleEndian;
  8. this._bigTiff = bigTiff;
  9. }
  10. get sliceOffset() {
  11. return this._sliceOffset;
  12. }
  13. get sliceTop() {
  14. return this._sliceOffset + this.buffer.byteLength;
  15. }
  16. get littleEndian() {
  17. return this._littleEndian;
  18. }
  19. get bigTiff() {
  20. return this._bigTiff;
  21. }
  22. get buffer() {
  23. return this._dataView.buffer;
  24. }
  25. covers(offset, length) {
  26. return this.sliceOffset <= offset && this.sliceTop >= offset + length;
  27. }
  28. readUint8(offset) {
  29. return this._dataView.getUint8(offset - this._sliceOffset, this._littleEndian);
  30. }
  31. readInt8(offset) {
  32. return this._dataView.getInt8(offset - this._sliceOffset, this._littleEndian);
  33. }
  34. readUint16(offset) {
  35. return this._dataView.getUint16(offset - this._sliceOffset, this._littleEndian);
  36. }
  37. readInt16(offset) {
  38. return this._dataView.getInt16(offset - this._sliceOffset, this._littleEndian);
  39. }
  40. readUint32(offset) {
  41. return this._dataView.getUint32(offset - this._sliceOffset, this._littleEndian);
  42. }
  43. readInt32(offset) {
  44. return this._dataView.getInt32(offset - this._sliceOffset, this._littleEndian);
  45. }
  46. readFloat32(offset) {
  47. return this._dataView.getFloat32(offset - this._sliceOffset, this._littleEndian);
  48. }
  49. readFloat64(offset) {
  50. return this._dataView.getFloat64(offset - this._sliceOffset, this._littleEndian);
  51. }
  52. readUint64(offset) {
  53. const left = this.readUint32(offset);
  54. const right = this.readUint32(offset + 4);
  55. let combined;
  56. if (this._littleEndian) {
  57. combined = left + ((2 ** 32) * right);
  58. if (!Number.isSafeInteger(combined)) {
  59. throw new Error(`${combined} exceeds MAX_SAFE_INTEGER. `
  60. + 'Precision may be lost. Please report if you get this message to https://github.com/geotiffjs/geotiff.js/issues');
  61. }
  62. return combined;
  63. }
  64. combined = ((2 ** 32) * left) + right;
  65. if (!Number.isSafeInteger(combined)) {
  66. throw new Error(`${combined} exceeds MAX_SAFE_INTEGER. `
  67. + 'Precision may be lost. Please report if you get this message to https://github.com/geotiffjs/geotiff.js/issues');
  68. }
  69. return combined;
  70. }
  71. // adapted from https://stackoverflow.com/a/55338384/8060591
  72. readInt64(offset) {
  73. let value = 0;
  74. const isNegative = (this._dataView.getUint8(offset + (this._littleEndian ? 7 : 0)) & 0x80)
  75. > 0;
  76. let carrying = true;
  77. for (let i = 0; i < 8; i++) {
  78. let byte = this._dataView.getUint8(offset + (this._littleEndian ? i : 7 - i));
  79. if (isNegative) {
  80. if (carrying) {
  81. if (byte !== 0x00) {
  82. byte = ~(byte - 1) & 0xff;
  83. carrying = false;
  84. }
  85. }
  86. else {
  87. byte = ~byte & 0xff;
  88. }
  89. }
  90. value += byte * (256 ** i);
  91. }
  92. if (isNegative) {
  93. value = -value;
  94. }
  95. return value;
  96. }
  97. readOffset(offset) {
  98. if (this._bigTiff) {
  99. return this.readUint64(offset);
  100. }
  101. return this.readUint32(offset);
  102. }
  103. }
  104. exports.default = DataSlice;
  105. //# sourceMappingURL=dataslice.js.map