pngexif: Import pngexifinfo as an externally-contributed project

We used this experimental project in the development of the PNG-EXIF
("eXIf") specification, back in 2017. The project evolved together
with the draft specification, which was finalized on 2017-Jun-15 and
approved by the PNG Group on 2017-Jul-13.

The EXIF specification, outside of the scope of PNG and libpng, is
quite complex. The libpng implementation cannot grow too much beyond
performing basic integrity checks on top of serialization. In order
to create and manipulate PNG-EXIF image files, the use of external
libraries and tools such as ExifTool is necessary.

Now, with the addition of contrib/pngexif to the libpng repository,
offline tasks like metadata inspection and linting can be performed
without importing external dependencies.
libpng16
Cosmin Truta 2024-02-22 11:32:53 +02:00
parent 7b888092b9
commit ec2e58c16a
10 changed files with 607 additions and 0 deletions

View File

@ -0,0 +1,11 @@
# https://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
insert_final_newline = true
max_doc_length = 79
max_line_length = 79
trim_trailing_whitespace = true

3
contrib/pngexif/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
__pycache__
*.py[co]
*$py.class

View File

@ -0,0 +1,8 @@
[COMPATIBILITY]
disable=consider-using-f-string
[COMPLEXITY]
disable=too-many-branches,too-many-instance-attributes
[STYLE]
disable=consider-using-in

View File

@ -0,0 +1,19 @@
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

20
contrib/pngexif/README.md Normal file
View File

@ -0,0 +1,20 @@
pngexifinfo
===========
Show the EXIF information embedded in a PNG file.
Sample usage
------------
Show the EXIF info inside a PNG file:
pngexifinfo /path/to/file.png
Show the EXIF info inside a raw `.exif` file, using base 16 for the EXIF tags:
pngexifinfo --hex /path/to/file.exif
Show the help text:
pngexifinfo --help

48
contrib/pngexif/bytepack.py Executable file
View File

@ -0,0 +1,48 @@
#!/usr/bin/env python
"""
Byte packing and unpacking utilities.
Copyright (C) 2017-2020 Cosmin Truta.
Use, modification and distribution are subject to the MIT License.
Please see the accompanying file LICENSE_MIT.txt
"""
from __future__ import absolute_import, division, print_function
import struct
def unpack_uint32be(buffer, offset=0):
"""Unpack an unsigned int from its 32-bit big-endian representation."""
return struct.unpack(">I", buffer[offset:offset + 4])[0]
def unpack_uint32le(buffer, offset=0):
"""Unpack an unsigned int from its 32-bit little-endian representation."""
return struct.unpack("<I", buffer[offset:offset + 4])[0]
def unpack_uint16be(buffer, offset=0):
"""Unpack an unsigned int from its 16-bit big-endian representation."""
return struct.unpack(">H", buffer[offset:offset + 2])[0]
def unpack_uint16le(buffer, offset=0):
"""Unpack an unsigned int from its 16-bit little-endian representation."""
return struct.unpack("<H", buffer[offset:offset + 2])[0]
def unpack_uint8(buffer, offset=0):
"""Unpack an unsigned int from its 8-bit representation."""
return struct.unpack("B", buffer[offset:offset + 1])[0]
if __name__ == "__main__":
# For testing only.
assert unpack_uint32be(b"ABCDEF", 1) == 0x42434445
assert unpack_uint32le(b"ABCDEF", 1) == 0x45444342
assert unpack_uint16be(b"ABCDEF", 1) == 0x4243
assert unpack_uint16le(b"ABCDEF", 1) == 0x4342
assert unpack_uint8(b"ABCDEF", 1) == 0x42

306
contrib/pngexif/exifinfo.py Executable file
View File

