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

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 filehead = %lu\n", sizeof(FimgFileHead));
fprintf(stderr, " sz metadata = %lu\n", sizeof(FimgMetaData));
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)
{
@@ -72,7 +147,7 @@ fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, fimg,
fname, notused);
#endif
if (3 != fimg->type) {
if (FIMG_TYPE_RGB != fimg->type) {
fprintf(stderr, "%s : bad type %d\n", __func__, fimg->type);
return -8;
}

View File

@@ -12,34 +12,62 @@
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 foo;
fprintf(stderr, ">>> %s ( %p '%s' 0x%08x )\n", __func__,
pmd, title, notused);
fprintf(stderr, "sizeof metadata = %ld\n", sizeof(FimgMetaData));
fprintf(stderr, "magic = '%8s'\n", pmd->magic);
/* TIMESTAMP HERE */
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 = %d\n", pmd->origin);
if (verbosity) {
fprintf(stderr, "sizeof(metadata) = %ld\n", \
sizeof(FimgMetaData));
puts_magic_8(pmd->magic, stderr);
}
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)
{
memset(pmd, 0, sizeof(FimgMetaData));
memcpy(pmd->magic, "metadata", 8);
/* set timestamp here ? */
pmd->pid = getpid();
pmd->count = 0;
pmd->fval = 255.0;
strcpy(pmd->idcam, "<unknow>");
pmd->origin = 999;
pmd->origin = 0x55555555;
return -1;
return 0;
}
/* ---------------------------------------------------------------- */
int fimg_get_metadata_from_file(char *fname, FimgMetaData *pmd)
@@ -49,17 +77,18 @@ FimgFileHead filehead;
FimgMetaData metadata;
int foo;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %s %p )\n", __func__, fname, pmd);
#endif
// #if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( '%s' %p )\n", __func__, fname, pmd);
// #endif
if (NULL==(fp=fopen(fname, "r"))) {
perror(fname);
return -1;
}
foo = fread(&filehead, sizeof(FimgFileHead), 1, fp);
if (sizeof(FimgFileHead) != foo) {
fprintf(stderr, "short read on %s (head)\n", fname);
if (1 != foo) {
fprintf(stderr, "short read (%d) on %s (head)\n", foo, fname);
fclose(fp);
return -2;
}
@@ -71,13 +100,13 @@ if (memcmp(filehead.magic, "FIMG", 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);
return -4;
}
foo = fread(&metadata, sizeof(FimgMetaData), 1, fp);
if (sizeof(FimgMetaData) != foo) {
if (1 != foo) {
fprintf(stderr, "short read on %s (metadata)\n", fname);
fclose(fp);
return -5;
@@ -87,7 +116,6 @@ fclose(fp); /* got all needed datas */
if (memcmp(metadata.magic, "metadata", 8)) {
fprintf(stderr, "'%s' invalid metadata.\n", fname);
fclose(fp);
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_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;
}
@@ -85,7 +90,7 @@ int essai_interpolate(int k)
FloatImg A, B, C;
int foo, idx;
char ligne[200];
float fval, minmax[6];
float fval;
foo = fimg_create(&A, WI, HI, FIMG_TYPE_RGB);
if (foo) {
@@ -314,7 +319,7 @@ if (verbosity) {
fimg_print_sizeof();
}
foo = essai_metadata("foo.fimg");
foo = essai_metadata("quux.fimg");
fprintf(stderr, "retour essai -> %d\n", foo);
// foo = essai_save_plane(0);