2022-03-19 11:21:58 +11:00
|
|
|
/*
|
|
|
|
* metadata.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
2022-04-12 23:54:07 +11:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <string.h>
|
2022-03-19 11:21:58 +11:00
|
|
|
|
|
|
|
#include "../floatimg.h"
|
|
|
|
|
|
|
|
extern int verbosity; /* must be declared around main() */
|
|
|
|
|
2022-04-10 08:18:14 +11:00
|
|
|
/* ---------------------------------------------------------------- */
|
|
|
|
static void puts_magic_8(char *ptr, FILE *fp)
|
|
|
|
{
|
|
|
|
int foo;
|
|
|
|
|
|
|
|
fputs("magic = ", fp);
|
|
|
|
fputc('[', fp);
|
|
|
|
for (foo=0; foo<8; foo++) {
|
|
|
|
fputc(ptr[foo], fp);
|
|
|
|
}
|
|
|
|
fputc(']', fp); fputc('\n', fp);
|
|
|
|
fflush(fp);
|
|
|
|
}
|
2022-03-19 11:21:58 +11:00
|
|
|
/* ---------------------------------------------------------------- */
|
|
|
|
int fimg_show_metadata(FimgMetaData *pmd, char *title, int notused)
|
|
|
|
{
|
2022-04-10 08:18:14 +11:00
|
|
|
int foo;
|
2022-04-12 23:54:07 +11:00
|
|
|
double doubletime;
|
2022-03-19 11:21:58 +11:00
|
|
|
|
2022-04-17 13:25:31 +11:00
|
|
|
#if DEBUG_LEVEL
|
2022-03-19 11:21:58 +11:00
|
|
|
fprintf(stderr, ">>> %s ( %p '%s' 0x%08x )\n", __func__,
|
|
|
|
pmd, title, notused);
|
2022-04-17 13:25:31 +11:00
|
|
|
#endif
|
2022-03-19 11:21:58 +11:00
|
|
|
|
2022-05-15 05:49:59 +11:00
|
|
|
if (NULL != title) {
|
|
|
|
fprintf(stderr, "==== metadate for %s\n", title);
|
|
|
|
}
|
|
|
|
|
2022-04-10 08:18:14 +11:00
|
|
|
if (verbosity) {
|
|
|
|
fprintf(stderr, "sizeof(metadata) = %ld\n", \
|
|
|
|
sizeof(FimgMetaData));
|
|
|
|
puts_magic_8(pmd->magic, stderr);
|
|
|
|
}
|
2022-03-19 11:21:58 +11:00
|
|
|
|
2022-04-10 08:18:14 +11:00
|
|
|
/* SHOW TIMESTAMP HERE */
|
2022-04-12 23:54:07 +11:00
|
|
|
fprintf(stderr, "seconds sc. epoch = %ld\n", pmd->timestamp.tv_sec);
|
|
|
|
doubletime = (double)pmd->timestamp.tv_sec + \
|
|
|
|
(double)pmd->timestamp.tv_usec / 1e6;
|
|
|
|
fprintf(stderr, "dtime of day = %e\n", doubletime);
|
2022-04-17 13:03:38 +11:00
|
|
|
fprintf(stderr, "creator pid = %ld\n", pmd->cpid);
|
2022-04-10 08:18:14 +11:00
|
|
|
fprintf(stderr, "float value = %.3f\n", pmd->fval);
|
2022-05-15 05:49:59 +11:00
|
|
|
fprintf(stderr, "counter = %d\n", pmd->count);
|
2022-04-10 08:18:14 +11:00
|
|
|
fprintf(stderr, "id camera = '%s'\n", pmd->idcam);
|
|
|
|
fprintf(stderr, "origin = 0x%x\n", pmd->origin);
|
|
|
|
|
|
|
|
fputs("reserved words are:\n ", stderr);
|
|
|
|
for (foo=0; foo<8; foo++) {
|
2022-04-12 23:54:07 +11:00
|
|
|
fprintf(stderr, " 0x%08x", pmd->reserved[foo]);
|
2022-04-10 08:18:14 +11:00
|
|
|
if (3 == foo) fputs("\n ", stderr);
|
|
|
|
}
|
|
|
|
fputc('\n', stderr);
|
|
|
|
return 0;
|
2022-03-19 11:21:58 +11:00
|
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- */
|
2022-05-15 05:49:59 +11:00
|
|
|
int fimg_default_metadata(FimgMetaData *pmd, int bla)
|
2022-03-19 11:21:58 +11:00
|
|
|
{
|
2022-04-12 23:54:07 +11:00
|
|
|
int foo;
|
|
|
|
struct timeval tvl;
|
2022-03-19 11:21:58 +11:00
|
|
|
|
2022-04-10 08:18:14 +11:00
|
|
|
memset(pmd, 0, sizeof(FimgMetaData));
|
2022-03-19 11:21:58 +11:00
|
|
|
memcpy(pmd->magic, "metadata", 8);
|
2022-04-12 23:54:07 +11:00
|
|
|
|
2022-04-10 08:18:14 +11:00
|
|
|
/* set timestamp here ? */
|
2022-04-12 23:54:07 +11:00
|
|
|
/* CHALLENGE ACCEPTED */
|
|
|
|
memset(&tvl, 0, sizeof(struct timeval));
|
|
|
|
foo = gettimeofday(&tvl, NULL);
|
|
|
|
if (foo) {
|
|
|
|
/* error on get time of day ? */
|
|
|
|
perror("omg");
|
|
|
|
}
|
|
|
|
else {
|
2022-04-17 13:25:31 +11:00
|
|
|
if (verbosity) {
|
2022-05-15 05:49:59 +11:00
|
|
|
fprintf(stderr, " set TimeOfDay to %12ld , %8ld\n", \
|
2022-04-17 13:25:31 +11:00
|
|
|
tvl.tv_sec, tvl.tv_usec);
|
|
|
|
}
|
2022-04-12 23:54:07 +11:00
|
|
|
memcpy(&(pmd->timestamp), &tvl, sizeof(struct timeval));
|
|
|
|
}
|
|
|
|
|
2022-05-15 05:49:59 +11:00
|
|
|
pmd->cpid = getpid(); // we are the creator, no ?
|
|
|
|
pmd->count = 1; // mmmm...
|
|
|
|
pmd->fval = 255.0; // Ok
|
|
|
|
strcpy(pmd->idcam, "<noname>");
|
|
|
|
pmd->origin = 0xdeadbeef; // classic joke, sorry
|
|
|
|
pmd->reserved[0] = bla;
|
|
|
|
pmd->reserved[7] = 0x55445544; // magic number is a crime
|
2022-04-08 12:10:43 +11:00
|
|
|
|
2022-04-10 08:18:14 +11:00
|
|
|
return 0;
|
2022-03-19 11:21:58 +11:00
|
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- */
|
2022-04-08 12:10:43 +11:00
|
|
|
int fimg_get_metadata_from_file(char *fname, FimgMetaData *pmd)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
FimgFileHead filehead;
|
|
|
|
FimgMetaData metadata;
|
|
|
|
int foo;
|
|
|
|
|
2022-04-17 13:25:31 +11:00
|
|
|
#if DEBUG_LEVEL
|
2022-04-10 08:18:14 +11:00
|
|
|
fprintf(stderr, ">>> %s ( '%s' %p )\n", __func__, fname, pmd);
|
2022-04-17 13:25:31 +11:00
|
|
|
#endif
|
2022-04-08 12:10:43 +11:00
|
|
|
|
|
|
|
if (NULL==(fp=fopen(fname, "r"))) {
|
2022-04-10 08:18:14 +11:00
|
|
|
perror(fname);
|
2022-04-08 12:10:43 +11:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
foo = fread(&filehead, sizeof(FimgFileHead), 1, fp);
|
2022-04-10 08:18:14 +11:00
|
|
|
if (1 != foo) {
|
|
|
|
fprintf(stderr, "short read (%d) on %s (head)\n", foo, fname);
|
2022-04-08 12:10:43 +11:00
|
|
|
fclose(fp);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(filehead.magic, "FIMG", 4)) {
|
|
|
|
fprintf(stderr, "'%s' is not a fimg file.\n", fname);
|
|
|
|
fclose(fp);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('a' != filehead.magic[4]) {
|
2022-04-10 08:18:14 +11:00
|
|
|
fprintf(stderr, "file '%s' have no metadata.\n", fname);
|
2022-04-08 12:10:43 +11:00
|
|
|
fclose(fp);
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
foo = fread(&metadata, sizeof(FimgMetaData), 1, fp);
|
2022-04-10 08:18:14 +11:00
|
|
|
if (1 != foo) {
|
2022-04-08 12:10:43 +11:00
|
|
|
fprintf(stderr, "short read on %s (metadata)\n", fname);
|
|
|
|
fclose(fp);
|
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp); /* got all needed datas */
|
|
|
|
|
|
|
|
if (memcmp(metadata.magic, "metadata", 8)) {
|
2022-04-17 13:03:38 +11:00
|
|
|
fprintf(stderr, "'%s' invalid metadata\n", fname);
|
|
|
|
puts_magic_8(metadata.magic, stderr);
|
2022-04-08 12:10:43 +11:00
|
|
|
return -6;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(pmd, &metadata, sizeof(FimgMetaData));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- */
|