summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-03-15 20:21:40 +0100
committerGravatar Lubomir I. Ivanov <neolit123@gmail.com>2018-04-01 16:04:48 +0300
commit9b2482aca9bed6dc512f52131b54b4db915f549c (patch)
treec53f4c4bf109ae8d146785e82a396ad8128b944a
parentd9df8c3f474b128b1631f1ff58ec2026ab21272d (diff)
downloadsubsurface-9b2482aca9bed6dc512f52131b54b4db915f549c.tar.gz
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>
-rw-r--r--core/CMakeLists.txt1
-rw-r--r--core/dive.c1
-rw-r--r--core/dive.h2
-rw-r--r--core/metadata.cpp109
-rw-r--r--core/metadata.h31
-rw-r--r--core/qthelper.cpp76
-rw-r--r--desktop-widgets/simplewidgets.cpp1
7 files changed, 143 insertions, 78 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt
index 564dfe6ac..34e12c59b 100644
--- a/core/CMakeLists.txt
+++ b/core/CMakeLists.txt
@@ -76,6 +76,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
gettextfromc.cpp
# dirk ported some core functionality to c++.
qthelper.cpp
+ metadata.cpp
divecomputer.cpp
exif.cpp
subsurfacesysinfo.cpp
diff --git a/core/dive.c b/core/dive.c
index b7ce01ea4..ab033858e 100644
--- a/core/dive.c
+++ b/core/dive.c
@@ -11,6 +11,7 @@
#include "device.h"
#include "divelist.h"
#include "qthelper.h"
+#include "metadata.h"
/* one could argue about the best place to have this variable -
* it's used in the UI, but it seems to make the most sense to have it
diff --git a/core/dive.h b/core/dive.h
index a1cb7fb60..62fe31917 100644
--- a/core/dive.h
+++ b/core/dive.h
@@ -433,8 +433,6 @@ extern void dive_add_picture(struct dive *d, struct picture *newpic);
extern bool dive_remove_picture(struct dive *d, const char *filename);
extern unsigned int dive_get_picture_count(struct dive *d);
extern bool picture_check_valid(const char *filename, int shift_time);
-extern void picture_load_exif_data(struct picture *p);
-extern timestamp_t picture_get_timestamp(const char *filename);
extern void dive_set_geodata_from_picture(struct dive *d, struct picture *pic);
extern void picture_free(struct picture *picture);
diff --git a/core/metadata.cpp b/core/metadata.cpp
new file mode 100644
index 000000000..bf4bfcb90
--- /dev/null
+++ b/core/metadata.cpp
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "metadata.h"
+#include "exif.h"
+#include "qthelper.h"
+#include <QString>
+#include <QFile>
+#include <QDateTime>
+
+// 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 bool parseExif(QFile &f, struct metadata *metadata)
+{
+ if (getShortBE(f) != 0xffd8)
+ return false;
+ 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 false;
+ f.seek(f.pos() + len - 2); // TODO: switch to QFile::skip()
+ break;
+ }
+ case 0xffe1: {
+ quint16 len = getShortBE(f);
+ if (len < 2)
+ return false;
+ len -= 2;
+ QByteArray data = f.read(len);
+ if (data.size() != len)
+ return false;
+ easyexif::EXIFInfo exif;
+ if (exif.parseFromEXIFSegment(reinterpret_cast<const unsigned char *>(data.constData()), len) != PARSE_EXIF_SUCCESS)
+ return false;
+ metadata->longitude.udeg = lrint(1000000.0 * exif.GeoLocation.Longitude);
+ metadata->latitude.udeg = lrint(1000000.0 * exif.GeoLocation.Latitude);
+ metadata->timestamp = exif.epoch();
+ return true;
+ }
+ case 0xffda:
+ case 0xffd9:
+ // We expect EXIF data before any scan data
+ return false;
+ default:
+ return false;
+ }
+ }
+}
+
+static bool parseMP4(QFile &, metadata *)
+{
+ // TODO: Implement MP4 parsing
+ return false;
+}
+
+extern "C" mediatype_t get_metadata(const char *filename_in, metadata *data)
+{
+ data->timestamp = 0;
+ data->latitude.udeg = 0;
+ data->longitude.udeg = 0;
+
+ QString filename = localFilePath(QString(filename_in));
+ QFile f(filename);
+ if (!f.open(QIODevice::ReadOnly))
+ return MEDIATYPE_IO_ERROR;
+
+ if (parseExif(f, data)) {
+ return MEDIATYPE_PICTURE;
+ } else if(parseMP4(f, data)) {
+ return MEDIATYPE_VIDEO;
+ } else {
+ // If we couldn't parse EXIF or MP4 data, use file creation date.
+ // TODO: QFileInfo::created is deprecated in newer Qt versions.
+ data->timestamp = QFileInfo(filename).created().toMSecsSinceEpoch() / 1000;
+ return MEDIATYPE_UNKNOWN;
+ }
+}
+
+extern "C" timestamp_t picture_get_timestamp(const char *filename)
+{
+ struct metadata data;
+ get_metadata(filename, &data);
+ return data.timestamp;
+}
+
+extern "C" void picture_load_exif_data(struct picture *p)
+{
+ struct metadata data;
+ if (get_metadata(p->filename, &data) == MEDIATYPE_IO_ERROR)
+ return;
+ p->longitude = data.longitude;
+ p->latitude = data.latitude;
+}
diff --git a/core/metadata.h b/core/metadata.h
new file mode 100644
index 000000000..b5af39b30
--- /dev/null
+++ b/core/metadata.h
@@ -0,0 +1,31 @@
+#ifndef METADATA_H
+#define METADATA_H
+
+#include "units.h"
+
+struct metadata {
+ timestamp_t timestamp;
+ degrees_t latitude;
+ degrees_t longitude;
+};
+
+enum mediatype_t {
+ MEDIATYPE_IO_ERROR, // Couldn't read file
+ MEDIATYPE_UNKNOWN, // Couldn't identify file
+ MEDIATYPE_PICTURE,
+ MEDIATYPE_VIDEO,
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum mediatype_t get_metadata(const char *filename, struct metadata *data);
+timestamp_t picture_get_timestamp(const char *filename);
+void picture_load_exif_data(struct picture *p);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // METADATA_H
diff --git a/core/qthelper.cpp b/core/qthelper.cpp
index 93c95ab5e..1cef38184 100644
--- a/core/qthelper.cpp
+++ b/core/qthelper.cpp
@@ -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;
diff --git a/desktop-widgets/simplewidgets.cpp b/desktop-widgets/simplewidgets.cpp
index 48e30036a..2566c7b12 100644
--- a/desktop-widgets/simplewidgets.cpp
+++ b/desktop-widgets/simplewidgets.cpp
@@ -20,6 +20,7 @@
#include "profile-widget/profilewidget2.h"
#include "desktop-widgets/undocommands.h"
#include "core/qthelper.h"
+#include "core/metadata.h"
class MinMaxAvgWidgetPrivate {
public: