exif() returns every EXIF tag in the file (closes #156)
check / check (push) Failing after 51s
check / check (push) Failing after 51s
`photo.exif()` returns `ExifTags`: every EXIF tag exifreader reads from the file, keyed by tag name, each with exifreader's `id`, `value`, `description` and `computed`. The embedded thumbnail's tags are under `Thumbnail`, without the thumbnail image. A file with no EXIF, or a video, gives `{}`.
The thirteen typed methods stay. Each picks its field from the tags `exif()` returns through `readPhotoExif`, which now takes the tags instead of the bytes. GPS latitude and longitude are worked out from their tags and reference tags, since exifreader's computed position is not among the tags.
Also closes #148.
Model: opus-5-5
This commit is contained in:
@@ -3,14 +3,18 @@
|
||||
* `quak backup-metadata --exif` records.
|
||||
*
|
||||
* The originals come from users' libraries, so a truncated or corrupt file
|
||||
* must neither hang the read nor throw out of it: `readPhotoExif` gives `{}`,
|
||||
* must neither hang the read nor throw out of it: `readAllExifTags` gives `{}`,
|
||||
* and `backup-metadata` tells an EXIF block it cannot read apart from a file
|
||||
* that simply has no EXIF, carrying the reason in `exifError`. Each JPEG below
|
||||
* is a short hand-built byte array; the HEIC is a real file.
|
||||
*/
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { readPhotoExif } from "../../src/exif.js";
|
||||
import {
|
||||
readAllExifTags,
|
||||
readExifTags,
|
||||
readPhotoExif,
|
||||
} from "../../src/exif.js";
|
||||
import { extractImageMetadata } from "../../src/metadata-backup.js";
|
||||
import { HEIC_WITH_EXIF } from "../exif-heic.js";
|
||||
|
||||
@@ -87,6 +91,27 @@ const TIFF_UNNAMED_TAG = [
|
||||
...[0x00, 0x00, 0x00, 0x00],
|
||||
];
|
||||
|
||||
// A big-endian TIFF block holding Orientation 6, and a thumbnail IFD holding
|
||||
// its own Orientation 1 and a 4-byte JPEG thumbnail.
|
||||
const TIFF_WITH_THUMBNAIL = [
|
||||
...[0x4d, 0x4d, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x08], // the first IFD at 8
|
||||
// The first IFD, at 8: one entry, then the thumbnail IFD at 26.
|
||||
...[0x00, 0x01],
|
||||
// Orientation (0x0112), SHORT, 6.
|
||||
...[0x01, 0x12, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00],
|
||||
...[0x00, 0x00, 0x00, 0x1a],
|
||||
// The thumbnail IFD, at 26: three entries, then no next IFD.
|
||||
...[0x00, 0x03],
|
||||
// Orientation (0x0112), SHORT, 1.
|
||||
...[0x01, 0x12, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00],
|
||||
// JPEGInterchangeFormat (0x0201), LONG: the thumbnail is at 68.
|
||||
...[0x02, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x44],
|
||||
// JPEGInterchangeFormatLength (0x0202), LONG: 4 bytes.
|
||||
...[0x02, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04],
|
||||
...[0x00, 0x00, 0x00, 0x00],
|
||||
...[0xff, 0xd8, 0xff, 0xd9], // the thumbnail, at 68: an empty JPEG
|
||||
];
|
||||
|
||||
// An APP1 segment whose length field matches its data.
|
||||
const app1 = (data: number[]): number[] => {
|
||||
const len = data.length + 2;
|
||||
@@ -96,31 +121,72 @@ const app1 = (data: number[]): number[] => {
|
||||
const bytes = (...parts: number[][]): Uint8Array =>
|
||||
new Uint8Array(parts.flat());
|
||||
|
||||
describe("readAllExifTags", () => {
|
||||
it("keys a tag exifreader has no name for by its number", () => {
|
||||
const data = [...EXIF_HEADER, ...TIFF_UNNAMED_TAG];
|
||||
expect(readAllExifTags(bytes(SOI, app1(data), SOS))).toStrictEqual({
|
||||
"undefined-49152": {
|
||||
id: 49152,
|
||||
value: 7,
|
||||
description: 7,
|
||||
computed: 7,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("puts the thumbnail's tags under Thumbnail, without its image", () => {
|
||||
const input = bytes(
|
||||
SOI,
|
||||
app1([...EXIF_HEADER, ...TIFF_WITH_THUMBNAIL]),
|
||||
SOS,
|
||||
);
|
||||
// exifreader finds the thumbnail's image.
|
||||
expect(readExifTags(input)?.Thumbnail?.type).toBe("image/jpeg");
|
||||
const tags = readAllExifTags(input);
|
||||
expect(tags.Orientation?.value).toBe(6);
|
||||
expect(Object.keys(tags.Thumbnail ?? {}).sort()).toEqual([
|
||||
"JPEGInterchangeFormat",
|
||||
"JPEGInterchangeFormatLength",
|
||||
"Orientation",
|
||||
]);
|
||||
expect(tags.Thumbnail?.Orientation?.value).toBe(1);
|
||||
expect(readPhotoExif(tags)).toStrictEqual({ orientation: 6 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("readPhotoExif", () => {
|
||||
it("reads the common fields of a valid JPEG", () => {
|
||||
const data = [...EXIF_HEADER, ...TIFF_ORIENTATION_6];
|
||||
expect(readPhotoExif(bytes(SOI, app1(data), SOS))).toStrictEqual({
|
||||
expect(
|
||||
readPhotoExif(readAllExifTags(bytes(SOI, app1(data), SOS))),
|
||||
).toStrictEqual({
|
||||
orientation: 6,
|
||||
});
|
||||
});
|
||||
|
||||
it("gives no dateTimeOriginal for a DateTimeOriginal of 0000:00:00 00:00:00", () => {
|
||||
const data = [...EXIF_HEADER, ...TIFF_UNSET_DATE];
|
||||
expect(readPhotoExif(bytes(SOI, app1(data), SOS))).toStrictEqual({
|
||||
expect(
|
||||
readPhotoExif(readAllExifTags(bytes(SOI, app1(data), SOS))),
|
||||
).toStrictEqual({
|
||||
orientation: 6,
|
||||
});
|
||||
});
|
||||
|
||||
it("reads a GPSAltitude without GPSAltitudeRef as above sea level", () => {
|
||||
const data = [...EXIF_HEADER, ...TIFF_ALTITUDE_WITHOUT_REF];
|
||||
expect(readPhotoExif(bytes(SOI, app1(data), SOS))).toStrictEqual({
|
||||
expect(
|
||||
readPhotoExif(readAllExifTags(bytes(SOI, app1(data), SOS))),
|
||||
).toStrictEqual({
|
||||
gpsAltitude: 12.5,
|
||||
});
|
||||
});
|
||||
|
||||
it("gives no make for a Make whose value lies past the end of the file", () => {
|
||||
const data = [...EXIF_HEADER, ...TIFF_MAKE_PAST_END];
|
||||
expect(readPhotoExif(bytes(SOI, app1(data), SOS))).toStrictEqual({
|
||||
expect(
|
||||
readPhotoExif(readAllExifTags(bytes(SOI, app1(data), SOS))),
|
||||
).toStrictEqual({
|
||||
orientation: 6,
|
||||
});
|
||||
});
|
||||
@@ -175,8 +241,9 @@ describe("readPhotoExif", () => {
|
||||
"an EXIF block that cannot be parsed",
|
||||
bytes(SOI, app1([...EXIF_HEADER, 0x58, 0x58]), SOS),
|
||||
],
|
||||
])("returns no fields for %s", (_, input) => {
|
||||
expect(readPhotoExif(input)).toStrictEqual({});
|
||||
])("returns no tags and no fields for %s", (_, input) => {
|
||||
expect(readAllExifTags(input)).toStrictEqual({});
|
||||
expect(readPhotoExif(readAllExifTags(input))).toStrictEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user