2020-04-10 07:42:14 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "picture.h"
|
2020-04-19 15:01:58 +00:00
|
|
|
#include "dive.h"
|
2020-11-18 20:49:08 +00:00
|
|
|
#if !defined(SUBSURFACE_MOBILE)
|
2020-04-19 15:01:58 +00:00
|
|
|
#include "metadata.h"
|
2020-11-18 20:49:08 +00:00
|
|
|
#endif
|
2024-05-30 13:00:28 +00:00
|
|
|
#include "range.h"
|
2020-04-11 15:41:56 +00:00
|
|
|
#include "subsurface-string.h"
|
2020-04-10 07:42:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-05-30 13:00:28 +00:00
|
|
|
bool picture::operator<(const struct picture &p) const
|
2020-04-10 07:42:14 +00:00
|
|
|
{
|
2024-05-30 13:00:28 +00:00
|
|
|
return std::tie(offset.seconds, filename) <
|
|
|
|
std::tie(p.offset.seconds, p.filename);
|
2020-04-10 07:42:14 +00:00
|
|
|
}
|
|
|
|
|
2024-05-30 13:00:28 +00:00
|
|
|
void add_picture(picture_table &t, struct picture newpic)
|
2020-04-10 07:42:14 +00:00
|
|
|
{
|
2024-05-30 13:00:28 +00:00
|
|
|
auto it = std::lower_bound(t.begin(), t.end(), newpic);
|
|
|
|
t.insert(it, std::move(newpic));
|
2020-04-11 15:41:56 +00:00
|
|
|
}
|
|
|
|
|
2024-05-30 13:00:28 +00:00
|
|
|
int get_picture_idx(const picture_table &t, const std::string &filename)
|
2020-04-11 15:41:56 +00:00
|
|
|
{
|
|
|
|
|
2024-05-30 13:00:28 +00:00
|
|
|
return index_of_if(t, [&filename] (const picture &p)
|
|
|
|
{ return p.filename == filename; });
|
2020-04-14 17:08:49 +00:00
|
|
|
}
|
2020-04-19 15:01:58 +00:00
|
|
|
|
2024-05-29 05:53:00 +00:00
|
|
|
#if !defined(SUBSURFACE_MOBILE)
|
2020-04-19 15:01:58 +00:00
|
|
|
/* Return distance of timestamp to time of dive. Result is always positive, 0 means during dive. */
|
|
|
|
static timestamp_t time_from_dive(const struct dive *d, timestamp_t timestamp)
|
|
|
|
{
|
2024-06-05 15:02:40 +00:00
|
|
|
timestamp_t end_time = d->endtime();
|
2020-04-19 15:01:58 +00:00
|
|
|
if (timestamp < d->when)
|
|
|
|
return d->when - timestamp;
|
|
|
|
else if (timestamp > end_time)
|
|
|
|
return timestamp - end_time;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return dive closest selected dive to given timestamp or NULL if no dives are selected. */
|
|
|
|
static struct dive *nearest_selected_dive(timestamp_t timestamp)
|
|
|
|
{
|
|
|
|
struct dive *d, *res = NULL;
|
|
|
|
int i;
|
|
|
|
timestamp_t offset, min = 0;
|
|
|
|
|
|
|
|
for_each_dive(i, d) {
|
|
|
|
if (!d->selected)
|
|
|
|
continue;
|
|
|
|
offset = time_from_dive(d, timestamp);
|
|
|
|
if (!res || offset < min) {
|
|
|
|
res = d;
|
|
|
|
min = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We suppose that dives are sorted chronologically. Thus
|
|
|
|
* if the offset starts to increase, we can end. This ignores
|
|
|
|
* pathological cases such as overlapping dives. In such a
|
|
|
|
* case the user will have to add pictures manually.
|
|
|
|
*/
|
|
|
|
if (offset == 0 || offset > min)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only add pictures that have timestamps between 30 minutes before the dive and
|
|
|
|
// 30 minutes after the dive ends
|
2024-05-30 13:00:28 +00:00
|
|
|
static constexpr timestamp_t d30min = 30 * 60;
|
2020-04-19 15:01:58 +00:00
|
|
|
static bool dive_check_picture_time(const struct dive *d, timestamp_t timestamp)
|
|
|
|
{
|
2024-05-30 13:00:28 +00:00
|
|
|
return time_from_dive(d, timestamp) < d30min;
|
2020-04-19 15:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Creates a picture and indicates the dive to which this picture should be added.
|
|
|
|
* The caller is responsible for actually adding the picture to the dive.
|
2024-05-30 13:00:28 +00:00
|
|
|
* If no appropriate dive was found, no picture is created and null is returned.
|
2020-04-19 15:01:58 +00:00
|
|
|
*/
|
2024-05-30 13:00:28 +00:00
|
|
|
std::pair<std::optional<picture>, dive *> create_picture(const std::string &filename, timestamp_t shift_time, bool match_all)
|
2020-04-19 15:01:58 +00:00
|
|
|
{
|
|
|
|
struct metadata metadata;
|
|
|
|
timestamp_t timestamp;
|
|
|
|
|
2024-05-30 13:00:28 +00:00
|
|
|
get_metadata(filename.c_str(), &metadata);
|
2020-04-19 15:01:58 +00:00
|
|
|
timestamp = metadata.timestamp + shift_time;
|
2024-05-30 13:00:28 +00:00
|
|
|
struct dive *dive = nearest_selected_dive(timestamp);
|
|
|
|
|
|
|
|
if (!dive)
|
|
|
|
return { {}, nullptr };
|
|
|
|
if (get_picture_idx(dive->pictures, filename) >= 0)
|
|
|
|
return { {}, nullptr };
|
|
|
|
if (!match_all && !dive_check_picture_time(dive, timestamp))
|
|
|
|
return { {}, nullptr };
|
|
|
|
|
|
|
|
struct picture picture;
|
|
|
|
picture.filename = filename;
|
|
|
|
picture.offset.seconds = metadata.timestamp - dive->when + shift_time;
|
|
|
|
picture.location = metadata.location;
|
|
|
|
return { picture, dive };
|
2020-04-19 15:01:58 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 20:39:14 +00:00
|
|
|
bool picture_check_valid_time(timestamp_t timestamp, timestamp_t shift_time)
|
2020-04-19 15:01:58 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dive *dive;
|
|
|
|
|
|
|
|
for_each_dive (i, dive)
|
|
|
|
if (dive->selected && dive_check_picture_time(dive, timestamp + shift_time))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-18 20:49:08 +00:00
|
|
|
#endif
|