Project

General

Profile

The Metadata in JPEG files » History » Version 17

Tuan Nhu, 23 Jul 2013 07:38

1 1 Tuan Nhu
h1. The Metadata in JPEG files
2 2 Robin Mills
3 3 Tuan Nhu
This article shares my investigations about the metadata structure in JPEG files. It also introduces briefly the algorithm used in exiv2 to read and write Exif, IPTC, XMP data and image comments on the JPEG files.
4 2 Robin Mills
5 7 Tuan Nhu
h2. 1. Background
6 1 Tuan Nhu
7 3 Tuan Nhu
JPEG ISO standard is a commonly used method of lossy compression for digital photography. The name "JPEG" stands for Joint Photographic Experts Group, the name of the committee.
8
 
9
JPEG refers only to a class of compression algorithms, not to a specific file format. In order to produce files with embedded JPEG streams, a number of file format standards have been adapted or devised. Some of them are  JPEG /JFIF(JPEG File Interchange Format), JPEG /SPIFF (Still Picture Interchange File Format), JPEG /CIFF(Camera Image File Format), JPEG/Exif (Exchangeable image file format).
10 1 Tuan Nhu
11 3 Tuan Nhu
Among them, the most common types are JPEG/Exif and JPEG/JFIF. 
12
* JPEG/Exif is the most common image format used by digital cameras and other photographic image capture devices. 
13
* JPEG/JFIF is the most common format for storing and transmitting photographic images on the World Wide Web.
14 1 Tuan Nhu
15 7 Tuan Nhu
h2. 2. The metadata structure in JPEG
16 3 Tuan Nhu
17
A JPEG file contains several segments; each segment contains different kinds of data, delimited by two-byte codes called markers.  The markers are hexadecimal; they begin with 0xFF and end with a code (1 byte) indicating the kind of marker.
18
19
Some markers consist of just those two bytes; others are followed by two bytes indicating the length of marker-specific payload data that follows.  The length includes the two bytes for the length, but not the two bytes for the marker. 
20 4 Tuan Nhu
21
|_.Short name |_.Bytes      |_.Payload     |_. Name and Comments |
22
|SOI	      |0xFF, 0xD8   |None	   |Start Of Image       |
23
|SOF0	      |0xFF, 0xC0   |Variable size |Start Of Frame (Baseline DCT)
24
Indicates that this is a baseline DCT-based JPEG, and specifies the width, height, number of components, and component subsampling|
25
|SOF2	      |0xFF, 0xC2   |Variable size |Start Of Frame (Progressive DCT)
26
Indicates that this is a progressive DCT-based JPEG, and specifies the width, height, number of components, and component subsampling|
27
|DHT	      |0xFF, 0xC4   |Variable size |Define Huffman Table(s)|
28
|DQT 	      |0xFF, 0xDB   |Variable size |Define Quantization Table(s)|
29
|DRI 	      |0xFF, 0xDD   |2 bytes       |Define Restart Interval
30
Specifies the interval between RSTn markers, in macroblocks. This marker is followed by two bytes indicating the fixed size so it can be treated like any other variable size segment.|
31
|SOS 	      |0xFF, 0xDA   |Variable size |Start Of Scan
32
Begins a top-to-bottom scan of the image. In baseline DCT JPEG images, there is generally a single scan. Progressive DCT JPEG images usually contain multiple scans. This marker specifies which slice of data it will contain, and is immediately followed by entropy-coded data.|
33 5 Tuan Nhu
|RSTn	      |0xFF, 0xDn
34 4 Tuan Nhu
               n(n=0..7)   |None          |Restart
