Subject: bug#73177: 29.4; Unable to retrieve Exif data from some JPEG files

From: AKIYAMA Kouhei <[email-masked]>
Date: Wed, 11 Sep 2024 20:09:40 +0900

exif.el does not correctly read the Exif data from some JPEG files,
which means that for example image rotation does not work properly in
image-mode.

* Issue 1: Unable to retrieve values such as orientation from Exif data
           written in Big-endian format.

Reproduction steps 1:
(The following command line creates test images using exiftool)

wget https://www.gnu.org/graphics/gnu-head.jpg
exiftool -Orientation#=6 -ExifByteOrder=Little-endian gnu-head.jpg -o gnu-head-le.jpg
exiftool -Orientation#=6 -ExifByteOrder=Big-endian gnu-head.jpg -o gnu-head-be.jpg
emacs --batch --eval "(progn (require 'exif) (princ (exif-orientation (exif-parse-file \"gnu-head-le.jpg\"))))" # => 90
emacs --batch --eval "(progn (require 'exif) (princ (exif-orientation (exif-parse-file \"gnu-head-be.jpg\"))))" # => nil (expected: 90)

Alternatively, the following expressions can be evaluated within Emacs:

  (exif-orientation (exif-parse-file "gnu-head-le.jpg")) ;; => 90
  (exif-orientation (exif-parse-file "gnu-head-be.jpg")) ;; => nil (expected: 90)

If you view gnu-head-be.jpg in image-mode, the image will not be
rotated. If you view it in an image viewer app or web browser,
it will be rotated correctly.

Related source code location 1: (In exif.el)

  (defun exif--parse-directory (le)
  ...
    for value = (exif--read-number 4 le)

For small field types, 4 bytes is too large.

(This is unrelated to the current issue, but if this part could read
triplet rational numbers such as latitude and longitude information,
the range of applications would be expanded. Currently, it can read
rational numbers, but it cannot read multiple values.)

* Issue 2: An error may occur depending on the order when non-Exif APP1
           segments exist in the file.
Reproduction steps 2-1: (When there is no Exif data and only XMP is embedded)

exiftool -xmp:creator="Etienne Suvasa" gnu-head.jpg -o gnu-head-xmp.jpg
emacs --batch --eval "(progn (require 'exif) (princ (exif-orientation (exif-parse-file \"gnu-head-xmp.jpg\"))))" # => Invalid Exif data (expected: 0)

Reproduction steps 2-2: (When XMP appears before Exif)

wget https://misohena.jp/work/emacs-exif-test/20220629_072635771_cw.jpg
emacs --batch --eval "(progn (require 'exif) (princ (exif-orientation (exif-parse-file \"20220629_072635771_cw.jpg\"))))" # => Invalid Exif data (expected: 90)

I'm not sure if JPEG files with XMP preceding Exif data conform to the
standard, but such files do exist in practice.

Currently, I have not found a way to generate such a JPEG file in my
environment. It may be a problem with the image conversion software I
used in the past.

Related source code location 2: (In exif.el)

  (defun exif-parse-buffer (&optional buffer)
  ...
    (when-let ((app1 (cdr (assq #xffe1 (exif--parse-jpeg)))))
      (exif--parse-exif-chunk app1))

Only the first APP1 segment found will be used, any subsequent APP1
segments (including Exif) will be ignored.

------
In GNU Emacs 29.4 (build 2, x86_64-w64-mingw32) of 2024-07-05 built on
 AVALON
Windowing system distributor 'Microsoft Corp.', version 10.0.19045
System Description: Microsoft Windows 10 Enterprise (v10.0.2009.19045.4780)

Configured using:
 'configure --with-modules --without-dbus --with-native-compilation=aot
 --without-compress-install --with-sqlite3 --with-tree-sitter
 CFLAGS=-O2'

Configured features:
ACL GIF GMP GNUTLS HARFBUZZ JPEG JSON LCMS2 LIBXML2 MODULES NATIVE_COMP
NOTIFY W32NOTIFY PDUMPER PNG RSVG SOUND SQLITE3 THREADS TIFF
TOOLKIT_SCROLL_BARS TREE_SITTER WEBP XPM ZLIB

Important settings:
  value of $LANG: ja_JP.CP932
  locale-coding-system: cp932

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util text-property-search time-date mm-decode mm-bodies
mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail
comp comp-cstr warnings icons subr-x rx cl-seq cl-macs gv cl-extra
help-mode cl-loaddefs cl-lib bytecomp byte-compile rfc2047 rfc2045
ietf-drums mm-util mail-prsvr mail-utils japan-util rmc iso-transl
tooltip cconv eldoc paren electric uniquify ediff-hook vc-hooks
lisp-float-type elisp-mode mwheel dos-w32 ls-lisp disp-table
term/w32-win w32-win w32-vars term/common-win tool-bar dnd fontset image
regexp-opt fringe tabulated-list replace newcomment text-mode lisp-mode
prog-mode register page tab-bar menu-bar rfn-eshadow isearch easymenu
timer select scroll-bar mouse jit-lock font-lock syntax font-core
term/tty-colors frame minibuffer nadvice seq simple cl-generic
indonesian philippine cham georgian utf-8-lang misc-lang vietnamese
tibetan thai tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek
romanian slovak czech european ethiopic indian cyrillic chinese
composite emoji-zwj charscript charprop case-table epa-hook
jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button loaddefs
theme-loaddefs faces cus-face macroexp files window text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget keymap
hashtable-print-readable backquote threads w32notify w32 lcms2 multi-tty
make-network-process native-compile emacs)

Memory information:
((conses 16 85672 6348)
 (symbols 48 8061 0)
 (strings 32 22174 4610)
 (string-bytes 1 623953)
 (vectors 16 17387)
 (vector-slots 8 439453 10338)
 (floats 8 29 33)
 (intervals 56 1089 769)
 (buffers 984 12))

--
# This report was created using machine translation.
AKIYAMA Kouhei
[email-masked]