• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libexif example program to extract an EXIF thumbnail from an image
3  * and save it into a new file.
4  *
5  * SPDX-FileCopyrightText: Placed into the public domain by Dan Fandrich
6  * SPDX-License-Identifier: CC0-1.0
7  */
8 
9 #include <stdio.h>
10 #include <libexif/exif-loader.h>
11 
main(int argc,char ** argv)12 int main(int argc, char **argv)
13 {
14     int rc = 1;
15     ExifLoader *l;
16 
17     if (argc < 2) {
18         printf("Usage: %s image.jpg\n", argv[0]);
19         printf("Extracts a thumbnail from the given EXIF image.\n");
20         return rc;
21     }
22 
23     /* Create an ExifLoader object to manage the EXIF loading process */
24     l = exif_loader_new();
25     if (l) {
26         ExifData *ed;
27 
28         /* Load the EXIF data from the image file */
29         exif_loader_write_file(l, argv[1]);
30 
31         /* Get a pointer to the EXIF data */
32         ed = exif_loader_get_data(l);
33 
34 	/* The loader is no longer needed--free it */
35         exif_loader_unref(l);
36 	l = NULL;
37         if (ed) {
38 	    /* Make sure the image had a thumbnail before trying to write it */
39             if (ed->data && ed->size) {
40                 FILE *thumb;
41                 char thumb_name[1024];
42 
43 		/* Try to create a unique name for the thumbnail file */
44                 snprintf(thumb_name, sizeof(thumb_name),
45                          "%s_thumb.jpg", argv[1]);
46 
47                 thumb = fopen(thumb_name, "wb");
48                 if (thumb) {
49 		    /* Write the thumbnail image to the file */
50                     fwrite(ed->data, 1, ed->size, thumb);
51                     fclose(thumb);
52                     printf("Wrote thumbnail to %s\n", thumb_name);
53                     rc = 0;
54                 } else {
55                     printf("Could not create file %s\n", thumb_name);
56                     rc = 2;
57                 }
58             } else {
59                 printf("No EXIF thumbnail in file %s\n", argv[1]);
60                 rc = 1;
61             }
62 	    /* Free the EXIF data */
63             exif_data_unref(ed);
64         }
65     }
66     return rc;
67 }
68