AstRawDark
Manage your astrophoto dark frames
 All Classes Functions Variables Enumerations Enumerator Pages
exifReader.cpp
1 /*
2  * AstRawDark
3  *
4  * Copyright (C) 2015 - Frédéric CORNU
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "exifReader.h"
21 #include "data/dto/imageInfo.h"
22 
23 #include <QStringList>
24 #include <QString>
25 
26 #ifndef QT_NO_DEBUG
27 #include <QDebug>
28 #endif
29 
30 ExifReader::ExifReader()
31 {
32 
33 }
34 
36 {
37  try {
38 
39  // Read EXIF bulk data from image
40  Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(imageInfo.getPath().toStdString());
41  image->readMetadata();
42  Exiv2::ExifData &exifData = image->exifData();
43 
44  if (exifData.empty()) {
45 
46 #ifndef QT_NO_DEBUG
47  qWarning() << imageInfo.getPath() << ": No Exif data found in the file";
48 #endif
49 
50 
51  } else {
52 
53  // extract relevant EXIF values
54  imageInfo.setMake(getValue(exifData, "Exif.Image.Make"));
55  imageInfo.setModel(getValue(exifData, "Exif.Image.Model"));
56 
57  imageInfo.setExposure(formatExposure(getValue(exifData, "Exif.Photo.ExposureTime")));
58 
59  if ( imageInfo.getExposure() == ImageInfo::UNDEFINED ) {
60 
61  return false;
62  }
63 
64 
65  QString tempIso = getValue(exifData, "Exif.Photo.ISOSpeedRatings");
66 
67  if ( tempIso == ImageInfo::NOT_AVAILABLE ) {
68 
69  imageInfo.setIso(ImageInfo::UNDEFINED);
70  return false;
71 
72  } else {
73 
74  imageInfo.setIso(tempIso.toInt());
75  }
76 
77  imageInfo.setDate(getValue(exifData, "Exif.Photo.DateTimeDigitized"));
78 
79  QString tempTemp = getValue(exifData, "Exif.CanonSi.0x000c");
80 
81  if ( tempTemp == ImageInfo::NOT_AVAILABLE ) {
82 
84  return false;
85 
86  } else {
87 
88  imageInfo.setTemperature(tempTemp.toInt() -128);
89  }
90 
91  QString serial = getValue(exifData, "Exif.Photo.BodySerialNumber");
92 
93  if ( ImageInfo::NOT_AVAILABLE == serial ) {
94 
95  serial = getValue(exifData, "Exif.Canon.SerialNumber");
96  }
97 
98  imageInfo.setCameraSerial(serial);
99 
100  }
101 
102  return true;
103 
104  } catch ( Exiv2::Error& e ) {
105 
106 #ifndef QT_NO_DEBUG
107  qWarning() << e.what();
108 #endif
109 
110  return false;
111 
112  }
113 }
114 
115 QString ExifReader::getValue(const Exiv2::ExifData &data, const QString tag)
116 {
117  /*
118  * retrieve value for specific EXIF tag.
119  * return "N/A" if tag is missing
120  */
121  Exiv2::ExifKey exifKey(tag.toStdString());
122  Exiv2::ExifData::const_iterator pos = data.findKey(exifKey);
123 
124  if (pos == data.end()) {
125 
127 
128  } else {
129 
130  return QString(pos->getValue()->toString().c_str());
131  }
132 }
133 
134 int ExifReader::formatExposure(QString expoString)
135 {
136  if ( expoString.isEmpty() || ImageInfo::NOT_AVAILABLE == expoString ) {
137 
138  return ImageInfo::UNDEFINED;
139  }
140 
141  double dRet = 0.0;
142 
143  if ( expoString.contains('/') ) {
144 
145  int num = QString(expoString.split('/').at(0)).toInt();
146  int denom = QString(expoString.split('/').at(1)).toInt();
147 
148  dRet = num / static_cast<double> (denom);
149 
150  } else {
151 
152  dRet = expoString.toDouble();
153  }
154 
155 
156 
157  return qRound(dRet);
158 
159 }
160