geotiffjs / geotiff.js

geotiff.js is a small library to parse TIFF files for visualization or analysis. It is written in pure JavaScript, and is usable in both the browser and node.js applications.
https://geotiffjs.github.io/
MIT License
878 stars 183 forks source link

Rational arrays not read correctly #437

Open sam6321 opened 1 month ago

sam6321 commented 1 month ago

https://github.com/geotiffjs/geotiff.js/blob/a2013a3790a657badade613169c9eaa1dc550a0b/src/geotiff.js#L161

When reading arrays of rational numbers (for example, when reading a GPSLatitude or GPSLongitude field) the reader does not index the output Uint32Array correctly, resulting in it skipping values on output.

A GPSLatitude array containing [71, 1, 25, 1, 47289959, 1000000] is currently being read as [71, 1, 47289959, 1000000, 0, 0]

The reading loop:

for (let i = 0; i < count; i += 2) {
  values[i] = readMethod.call(
    dataSlice, offset + (i * fieldTypeLength),
  );
  values[i + 1] = readMethod.call(
    dataSlice, offset + ((i * fieldTypeLength) + 4),
  );
}

Should instead be:

for (let i = 0; i < count; i++) {
  values[i * 2] = readMethod.call(
    dataSlice, offset + (i * fieldTypeLength),
  );
  values[i * 2 + 1] = readMethod.call(
    dataSlice, offset + ((i * fieldTypeLength) + 4),
  );
}