@ -0,0 +1,306 @@
#!/usr/bin/env python
"""
Show the EXIF information.
Copyright (C) 2017-2020 Cosmin Truta.
Use, modification and distribution are subject to the MIT License.
Please see the accompanying file LICENSE_MIT.txt
"""
from __future__ import absolute_import, division, print_function
import sys
from bytepack import (unpack_uint32be,
unpack_uint32le,
unpack_uint16be,
unpack_uint16le,
unpack_uint8)
# Generously allow the TIFF file to occupy up to a quarter-gigabyte.
# TODO: Reduce this limit to 64K and use file seeking for anything larger.
_READ_DATA_SIZE_MAX = 256 * 1024 * 1024
_TIFF_TAG_TYPES = {
1: "byte",
2: "ascii",
3: "short",
4: "long",
5: "rational",
6: "sbyte",
7: "undefined",
8: "sshort",
9: "slong",
10: "srational",
11: "float",
12: "double",
}
# See http://www.digitalpreservation.gov/formats/content/tiff_tags.shtml
_TIFF_TAGS = {
0x00fe: "Subfile Type",
0x0100: "Width",
0x0101: "Height",
0x0102: "Bits per Sample",
0x0103: "Compression",
0x0106: "Photometric",
0x010d: "Document Name",
0x010e: "Image Description",
0x010f: "Make",
0x0110: "Model",
0x0111: "Strip Offsets",
0x0112: "Orientation",
0x0115: "Samples per Pixel",
0x0116: "Rows per Strip",
0x0117: "Strip Byte Counts",
0x0118: "Min Sample Value",
0x0119: "Max Sample Value",
0x011a: "X Resolution",
0x011b: "Y Resolution",
0x011c: "Planar Configuration",
0x011d: "Page Name",
0x011e: "X Position",
0x011f: "Y Position",
0x0128: "Resolution Unit",
0x0129: "Page Number",
0x0131: "Software",
0x0132: "Date Time",
0x013b: "Artist",
0x013c: "Host Computer",
0x013d: "Predictor",
0x013e: "White Point",
0x013f: "Primary Chromaticities",
0x0140: "Color Map",
0x0141: "Half-Tone Hints",
0x0142: "Tile Width",
0x0143: "Tile Length",
0x0144: "Tile Offsets",
0x0145: "Tile Byte Counts",
0x0211: "YCbCr Coefficients",
0x0212: "YCbCr Subsampling",
0x0213: "YCbCr Positioning",
0x0214: "Reference Black White",
0x022f: "Strip Row Counts",
0x02bc: "XMP",
0x8298: "Copyright",
0x83bb: "IPTC",
0x8769: "EXIF IFD",
0x8773: "ICC Profile",
0x8825: "GPS IFD",
0xa005: "Interoperability IFD",
0xc4a5: "Print IM",
# EXIF IFD tags
0x829a: "Exposure Time",
0x829d: "F-Number",
0x8822: "Exposure Program",
0x8824: "Spectral Sensitivity",
0x8827: "ISO Speed Ratings",
0x8828: "OECF",
0x9000: "EXIF Version",
0x9003: "DateTime Original",
0x9004: "DateTime Digitized",
0x9101: "Components Configuration",
0x9102: "Compressed Bits Per Pixel",
0x9201: "Shutter Speed Value",
0x9202: "Aperture Value",
0x9203: "Brightness Value",
0x9204: "Exposure Bias Value",
0x9205: "Max Aperture Value",
0x9206: "Subject Distance",
0x9207: "Metering Mode",
0x9208: "Light Source",
0x9209: "Flash",
0x920a: "Focal Length",
0x9214: "Subject Area",
0x927c: "Maker Note",
0x9286: "User Comment",
# ... TODO
0xa000: "Flashpix Version",
0xa001: "Color Space",
0xa002: "Pixel X Dimension",
0xa003: "Pixel Y Dimension",
0xa004: "Related Sound File",
# ... TODO
# GPS IFD tags
# ... TODO
}
_TIFF_EXIF_IFD = 0x8769
_GPS_IFD = 0x8825
_INTEROPERABILITY_IFD = 0xa005
class ExifInfo:
"""EXIF reader and information lister."""
_endian = None
_buffer = None
_offset = 0
_global_ifd_offset = 0
_exif_ifd_offset = 0
_gps_ifd_offset = 0
_interoperability_ifd_offset = 0
_hex = False
def __init__(self, buffer, **kwargs):
"""Initialize the EXIF data reader."""
self._hex = kwargs.get("hex", False)
self._verbose = kwargs.get("verbose", False)
if not isinstance(buffer, bytes):
raise RuntimeError("invalid EXIF data type")
if buffer.startswith(b"MM\x00\x2a"):
self._endian = "MM"
elif buffer.startswith(b"II\x2a\x00"):
self._endian = "II"
else:
raise RuntimeError("invalid EXIF header")
self._buffer = buffer
self._offset = 4
self._global_ifd_offset = self._ui32()
def endian(self):
"""Return the endianness of the EXIF data."""
return self._endian
def _tags_for_ifd(self, ifd_offset):
"""Yield the tags found at the given TIFF IFD offset."""
if ifd_offset < 8:
raise RuntimeError("invalid TIFF IFD offset")
self._offset = ifd_offset
ifd_size = self._ui16()
for _ in range(0, ifd_size):
tag_id = self._ui16()
tag_type = self._ui16()
count = self._ui32()
value_or_offset = self._ui32()
if self._endian == "MM":
# FIXME:
# value_or_offset requires a fixup under big-endian encoding.
if tag_type == 2:
# 2 --> "ascii"
value_or_offset >>= 24
elif tag_type == 3:
# 3 --> "short"
value_or_offset >>= 16
else:
# ... FIXME
pass
if count == 0:
raise RuntimeError("unsupported count=0 in tag 0x%x" % tag_id)
if tag_id == _TIFF_EXIF_IFD:
if tag_type != 4:
raise RuntimeError("incorrect tag type for EXIF IFD")
self._exif_ifd_offset = value_or_offset
elif tag_id == _GPS_IFD:
if tag_type != 4:
raise RuntimeError("incorrect tag type for GPS IFD")
self._gps_ifd_offset = value_or_offset
elif tag_id == _INTEROPERABILITY_IFD:
if tag_type != 4:
raise RuntimeError("incorrect tag type for Interop IFD")
self._interoperability_ifd_offset = value_or_offset
yield (tag_id, tag_type, count, value_or_offset)
def tags(self):
"""Yield all TIFF/EXIF tags."""
if self._verbose:
print("TIFF IFD : 0x%08x" % self._global_ifd_offset)
for tag in self._tags_for_ifd(self._global_ifd_offset):
yield tag
if self._exif_ifd_offset > 0:
if self._verbose:
print("EXIF IFD : 0x%08x" % self._exif_ifd_offset)
for tag in self._tags_for_ifd(self._exif_ifd_offset):
yield tag
if self._gps_ifd_offset > 0:
if self._verbose:
print("GPS IFD : 0x%08x" % self._gps_ifd_offset)
for tag in self._tags_for_ifd(self._gps_ifd_offset):
yield tag
if self._interoperability_ifd_offset > 0:
if self._verbose:
print("Interoperability IFD : 0x%08x" %
self._interoperability_ifd_offset)
for tag in self._tags_for_ifd(self._interoperability_ifd_offset):
yield tag
def tagid2str(self, tag_id):
"""Return an informative string representation of a TIFF tag id."""
idstr = _TIFF_TAGS.get(tag_id, "[Unknown]")
if self._hex:
idnum = "0x%04x" % tag_id
else:
idnum = "%d" % tag_id
return "%s (%s)" % (idstr, idnum)
@staticmethod
def tagtype2str(tag_type):
"""Return an informative string representation of a TIFF tag type."""
typestr = _TIFF_TAG_TYPES.get(tag_type, "[unknown]")
return "%d:%s" % (tag_type, typestr)
def tag2str(self, tag_id, tag_type, count, value_or_offset):
"""Return an informative string representation of a TIFF tag tuple."""
return "%s (type=%s) (count=%d) : 0x%08x" \
% (self.tagid2str(tag_id), self.tagtype2str(tag_type), count,
value_or_offset)
def _ui32(self):
"""Decode a 32-bit unsigned int found at the current offset;
advance the offset by 4.
"""
if self._offset + 4 > len(self._buffer):
raise RuntimeError("out-of-bounds uint32 access in EXIF")
if self._endian == "MM":
result = unpack_uint32be(self._buffer, self._offset)
else:
result = unpack_uint32le(self._buffer, self._offset)
self._offset += 4
return result
def _ui16(self):
"""Decode a 16-bit unsigned int found at the current offset;
advance the offset by 2.
"""
if self._offset + 2 > len(self._buffer):
raise RuntimeError("out-of-bounds uint16 access in EXIF")
if self._endian == "MM":
result = unpack_uint16be(self._buffer, self._offset)
else:
result = unpack_uint16le(self._buffer, self._offset)
self._offset += 2
return result
def _ui8(self):
"""Decode an 8-bit unsigned int found at the current offset;
advance the offset by 1.
"""
if self._offset + 1 > len(self._buffer):
raise RuntimeError("out-of-bounds uint8 access in EXIF")
result = unpack_uint8(self._buffer, self._offset)
self._offset += 1
return result
def print_raw_exif_info(buffer, **kwargs):
"""Print the EXIF information found in a raw byte stream."""
lister = ExifInfo(buffer, **kwargs)
print("EXIF (endian=%s)" % lister.endian())
for (tag_id, tag_type, count, value_or_offset) in lister.tags():
print(lister.tag2str(tag_id=tag_id,
tag_type=tag_type,
count=count,
value_or_offset=value_or_offset))
if __name__ == "__main__":
# For testing only.
for arg in sys.argv[1:]:
with open(arg, "rb") as test_stream:
test_buffer = test_stream.read(_READ_DATA_SIZE_MAX)
print_raw_exif_info(test_buffer, hex=True, verbose=True)

