1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
| | ;;; exif.el --- parsing Exif data in JPEG images -*- lexical-binding: t -*-
;; Copyright (C) 2019-2020 Free Software Foundation, Inc.
;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
;; Keywords: images
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Specification at:
;; https://www.media.mit.edu/pia/Research/deepview/exif.html
;; but it's kinda er not very easy to read.
;; The JPEG format is:
;;
;; FFD8 and then any number of chunks on the format: FFxx SSSS ...,
;; where FFxx is the ID, and SSSS is the length of the chunk plus 2.
;; When you get to ID FFDA, the image itself is over and you can stop
;; parsing.
;;
;; The Exif data is in the TIFF format. It starts off with the six
;; bytes "Exif^0^0".
;;
;; Then either "II" or "MM", where "II" means little-endian and "MM"
;; means big-endian. All subsequent numbers should be read in
;; according to this.
;;
;; Next follows two bytes that should always represent 0x2a, and then
;; four bytes that's the offset to where the IFD "image file
;; directory" starts. (It's an offset from the start of this chunk;
;; i.e., where "II"/"MM" is; all offsets in the TIFF format are from
;; this point.)
;;
;; The IFD starts with two bytes that says how many entries there are
;; in the directory, and then that number of entries follows, and then
;; an offset to the next IFD.
;; Usage: (exif-parse-file "test.jpg") =>
;; ((:tag 274 :tag-name orientation :format 3 :format-type short :value 1)
;; (:tag 282 :tag-name x-resolution :format 5 :format-type rational :value
;; (180 . 1))
;; (:tag 306 :tag-name date-time :format 2 :format-type ascii
;; :value "2019:09:21 16:22:13")
;; ...)
;;; Code:
(require 'cl-lib)
(eval-when-compile (require 'subr-x))
(defvar exif-tag-alist
'((11 processing-software)
(271 make)
(272 model)
(274 orientation)
(282 x-resolution)
(283 y-resolution)
(296 resolution-unit)
(305 software)
(306 date-time))
"Alist of tag values and their names.")
(defconst exif--orientation
'((1 0 nil)
(2 0 t)
(3 180 nil)
(4 180 t)
(5 90 nil)
(6 90 t)
(7 270 nil)
(8 270 t))
"Alist of Exif orientation codes.
These are mapped onto rotation values and whether the image is
mirrored or not.")
(define-error 'exif-error "Invalid Exif data")
(defun exif-parse-file (file)
"Parse FILE (a JPEG file) and return the Exif data, if any.
The return value is a list of Exif items.
If the data is invalid, an `exif-error' is signalled."
(with-temp-buffer
(set-buffer-multibyte nil)
(insert-file-contents-literally file)
(exif-parse-buffer)))
(defun exif-parse-buffer (&optional buffer)
"Parse BUFFER (which should be a JPEG file) and return the Exif data, if any.
The return value is a list of Exif items.
If the data is invalid, an `exif-error' is signalled."
(setq buffer (or buffer (current-buffer)))
(with-current-buffer buffer
(if enable-multibyte-characters
(with-temp-buffer
(set-buffer-multibyte nil)
(let ((dest (current-buffer)))
(with-current-buffer buffer
(encode-coding-region (point-min) (point-max)
buffer-file-coding-system
dest))
(when-let ((app1 (cdr (assq #xffe1 (exif--parse-jpeg)))))
(exif--parse-exif-chunk app1))))
(when-let ((app1 (cdr (assq #xffe1 (exif--parse-jpeg)))))
(exif--parse-exif-chunk app1)))))
(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)))
(cadr (assq code exif--orientation))))
(defun exif--parse-jpeg ()
(unless (= (exif--read-number-be 2) #xffd8) ; SOI (start of image)
(signal 'exif-error "Not a valid JPEG file"))
(cl-loop for segment = (exif--read-number-be 2)
for size = (exif--read-number-be 2)
;; Stop parsing when we get to SOS (start of stream);
;; this is when the image itself starts, and there will
;; be no more chunks of interest after that.
while (not (= segment #xffda))
collect (cons segment (exif--read-chunk (- size 2)))))
(defun exif--parse-exif-chunk (data)
(with-temp-buffer
(set-buffer-multibyte nil)
(insert data)
(goto-char (point-min))
;; The Exif data is in the APP1 JPEG chunk and starts with
;; "Exif\0\0".
(unless (equal (exif--read-chunk 6) (string ?E ?x ?i ?f ?\0 ?\0))
(signal 'exif-error "Not a valid Exif chunk"))
(delete-region (point-min) (point))
(let* ((endian-marker (exif--read-chunk 2))
(le (cond
;; "Motorola" is big-endian.
((equal endian-marker "MM")
nil)
;; "Intel" is little-endian.
((equal endian-marker "II")
t)
(t
(signal 'exif-error
(format "Invalid endian-ness %s" endian-marker))))))
;; Another magical number.
(unless (= (exif--read-number 2 le) #x002a)
(signal 'exif-error "Invalid TIFF header length"))
(let ((offset (exif--read-number 2 le)))
;; Jump to where the IFD (directory) starts and parse it.
(when (> (1+ offset) (point-max))
(signal 'exif-error "Invalid IFD (directory) offset"))
(goto-char (1+ offset))
(exif--parse-directory le)))))
(defun exif--field-format (number)
(cl-case number
(1 (cons 'byte 1))
(2 (cons 'ascii 1))
(3 (cons 'short 2))
(4 (cons 'long 4))
(5 (cons 'rational 8))
(otherwise (cons 'unknown 1))))
(defun exif--parse-directory (le)
(let ((dir
(cl-loop repeat (exif--read-number 2 le)
for tag = (exif--read-number 2 le)
for format = (exif--read-number 2 le)
for field-format = (exif--field-format format)
;; The actual length is the number in this field
;; times the "inherent" length of the field format
;; (i.e., "long integer" (4 bytes) or "ascii" (1
;; byte)).
for length = (* (exif--read-number 4 le)
(cdr field-format))
for value = (exif--read-number 4 le)
collect (list
:tag tag
:tag-name (cadr (assq tag exif-tag-alist))
:format format
:format-type (car field-format)
:value (exif--process-value
(if (> length 4)
;; If the length of the data is
;; more than 4 bytes, then it's
;; actually stored after this
;; directory, and the value
;; here is just the offset to
;; use to find the data.
(progn
(when (> (+ (1+ value) length)
(point-max))
(signal 'exif-error
"Premature end of file"))
(buffer-substring
(1+ value)
(+ (1+ value) length)))
;; The value is stored directly
;; in the directory.
value)
(car field-format)
le)))))
(let ((next (exif--read-number 4 le)))
(if (> next 0)
;; There's more than one directory; if so, jump to it and
;; keep parsing.
(progn
(when (> (1+ next) (point-max))
(signal 'exif-error "Invalid IFD (directory) next-offset"))
(goto-char (1+ next))
(nconc dir (exif--parse-directory le)))
;; We've reached the end of the directories.
dir))))
(defun exif--process-value (value type le)
"Do type-based post-processing of the value."
(cl-case type
;; Chop off trailing zero byte.
('ascii (substring value 0 (1- (length value))))
('rational (with-temp-buffer
(set-buffer-multibyte nil)
(insert value)
(goto-char (point-min))
(cons (exif--read-number 4 le)
(exif--read-number 4 le))))
(otherwise value)))
(defun exif--read-chunk (bytes)
"Return BYTES octets from the buffer and advance point that much."
(when (> (+ (point) bytes) (point-max))
(signal 'exif-error "Premature end of file"))
(prog1
(buffer-substring (point) (+ (point) bytes))
(forward-char bytes)))
(defun exif--read-number-be (bytes)
"Read BYTES octets from the buffer as a chunk of big-endian bytes.
Advance point to after the read bytes."
(when (> (+ (point) bytes) (point-max))
(signal 'exif-error "Premature end of file"))
(let ((sum 0))
(dotimes (_ bytes)
(setq sum (+ (* sum 256) (following-char)))
(forward-char 1))
sum))
(defun exif--read-number-le (bytes)
"Read BYTES octets from the buffer as a chunk of low-endian bytes.
Advance point to after the read bytes."
(when (> (+ (point) bytes) (point-max))
(signal 'exif-error "Premature end of file"))
(let ((sum 0))
(dotimes (i bytes)
(setq sum (+ (* (following-char) (expt 256 i)) sum))
(forward-char 1))
sum))
(defun exif--read-number (bytes lower-endian)
"Read BYTES octets from the buffer with endianness determined by LOWER-ENDIAN.
Advance point to after the read bytes."
(if lower-endian
(exif--read-number-le bytes)
(exif--read-number-be bytes)))
(provide 'exif)
;;; exif.el ends here
|