• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * write-exif.c
3  *
4  * Create a new EXIF data block and write it into a JPEG image file.
5  *
6  * The JPEG image data used in this example is fixed and is guaranteed not
7  * to contain an EXIF tag block already, so it is easy to precompute where
8  * in the file the EXIF data should be. In real life, a library like
9  * libjpeg (included with the exif command-line tool source code) would
10  * be used to write to an existing JPEG file.
11  *
12  * SPDX-FileCopyrightText: Placed into the public domain by Dan Fandrich
13  * SPDX-License-Identifier: CC0-1.0
14  */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <assert.h>
20 #include <libexif/exif-data.h>
21 
22 /* this file will be unilaterally overwritten */
23 #define FILE_NAME "write-exif.jpg"
24 
25 /* raw JPEG image data */
26 static const unsigned char image_jpg[] = {
27   0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
28   0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43,
29   0x00, 0x14, 0x0e, 0x0f, 0x12, 0x0f, 0x0d, 0x14, 0x12, 0x10, 0x12, 0x17,
30   0x15, 0x14, 0x18, 0x1e, 0x32, 0x21, 0x1e, 0x1c, 0x1c, 0x1e, 0x3d, 0x2c,
31   0x2e, 0x24, 0x32, 0x49, 0x40, 0x4c, 0x4b, 0x47, 0x40, 0x46, 0x45, 0x50,
32   0x5a, 0x73, 0x62, 0x50, 0x55, 0x6d, 0x56, 0x45, 0x46, 0x64, 0x88, 0x65,
33   0x6d, 0x77, 0x7b, 0x81, 0x82, 0x81, 0x4e, 0x60, 0x8d, 0x97, 0x8c, 0x7d,
34   0x96, 0x73, 0x7e, 0x81, 0x7c, 0xff, 0xc0, 0x00, 0x0b, 0x08, 0x00, 0x40,
35   0x00, 0x40, 0x01, 0x01, 0x11, 0x00, 0xff, 0xc4, 0x00, 0x1b, 0x00, 0x00,
36   0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37   0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x04, 0x03, 0x07, 0x02, 0x01, 0xff,
38   0xc4, 0x00, 0x2f, 0x10, 0x00, 0x01, 0x03, 0x03, 0x02, 0x05, 0x03, 0x03,
39   0x03, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x11,
40   0x00, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x81, 0x61, 0x71,
41   0x91, 0x13, 0x32, 0xa1, 0x14, 0x22, 0xc1, 0x15, 0x23, 0x52, 0xd1, 0xf0,
42   0xff, 0xda, 0x00, 0x08, 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, 0xb3, 0xa2,
43   0x8a, 0x28, 0xa2, 0x8a, 0x28, 0xa2, 0x97, 0x64, 0x72, 0xd6, 0x58, 0xd4,
44   0x8f, 0xd5, 0x3d, 0xca, 0xa5, 0x7d, 0xa8, 0x4e, 0xaa, 0x3e, 0xb0, 0x3a,
45   0x7a, 0x9d, 0x2b, 0x2d, 0x87, 0x13, 0xe3, 0xaf, 0x9f, 0x0c, 0xb6, 0xb5,
46   0xb6, 0xe2, 0xb4, 0x48, 0x71, 0x30, 0x14, 0x7b, 0x02, 0x09, 0x14, 0xee,
47   0x94, 0x64, 0x38, 0x87, 0x1f, 0x8d, 0x5f, 0xd3, 0x7d, 0xe2, 0xa7, 0x46,
48   0xe8, 0x6c, 0x49, 0x1e, 0xfd, 0x07, 0xb1, 0x33, 0x5f, 0xb8, 0xdc, 0xf5,
49   0x96, 0x51, 0x7c, 0x96, 0xee, 0x14, 0xba, 0x04, 0xf2, 0x2c, 0x42, 0xa3,
50   0xd3, 0x70, 0x7c, 0x1a, 0x6d, 0x45, 0x2c, 0xcd, 0xe5, 0x11, 0x89, 0xb1,
51   0x5b, 0xc4, 0x02, 0xe1, 0xd1, 0xb4, 0xcf, 0xdc, 0xa3, 0xfc, 0x0d, 0xcd,
52   0x41, 0xde, 0xe3, 0xb2, 0x2e, 0xda, 0x1c, 0xbd, 0xe2, 0x4a, 0x90, 0xe9,
53   0x04, 0x95, 0x2b, 0xf7, 0x41, 0xd0, 0x18, 0xe8, 0x36, 0x03, 0xc6, 0x91,
54   0x4a, 0x81, 0x20, 0x82, 0x24, 0x10, 0x66, 0x45, 0x5a, 0xdd, 0xf1, 0x32,
55   0xff, 0x00, 0xa2, 0x5a, 0x8b, 0x62, 0x57, 0x7d, 0x70, 0x80, 0x93, 0x1a,
56   0x94, 0x10, 0x79, 0x4a, 0xa3, 0xb9, 0x20, 0xc0, 0xf3, 0xd3, 0x55, 0x59,
57   0x4e, 0x1c, 0x7a, 0xc7, 0x12, 0x9b, 0xc7, 0x94, 0xa7, 0x2e, 0x14, 0xa9,
58   0x74, 0x0d, 0x42, 0x01, 0x04, 0xc9, 0x3b, 0x93, 0x30, 0x09, 0xdb, 0x5f,
59   0x35, 0x9b, 0x85, 0xad, 0xde, 0x7f, 0x39, 0x6e, 0xa6, 0x82, 0xa1, 0xa5,
60   0x73, 0x2d, 0x40, 0x68, 0x13, 0x06, 0x67, 0xdf, 0x6f, 0x35, 0xe9, 0xb4,
61   0x52, 0x4c, 0xbe, 0x0c, 0x65, 0x72, 0x16, 0xcf, 0x3a, 0xff, 0x00, 0xf6,
62   0x5a, 0xd1, 0x4d, 0x11, 0xa1, 0x13, 0x26, 0x0f, 0x73, 0x00, 0x1f, 0x41,
63   0x5b, 0x72, 0xd6, 0xa2, 0xeb, 0x15, 0x73, 0x6e, 0x13, 0xf7, 0x36, 0x42,
64   0x40, 0xee, 0x04, 0x8f, 0xc8, 0x15, 0xe4, 0xf5, 0x59, 0xc1, 0x16, 0x0d,
65   0x3d, 0x70, 0xed, 0xdb, 0x84, 0x29, 0x6c, 0x40, 0x42, 0x3b, 0x13, 0x3f,
66   0xb8, 0xfc, 0x10, 0x3c, 0xd5, 0xc1, 0x00, 0x88, 0x22, 0x41, 0xef, 0x5f,
67   0x0d, 0xb4, 0x86, 0x81, 0x0d, 0xa1, 0x28, 0x07, 0x52, 0x12, 0x00, 0x06,
68   0xba, 0xd1, 0x45, 0x71, 0xb8, 0xb8, 0x6e, 0xd9, 0x85, 0xbc, 0xf2, 0xb9,
69   0x5b, 0x40, 0x25, 0x47, 0xb0, 0xaf, 0x24, 0x79, 0x48, 0x53, 0xce, 0x29,
70   0xb4, 0x94, 0xa0, 0xa8, 0x94, 0xa7, 0xb0, 0x27, 0x41, 0xf1, 0x54, 0x9c,
71   0x09, 0x71, 0xc9, 0x92, 0x7d, 0x82, 0x74, 0x75, 0xa9, 0x1e, 0xa5, 0x27,
72   0xfd, 0x13, 0x55, 0xd9, 0x3c, 0x83, 0x38, 0xbb, 0x45, 0x5c, 0xbf, 0x3c,
73   0xa1, 0x40, 0x40, 0x02, 0x54, 0x4f, 0x41, 0x3e, 0x4f, 0x83, 0x5a, 0x2d,
74   0xae, 0x1b, 0xba, 0xb7, 0x6d, 0xf6, 0x89, 0x28, 0x71, 0x21, 0x49, 0x24,
75   0x41, 0x83, 0xe9, 0x5d, 0xe9, 0x3f, 0x12, 0x64, 0x97, 0x8b, 0xc6, 0x17,
76   0x59, 0x03, 0xea, 0xa9, 0x61, 0x08, 0x24, 0x48, 0x04, 0xc9, 0x9f, 0x80,
77   0x6a, 0x4b, 0x17, 0xc4, 0xd7, 0xac, 0xdf, 0x36, 0xab, 0xbb, 0x85, 0x39,
78   0x6e, 0xb5, 0x00, 0xe0, 0x50, 0x06, 0x01, 0x3a, 0x91, 0x02, 0x44, 0x6f,
79   0x02, 0xb7, 0x65, 0xb2, 0x0e, 0x71, 0x25, 0xf3, 0x58, 0xdc, 0x72, 0x8f,
80   0xe9, 0xc1, 0xe6, 0x52, 0xe0, 0xc2, 0xa3, 0x72, 0x46, 0xe0, 0x0e, 0x83,
81   0xa9, 0xf1, 0x4b, 0x38, 0x9f, 0x16, 0xde, 0x2e, 0xfd, 0xa4, 0x32, 0x92,
82   0x1a, 0x5b, 0x40, 0x83, 0xdc, 0x8d, 0x0f, 0x9d, 0x01, 0x3e, 0xf5, 0x83,
83   0x17, 0x7a, 0xac, 0x76, 0x41, 0x9b, 0xa4, 0xa7, 0x9b, 0xe9, 0xab, 0x51,
84   0x31, 0x20, 0x82, 0x08, 0xf8, 0x34, 0xf9, 0xdb, 0x97, 0xb8, 0xb7, 0x2c,
85   0xcd, 0xba, 0x12, 0xa6, 0xec, 0xda, 0x3c, 0xca, 0x13, 0x24, 0x0e, 0xa4,
86   0x91, 0xa4, 0x9d, 0x87, 0x69, 0xf7, 0xab, 0x74, 0x21, 0x2d, 0xa1, 0x28,
87   0x4a, 0x42, 0x52, 0x90, 0x02, 0x40, 0xd8, 0x01, 0xb0, 0xae, 0x95, 0x87,
88   0x29, 0x8f, 0x6f, 0x27, 0x64, 0xe5, 0xb3, 0xc4, 0x80, 0xa8, 0x29, 0x50,
89   0xdd, 0x24, 0x6c, 0x47, 0xfd, 0xd4, 0xd4, 0x92, 0x78, 0x1e, 0xef, 0xeb,
90   0x42, 0xae, 0x98, 0x0d, 0x7f, 0x90, 0x0a, 0x27, 0xe2, 0x00, 0xfc, 0xd5,
91   0x4e, 0x27, 0x11, 0x6d, 0x89, 0x60, 0xb7, 0x6e, 0x92, 0x54, 0xa8, 0xe6,
92   0x71, 0x5b, 0xa8, 0xff, 0x00, 0x03, 0xd2, 0xbe, 0x73, 0x38, 0x86, 0x72,
93   0xf6, 0xc1, 0xb7, 0x4f, 0x2a, 0xd1, 0x25, 0x0e, 0x01, 0x25, 0x27, 0xae,
94   0x9d, 0x41, 0xed, 0xed, 0x52, 0xe8, 0xe0, 0x7b, 0xcf, 0xab, 0x0e, 0x5d,
95   0x30, 0x96, 0xe7, 0xee, 0x4f, 0x31, 0x57, 0xc1, 0x00, 0x7e, 0x6a, 0xaf,
96   0x15, 0x8b, 0x63, 0x17, 0x6e, 0x1a, 0x61, 0x24, 0x92, 0x65, 0x6b, 0x3b,
97   0xa8, 0xf7, 0x3e, 0x9d, 0x87, 0x4a, 0x63, 0x45, 0x14, 0x51, 0x45, 0x14,
98   0x51, 0x45, 0x7f, 0xff, 0xd9
99 };
100 
101 /* length of data in image_jpg */
102 static const unsigned int image_jpg_len = sizeof(image_jpg);
103 
104 /* dimensions of image */
105 static const unsigned int image_jpg_x = 64;
106 static const unsigned int image_jpg_y = 64;
107 
108 /* start of JPEG image data section */
109 static const unsigned int image_data_offset = 20;
110 #define image_data_len (image_jpg_len - image_data_offset)
111 
112 /* raw EXIF header data */
113 static const unsigned char exif_header[] = {
114   0xff, 0xd8, 0xff, 0xe1
115 };
116 /* length of data in exif_header */
117 static const unsigned int exif_header_len = sizeof(exif_header);
118 
119 /* byte order to use in the EXIF block */
120 #define FILE_BYTE_ORDER EXIF_BYTE_ORDER_INTEL
121 
122 /* comment to write into the EXIF block */
123 #define FILE_COMMENT "libexif demonstration image"
124 
125 /* special header required for EXIF_TAG_USER_COMMENT */
126 #define ASCII_COMMENT "ASCII\0\0\0"
127 
128 
129 /* Get an existing tag, or create one if it doesn't exist */
init_tag(ExifData * exif,ExifIfd ifd,ExifTag tag)130 static ExifEntry *init_tag(ExifData *exif, ExifIfd ifd, ExifTag tag)
131 {
132 	ExifEntry *entry;
133 	/* Return an existing tag if one exists */
134 	if (!((entry = exif_content_get_entry (exif->ifd[ifd], tag)))) {
135 	    /* Allocate a new entry */
136 	    entry = exif_entry_new ();
137 	    assert(entry != NULL); /* catch an out of memory condition */
138 	    entry->tag = tag; /* tag must be set before calling
139 				 exif_content_add_entry */
140 
141 	    /* Attach the ExifEntry to an IFD */
142 	    exif_content_add_entry (exif->ifd[ifd], entry);
143 
144 	    /* Allocate memory for the entry and fill with default data */
145 	    exif_entry_initialize (entry, tag);
146 
147 	    /* Ownership of the ExifEntry has now been passed to the IFD.
148 	     * One must be very careful in accessing a structure after
149 	     * unref'ing it; in this case, we know "entry" won't be freed
150 	     * because the reference count was bumped when it was added to
151 	     * the IFD.
152 	     */
153 	    exif_entry_unref(entry);
154 	}
155 	return entry;
156 }
157 
158 /* Create a brand-new tag with a data field of the given length, in the
159  * given IFD. This is needed when exif_entry_initialize() isn't able to create
160  * this type of tag itself, or the default data length it creates isn't the
161  * correct length.
162  */
create_tag(ExifData * exif,ExifIfd ifd,ExifTag tag,size_t len)163 static ExifEntry *create_tag(ExifData *exif, ExifIfd ifd, ExifTag tag, size_t len)
164 {
165 	void *buf;
166 	ExifEntry *entry;
167 
168 	/* Create a memory allocator to manage this ExifEntry */
169 	ExifMem *mem = exif_mem_new_default();
170 	assert(mem != NULL); /* catch an out of memory condition */
171 
172 	/* Create a new ExifEntry using our allocator */
173 	entry = exif_entry_new_mem (mem);
174 	assert(entry != NULL);
175 
176 	/* Allocate memory to use for holding the tag data */
177 	buf = exif_mem_alloc(mem, len);
178 	assert(buf != NULL);
179 
180 	/* Fill in the entry */
181 	entry->data = buf;
182 	entry->size = len;
183 	entry->tag = tag;
184 	entry->components = len;
185 	entry->format = EXIF_FORMAT_UNDEFINED;
186 
187 	/* Attach the ExifEntry to an IFD */
188 	exif_content_add_entry (exif->ifd[ifd], entry);
189 
190 	/* The ExifMem and ExifEntry are now owned elsewhere */
191 	exif_mem_unref(mem);
192 	exif_entry_unref(entry);
193 
194 	return entry;
195 }
196 
main(void)197 int main (void)
198 {
199 	int rc = 1;
200 	FILE *f;
201 	unsigned char *exif_data;
202 	unsigned int exif_data_len;
203 	ExifEntry *entry;
204 	ExifData *exif = exif_data_new();
205 	if (!exif) {
206 		fprintf(stderr, "Out of memory\n");
207 		return 2;
208 	}
209 
210 	/* Set the image options */
211 	exif_data_set_option(exif, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
212 	exif_data_set_data_type(exif, EXIF_DATA_TYPE_COMPRESSED);
213 	exif_data_set_byte_order(exif, FILE_BYTE_ORDER);
214 
215 	/* Create the mandatory EXIF fields with default data */
216 	exif_data_fix(exif);
217 
218 	/* All these tags are created with default values by exif_data_fix() */
219 	/* Change the data to the correct values for this image. */
220 	entry = init_tag(exif, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION);
221 	exif_set_long(entry->data, FILE_BYTE_ORDER, image_jpg_x);
222 
223 	entry = init_tag(exif, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION);
224 	exif_set_long(entry->data, FILE_BYTE_ORDER, image_jpg_y);
225 
226 	entry = init_tag(exif, EXIF_IFD_EXIF, EXIF_TAG_COLOR_SPACE);
227 	exif_set_short(entry->data, FILE_BYTE_ORDER, 1);
228 
229 	/* Create a EXIF_TAG_USER_COMMENT tag. This one must be handled
230 	 * differently because that tag isn't automatically created and
231 	 * allocated by exif_data_fix(), nor can it be created using
232 	 * exif_entry_initialize() so it must be explicitly allocated here.
233 	 */
234 	entry = create_tag(exif, EXIF_IFD_EXIF, EXIF_TAG_USER_COMMENT,
235 			sizeof(ASCII_COMMENT) + sizeof(FILE_COMMENT) - 2);
236 	/* Write the special header needed for a comment tag */
237 	memcpy(entry->data, ASCII_COMMENT, sizeof(ASCII_COMMENT)-1);
238 	/* Write the actual comment text, without the trailing NUL character */
239 	memcpy(entry->data+8, FILE_COMMENT, sizeof(FILE_COMMENT)-1);
240 	/* create_tag() happens to set the format and components correctly for
241 	 * EXIF_TAG_USER_COMMENT, so there is nothing more to do. */
242 
243 	/* Create a EXIF_TAG_SUBJECT_AREA tag */
244 	entry = create_tag(exif, EXIF_IFD_EXIF, EXIF_TAG_SUBJECT_AREA,
245 			   4 * exif_format_get_size(EXIF_FORMAT_SHORT));
246 	entry->format = EXIF_FORMAT_SHORT;
247 	entry->components = 4;
248 	exif_set_short(entry->data, FILE_BYTE_ORDER, image_jpg_x / 2);
249 	exif_set_short(entry->data+2, FILE_BYTE_ORDER, image_jpg_y / 2);
250 	exif_set_short(entry->data+4, FILE_BYTE_ORDER, image_jpg_x);
251 	exif_set_short(entry->data+6, FILE_BYTE_ORDER, image_jpg_y);
252 
253 	/* Get a pointer to the EXIF data block we just created */
254 	exif_data_save_data(exif, &exif_data, &exif_data_len);
255 	assert(exif_data != NULL);
256 
257 	f = fopen(FILE_NAME, "wb");
258 	if (!f) {
259 		fprintf(stderr, "Error creating file %s\n", FILE_NAME);
260 		exif_data_unref(exif);
261 		return rc;
262 	}
263 	/* Write EXIF header */
264 	if (fwrite(exif_header, exif_header_len, 1, f) != 1) {
265 		fprintf(stderr, "Error writing to file %s\n", FILE_NAME);
266 		goto errout;
267 	}
268 	/* Write EXIF block length in big-endian order */
269 	if (fputc((exif_data_len+2) >> 8, f) < 0) {
270 		fprintf(stderr, "Error writing to file %s\n", FILE_NAME);
271 		goto errout;
272 	}
273 	if (fputc((exif_data_len+2) & 0xff, f) < 0) {
274 		fprintf(stderr, "Error writing to file %s\n", FILE_NAME);
275 		goto errout;
276 	}
277 	/* Write EXIF data block */
278 	if (fwrite(exif_data, exif_data_len, 1, f) != 1) {
279 		fprintf(stderr, "Error writing to file %s\n", FILE_NAME);
280 		goto errout;
281 	}
282 	/* Write JPEG image data, skipping the non-EXIF header */
283 	if (fwrite(image_jpg+image_data_offset, image_data_len, 1, f) != 1) {
284 		fprintf(stderr, "Error writing to file %s\n", FILE_NAME);
285 		goto errout;
286 	}
287 	printf("Wrote file %s\n", FILE_NAME);
288 	rc = 0;
289 
290 errout:
291 	if (fclose(f)) {
292 		fprintf(stderr, "Error writing to file %s\n", FILE_NAME);
293 		rc = 1;
294 	}
295 	/* The allocator we're using for ExifData is the standard one, so use
296 	 * it directly to free this pointer.
297 	 */
298 	free(exif_data);
299 	exif_data_unref(exif);
300 
301 	return rc;
302 }
303