Dive pictures: Move metadata functions into own translation unit

Move all metadata function into new core/metadata.cpp file.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-03-15 20:21:40 +01:00 committed by Lubomir I. Ivanov
parent d9df8c3f47
commit 9b2482aca9
7 changed files with 143 additions and 78 deletions

View file

@ -10,7 +10,6 @@
#include "time.h"
#include "gettextfromc.h"
#include <sys/time.h>
#include "exif.h"
#include "prefs-macros.h"
#include <QFile>
#include <QRegExp>
@ -345,72 +344,6 @@ extern "C" xsltStylesheetPtr get_stylesheet(const char *name)
return xslt;
}
// Fetch quint16 in big endian mode from QFile and return 0 on error.
// This is a very specialized function for parsing JPEGs, therefore we can get away with such an in-band error code.
static inline quint16 getShortBE(QFile &f)
{
unsigned char buf[2];
if (f.read(reinterpret_cast<char *>(buf), 2) != 2)
return 0;
return (buf[0] << 8) | buf[1];
}
static int parseExif(const QString &filename, easyexif::EXIFInfo &exif)
{
QFile f { filename };
if (!f.open(QIODevice::ReadOnly))
return PARSE_EXIF_ERROR_NO_JPEG;
if (getShortBE(f) != 0xffd8)
return PARSE_EXIF_ERROR_NO_JPEG;
for (;;) {
switch (getShortBE(f)) {
case 0xffc0:
case 0xffc2:
case 0xffc4:
case 0xffd0 ... 0xffd7:
case 0xffdb:
case 0xffdd:
case 0xffe0:
case 0xffe2 ... 0xffef:
case 0xfffe: {
quint16 len = getShortBE(f);
if (len < 2)
return PARSE_EXIF_ERROR_NO_JPEG;
f.seek(f.pos() + len - 2); // TODO: switch to QFile::skip()
break;
}
case 0xffe1: {
quint16 len = getShortBE(f);
if (len < 2)
return PARSE_EXIF_ERROR_NO_JPEG;
len -= 2;
QByteArray data = f.read(len);
if (data.size() != len)
return PARSE_EXIF_ERROR_NO_JPEG;
return exif.parseFromEXIFSegment(reinterpret_cast<const unsigned char *>(data.constData()), len);
}
case 0xffda:
case 0xffd9:
// We expect EXIF data before any scan data
return PARSE_EXIF_ERROR_NO_EXIF;
default:
return PARSE_EXIF_ERROR_NO_JPEG;
}
}
}
extern "C" timestamp_t picture_get_timestamp(const char *filename)
{
easyexif::EXIFInfo exif;
if (parseExif(localFilePath(QString(filename)), exif) != PARSE_EXIF_SUCCESS) {
// If we couldn't parse EXIF data, use file creation date.
// TODO: QFileInfo::created is deprecated in newer Qt versions.
QDateTime created = QFileInfo(QString(filename)).created();
return created.toMSecsSinceEpoch() / 1000;
}
return exif.epoch();
}
extern "C" char *move_away(const char *old_path)
{
if (verbose > 1)
@ -1336,15 +1269,6 @@ extern "C" void savePictureLocal(struct picture *picture, const char *hash, cons
}
}
extern "C" void picture_load_exif_data(struct picture *p)
{
easyexif::EXIFInfo exif;
if (parseExif(localFilePath(QString(p->filename)), exif) != PARSE_EXIF_SUCCESS)
return;
p->longitude.udeg = lrint(1000000.0 * exif.GeoLocation.Longitude);
p->latitude.udeg = lrint(1000000.0 * exif.GeoLocation.Latitude);
}
QString get_gas_string(struct gasmix gas)
{
uint o2 = (get_o2(&gas) + 5) / 10, he = (get_he(&gas) + 5) / 10;