Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature write float32array #106

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/geotiffwriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


import { fieldTagNames, fieldTagTypes, fieldTypeNames, geoKeyNames } from './globals';
import { assign, endsWith, forEach, invert, times } from './utils';
import { assign, endsWith, forEach, invert, times, isTypedFloatArray, isTypedUintArray, isTypedIntArray } from './utils';


const tagName2Code = invert(fieldTagNames);
Expand Down Expand Up @@ -252,11 +252,17 @@ const encodeImage = (values, width, height, metadata) => {

const prfx = new Uint8Array(encodeIfds([ifd]));

const img = new Uint8Array(values);
// This should allow other kinds of typed arrays to be used. Uint8 is just a view of the real underlying data.
let img = undefined
if (ArrayBuffer.isView(values)) { // It is a typed array
img = new Uint8Array(values.buffer);
} else { // It is a normal js array
img = new Uint8Array(values)
}

const samplesPerPixel = ifd[277];

const data = new Uint8Array(numBytesInIfd + (width * height * samplesPerPixel));
const data = new Uint8Array(numBytesInIfd + (img.length * samplesPerPixel));
m0ose marked this conversation as resolved.
Show resolved Hide resolved
times(prfx.length, (i) => { data[i] = prfx[i]; });
forEach(img, (value, i) => {
data[numBytesInIfd + i] = value;
Expand Down Expand Up @@ -333,9 +339,12 @@ const writeGeotiff = (data, metadata) => {
delete metadata.width;

// consult https://www.loc.gov/preservation/digital/formats/content/tiff_tags.shtml

if (!metadata.BitsPerSample) {
metadata.BitsPerSample = times(numBands, () => 8);
let bitsPerSample = 8
if (ArrayBuffer.isView(flattenedValues)) {
bitsPerSample = 8 * flattenedValues.BYTES_PER_ELEMENT
}
metadata.BitsPerSample = times(numBands, () => bitsPerSample);
}

metadataDefaults.forEach((tag) => {
Expand Down Expand Up @@ -368,7 +377,17 @@ const writeGeotiff = (data, metadata) => {
}

if (!metadata.SampleFormat) {
metadata.SampleFormat = times(numBands, () => 1);
let sampleFormat = 1
if (isTypedFloatArray(flattenedValues)) {
sampleFormat = 3
}
if (isTypedIntArray(flattenedValues)) {
sampleFormat = 2
}
if (isTypedUintArray(flattenedValues)) {
sampleFormat = 1
}
metadata.SampleFormat = times(numBands, () => sampleFormat);
}


Expand Down
35 changes: 35 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,38 @@ export function toArrayRecursively(input) {
}
return input;
}

export function isTypedFloatArray(input) {
if (ArrayBuffer.isView(input)) {
const ctr = input.constructor
if (ctr === Float32Array || ctr === Float64Array) {
return true
}
}
return false
}

export function isTypedIntArray(input) {
if (ArrayBuffer.isView(input)) {
const ctr = input.constructor
if (ctr == Int8Array || ctr === Int16Array || ctr === Int32Array) {
return true
}
}
return false
}

export function isTypedUintArray(input) {
if (ArrayBuffer.isView(input)) {
const ctr = input.constructor
if (ctr == Uint8Array || ctr === Uint16Array || ctr === Uint32Array || ctr === Uint8ClampedArray) {
return true
}
}
return false
}





2 changes: 1 addition & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<select id="bands">
</select>

<script src="geotiff.bundle.js"></script>
<script src="../dist/geotiff.bundle.js"></script>
<script src="lib/plotty.min.js"></script>

<script>
Expand Down