dataslice.js 3.3 KB

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