one more step on the metadata road

This commit is contained in:
tth 2022-04-09 23:18:14 +02:00
parent 4b64330884
commit a5fac7effd
11 changed files with 254 additions and 33 deletions

1
.gitignore vendored
View File

@ -68,6 +68,7 @@ tools/addpnm2fimg
tools/cumulfimgs tools/cumulfimgs
tools/fimgops tools/fimgops
tools/fimgfx tools/fimgfx
tools/fimgmetadata
tools/*.png tools/*.png
tools/*.tiff tools/*.tiff

View File

@ -4,7 +4,7 @@
* http://la.buvette.org/photos/cumul * http://la.buvette.org/photos/cumul
*/ */
#define FIMG_VERSION 177 #define FIMG_VERSION 179
/* /*
* in memory descriptor * in memory descriptor
@ -34,10 +34,13 @@ typedef struct {
typedef struct { typedef struct {
char magic[8]; char magic[8];
struct timeval timestamp; struct timeval timestamp;
uint64_t pid; // WTF ?
int32_t count; int32_t count;
float fval; float fval;
char idcam[32]; char idcam[32];
int32_t origin; // enum ? int32_t origin; // enum ?
uint32_t reserved[8];
} FimgMetaData; } FimgMetaData;
/* /*
* we MUST look at packing and endianess problems NOW */ * we MUST look at packing and endianess problems NOW */
@ -221,11 +224,15 @@ int fimg_vdeg_a(FloatImg *img, double dcoef);
/* FIMG native file module */ /* FIMG native file module */
int fimg_fileinfos(char *fname, int *datas); int fimg_fileinfos(char *fname, int *datas);
int fimg_dump_to_file(FloatImg *head, char *fname, int notused); int fimg_dump_to_file(FloatImg *head, char *fname, int notused);
int fimg_dumpmd_to_file(FloatImg *fi, char *nm, FimgMetaData *pmd, int nu);
int fimg_load_from_dump(char *fname, FloatImg *where); int fimg_load_from_dump(char *fname, FloatImg *where);
int fimg_create_from_dump(char *fname, FloatImg *head); int fimg_create_from_dump(char *fname, FloatImg *head);
/* FIMG metadata module */ /* FIMG metadata module */
int fimg_show_metadata(FimgMetaData *pmd, char *title, int notused); int fimg_show_metadata(FimgMetaData *pmd, char *title, int notused);
int fimg_default_metadata(FimgMetaData *pmd); int fimg_default_metadata(FimgMetaData *pmd);
int fimg_get_metadata_from_file(char *fname, FimgMetaData *pmd);
int fimg_save_R_as_fits(FloatImg *src, char *outname, int flags); int fimg_save_R_as_fits(FloatImg *src, char *outname, int flags);
int fimg_save_G_as_fits(FloatImg *src, char *outname, int flags); int fimg_save_G_as_fits(FloatImg *src, char *outname, int flags);

View File

@ -0,0 +1,4 @@
# Fimg tools
Need more explanations...

View File

@ -55,6 +55,7 @@ void fimg_print_sizeof(void)
{ {
fprintf(stderr, " sz FloatImg = %lu\n", sizeof(FloatImg)); fprintf(stderr, " sz FloatImg = %lu\n", sizeof(FloatImg));
fprintf(stderr, " sz filehead = %lu\n", sizeof(FimgFileHead)); fprintf(stderr, " sz filehead = %lu\n", sizeof(FimgFileHead));
fprintf(stderr, " sz metadata = %lu\n", sizeof(FimgMetaData));
fprintf(stderr, " sz filter = %lu\n", sizeof(FimgFilter3x3)); fprintf(stderr, " sz filter = %lu\n", sizeof(FimgFilter3x3));
} }
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */

View File

