emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master bb06d56 1/2: Add new function exif-field


From: Stefan Kangas
Subject: master bb06d56 1/2: Add new function exif-field
Date: Sat, 23 Oct 2021 02:34:56 -0400 (EDT)

branch: master
commit bb06d5648eac297e5574b15c7723903c8c922d03
Author: Stefan Kangas <stefan@marxist.se>
Commit: Stefan Kangas <stefan@marxist.se>

    Add new function exif-field
    
    * test/lisp/image/exif-tests.el (exif-elem): Move function from here...
    * lisp/image/exif.el (exif-field): ...to here, and rename.
    (exif-orientation):
    * test/lisp/image/exif-tests.el (test-exif-parse)
    (test-exif-parse-short): Use above new function.
---
 etc/NEWS                      |  6 ++++++
 lisp/image/exif.el            | 18 ++++++++++++++----
 test/lisp/image/exif-tests.el | 21 ++++++++-------------
 3 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 36d04aa..7f61cf9 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -189,6 +189,12 @@ To improve security, if an sql product has 
':password-in-comint' set
 to t, a password supplied via the minibuffer will be sent in-process,
 as opposed to via the command-line.
 
+** Exif
+
+*** New function 'exif-field'.
+This is a convenience function to extract the field data from
+`exif-parse-file' and `exif-parse-buffer'.
+
 
 * New Modes and Packages in Emacs 29.1
 
diff --git a/lisp/image/exif.el b/lisp/image/exif.el
index c2cf234..67b0084 100644
--- a/lisp/image/exif.el
+++ b/lisp/image/exif.el
@@ -58,6 +58,9 @@
 ;;  (:tag 306 :tag-name date-time :format 2 :format-type ascii
 ;;   :value "2019:09:21 16:22:13")
 ;;   ...)
+;;
+;; (exif-field 'date-time (exif-parse-file "test.jpg")) =>
+;; "2022:09:14 18:46:19"
 
 ;;; Code:
 
@@ -122,13 +125,20 @@ If the data is invalid, an `exif-error' is signaled."
         (when-let ((app1 (cdr (assq #xffe1 (exif--parse-jpeg)))))
           (exif--parse-exif-chunk app1))))))
 
+(defun exif-field (field data)
+  "Return raw FIELD from EXIF.
+If FIELD is not present in the data, return nil.
+FIELD is a symbol in the cdr of `exif-tag-alist'.
+DATA is the result of calling `exif-parse-file'."
+  (plist-get (seq-find (lambda (e)
+                         (eq field (plist-get e :tag-name)))
+                       data)
+             :value))
+
 (defun exif-orientation (exif)
   "Return the orientation (in degrees) in EXIF.
 If the orientation isn't present in the data, return nil."
-  (let ((code (plist-get (cl-find 'orientation exif
-                                  :key (lambda (e)
-                                         (plist-get e :tag-name)))
-                         :value)))
+  (let ((code (exif-field 'orientation exif)))
     (cadr (assq code exif--orientation))))
 
 (defun exif--parse-jpeg ()
diff --git a/test/lisp/image/exif-tests.el b/test/lisp/image/exif-tests.el
index ddbee75..2357113 100644
--- a/test/lisp/image/exif-tests.el
+++ b/test/lisp/image/exif-tests.el
@@ -28,24 +28,19 @@
                           (or (getenv "EMACS_TEST_DIRECTORY")
                               "../../"))))
 
-(defun exif-elem (exif elem)
-  (plist-get (seq-find (lambda (e)
-                         (eq elem (plist-get e :tag-name)))
-                       exif)
-             :value))
-
 (ert-deftest test-exif-parse ()
   (let ((exif (exif-parse-file (test-image-file "black.jpg"))))
-    (should (equal (exif-elem exif 'make) "Panasonic"))
-    (should (equal (exif-elem exif 'orientation) 1))
-    (should (equal (exif-elem exif 'x-resolution) '(180 . 1)))))
+    (should (equal (exif-field 'make exif) "Panasonic"))
+    (should (equal (exif-field 'orientation exif) 1))
+    (should (equal (exif-field 'x-resolution exif) '(180 . 1)))
+    (should (equal (exif-field 'date-time exif) "2019:09:21 16:22:13"))))
 
 (ert-deftest test-exif-parse-short ()
   (let ((exif (exif-parse-file (test-image-file "black-short.jpg"))))
-    (should (equal (exif-elem exif 'make) "thr"))
-    (should (equal (exif-elem exif 'model) "four"))
-    (should (equal (exif-elem exif 'software) "em"))
-    (should (equal (exif-elem exif 'artist) "z"))))
+    (should (equal (exif-field 'make exif) "thr"))
+    (should (equal (exif-field 'model exif) "four"))
+    (should (equal (exif-field 'software exif) "em"))
+    (should (equal (exif-field 'artist exif) "z"))))
 
 (ert-deftest test-exit-direct-ascii-value ()
   (should (equal (exif--direct-ascii-value 28005 2 t) (string ?e ?m 0)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]