Skip to content

Commit

Permalink
feat: add all tif metadata tags
Browse files Browse the repository at this point in the history
* feat: add all tif metadata tags

* feat: display parsed tag values at the first level

* feat: add try/catch block for xml parsing

* chore: update react-science

* fix: fix try/catch block

* chore: remove duplicated meta tags
  • Loading branch information
moonayyur authored May 23, 2024
1 parent 0ac8a15 commit 53d187c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
"react-plot": "^1.4.2",
"react-roi": "^1.4.0",
"react-science": "^3.1.0",
"react-use": "^17.4.0"
"react-use": "^17.4.0",
"tiff": "^6.1.0"
},
"volta": {
"node": "18.16.0",
Expand Down
38 changes: 37 additions & 1 deletion src/hooks/useImageInformations.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { Image } from 'image-js';
import { useMemo } from 'react';
import { tagNames } from 'tiff';

function getTagName(tagCode: string): string {
return (
tagNames.standard[tagCode] ||
tagNames.exif[tagCode] ||
tagNames.gps[tagCode] ||
tagCode
);
}

export default function useImageInformations(image: Image | null) {
return useMemo(() => {
Expand All @@ -13,7 +23,33 @@ export default function useImageInformations(image: Image | null) {
components: image.components,
colorModel: image.colorModel,
};
const meta = image.meta?.tiff.tags || {};

const fields = Object.fromEntries(image.meta?.tiff?.fields || []);
let meta: Record<string, unknown> = {};

for (const [tagCode, tagValue] of Object.entries(fields)) {
const tagName = getTagName(tagCode);
if (typeof tagValue === 'string' && tagValue.startsWith('<')) {
try {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(tagValue, 'text/xml');
const dataNodes = xmlDoc.querySelectorAll('Data');
for (const dataNode of dataNodes) {
const label = dataNode.querySelector('Label')?.textContent;
const value = dataNode.querySelector('Value')?.textContent;
if (label && value) {
const newTagName = tagName.concat('.').concat(label);
meta[newTagName] = value;
}
}
} catch {
meta[tagName] = tagValue;
}
} else {
meta[tagName] = tagValue;
}
}

return { info, meta };
}, [image]);
}

0 comments on commit 53d187c

Please sign in to comment.