Design
Scope
Read, change and remove the EXIF metadata of JPEG, PNG and WebP images in pure Jennifer, on bytes: no subprocess, no network, fs only in the ...File helpers.
- Containers: JPEG (an APP1 segment starting
Exif\0\0), PNG (theeXIfchunk) and WebP (theEXIFchunk of the extended format, flagged inVP8X). The container is detected from the first bytes, never from a file name; anything else is an error. - The EXIF structure: the TIFF header (
II/MM, 42), IFD0, the Exif IFD (pointer tag0x8769), the GPS IFD (0x8825), the Interoperability IFD (0xA005, inside the Exif IFD) and IFD1 with its JPEG thumbnail. All twelve TIFF field types are read and written.
Not in scope:
- TIFF-based raw files (DNG, CR2, NEF, ...) and HEIC / AVIF. In a raw file the EXIF structure is the image container, with strip and tile offsets pointing at pixel data; rewriting it is a different problem. HEIC keeps EXIF in an ISO-BMFF item whose location table would have to be rewritten.
- XMP and IPTC. They live in their own segments and chunks (JPEG APP1 with the XMP namespace, APP13; PNG
iTXt; WebPXMP) and are left as they are, also bystrip. A privacy tool must remove them separately. - Decoding MakerNotes. Vendor data is opaque.
- Multi-segment EXIF (the FlashPix extension over several APP2 segments) and EXIF in PNG text chunks (
zTXt"Raw profile type exif", ImageMagick's older form): not read, not written.
Decisions
- Bytes in, bytes out.
read(data)andwrite(data, exif)work on the whole image;parseandrenderon the bare TIFF payload. The image is passed read-only through every helper, so it is never copied while it is parsed; only the values themselves are sliced out. - Replace only the EXIF segment.
writere-serialises the complete TIFF structure and swaps it in for the old segment or chunk; every other byte of the file is copied unchanged, pixel data included. Only what the container makes depend on it changes with it: the PNG chunk CRC, the RIFF size and theVP8XEXIF flag. A new segment goes where readers look first: in JPEG afterSOIand anyAPP0(JFIF), in PNG afterIHDR, in WebP beforeXMPor at the end. - A simple-format WebP is extended on write. A
VP8orVP8Lfile has no room for metadata, sowriteputs aVP8Xchunk in front, with the canvas size read from the bitstream header and the alpha flag from theVP8Lheader.stripleaves aVP8Xfile extended, with the flag cleared. - The model is a flat list of entries.
Exifholds the byte order, everyEntry(IFD name, tag, field type, count, value) and the thumbnail bytes. The structural tags (the three IFD pointers and the thumbnail's offset and length) are not entries: the writer recomputes them, so a caller can neither forget nor corrupt them. Unknown tags stay as entries and are written back unchanged. - One
Valueenum for twelve field types.IntsholdsBYTE,SHORT,LONG,SBYTE,SSHORTandSLONG;AsciiandUndefinedhold raw bytes;Rationalsboth rational types;FloatsFLOATandDOUBLE. The entry'stypesays which exactly, and the writer checks the value fits it (range, variant). ASCII stays bytes because cameras write Latin-1 and UTF-8 into fields the standard calls 7-bit;textdecodes UTF-8 on demand and refuses anything else, so one odd field does not make the whole image unreadable. - Tags are named by a string.
"Make","GPSLatitude","ifd1:XResolution", or"exif:0xA432"/"gps:5"for any tag by number. A bare name finds its IFD in the table (TIFF tags default to IFD0); theifd:prefix picks IFD1 or a tag outside the table. Numbers are not unique across IFDs (GPS tag 1 isGPSLatitudeRef, Interop tag 1InteroperabilityIndex), so a number always needs its IFD. - The table knows each tag's type, so
setText,setInts,setRationalsandsetRawpick it (SHORTorLONGby value where the standard allows both). A tag outside the table is set throughmakeEntrywith an explicit type. - The writer's layout: header, then per IFD (0, Exif, Interop, GPS, 1) the entry table sorted by tag and its out-of-line values, each padded to an even length so every IFD and value starts on a word boundary, the thumbnail last. An IFD is written only when it has entries (the Exif IFD also when only the Interop IFD has).
- One canonical entry order.
readlists entries by IFD (0, Exif, Interop, GPS, 1) and tag, andsetinserts there, soread(write(img, e)) == efor anyebuilt with the setters. - MakerNote is kept byte for byte but may move. Many vendors store offsets in it relative to the TIFF header (Canon, older Olympus, Sony); after a rewrite those point at the wrong bytes, and vendor tools can no longer read the MakerNote. Keeping it at its old offset would constrain the whole layout around one opaque block, for a guarantee the writer still could not give (the block can overlap anything). The deck keeps the data and documents the risk;
remove($e, "MakerNote")drops it. - Uncompressed thumbnails are read, not rewritten.
StripOffsetsandTileOffsetspoint at data the writer would not carry over;writerefuses anExifthat holds one, andremoveThumbnailclears IFD1. - Privacy helpers:
removeGps(the GPS IFD) andremoveThumbnail(IFD1 and its JPEG; the preview keeps the original picture after a crop). Both are one call because both are what people strip by hand. Serial numbers, owner names and MakerNotes are singleremovecalls;stripremoves everything. - Readers for the common questions:
orientation,gps(decimal degrees and altitude),gpsTime(UTC by definition),dateTime(theOffsetTime*tag when the image has one, else the zone the caller passes: the deck never guesses a camera's zone),setGps. - Integer writing in one place.
binaryhas readers but no writers yet (roadmap M29.54);uintBytesincore.jshifts and masks, and is the one function to switch to the builtin. Floats are encoded byfloatBits(IEEE 754, round half to even) for the same reason. - Files are replaced atomically.
writeFileandstripFilewrite a temporary file beside the target and rename it over, so a crash never leaves half an image. A symlinked target becomes a regular file, and the file gets the default mode of a new file rather than the old one's. - No EXIF is an empty
Exif, not an error.readof an image without EXIF returnsempty("be"), ready for the setters andwrite;hasExiftells the two cases apart.
Errors
Malformed data throws ExifError (kind "exif", message starting exif: , offset the byte offset in the input where the problem is, -1 when there is none). Every throw of kind "exif" is that struct. Misuse (an unknown tag name, a value that does not fit its type, setting a structural tag) is also kind "exif", with offset -1.
A reached cap is a plain Error with kind "limit". fs errors from the file helpers pass through unchanged.
What is refused, rather than guessed:
- an unknown container, a truncated segment, chunk or IFD, a bad TIFF header, an offset or value past the end of the payload;
- an IFD offset seen before (a loop), a third IFD in the main chain, a sub-IFD with a next-IFD offset, a pointer tag in the wrong IFD;
- a field type outside 1-12, a tag twice in one IFD;
- two EXIF segments or chunks in one image, a PNG
eXIfwith a wrong CRC, a WebPEXIFchunk without theVP8Xflag (or the flag without a chunk), anEXIFchunk in a simple-format WebP, a RIFF size that does not match the file; - a payload too large for its container (a JPEG APP1 segment holds at most 65,527 bytes of EXIF).
Not refused: entries out of tag order (TIFF says they must be sorted; the order carries no meaning and the writer sorts), ASCII without a terminating NUL, bytes after a PNG's IEND (copied unchanged).
Security and limits
| Cap | Default (Options field) | Reached |
|---|---|---|
| EXIF payload size | 16 MiB (maxBytes) | "limit" |
| entries in one IFD | 1,000 (maxEntries) | "limit" |
bytes of one value (count x type size), and the thumbnail | 1 MiB (maxValueBytes) | "limit" |
| bytes all values decode to | the payload cap (maxBytes) | "limit" |
| numbers all values decode to (integers, rational parts, floats) | 65,536, fixed | "limit" |
readWith and parseWith take Options; a zero field keeps the default, a negative one or one above the default is an "exif" error (the caps only go down). The IFD structure itself is bounded: at most five IFDs (IFD0, IFD1, Exif, GPS, Interop), nesting at most two deep, and every IFD offset is checked against those already read, so a crafted loop is an error, not a hang. The value cap is checked before the value is bounds-checked, so a huge count is "limit" even when it also runs past the end.
The two totals bound memory where the payload cap does not: values may all point at the same bytes, so a 1 MiB payload could otherwise decode to gigabytes, and a decoded number costs hundreds of bytes (a 1 MiB BYTE value took 2 GB). Real images keep their numeric values small; long data is UNDEFINED. render, and with it write, refuses metadata over any default cap with "limit", so the deck never writes what it cannot read.
Testing and validation
- Fixtures are built in memory: JPEG and PNG from
images.encode, WebP from two small base64 files made by ImageMagick (src/topics/fixtures/), EXIF payloads from hand-assembled IFDs in both byte orders. There is notestdata/, so the overlays run from a copy ofsrc/alone. - Covered: both byte orders, all twelve types inline and out of line, all five IFDs, the thumbnail, round trips (read, write, read equal), set and remove, strip, PNG CRCs, the WebP flag and simple-to-extended conversion, and every refusal above.
- Cross-checked by hand with ImageMagick 7.1.2 (
identify -format '%[EXIF:*]'): anExifwith tags in all five IFDs, a thumbnail and every field type, written into JPEG, PNG, lossy and lossless WebP in both byte orders, reads the same in all eight files and as written; the stripped files show no EXIF and the same dimensions. ImageMagick printsFLOATandDOUBLEas the integer of their first four bytes and repeats the first value of a multi-value rational outside the GPS IFD; the bytes were checked against Python'sstructinstead. ImageMagick is not available in CI, so this is not part of the gate.
Known limitations and open questions
- MakerNote offsets (above). A future option could keep the MakerNote at its old offset when the layout allows it.
- A JPEG whose EXIF needs more than one APP1 segment cannot be written.
stripremoves EXIF only, not XMP, IPTC or ICC profiles.- Text is written as UTF-8 although the standard asks for 7-bit ASCII; readers that insist on ASCII show the bytes.
- PNG
eXIfis placed afterIHDR; PNG editors that only look beforeIDATfind it, ones that expect it elsewhere were not seen.
Dependencies on the interpreter
Floor >=0.27.0: binary.readUint / readInts / readFloats / indexOf, crc, thrown structs keeping their identity. Runs on the default binary; jennifer-tiny is not declared.