@ -59,7 +59,82 @@ return 0;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
/* /*
* /!\ thi func work ONLY on RGB image * new avril 2022 : trying to save metadatas...
*/
int fimg_dumpmd_to_file(FloatImg *fimg, char *fname, \
FimgMetaData *pmd, int notused)
{
FILE *fp;
int foo, nbre;
FimgFileHead filehead;
#if 1 // DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p '%s' %p %d )\n", __func__, fimg,
fname, pmd, notused);
#endif
if (FIMG_TYPE_RGB != fimg->type) {
fprintf(stderr, "%s : bad type %d\n", __func__, fimg->type);
return -8;
}
fp = fopen(fname, "w");
if (NULL==fp) {
perror(fname);
return -1;
}
memset(&filehead, 0, sizeof(filehead));
memcpy(filehead.magic, "FIMG", 4);
filehead.w = fimg->width; filehead.h = fimg->height;
filehead.t = fimg->type;
/* XXX metadata */
if (NULL != pmd) {
filehead.magic[4] = 'a';
}
foo = fwrite(&filehead, sizeof(FimgFileHead), 1, fp);
if (1 != foo) {
perror(fname);
fclose(fp);
/* may be here, we can remove the broken file ? */
return -2;
}
/* XXX metadata */
if (NULL != pmd) {
foo = fwrite(pmd, sizeof(FimgMetaData), 1, fp);
if (1 != foo) {
perror(fname);
fclose(fp);
/* may be here, we can remove the broken file ? */
return -2;
}
}
nbre = fimg->width * fimg->height; /* pixels per frame */
foo = fwrite(fimg->R, sizeof(float), nbre, fp);
if (nbre != foo) {
perror(fname); fclose(fp); return -3;
}
foo = fwrite(fimg->G, sizeof(float), nbre, fp);
if (nbre != foo) {
perror(fname); fclose(fp); return -3;
}
foo = fwrite(fimg->B, sizeof(float), nbre, fp);
if (nbre != foo) {
perror(fname); fclose(fp); return -3;
}
fclose(fp);
return 0;
}
/* ---------------------------------------------------------------- */
/*
* /!\ this func work ONLY on RGB image
*/ */
int fimg_dump_to_file(FloatImg *fimg, char *fname, int notused) int fimg_dump_to_file(FloatImg *fimg, char *fname, int notused)
{ {
@ -72,7 +147,7 @@ fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, fimg,
fname, notused); fname, notused);
#endif #endif
if (3 != fimg->type) { if (FIMG_TYPE_RGB != fimg->type) {
fprintf(stderr, "%s : bad type %d\n", __func__, fimg->type); fprintf(stderr, "%s : bad type %d\n", __func__, fimg->type);
return -8; return -8;
} }

View File

