Refactored image timestamp checking.

Seperated getting image timestamp from picture_load_exif_data() and
ShiftImageTimesDialog::syncCameraClicked() into picture_get_timestamp()
and seperated checking timestamp from dive_create_picture() to
dive_check_picture_time().

Signed-off-by: Jan Darowski <jan.darowski@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Jan Darowski 2015-03-14 15:35:47 +01:00 committed by Dirk Hohndel
parent 838b450066
commit 7d37a3f5a6
4 changed files with 60 additions and 22 deletions

44
dive.c
View file

@ -2884,22 +2884,47 @@ static bool new_picture_for_dive(struct dive *d, char *filename)
// only add pictures that have timestamps between 30 minutes before the dive and
// 30 minutes after the dive ends
#define D30MIN (30 * 60)
bool dive_check_picture_time(struct dive *d, char *filename, int shift_time)
{
timestamp_t timestamp = 0;
picture_get_timestamp(filename, &timestamp);
offset_t offset;
if (timestamp) {
offset.seconds = timestamp - d->when + shift_time;
if (offset.seconds > -D30MIN && offset.seconds < (int)d->duration.seconds + D30MIN) {
// this picture belongs to this dive
return true;
}
}
return false;
}
bool picture_check_valid(char *filename, int shift_time)
{
bool result = false;
int i;
struct dive *d;
for_each_dive (i, d)
if (d->selected)
result = result || dive_check_picture_time(d, filename, shift_time);
return result;
}
void dive_create_picture(struct dive *d, char *filename, int shift_time)
{
timestamp_t timestamp;
if (!new_picture_for_dive(d, filename))
return;
if (!dive_check_picture_time(d, filename, shift_time))
return;
struct picture *p = alloc_picture();
p->filename = filename;
picture_load_exif_data(p, &timestamp);
if (timestamp) {
p->offset.seconds = timestamp - d->when + shift_time;
if (p->offset.seconds < -D30MIN || p->offset.seconds > (int)d->duration.seconds + D30MIN) {
// this picture doesn't belong to this dive
free(p);
return;
}
}
picture_get_timestamp(filename, &timestamp);
p->offset.seconds = timestamp - d->when + shift_time;
picture_load_exif_data(p);
dive_add_picture(d, p);
dive_set_geodata_from_picture(d, p);
}
@ -2940,6 +2965,7 @@ static void picture_free(struct picture *p)
free(p->hash);
free(p);
}
void dive_remove_picture(char *filename)
{
struct picture **ep = &current_dive->picture_list;

5
dive.h
View file

@ -381,11 +381,14 @@ struct picture {
for (struct picture *picture = (_divestruct).picture_list; picture; picture = picture->next)
extern struct picture *alloc_picture();
extern bool dive_check_picture_time(struct dive *d, char *filename, int shift_time);
extern void dive_create_picture(struct dive *d, char *filename, int shift_time);
extern void dive_add_picture(struct dive *d, struct picture *newpic);
extern void dive_remove_picture(char *filename);
extern unsigned int dive_get_picture_count(struct dive *d);
extern void picture_load_exif_data(struct picture *p, timestamp_t *timestamp);
extern bool picture_check_valid(char *filename, int shift_time);
extern void picture_load_exif_data(struct picture *p);
extern void picture_get_timestamp(char *filename, timestamp_t *t);
extern void dive_set_geodata_from_picture(struct dive *d, struct picture *pic);
extern int explicit_first_cylinder(struct dive *dive, struct divecomputer *dc);

View file

@ -272,9 +272,7 @@ void ShiftImageTimesDialog::buttonClicked(QAbstractButton *button)
void ShiftImageTimesDialog::syncCameraClicked()
{
struct memblock mem;
EXIFInfo exiv;
int retval;
timestamp_t timestamp;
QPixmap picture;
QDateTime dcDateTime = QDateTime();
QStringList fileNames = QFileDialog::getOpenFileNames(this,
@ -290,13 +288,9 @@ void ShiftImageTimesDialog::syncCameraClicked()
scene->addPixmap(picture.scaled(ui.DCImage->size()));
ui.DCImage->setScene(scene);
if (readfile(fileNames.at(0).toUtf8().data(), &mem) <= 0)
return;
retval = exiv.parseFrom((const unsigned char *)mem.buffer, (unsigned)mem.size);
free(mem.buffer);
if (retval != PARSE_EXIF_SUCCESS)
return;
dcImageEpoch = exiv.epoch();
picture_get_timestamp(fileNames.at(0).toUtf8().data(), &timestamp);
dcImageEpoch = timestamp;
dcDateTime.setTime_t(dcImageEpoch);
ui.dcTime->setDateTime(dcDateTime);
connect(ui.dcTime, SIGNAL(dateTimeChanged(const QDateTime &)), this, SLOT(dcDateTimeChanged(const QDateTime &)));

View file

@ -337,7 +337,7 @@ extern "C" xsltStylesheetPtr get_stylesheet(const char *name)
return xslt;
}
extern "C" void picture_load_exif_data(struct picture *p, timestamp_t *timestamp)
extern "C" void picture_load_exif_data(struct picture *p)
{
EXIFInfo exif;
memblock mem;
@ -346,7 +346,6 @@ extern "C" void picture_load_exif_data(struct picture *p, timestamp_t *timestamp
goto picture_load_exit;
if (exif.parseFrom((const unsigned char *)mem.buffer, (unsigned)mem.size) != PARSE_EXIF_SUCCESS)
goto picture_load_exit;
*timestamp = exif.epoch();
p->longitude.udeg= lrint(1000000.0 * exif.GeoLocation.Longitude);
p->latitude.udeg = lrint(1000000.0 * exif.GeoLocation.Latitude);
@ -355,6 +354,22 @@ picture_load_exit:
return;
}
extern "C" void picture_get_timestamp(char *filename, timestamp_t *t)
{
EXIFInfo exif;
memblock mem;
int retval;
if (readfile(filename, &mem) <= 0)
return;
retval = exif.parseFrom((const unsigned char *)mem.buffer, (unsigned)mem.size);
free(mem.buffer);
if (retval != PARSE_EXIF_SUCCESS)
return;
*t = exif.epoch();
return;
}
extern "C" const char *system_default_directory(void)
{
static char filename[PATH_MAX];