From 538023b586a4c23675100600d3f8ffd8231bf2f3 Mon Sep 17 00:00:00 2001 From: Tonton Th Date: Wed, 18 Dec 2019 14:39:47 +0100 Subject: [PATCH] adding a file loading func - ugly code --- floatimg.h | 3 ++- lib/fimg-file.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/floatimg.h b/floatimg.h index de7692b..92a2a1c 100644 --- a/floatimg.h +++ b/floatimg.h @@ -2,7 +2,7 @@ * floatimg.h */ -#define FIMG_VERSION 81 +#define FIMG_VERSION 82 /* * in memory descriptor @@ -91,6 +91,7 @@ int fimg_mk_gray_from(FloatImg *src, FloatImg*dst, int k); /* FIMG files module */ int fimg_fileinfos(char *fname, int *datas); int fimg_dump_to_file(FloatImg *head, char *fname, int notused); +int fimg_load_from_dump(char *fname, FloatImg *where); int fimg_create_from_dump(char *fname, FloatImg *head); /* mathematics operations */ diff --git a/lib/fimg-file.c b/lib/fimg-file.c index 6289ad0..7523ca2 100644 --- a/lib/fimg-file.c +++ b/lib/fimg-file.c @@ -8,7 +8,7 @@ #include #include #include - +#include #include "../floatimg.h" extern int verbosity; /* must be declared around main() */ @@ -99,6 +99,47 @@ if (nbre != foo) { fclose(fp); +return 0; +} +/* ---------------------------------------------------------------- */ +int fimg_load_from_dump(char *fname, FloatImg *where) +{ +FILE *fp; +int foo, nbre; +FimgFileHead filehead; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, head); +#endif + +if (NULL==(fp = fopen(fname, "r"))) { + perror(fname); exit(1); + } +foo = fread(&filehead, sizeof(FimgFileHead), 1, fp); +if (1 != foo) { + fprintf(stderr, "%s : short read\n", fname); + fclose(fp); + return -1; + } + +/* check compatibility */ +if ( (filehead.w != where->width) || + (filehead.h != where->height) || + (filehead.t != where->type) ) { + + fprintf(stderr, "file '%s' incompatible\n", fname); + exit(3); + } + +nbre = filehead.w*filehead.h*filehead.t; /* ugly quirk */ +foo = fread(where->R, sizeof(float), nbre, fp); +if (nbre != foo) { + fprintf(stderr, "err read '%s' : %d\n", fname, errno); + exit(3); + } + +fclose(fp); + return 0; } /* ---------------------------------------------------------------- */