Do not use functions deprecated in Qt 6.8
[GPXSee.git] / src / data / exifparser.cpp
blobf84c59cdd33707167e177df574dc54fddeb1b85e
1 #include <QDataStream>
2 #include <QTimeZone>
3 #include "common/tifffile.h"
4 #include "common/util.h"
5 #include "exifparser.h"
8 #define SOI_MARKER 0xFFD8
9 #define APP1_MARKER 0xFFE1
11 #define GPSIFDTag 34853
12 #define ImageDescription 270
14 #define GPSLatitudeRef 1
15 #define GPSLatitude 2
16 #define GPSLongitudeRef 3
17 #define GPSLongitude 4
18 #define GPSAltitudeRef 5
19 #define GPSAltitude 6
20 #define GPSTimeStamp 7
21 #define GPSDateStamp 29
24 QString EXIFParser::text(TIFFFile &file, const IFDEntry &e) const
26 if (e.type != TIFF_ASCII || !e.count)
27 return QString();
29 if (e.count <= sizeof(e.offset))
30 return QString(QByteArray((const char *)&e.offset, sizeof(e.offset)));
32 if (!file.seek(e.offset))
33 return QString();
35 QByteArray str(file.read(e.count));
36 if (str.size() < (int)e.count)
37 return QString();
39 return QString(str);
42 QTime EXIFParser::time(TIFFFile &file, const IFDEntry &ts) const
44 if (!(ts.type == TIFF_RATIONAL && ts.count == 3))
45 return QTime();
47 if (!file.seek(ts.offset))
48 return QTime();
50 double hms[3];
51 for (int i = 0; i < 3; i++) {
52 quint32 num, den;
53 if (!file.readValue(num))
54 return QTime();
55 if (!file.readValue(den))
56 return QTime();
58 hms[i] = num/(double)den;
61 return QTime((int)hms[0], (int)hms[1], (int)hms[2]);
64 double EXIFParser::altitude(TIFFFile &file, const IFDEntry &alt,
65 const IFDEntry &altRef) const
67 if (!(alt.type == TIFF_RATIONAL && alt.count == 1))
68 return NAN;
70 if (!file.seek(alt.offset))
71 return NAN;
73 quint32 num, den;
74 if (!file.readValue(num))
75 return NAN;
76 if (!file.readValue(den))
77 return NAN;
79 return (altRef.type == TIFF_BYTE && altRef.count == 1 && altRef.offset)
80 ? -(num/(double)den) : num/(double)den;
83 double EXIFParser::coordinate(TIFFFile &file, const IFDEntry &ll) const
85 // Some broken image creators like NOKIA phones use a wrong (SRATIONAL)
86 // data type
87 if (!((ll.type == TIFF_RATIONAL || ll.type == TIFF_SRATIONAL)
88 && ll.count == 3))
89 return NAN;
91 if (!file.seek(ll.offset))
92 return NAN;
94 double dms[3];
95 for (int i = 0; i < 3; i++) {
96 quint32 num, den;
97 if (!file.readValue(num))
98 return NAN;
99 if (!file.readValue(den))
100 return NAN;
102 dms[i] = num/(double)den;
105 return dms[0] + dms[1]/60 + dms[2]/3600;
108 Coordinates EXIFParser::coordinates(TIFFFile &file, const IFDEntry &lon,
109 const IFDEntry &lonRef, const IFDEntry &lat, const IFDEntry &latRef) const
111 if (!(latRef.type == TIFF_ASCII && latRef.count == 2
112 && lonRef.type == TIFF_ASCII && lonRef.count == 2))
113 return Coordinates();
115 Coordinates c(coordinate(file, lon), coordinate(file, lat));
116 if (!c.isValid())
117 return Coordinates();
119 char ew = file.isBE() ? lonRef.offset >> 24 : lonRef.offset;
120 char ns = file.isBE() ? latRef.offset >> 24 : latRef.offset;
122 if (ew == 'W')
123 c.rlon() = -c.lon();
124 if (ns == 'S')
125 c.rlat() = -c.lat();
127 return c;
130 bool EXIFParser::readEntry(TIFFFile &file, const QSet<quint16> &tags,
131 QMap<quint16, IFDEntry> &entries) const
133 IFDEntry entry;
134 quint16 tag;
136 if (!file.readValue(tag))
137 return false;
138 if (!file.readValue(entry.type))
139 return false;
140 if (!file.readValue(entry.count))
141 return false;
142 if (!file.readValue(entry.offset))
143 return false;
145 if (tags.contains(tag))
146 entries.insert(tag, entry);
148 return true;
151 bool EXIFParser::readIFD(TIFFFile &file, quint32 offset,
152 const QSet<quint16> &tags, QMap<quint16, IFDEntry> &entries) const
154 quint16 count;
156 if (!file.seek(offset))
157 return false;
158 if (!file.readValue(count))
159 return false;
161 for (quint16 i = 0; i < count; i++)
162 if (!readEntry(file, tags, entries))
163 return false;
165 return true;
168 bool EXIFParser::parseTIFF(QFile *file, QVector<Waypoint> &waypoints)
170 TIFFFile tiff(file);
171 if (!tiff.isValid()) {
172 _errorString = "Invalid EXIF data";
173 return false;
176 QSet<quint16> IFD0Tags;
177 IFD0Tags << GPSIFDTag << ImageDescription;
178 QMap<quint16, IFDEntry> IFD0;
179 for (quint32 ifd = tiff.ifd(); ifd; ) {
180 if (!readIFD(tiff, ifd, IFD0Tags, IFD0) || !tiff.readValue(ifd)) {
181 _errorString = "Invalid IFD0";
182 return false;
185 if (!IFD0.contains(GPSIFDTag)) {
186 _errorString = "GPS IFD not found";
187 return false;
190 QSet<quint16> GPSIFDTags;
191 GPSIFDTags << GPSLatitude << GPSLongitude << GPSLatitudeRef
192 << GPSLongitudeRef << GPSAltitude << GPSAltitudeRef << GPSDateStamp
193 << GPSTimeStamp;
194 QMap<quint16, IFDEntry> GPSIFD;
195 for (quint32 ifd = IFD0.value(GPSIFDTag).offset; ifd; ) {
196 if (!readIFD(tiff, ifd, GPSIFDTags, GPSIFD) || !tiff.readValue(ifd)) {
197 _errorString = "Invalid GPS IFD";
198 return false;
202 Coordinates c(coordinates(tiff, GPSIFD.value(GPSLongitude),
203 GPSIFD.value(GPSLongitudeRef), GPSIFD.value(GPSLatitude),
204 GPSIFD.value(GPSLatitudeRef)));
205 if (!c.isValid()) {
206 _errorString = "Invalid/missing GPS coordinates";
207 return false;
210 Waypoint wp(c);
211 wp.setName(Util::file2name(file->fileName()));
212 wp.addImage(file->fileName());
213 wp.setElevation(altitude(tiff, GPSIFD.value(GPSAltitude),
214 GPSIFD.value(GPSAltitudeRef)));
215 wp.setTimestamp(QDateTime(QDate::fromString(text(tiff,
216 GPSIFD.value(GPSDateStamp)), "yyyy:MM:dd"), time(tiff,
217 GPSIFD.value(GPSTimeStamp)), QTimeZone::utc()));
218 wp.setDescription(text(tiff, IFD0.value(ImageDescription)).trimmed());
220 waypoints.append(wp);
222 return true;
225 bool EXIFParser::parse(QFile *file, QList<TrackData> &tracks,
226 QList<RouteData> &routes, QList<Area> &polygons,
227 QVector<Waypoint> &waypoints)
229 Q_UNUSED(tracks);
230 Q_UNUSED(routes);
231 Q_UNUSED(polygons);
232 quint16 marker;
234 QDataStream stream(file);
235 stream.setByteOrder(QDataStream::BigEndian);
236 stream >> marker;
237 if (marker != SOI_MARKER) {
238 _errorString = "Not a JPEG file";
239 return false;
242 while (!stream.atEnd()) {
243 stream >> marker;
244 if (marker == APP1_MARKER) {
245 quint16 size;
246 char magic[6];
247 stream >> size;
248 if (stream.readRawData(magic, sizeof(magic)) == sizeof(magic) &&
249 !memcmp(magic, "Exif\0\0", sizeof(magic)))
250 return parseTIFF(file, waypoints);
251 else
252 break;
256 _errorString = "No EXIF data found";
257 return false;