xref: /third_party/libexif/test/test-gps.c (revision b8bc0d8a)
1/* test-gps.c
2 *
3 * Creates ExifEntries for the GPS ifd and initializes them.
4 * Checks for formats and component counts.
5 *
6 * Copyright 2020 Heiko Lewin <hlewin@gmx.de>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA  02110-1301  USA.
22 */
23
24#include <libexif/exif-utils.h>
25#include <libexif/exif-data.h>
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdint.h>
31
32/*
33 * List of tags to test,
34 */
35const uint16_t test_tags [] = {
36 EXIF_TAG_GPS_VERSION_ID,
37 EXIF_TAG_GPS_LATITUDE_REF,
38 EXIF_TAG_GPS_LATITUDE,
39 EXIF_TAG_GPS_LONGITUDE_REF,
40 EXIF_TAG_GPS_LONGITUDE,
41 EXIF_TAG_GPS_ALTITUDE_REF,
42 EXIF_TAG_GPS_ALTITUDE,
43 EXIF_TAG_GPS_TIME_STAMP,
44 EXIF_TAG_GPS_SATELLITES,
45 EXIF_TAG_GPS_STATUS,
46 EXIF_TAG_GPS_MEASURE_MODE,
47 EXIF_TAG_GPS_DOP,
48 EXIF_TAG_GPS_SPEED_REF,
49 EXIF_TAG_GPS_SPEED,
50 EXIF_TAG_GPS_TRACK_REF,
51 EXIF_TAG_GPS_TRACK,
52 EXIF_TAG_GPS_IMG_DIRECTION_REF,
53 EXIF_TAG_GPS_IMG_DIRECTION,
54 EXIF_TAG_GPS_MAP_DATUM,
55 EXIF_TAG_GPS_DEST_LATITUDE_REF,
56 EXIF_TAG_GPS_DEST_LATITUDE,
57 EXIF_TAG_GPS_DEST_LONGITUDE_REF,
58 EXIF_TAG_GPS_DEST_LONGITUDE,
59 EXIF_TAG_GPS_DEST_BEARING_REF,
60 EXIF_TAG_GPS_DEST_BEARING,
61 EXIF_TAG_GPS_DEST_DISTANCE_REF,
62 EXIF_TAG_GPS_DEST_DISTANCE,
63 EXIF_TAG_GPS_PROCESSING_METHOD,
64 EXIF_TAG_GPS_AREA_INFORMATION,
65 EXIF_TAG_GPS_DATE_STAMP,
66 EXIF_TAG_GPS_DIFFERENTIAL,
67 EXIF_TAG_GPS_H_POSITIONING_ERROR,
68 0xFFFFu
69};
70
71
72/*
73 * Verify that the entry is properly initialized.
74 */
75static int check_entry_format(ExifEntry *e)
76{
77	if(e->tag > EXIF_TAG_GPS_H_POSITIONING_ERROR) {
78		/* unknown tags should get EXIF_FORMAT_UNDEFINED, no size and no data */
79		if(e->format != EXIF_FORMAT_UNDEFINED || e->size || e->components || e->data) {
80		    fprintf(stderr, "check_entry_format: Unknown tag not handled correctly (tag=%x)\n", e->tag);
81		    return 1;
82		}
83		return 0;
84	}
85	switch(e->format) {
86	case EXIF_FORMAT_UNDEFINED:
87	case EXIF_FORMAT_ASCII:
88		/* entries with ASCII or UNDEFINED format do not necessarily need to have the component count set.
89		   only check here is, if component count is set, the size should match the count */
90		if(e->size != e->components) {
91			fprintf (stderr, "check_entry_format: Entry has bad component count or size (tag=%x)\n", e->tag);
92			return 1;
93		}
94		break;
95
96	default:
97		/* All other formats should have a nonzero component count. */
98		if(!e->components) {
99			fprintf (stderr, "check_entry_format: Entry should have component count set (tag=%x)\n", e->tag);
100			return 1;
101		}
102		return 0;
103	}
104
105	/* If a value is present the size should be set to the right value */
106	if(e->data && e->size != e->components * exif_format_get_size(e->format)) {
107		fprintf (stderr, "check_entry_format: Entry has bad size (tag=%x)\n", e->tag);
108		return 1;
109	}
110	return 0;
111}
112
113int
114main ()
115{
116	size_t i;
117	ExifData *data = NULL;
118	ExifEntry *e = NULL;
119
120	data = exif_data_new ();
121	if (!data) {
122		fprintf (stderr, "Error running exif_data_new()\n");
123		goto ERROR_EXIT;
124	}
125
126	/* Run tests */
127	for (i=0; i < sizeof(test_tags)/sizeof(test_tags[0]); ++i) {
128		e = exif_entry_new ();
129		if (!e) {
130			fprintf (stderr, "Error running exif_entry_new()\n");
131			goto ERROR_EXIT;
132		}
133		exif_content_add_entry(data->ifd[EXIF_IFD_GPS], e);
134		exif_entry_initialize (e, (ExifTag)test_tags[i]);
135		if(check_entry_format(e)) goto ERROR_EXIT;
136		exif_content_remove_entry (data->ifd[EXIF_IFD_GPS], e);
137		exif_entry_unref (e);
138	}
139	exif_data_unref(data);
140	return 0;
141ERROR_EXIT:
142	exif_entry_unref (e);
143	exif_data_unref (data);
144	exit(EXIT_FAILURE);
145}
146
147