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