35
Inserted every r macroblocks, where r is the restart interval set by a DRI marker. Not used if there was no DRI marker. The low 3 bits of the marker code cycle in value from 0 to 7.|
36
|APPn 	      |0xFF, 0xEn  |Variable size |Application-specific
37
For example, an Exif JPEG file uses an APP1 marker to store metadata, laid out in a structure based closely on TIFF.|
38
|COM	      |0xFF, 0xFE  |Variable size |Comment|
39 1 Tuan Nhu
|EOI          |0xFF, 0xD9  |None          |End Of Image|
40
41 7 Tuan Nhu
p=. _Fig.1. The common JPEG markers. From Wikipedia, https://en.wikipedia.org/wiki/JPEG_
42
43
The metadata in JPEG file is stored in APPn (0xFF, 0xEn) segment and the comment is stored in COM segment (0xFF, 0xFE).  Several vendors might use the same APPn marker type to include their information, so these markers often begin with a vendor name (e.g., "Exif" or "Adobe") or some other identifying string.
44
45
Exiv2 provides fast and easy read write access to the Exif, IPTC and XMP.  Hence, this article only focuses on the position of Exif, IPTC and XMP data in JPEG files.
46
47
h3. 2.1 Exif
48
49
Exif JPEG file uses an APP1 segment to store the information (and multiples APP2 segments for flashPix data). Exif APP1 segment stores a great amount of information on photographic parameters for digital cameras and it is the preferred way to store thumbnail images nowadays. It can also host an additional section with GPS data. All details about Exif are available at http://www.exif.org/Exif2-2.PDF
50
51
In theory, Exif APP1 is recorded immediately after the SOI marker (the marker indicating the beginning of the file). However, this leads to the incompatibility between the Exif and JFIF standards because both of them specify that their particular application segment (APP0 for JFIF, APP1 for Exif) must be the first in the image file. In practice, most JPEG files contain a JFIF marker segment (APP0) that precedes the Exif APP1. This allows older readers to correctly handle the format JFIF segment, while newer readers also decode the following Exif segment, being less strict about requiring it to appear first. This way will not affect the image decoding for most decoders, but poorly designed JFIF or Exif parsers may not recognize the file properly.
52
53
Exif APP1 segment consists of the APP1 marker (0xFFE1), Exif identifier string (“Exif\0\0”), and the attribute information itself.  The identifier string "Exif\0\0” is used to avoid a conflict with other applications using APP1 (e.g XMP).
54 8 Tuan Nhu
55 11 Tuan Nhu
p=. !http://exiv2.nuditu.com/doc/basic_jpg_file_exif.jpg!
56 1 Tuan Nhu
57 12 Tuan Nhu
p=. _Fig.2. Basic Structure of JPEG Files. From Exif.org, http://www.exif.org/Exif2-2.PDF_
58
59 1 Tuan Nhu
Exif does not use APPn segments other than APP1, APP2 and COM segments. However, some unknown APPn may still exist on the file structure and Exif readers should be designed to skip over them.
60 12 Tuan Nhu
61
h3. 2.2 XMP
62
63
In a typical edited JPEG file, XMP (eXtensible Metadata Platform) information is typically included alongside Exif and IPTC (Information Interchange Model data). XMP uses an APP1 segment in order to store metadata information; the storage format is RDF (Resource Description Framework) implemented as an application of XML. 
64
65
XMP APP1 segment consists of the APP1 marker (0xFFE1), XMP identifier string (“http://ns.adobe.com/xap/1.0/\x00”), and Unicode XMP packet (the encoding is usually UTF-8, but it can also be UTF-16 or UTF-32). The packet cannot be split in multiple segments, so there is a maximum size of approximately 64KB (2^16-1 bytes).
66
67
The structure of the packet content can be found at http://www.w3.org/TR/REC-rdf-syntax/.
68
The reference document for XMP 3.2 can be downloaded from Adobe Systems Incorporated http://xml.coverpages.org/xmp.html
69
70
h3. 2.3 IPTC
71
72
Adobe Photoshop uses the APP13 segment for storing non-graphic information, such as layers, paths, IPTC data and more. The content of an APP13 segment is formed by APP1 marker (0xFFE1), an identifier string (usually "Photoshop 3.0\000", but also 'Adobe_Photoshop2.5:', used by earlier versions) followed by a sequence of resource data blocks. In general, a resource block contains only a few bytes, but there is the important IPTC block can be quite large.  The IPTC block may not fit into one APP13 segment, so it can be split into multiple APP13 segments.
73
74
The reference document for the Photoshop file format is available at http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/
75
76
h2. 3 Exiv2 JPEG read/write metadata algorithms
77
78 13 Tuan Nhu
This section introduces briefly about the algorithm used in Exiv2. For more details, please download the source code of Exiv2 and read the code in src/jpgimage.cpp file.
79
80 15 Tuan Nhu
h3. 3.1 Read Algorithm
81 13 Tuan Nhu
82 1 Tuan Nhu
From the above investigation, the read algorithm is quite simple. We just need to go through the markers, find and read the content of Exif APP1, XMP APP1, IPTC APP13 segments. Those segments all locate before the SOS segment (which is immediately followed by entropy-coded data) and often locate right after the SOI segment (not guaranteed).  Hence, it’s not necessary to read the whole JPEG file to check whether the metadata exists or not; the process can be stopped whenever the marker SOS is found.
83 15 Tuan Nhu
84
p=. !http://exiv2.nuditu.com/doc/read_jpeg_algo.jpg!
85
86
p=. _Fig.3. Flowchart of readMetadata_
87
88
_Notes:_
89
* The standard JPEG files should have at most one Exif APP1 segment and one XMP APP1 segment. Hence, if there’s more than one Exif APP1 segment or one XMP APP1 segment, exiv2 only reads the first segment.
90
* JPEG files can have multiple comments, but for now exiv2 only reads the first one (most of the jpeg files only have one anyway).
91
92
h3. 3.2 Write Algorithm
93 16 Tuan Nhu
94 17 Tuan Nhu
The general idea for the write algorithm is to find Exif APP1, XMP APP1, IPTC APP13 segments, remove them from the JPEG files and replace with the new data. To simplify this a bit, we agree the following (this is standard in most jpegs anyway).
95
* The order of the Exif, XMP, IPTC segments in the output file (regardless of their positions in the input file)
96
  SOI | (APP0) | (Exif App1) | (XMP App1) | (IPTC App13) | … | EOI
97
* The COM segment is located just before SOFn in the output file (regardless of its position in the input file).
98
99 16 Tuan Nhu
p=. !http://exiv2.nuditu.com/doc/write_jpeg_algo.jpg!
100
101
p=. _Fig.4. Flowchart of writeMetadata_
102 17 Tuan Nhu
103
h2. References