@ -12,34 +12,62 @@
extern int verbosity; /* must be declared around main() */ extern int verbosity; /* must be declared around main() */
/* ---------------------------------------------------------------- */
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);
}
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
int fimg_show_metadata(FimgMetaData *pmd, char *title, int notused) int fimg_show_metadata(FimgMetaData *pmd, char *title, int notused)
{ {
int foo;
fprintf(stderr, ">>> %s ( %p '%s' 0x%08x )\n", __func__, fprintf(stderr, ">>> %s ( %p '%s' 0x%08x )\n", __func__,
pmd, title, notused); pmd, title, notused);
fprintf(stderr, "sizeof metadata = %ld\n", sizeof(FimgMetaData)); if (verbosity) {
fprintf(stderr, "magic = '%8s'\n", pmd->magic); fprintf(stderr, "sizeof(metadata) = %ld\n", \
/* TIMESTAMP HERE */ sizeof(FimgMetaData));
fprintf(stderr, "counter = %d\n", pmd->count); puts_magic_8(pmd->magic, stderr);
fprintf(stderr, "float value = %.3f\n", pmd->fval); }
fprintf(stderr, "id camera = '%s'\n", pmd->idcam);
fprintf(stderr, "origin = %d\n", pmd->origin);
return -1; /* SHOW TIMESTAMP HERE */
fprintf(stderr, "creator pid = %ld\n", pmd->pid);
fprintf(stderr, "counter = %d\n", pmd->count);
fprintf(stderr, "float value = %.3f\n", pmd->fval);
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++) {
fprintf(stderr, " 0x%08x", pmd->reserved[foo]);
if (3 == foo) fputs("\n ", stderr);
}
fputc('\n', stderr);
return 0;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
int fimg_default_metadata(FimgMetaData *pmd) int fimg_default_metadata(FimgMetaData *pmd)
{ {
memset(pmd, 0, sizeof(FimgMetaData));
memcpy(pmd->magic, "metadata", 8); memcpy(pmd->magic, "metadata", 8);
/* set timestamp here ? */
pmd->pid = getpid();
pmd->count = 0; pmd->count = 0;
pmd->fval = 255.0; pmd->fval = 255.0;
strcpy(pmd->idcam, "<unknow>"); strcpy(pmd->idcam, "<unknow>");
pmd->origin = 999; pmd->origin = 0x55555555;
return -1; return 0;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
int fimg_get_metadata_from_file(char *fname, FimgMetaData *pmd) int fimg_get_metadata_from_file(char *fname, FimgMetaData *pmd)
@ -49,17 +77,18 @@ FimgFileHead filehead;
FimgMetaData metadata; FimgMetaData metadata;
int foo; int foo;
#if DEBUG_LEVEL // #if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %s %p )\n", __func__, fname, pmd); fprintf(stderr, ">>> %s ( '%s' %p )\n", __func__, fname, pmd);
#endif // #endif
if (NULL==(fp=fopen(fname, "r"))) { if (NULL==(fp=fopen(fname, "r"))) {
perror(fname);
return -1; return -1;
} }
foo = fread(&filehead, sizeof(FimgFileHead), 1, fp); foo = fread(&filehead, sizeof(FimgFileHead), 1, fp);
if (sizeof(FimgFileHead) != foo) { if (1 != foo) {
fprintf(stderr, "short read on %s (head)\n", fname); fprintf(stderr, "short read (%d) on %s (head)\n", foo, fname);
fclose(fp); fclose(fp);
return -2; return -2;
} }
@ -71,13 +100,13 @@ if (memcmp(filehead.magic, "FIMG", 4)) {
} }
if ('a' != filehead.magic[4]) { if ('a' != filehead.magic[4]) {
fprintf(stderr, "'%s' have no metadata.\n", fname); fprintf(stderr, "file '%s' have no metadata.\n", fname);
fclose(fp); fclose(fp);
return -4; return -4;
} }
foo = fread(&metadata, sizeof(FimgMetaData), 1, fp); foo = fread(&metadata, sizeof(FimgMetaData), 1, fp);
if (sizeof(FimgMetaData) != foo) { if (1 != foo) {
fprintf(stderr, "short read on %s (metadata)\n", fname); fprintf(stderr, "short read on %s (metadata)\n", fname);
fclose(fp); fclose(fp);
return -5; return -5;
@ -87,7 +116,6 @@ fclose(fp); /* got all needed datas */
if (memcmp(metadata.magic, "metadata", 8)) { if (memcmp(metadata.magic, "metadata", 8)) {
fprintf(stderr, "'%s' invalid metadata.\n", fname); fprintf(stderr, "'%s' invalid metadata.\n", fname);
fclose(fp);
return -6; return -6;
} }

11
lib/t.c
View File

@ -24,7 +24,12 @@ fprintf(stderr, "-------- %s ( %s ) --------\n", __func__, fname);
foo = fimg_default_metadata(&Md); foo = fimg_default_metadata(&Md);
foo = fimg_show_metadata(&Md, "from t.c", 0); // foo = fimg_show_metadata(&Md, "default from t.c", 0);
foo = fimg_get_metadata_from_file(fname, &Md);
fprintf(stderr, "%s : get metadata -> %d\n", fname, foo);
foo = fimg_show_metadata(&Md, fname, 0);
return -1; return -1;
} }
@ -85,7 +90,7 @@ int essai_interpolate(int k)
FloatImg A, B, C; FloatImg A, B, C;
int foo, idx; int foo, idx;
char ligne[200]; char ligne[200];
float fval, minmax[6]; float fval;
foo = fimg_create(&A, WI, HI, FIMG_TYPE_RGB); foo = fimg_create(&A, WI, HI, FIMG_TYPE_RGB);
if (foo) { if (foo) {
@ -314,7 +319,7 @@ if (verbosity) {
fimg_print_sizeof(); fimg_print_sizeof();
} }
foo = essai_metadata("foo.fimg"); foo = essai_metadata("quux.fimg");
fprintf(stderr, "retour essai -> %d\n", foo); fprintf(stderr, "retour essai -> %d\n", foo);
// foo = essai_save_plane(0); // foo = essai_save_plane(0);

View File

@ -13,8 +13,12 @@ DEPS = ../floatimg.h ../libfloatimg.a Makefile
all: fimg2pnm mkfimg png2fimg fimgstats fimg2png \ all: fimg2pnm mkfimg png2fimg fimgstats fimg2png \
fimg2tiff fimg2text fimg2fits \ fimg2tiff fimg2text fimg2fits \
addpnm2fimg cumulfimgs fimgops fimgfx \ addpnm2fimg cumulfimgs fimgops fimgfx \
fimgmetadata \
fimghalfsize fimghalfsize
fimgmetadata: fimgmetadata.c $(DEPS)
gcc $(COPT) $< ../libfloatimg.a -lm -o $@
fimgstats: fimgstats.c $(DEPS) fimgstats: fimgstats.c $(DEPS)
gcc $(COPT) $< ../libfloatimg.a -lm -o $@ gcc $(COPT) $< ../libfloatimg.a -lm -o $@

View File

@ -10,7 +10,17 @@ Génération d'une image flottante avec des choses dedans.
Un [../scripts/demo-mkfimg.sh](script) permet de créer toutes Un [../scripts/demo-mkfimg.sh](script) permet de créer toutes
les images disponibles. les images disponibles.
L'option `-m` rajoute des méta-données, cette option
**ne** doit **pas** encore être utilisée dans la vrai vie.
## fimgops ## fimgops
```
usage:
fimgops [options] A.fimg B.fimg operator D.fimg
options:
-k N.N set float value (def=0.500)
-v increase verbosity
```
## fimgfx ## fimgfx
@ -22,7 +32,7 @@ effects:
Compute some useless numbers... Compute some useless numbers...
## fimg2pnm - fimg2png - fimg2tiff ## fimg2pnm - fimg2png - fimg2tiff - fimg2fips
Exportation d'image flottante vers divers formats. Certains d'entre eux Exportation d'image flottante vers divers formats. Certains d'entre eux
ne sont gérés que de façon très rudimentaire. ne sont gérés que de façon très rudimentaire.
@ -32,3 +42,6 @@ ne sont gérés que de façon très rudimentaire.
Nouveau de l'année 2020+1 : exfiltrer toutes des données d'une image flottante Nouveau de l'année 2020+1 : exfiltrer toutes des données d'une image flottante
afin de les rendre machinables. afin de les rendre machinables.
## fimgmetadata
Nouveau avril 2022. Need more doc...

69
tools/fimgmetadata.c Normal file
View File

@ -0,0 +1,69 @@
/*
* FIMG METADATA TOOL
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "../floatimg.h"
int verbosity;
/* --------------------------------------------------------------------- */
int get_print_metadata(char *fname, char *command)
{
int foo;
FimgMetaData metadata;
// #if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( '%s' %s )\n", __func__, fname, command);
// #endif
foo = fimg_get_metadata_from_file(fname, &metadata);
if (foo) return foo;
/* switch on command here, please */
fimg_show_metadata(&metadata, fname, 0);
return 0;
}
/* --------------------------------------------------------------------- */
void help(void)
{
fprintf(stderr, "*** fimgmetadata (%s, %s)\n", __DATE__, __TIME__);
fimg_print_version(1);
exit(0);
}
/* --------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int foo, opt, nbargs;
char *fname;
while ((opt = getopt(argc, argv, "hv")) != -1) {
switch(opt) {
case 'h': help(); break;
case 'v': verbosity++; break;
}
}
nbargs = argc - optind;
// fprintf(stderr, "nbargs = %d\n", nbargs);
if (2 != nbargs) {
fprintf(stderr, "%s need two args: command & filename\n", argv[0]);
exit(1);
}
foo = get_print_metadata(argv[optind], argv[optind+1]);
if (foo) fprintf(stderr, "got a %d from job\n", foo);
return 0;
}
/* --------------------------------------------------------------------- */

View File

@ -68,7 +68,7 @@ for (type = types; type->code; type++) {
exit(0); exit(0);
} }
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
static void help(int lj) static void help(void)
{ {
int foo, cc; int foo, cc;
@ -76,11 +76,11 @@ puts("Usage:\tmkfimg [options] quux.fimg width height");
puts("\t-k N.N\tgive a float parameter"); puts("\t-k N.N\tgive a float parameter");
puts("\t-L\tlist howto make a pic"); puts("\t-L\tlist howto make a pic");
fputs("\t-t bla\thowto make the pic :\n\t\t| ", stdout); fputs("\t-t bla\thowto make the pic :\n\t\t | ", stdout);
for (foo=cc=0; types[foo].code; foo++) { for (foo=cc=0; types[foo].code; foo++) {
cc += printf("%s ", types[foo].name); cc += printf("%s ", types[foo].name);
if (cc>42) { cc=0; printf("\n\t\t| "); } if (cc>35) { cc=0; printf("\n\t\t | "); }
} }
puts("\n\t-v\tincrease verbosity"); puts("\n\t-v\tincrease verbosity");
@ -100,14 +100,17 @@ int width, height;
char *fname; char *fname;
float fvalue = 1.0; float fvalue = 1.0;
int type = T_BLACK; int type = T_BLACK;
int wrmdata = 0;
char *tname = "wtf?"; char *tname = "wtf?";
FloatImg fimg; FloatImg fimg;
FimgMetaData metadata;
while ((opt = getopt(argc, argv, "hk:Lt:v")) != -1) { while ((opt = getopt(argc, argv, "hk:Lmt:v")) != -1) {
switch(opt) { switch(opt) {
case 'h': help(0); break; case 'h': help(); break;
case 'k': fvalue = atof(optarg); break; case 'k': fvalue = atof(optarg); break;
case 'm': wrmdata = 1; break;
case 't': type = get_type_by_name(tname=optarg); case 't': type = get_type_by_name(tname=optarg);
break; break;
case 'L': list_types(); break; case 'L': list_types(); break;
@ -147,8 +150,8 @@ switch (nbargs) {
fname = argv[optind]; fname = argv[optind];
if (verbosity>1) fprintf(stderr, "*** mkfimg *** %s %s\n", if (verbosity>1) fprintf(stderr, "*** mkfimg *** %s %s *** pid %ld\n",
__DATE__, __TIME__); __DATE__, __TIME__, (long)getpid());
if (verbosity) fprintf(stderr, "making '%s' %dx%d, type %d\n", if (verbosity) fprintf(stderr, "making '%s' %dx%d, type %d\n",
fname, width, height, type); fname, width, height, type);
@ -174,7 +177,18 @@ switch(type) {
case -1: exit(1); case -1: exit(1);
} }
foo = fimg_dump_to_file(&fimg, fname, 0); if (wrmdata) {
fprintf(stderr, "%s: warning, metadata is bogus\n", argv[0]);
(void)fimg_default_metadata(&metadata);
sprintf(metadata.idcam, "mkfimg (libv %d)", FIMG_VERSION);
foo = fimg_dumpmd_to_file(&fimg, fname, &metadata, 0);
#if DEBUG_LEVEL
fprintf(stderr, "save w. metadata -> %d\n", foo);
#endif
}
else {
foo = fimg_dump_to_file(&fimg, fname, 0);
}
if (foo) { if (foo) {
fprintf(stderr, "dump fimg to %s -> %d\n", fname, foo); fprintf(stderr, "dump fimg to %s -> %d\n", fname, foo);
exit(1); exit(1);