10
contrib/pngexif/pngexifinfo Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
set -eu
my_python="$(command -v python3 || command -v python)" || {
echo >&2 "error: program not found: Python interpreter"
exit 127
}
my_python_flags="-BES"
exec "$my_python" "$my_python_flags" "$(dirname "$0")/pngexifinfo.py" "$@"

View File

@ -0,0 +1,4 @@
@echo off
@setlocal enableextensions
python.exe -BES %~dp0.\pngexifinfo.py %*

178
contrib/pngexif/pngexifinfo.py Executable file
View File

@ -0,0 +1,178 @@
#!/usr/bin/env python
"""
Show the PNG EXIF information.
Copyright (C) 2017-2020 Cosmin Truta.
Use, modification and distribution are subject to the MIT License.
Please see the accompanying file LICENSE_MIT.txt
"""
from __future__ import absolute_import, division, print_function
import argparse
import io
import re
import sys
import zlib
from bytepack import unpack_uint32be, unpack_uint8
from exifinfo import print_raw_exif_info
_PNG_SIGNATURE = b"\x89PNG\x0d\x0a\x1a\x0a"
_PNG_CHUNK_SIZE_MAX = 0x7fffffff
_READ_DATA_SIZE_MAX = 0x3ffff
def print_error(msg):
"""Print an error message to stderr."""
sys.stderr.write("%s: error: %s\n" % (sys.argv[0], msg))
def print_debug(msg):
"""Print a debug message to stderr."""
sys.stderr.write("%s: debug: %s\n" % (sys.argv[0], msg))
def _check_png(condition, chunk_sig=None):
"""Check a PNG-specific assertion."""
if condition:
return
if chunk_sig is None:
raise RuntimeError("bad PNG data")
raise RuntimeError("bad PNG data in '%s'" % chunk_sig)
def _check_png_crc(data, checksum, chunk_sig):
"""Check a CRC32 value inside a PNG stream."""
if unpack_uint32be(data) == (checksum & 0xffffffff):
return
raise RuntimeError("bad PNG checksum in '%s'" % chunk_sig)
def _extract_png_exif(data, **kwargs):
"""Extract the EXIF header and data from a PNG chunk."""
debug = kwargs.get("debug", False)
if unpack_uint8(data, 0) == 0:
if debug:
print_debug("found compressed EXIF, compression method 0")
if (unpack_uint8(data, 1) & 0x0f) == 0x08:
data = zlib.decompress(data[1:])
elif unpack_uint8(data, 1) == 0 \
and (unpack_uint8(data, 5) & 0x0f) == 0x08:
if debug:
print_debug("found uncompressed-length EXIF field")
data_len = unpack_uint32be(data, 1)
data = zlib.decompress(data[5:])
if data_len != len(data):
raise RuntimeError(
"incorrect uncompressed-length field in PNG EXIF")
else:
raise RuntimeError("invalid compression method in PNG EXIF")
if data.startswith(b"MM\x00\x2a") or data.startswith(b"II\x2a\x00"):
return data
raise RuntimeError("invalid TIFF/EXIF header in PNG EXIF")
def print_png_exif_info(instream, **kwargs):
"""Print the EXIF information found in the given PNG datastream."""
debug = kwargs.get("debug", False)
has_exif = False
while True:
chunk_hdr = instream.read(8)
_check_png(len(chunk_hdr) == 8)
chunk_len = unpack_uint32be(chunk_hdr, offset=0)
chunk_sig = chunk_hdr[4:8].decode("latin_1", errors="ignore")
_check_png(re.search(r"^[A-Za-z]{4}$", chunk_sig), chunk_sig=chunk_sig)
_check_png(chunk_len < _PNG_CHUNK_SIZE_MAX, chunk_sig=chunk_sig)
if debug:
print_debug("processing chunk: %s" % chunk_sig)
if chunk_len <= _READ_DATA_SIZE_MAX:
# The chunk size does not exceed an arbitrary, reasonable limit.
chunk_data = instream.read(chunk_len)
chunk_crc = instream.read(4)
_check_png(len(chunk_data) == chunk_len and len(chunk_crc) == 4,
chunk_sig=chunk_sig)
checksum = zlib.crc32(chunk_hdr[4:8])
checksum = zlib.crc32(chunk_data, checksum)
_check_png_crc(chunk_crc, checksum, chunk_sig=chunk_sig)
else:
# The chunk is too big. Skip it.
instream.seek(chunk_len + 4, io.SEEK_CUR)
continue
if chunk_sig == "IEND":
_check_png(chunk_len == 0, chunk_sig=chunk_sig)
break
if chunk_sig.lower() in ["exif", "zxif"] and chunk_len > 8:
has_exif = True
exif_data = _extract_png_exif(chunk_data, **kwargs)
print_raw_exif_info(exif_data, **kwargs)
if not has_exif:
raise RuntimeError("no EXIF data in PNG stream")
def print_exif_info(file, **kwargs):
"""Print the EXIF information found in the given file."""
with open(file, "rb") as stream:
header = stream.read(4)
if header == _PNG_SIGNATURE[0:4]:
if stream.read(4) != _PNG_SIGNATURE[4:8]:
raise RuntimeError("corrupted PNG file")
print_png_exif_info(instream=stream, **kwargs)
elif header == b"II\x2a\x00" or header == b"MM\x00\x2a":
data = header + stream.read(_READ_DATA_SIZE_MAX)
print_raw_exif_info(data, **kwargs)
else:
raise RuntimeError("not a PNG file")
def main():
"""The main function."""
parser = argparse.ArgumentParser(
prog="pngexifinfo",
usage="%(prog)s [options] [--] files...",
description="Show the PNG EXIF information.")
parser.add_argument("files",
metavar="file",
nargs="*",
help="a PNG file or a raw EXIF blob")
parser.add_argument("-x",
"--hex",
dest="hex",
action="store_true",
help="show EXIF tags in base 16")
parser.add_argument("-v",
"--verbose",
dest="verbose",
action="store_true",
help="run in verbose mode")
parser.add_argument("--debug",
dest="debug",
action="store_true",
help="run in debug mode")
args = parser.parse_args()
if not args.files:
parser.error("missing file operand")
result = 0
for file in args.files:
try:
print_exif_info(file,
hex=args.hex,
debug=args.debug,
verbose=args.verbose)
except (IOError, OSError) as err:
print_error(str(err))
result = 66 # os.EX_NOINPUT
except RuntimeError as err:
print_error("%s: %s" % (file, str(err)))
result = 69 # os.EX_UNAVAILABLE
parser.exit(result)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.stderr.write("INTERRUPTED\n")
sys.exit(130) # SIGINT