diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-04-19 17:01:58 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-05-06 13:58:09 -0700 |
commit | 6ae2d36e381b4f676eb9d8c0e06245989ef16383 (patch) | |
tree | 169e0b4119fc9c549a32528952215cd78a64221d /core | |
parent | 74f03e3537ee530000d4b11013bb12de91cb21dd (diff) | |
download | subsurface-6ae2d36e381b4f676eb9d8c0e06245989ef16383.tar.gz |
core: move picture-related function from dive.c to picture.c
Move the two functions create_picture() and picture_check_valid_time()
from dive.c to picture.c.
This might be somewhat questionable, as these functions are not purely
picture related, but check the nearest selected dives, etc. However,
dive.c is so huge, that slimming it down can't hurt. Moreover,
getting the nearest selected dive is more divelist- than dive
functionality anyway.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core')
-rw-r--r-- | core/dive.c | 86 | ||||
-rw-r--r-- | core/dive.h | 8 | ||||
-rw-r--r-- | core/picture.c | 89 | ||||
-rw-r--r-- | core/picture.h | 12 |
4 files changed, 100 insertions, 95 deletions
diff --git a/core/dive.c b/core/dive.c index 5ac008e8f..d5ffc162b 100644 --- a/core/dive.c +++ b/core/dive.c @@ -14,7 +14,6 @@ #include "divesite.h" #include "errorhelper.h" #include "qthelper.h" -#include "metadata.h" #include "membuffer.h" #include "picture.h" #include "tag.h" @@ -3498,64 +3497,6 @@ void set_git_prefs(const char *prefs) git_prefs.pp_graphs.po2 = 1; } -/* 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) -{ - timestamp_t end_time = dive_endtime(d); - if (timestamp < d->when) - return d->when - timestamp; - else if (timestamp > end_time) - return timestamp - end_time; - else - return 0; -} - -// only add pictures that have timestamps between 30 minutes before the dive and -// 30 minutes after the dive ends -#define D30MIN (30 * 60) -static bool dive_check_picture_time(const struct dive *d, timestamp_t timestamp) -{ - return time_from_dive(d, timestamp) < D30MIN; -} - -/* 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; -} - -bool picture_check_valid_time(timestamp_t timestamp, int shift_time) -{ - 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; -} - void dive_set_geodata_from_picture(struct dive *dive, struct picture *picture, struct dive_site_table *table) { struct dive_site *ds = dive->dive_site; @@ -3570,33 +3511,6 @@ void dive_set_geodata_from_picture(struct dive *dive, struct picture *picture, s } } -/* 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. - * If no appropriate dive was found, no picture is created and NULL is returned. - */ -struct picture *create_picture(const char *filename, int shift_time, bool match_all, struct dive **dive) -{ - struct metadata metadata; - timestamp_t timestamp; - - get_metadata(filename, &metadata); - timestamp = metadata.timestamp + shift_time; - *dive = nearest_selected_dive(timestamp); - - if (!*dive) - return NULL; - if (get_picture_idx(&(*dive)->pictures, filename) >= 0) - return NULL; - if (!match_all && !dive_check_picture_time(*dive, timestamp)) - return NULL; - - struct picture *picture = malloc(sizeof(struct picture)); - picture->filename = strdup(filename); - picture->offset.seconds = metadata.timestamp - (*dive)->when + shift_time; - picture->location = metadata.location; - return picture; -} - /* clones a dive and moves given dive computer to front */ struct dive *make_first_dc(const struct dive *d, int dc_number) { diff --git a/core/dive.h b/core/dive.h index 8e9ee280a..2113c2a9c 100644 --- a/core/dive.h +++ b/core/dive.h @@ -204,14 +204,6 @@ extern enum divemode_t get_current_divemode(const struct divecomputer *dc, int t extern struct event *get_next_divemodechange(const struct event **evd, bool update_pointer); extern enum divemode_t get_divemode_at_time(const struct divecomputer *dc, int dtime, const struct event **ev_dmc); -/* picture list and methods related to dive picture handling */ -#define FOR_EACH_PICTURE(_dive) \ - if ((_dive) && (_dive)->pictures.nr) \ - for (struct picture *picture = (_dive)->pictures.pictures; \ - picture < (_dive)->pictures.pictures + (_dive)->pictures.nr; \ - picture++) -extern struct picture *create_picture(const char *filename, int shift_time, bool match_all, struct dive **dive); -extern bool picture_check_valid_time(timestamp_t timestamp, int shift_time); extern void dive_set_geodata_from_picture(struct dive *dive, struct picture *picture, struct dive_site_table *table); extern bool has_gaschange_event(const struct dive *dive, const struct divecomputer *dc, int idx); diff --git a/core/picture.c b/core/picture.c index 36953b8b1..9440cef35 100644 --- a/core/picture.c +++ b/core/picture.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: GPL-2.0 #include "picture.h" -#include "table.h" +#include "dive.h" +#include "metadata.h" #include "subsurface-string.h" +#include "table.h" #include <stdlib.h> #include <string.h> @@ -66,3 +68,88 @@ int get_picture_idx(const struct picture_table *t, const char *filename) } return -1; } + +/* 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) +{ + timestamp_t end_time = dive_endtime(d); + 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 +#define D30MIN (30 * 60) +static bool dive_check_picture_time(const struct dive *d, timestamp_t timestamp) +{ + return time_from_dive(d, timestamp) < D30MIN; +} + +/* 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. + * If no appropriate dive was found, no picture is created and NULL is returned. + */ +struct picture *create_picture(const char *filename, int shift_time, bool match_all, struct dive **dive) +{ + struct metadata metadata; + timestamp_t timestamp; + + get_metadata(filename, &metadata); + timestamp = metadata.timestamp + shift_time; + *dive = nearest_selected_dive(timestamp); + + if (!*dive) + return NULL; + if (get_picture_idx(&(*dive)->pictures, filename) >= 0) + return NULL; + if (!match_all && !dive_check_picture_time(*dive, timestamp)) + return NULL; + + struct picture *picture = malloc(sizeof(struct picture)); + picture->filename = strdup(filename); + picture->offset.seconds = metadata.timestamp - (*dive)->when + shift_time; + picture->location = metadata.location; + return picture; +} + +bool picture_check_valid_time(timestamp_t timestamp, int shift_time) +{ + 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; +} diff --git a/core/picture.h b/core/picture.h index 61492fffc..b763adfe9 100644 --- a/core/picture.h +++ b/core/picture.h @@ -10,6 +10,8 @@ extern "C" { #endif +struct dive; + struct picture { char *filename; offset_t offset; @@ -17,6 +19,13 @@ struct picture { }; static const struct picture empty_picture = { NULL, { 0 }, { { 0 }, { 0 } } }; +/* loop through all pictures of a dive */ +#define FOR_EACH_PICTURE(_dive) \ + if ((_dive) && (_dive)->pictures.nr) \ + for (struct picture *picture = (_dive)->pictures.pictures; \ + picture < (_dive)->pictures.pictures + (_dive)->pictures.nr; \ + picture++) + /* Table of pictures. Attention: this stores pictures, * *not* pointers to pictures. This has two crucial consequences: * 1) Pointers to pictures are not stable. They may be @@ -37,6 +46,9 @@ extern void remove_from_picture_table(struct picture_table *, int idx); extern int get_picture_idx(const struct picture_table *, const char *filename); /* Return -1 if not found */ extern void sort_picture_table(struct picture_table *); +extern struct picture *create_picture(const char *filename, int shift_time, bool match_all, struct dive **dive); +extern bool picture_check_valid_time(timestamp_t timestamp, int shift_time); + #ifdef __cplusplus } #endif |