1 /*
2  * Copyright (c) 2001-2007, Eric M. Johnston <emj@postal.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Eric M. Johnston.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: exif.h,v 1.38 2007/12/16 03:44:32 ejohnst Exp $
33  */
34 
35 /*
36  * Exchangeable image file format (Exif) parser.
37  *
38  * Developed using the TIFF 6.0 specification
39  * (http://partners.adobe.com/asn/developer/pdfs/tn/TIFF6.pdf)
40  * and the EXIF 2.21 standard: (http://tsc.jeita.or.jp/avs/data/cp3451_1.pdf).
41  *
42  */
43 
44 #ifndef _EXIF_H
45 #define _EXIF_H
46 
47 #include <sys/types.h>
48 
49 
50 /*
51  * XXX Only checking for Solaris, HP, & Windows now.  Other platforms will
52  * probably need something similar if they don't have u_int16_t or u_int32_t.
53  */
54 
55 #if (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
56 typedef unsigned short u_int16_t;
57 typedef unsigned int u_int32_t;
58 #endif
59 
60 #if (defined(__hpux))
61 typedef uint16_t u_int16_t;
62 typedef uint32_t u_int32_t;
63 #endif
64 
65 #ifdef WIN32
66 typedef unsigned __int16 u_int16_t;
67 typedef unsigned __int32 u_int32_t;
68 typedef __int16 int16_t;
69 typedef __int32 int32_t;
70 #define snprintf _snprintf
71 #define isnan _isnan
72 #endif
73 
74 #ifndef TRUE
75 #define TRUE 1
76 #endif
77 #ifndef FALSE
78 #define FALSE 0
79 #endif
80 
81 
82 /* TIFF types. */
83 
84 #define TIFF_UNKN	0
85 #define TIFF_BYTE	1
86 #define TIFF_ASCII	2
87 #define TIFF_SHORT	3
88 #define TIFF_LONG	4
89 #define TIFF_RTNL	5
90 #define TIFF_SBYTE	6
91 #define TIFF_UNDEF	7
92 #define TIFF_SSHORT	8
93 #define TIFF_SLONG	9
94 #define TIFF_SRTNL	10
95 #define TIFF_FLOAT	11
96 #define TIFF_DBL	12
97 
98 
99 /* Dump level. */
100 
101 #define ED_UNK	0x01	/* Unknown or unimplemented info. */
102 #define ED_CAM	0x02	/* Camera-specific info. */
103 #define ED_IMG	0x04	/* Image-specific info. */
104 #define ED_VRB	0x08	/* Verbose info. */
105 #define ED_PAS	0x10	/* Point-and-shoot info. */
106 #define ED_OVR	0x20	/* Overridden info. */
107 #define ED_BAD	0x40	/* 'Bad' or incorrect info (given other values). */
108 
109 
110 /* Interesting tags. */
111 
112 #define EXIF_T_UNKNOWN		0xffff		/* XXX Non-standard. */
113 
114 #define EXIF_T_COMPRESS		0x0103
115 #define EXIF_T_PHOTOINTERP	0x0106
116 #define EXIF_T_EQUIPMAKE	0x010f
117 #define EXIF_T_MODEL		0x0110
118 #define EXIF_T_ORIENT		0x0112
119 #define EXIF_T_XRES		0x011a
120 #define EXIF_T_YRES		0x011b
121 #define EXIF_T_PLANARCONFIG	0x011c
122 #define EXIF_T_RESUNITS		0x0128
123 #define EXIF_T_XFERFUNC		0x012d
124 #define EXIF_T_DATETIME		0x0132
125 #define EXIF_T_CHROMRATIO	0x0212
126 #define EXIF_T_CHROMPOS		0x0213
127 #define EXIF_T_EXPOSURE		0x829a
128 #define EXIF_T_FNUMBER		0x829d
129 #define EXIF_T_EXPPROG		0x8822
130 #define EXIF_T_GPSIFD		0x8825
131 #define EXIF_T_ISOSPEED		0x8827
132 #define EXIF_T_VERSION		0x9000
133 #define EXIF_T_DATETIMEORIG	0x9003
134 #define EXIF_T_DATETIMEDIGI	0x9004
135 #define EXIF_T_COMPCONFIG	0x9101
136 #define EXIF_T_SHUTTER		0x9201
137 #define EXIF_T_LAPERTURE	0x9202
138 #define EXIF_T_BRIGHTVAL	0x9203
139 #define EXIF_T_EXPBIASVAL	0x9204
140 #define EXIF_T_MAXAPERTURE	0x9205
141 #define EXIF_T_DISTANCE		0x9206
142 #define EXIF_T_METERMODE	0x9207
143 #define EXIF_T_LIGHTSRC		0x9208
144 #define EXIF_T_FLASH		0x9209
145 #define EXIF_T_FOCALLEN		0x920a
146 #define EXIF_T_USERCOMMENT	0x9286
147 #define EXIF_T_COLORSPC		0xa001
148 #define EXIF_T_FPXRES		0xa20e
149 #define EXIF_T_FPYRES		0xa20f
150 #define EXIF_T_FPRESUNITS	0xa210
151 #define EXIF_T_IMGSENSOR	0xa217
152 #define EXIF_T_FILESRC		0xa300
153 #define EXIF_T_SCENETYPE	0xa301
154 #define EXIF_T_EXPMODE		0xa402
155 #define EXIF_T_WHITEBAL		0xa403
156 #define EXIF_T_DIGIZOOM		0xa404
157 #define EXIF_T_FOCALLEN35	0xa405
158 #define EXIF_T_SCENECAPTYPE	0xa406
159 #define EXIF_T_CONTRAST		0xa408
160 #define EXIF_T_SATURATION	0xa409
161 #define EXIF_T_SHARPNESS	0xa40a
162 
163 
164 /* Byte order. */
165 
166 enum byteorder { LITTLE, BIG };
167 
168 
169 /* Generic field description lookup table. */
170 
171 struct descrip {
172 	int32_t val;
173 	const char *descr;
174 };
175 
176 
177 /* Tag lookup table. */
178 
179 struct exiftag {
180 	u_int16_t tag;		/* Tag ID. */
181 	u_int16_t type;		/* Expected type. */
182 	u_int16_t count;	/* Expected count. */
183 	unsigned short lvl;	/* Output level. */
184 	const char *name;
185 	const char *descr;
186 	struct descrip *table;	/* Value lookup table. */
187 };
188 
189 
190 /* Final Exif property info.  (Note: descr can be NULL.) */
191 
192 struct exifprop {
193 	u_int16_t tag;		/* The Exif tag. */
194 	u_int16_t type;
195 	u_int32_t count;
196 	u_int32_t value;
197 	const char *name;
198 	const char *descr;
199 	char *str;		/* String representation of value (dynamic). */
200 	unsigned short lvl;	/* Verbosity level. */
201 	int ifdseq;		/* Sequence number of parent IFD. */
202 	u_int16_t override;	/* Override display of another tag. */
203 	struct exiftag *tagset;	/* Tags used to create property. */
204 	struct exifprop *par;	/* Parent property association. */
205 	struct exifprop *next;
206 };
207 
208 
209 /*
210  * TIFF/IFD metadata.
211  *
212  * Implementation note: ordinarily, this information wouldn't be stored
213  * at the directory (IFD) level -- it's characteristic of the TIFF itself.
214  * However, the format of some maker notes force this level of detail.
215  * For example, Fuji notes can be in a different byte order than the rest of
216  * the TIFF.  Also, some Nikon notes actually contain a full TIFF header
217  * and specify their own byte order and offset base.
218  *
219  * Therefore, while this information is generally true for the TIFF, it
220  * may not apply to maker note properties.
221  */
222 
223 struct tiffmeta {
224 	enum byteorder order;	/* Endianness of IFD. */
225 	unsigned char *btiff;	/* Beginning of TIFF (offset base). */
226 	unsigned char *etiff;	/* End of TIFF. */
227 	void *ifdoffs;		/* List of IFD offsets (internal only). */
228 };
229 
230 
231 /* Image info and exifprop pointer returned by exifscan(). */
232 
233 struct exiftags {
234 	struct exifprop *props;	/* The good stuff. */
235 	struct tiffmeta md;	/* Beginning, end, and endianness of TIFF. */
236 
237 	const char *model;	/* Camera model, to aid maker tag processing. */
238 	short mkrval;		/* Maker index (see makers.h). */
239 	struct tiffmeta mkrmd;	/* Maker TIFF info. */
240 
241 	/* Version info. */
242 
243 	short exifmaj;		/* Exif version, major. */
244 	short exifmin;		/* Exif version, minor. */
245 };
246 
247 
248 /* Eternal interfaces. */
249 
250 extern int debug;
251 extern const char *progname;
252 extern struct exiftag tags[];
253 
254 extern struct exifprop *findprop(struct exifprop *prop,
255     struct exiftag *tagset, u_int16_t tag);
256 extern void exifdie(const char *msg);
257 extern void exifwarn(const char *msg);
258 extern void exifwarn2(const char *msg1, const char *msg2);
259 
260 extern void exiffree(struct exiftags *t);
261 extern struct exiftags *exifscan(unsigned char *buf, int len, int domkr);
262 extern struct exiftags *exifparse(unsigned char *buf, int len);
263 
264 #endif
265