From cef00ac04e70cff90ab4d2cbe55c63778d121ff0 Mon Sep 17 00:00:00 2001 From: Tonton Th Date: Tue, 7 Jan 2020 13:46:09 +0100 Subject: [PATCH] added normalize function, need more tests --- floatimg.h | 5 +++-- lib/fimg-math.c | 35 ++++++++++++++++++++++++++++++++--- lib/t.c | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/floatimg.h b/floatimg.h index 60d94a5e..2716bba7 100644 --- a/floatimg.h +++ b/floatimg.h @@ -2,7 +2,7 @@ * floatimg.h */ -#define FIMG_VERSION 85 +#define FIMG_VERSION 86 /* * in memory descriptor @@ -51,7 +51,7 @@ typedef struct { int fimg_create(FloatImg *fimg, int w, int h, int t); int fimg_destroy(FloatImg *fimg); int fimg_clone(FloatImg *fimg, FloatImg *newpic, int flags); -int fimg_copy_data(FloatImg *from, FloatImg *to); +int fimg_copy_data(FloatImg *from, FloatImg *to); int fimg_print_version(int k); void fimg_printhead(FloatImg *h); @@ -107,6 +107,7 @@ int fimg_meanvalues(FloatImg *head, float means[4]); int fimg_to_gray(FloatImg *head); void fimg_add_cste(FloatImg *fi, float value); void fimg_mul_cste(FloatImg *fi, float value); +int fimg_normalize(FloatImg *fi, double maxima, int notused); void fimg_drand48(FloatImg *fi, float kmul); int fimg_count_negativ(FloatImg *fi); diff --git a/lib/fimg-math.c b/lib/fimg-math.c index 2b9ef1a3..a1ec02dc 100644 --- a/lib/fimg-math.c +++ b/lib/fimg-math.c @@ -158,14 +158,43 @@ if (fi->type != FIMG_TYPE_RGB) { } nbre = fi->width * fi->height * fi->type; -// #if DEBUG_LEVEL -fprintf(stderr, "%s, nbre is %d\n", __func__, nbre); -// #endif +#if DEBUG_LEVEL +fprintf(stderr, "%s, nbre of datum is %d\n", __func__, nbre); +#endif for (idx=0; idxR[idx] *= value; } } /* ---------------------------------------------------------------- */ +int fimg_normalize(FloatImg *fi, double maxima, int notused) +{ +double coef; + +if (fi->type != FIMG_TYPE_RGB) { + fprintf(stderr, "%s : type %d invalide\n", + __func__, fi->type); + return -99; + } + +if (fi->count < 1) { + fprintf(stderr, "%s : count %d is invalid\n", __func__, fi->count); + return -98; + } + +coef = 1.0 / ((double)fi->count * (double)fi->fval); + +if (verbosity) { + fprintf(stderr, "image @ %p\n", fi); + fprintf(stderr, "fval %f\n", fi->fval); + fprintf(stderr, "count %d\n", fi->count); + fprintf(stderr, "coef %f\n", coef); + } + +fimg_mul_cste(fi, coef); + +return 0; +} +/* ---------------------------------------------------------------- */ /* Warning: this function is _very_ slow */ void fimg_drand48(FloatImg *fi, float kmul) { diff --git a/lib/t.c b/lib/t.c index ea1c87f0..95ac90de 100644 --- a/lib/t.c +++ b/lib/t.c @@ -13,6 +13,36 @@ int verbosity; +/* ---------------------------------------------------------------- */ +int essai_normalize(void) +{ +FloatImg A; +float val; +int foo; + +foo = fimg_create(&A, 512, 512, FIMG_TYPE_RGB); +if (foo) { + fprintf(stderr, "%s err create A %d\n", __func__, foo); + return foo; + } + +fimg_drand48(&A, 255.0); +val = fimg_get_maxvalue(&A); +fprintf(stderr, "BEFORE max pixel %f\n", val); + +A.fval = 255.0; A.count = 1; + +foo = fimg_normalize(&A, 1.0, 0); +if (foo) { + fprintf(stderr, "%s err normalize A %d\n", __func__, foo); + return foo; + } + +val = fimg_get_maxvalue(&A); +fprintf(stderr, "AFTER max pixel %f\n", val); + +return 0; +} /* ---------------------------------------------------------------- */ int essai_2gray(FloatImg *picz, char *outname) @@ -126,7 +156,7 @@ return -1; #define H 240 int main(int argc, char *argv[]) { -int foo, idx, opt; +int foo, opt; // char outname[100]; int gray = 0; @@ -140,8 +170,8 @@ while ((opt = getopt(argc, argv, "gn:v")) != -1) { if (verbosity) fimg_print_version(0); -foo = essai_clone_et_copy(0); -fprintf(stderr, "retour essai clone_et_copy -> %d\n", foo); +foo = essai_normalize(); +fprintf(stderr, "retour essai normalize -> %d\n", foo); return 0; }