From 2aabc8b26bb8bbb59c1d1c85dd3a01c36ea47e3c Mon Sep 17 00:00:00 2001 From: tth Date: Tue, 10 Sep 2019 01:31:48 +0200 Subject: [PATCH] commit du soir, espoir --- floatimg.h | 7 +++-- lib/Makefile | 7 +++-- lib/fimg-compare.c | 40 ++++++++++++++++++++++++++ lib/fimg-core.h | 0 lib/fimg-math.c | 21 +++++++++++++- lib/interpolate.c | 71 +++++++++++++++++++++++++++++----------------- lib/t.c | 41 +++++++++++++++----------- 7 files changed, 140 insertions(+), 47 deletions(-) create mode 100644 lib/fimg-compare.c delete mode 100644 lib/fimg-core.h diff --git a/floatimg.h b/floatimg.h index 9f0effc..08c69c8 100644 --- a/floatimg.h +++ b/floatimg.h @@ -2,7 +2,7 @@ * floatimg.h */ -#define FIMG_VERSION 71 +#define FIMG_VERSION 72 /* * in memory descriptor @@ -47,6 +47,8 @@ int fimg_clear(FloatImg *fimg); int fimg_add_rgb(FloatImg *head, int x, int y, float r, float g, float b); +int fimg_images_compatible(FloatImg *a, FloatImg *b); + int fimg_interpolate(FloatImg *s1, FloatImg *s2, FloatImg *d, float coef); /* 'operats' module */ @@ -69,11 +71,12 @@ int fimg_fileinfos(char *fname, int *datas); int fimg_dump_to_file(FloatImg *head, char *fname, int notused); int fimg_create_from_dump(char *fname, FloatImg *head); -/* mathematics oprations */ +/* mathematics operations */ float fimg_get_maxvalue(FloatImg *head); 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); void fimg_drand48(FloatImg *fi, float kmul); /* various funcs modules */ diff --git a/lib/Makefile b/lib/Makefile index 7d58bbd..1cd48c0 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -2,10 +2,10 @@ # building the base library # -COPT = -Wall -fpic -g -no-pie -DDEBUG_LEVEL=0 +COPT = -Wall -fpic -g -pg -no-pie -DDEBUG_LEVEL=0 OBJS = fimg-core.o fimg-pnm.o fimg-file.o fimg-math.o \ fimg-timers.o operators.o fimg-2gray.o \ - interpolate.o + interpolate.o fimg-compare.o DEPS = Makefile ../floatimg.h @@ -25,6 +25,9 @@ t: t.c ../libfloatimg.a $(DEPS) fimg-core.o: fimg-core.c $(DEPS) gcc $(COPT) -c $< +fimg-compare.o: fimg-compare.c $(DEPS) + gcc $(COPT) -c $< + fimg-2gray.o: fimg-2gray.c $(DEPS) gcc $(COPT) -c $< diff --git a/lib/fimg-compare.c b/lib/fimg-compare.c new file mode 100644 index 0000000..a8f2caf --- /dev/null +++ b/lib/fimg-compare.c @@ -0,0 +1,40 @@ +/* + * fimg-core.c + * + * + */ + +#include +#include +#include +#include "string.h" + +#include "../floatimg.h" + +extern int verbosity; /* must be declared around main() */ + +/* ---------------------------------------------------------------- */ +int fimg_images_compatible(FloatImg *a, FloatImg *b) +{ +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %p %p )\n", __func__, a, b); +#endif + +if (a->type != b->type) { + if (verbosity) fprintf(stderr, "%p %p != type\n", a, b); + return -10; + } + +if (a->width != b->width) { + if (verbosity) fprintf(stderr, "%p %p != width\n", a, b); + return -11; + } + +if (a->height != b->height) { + if (verbosity) fprintf(stderr, "%p %p != height\n", a, b); + return -12; + } + +return 0; +} +/* ---------------------------------------------------------------- */ diff --git a/lib/fimg-core.h b/lib/fimg-core.h deleted file mode 100644 index e69de29..0000000 diff --git a/lib/fimg-math.c b/lib/fimg-math.c index b90e8fb..df470b3 100644 --- a/lib/fimg-math.c +++ b/lib/fimg-math.c @@ -103,11 +103,30 @@ nbre = fi->width * fi->height * fi->type; #if DEBUG_LEVEL fprintf(stderr, "%s, nbre is %d\n", __func__, nbre); #endif -for (idx=0; idxR[idx] += value; } } /* ---------------------------------------------------------------- */ +void fimg_mul_cste(FloatImg *fi, float value) +{ +int nbre, idx; + +if (fi->type != FIMG_TYPE_RGB) { + fprintf(stderr, "%s : type %d invalide\n", + __func__, fi->type); + return; + } + +nbre = fi->width * fi->height * fi->type; +// #if DEBUG_LEVEL +fprintf(stderr, "%s, nbre is %d\n", __func__, nbre); +// #endif +for (idx=0; idxR[idx] *= value; + } +} +/* ---------------------------------------------------------------- */ /* Warning: this function is _very_ slow */ void fimg_drand48(FloatImg *fi, float kmul) { diff --git a/lib/interpolate.c b/lib/interpolate.c index 03411f1..b73f54c 100644 --- a/lib/interpolate.c +++ b/lib/interpolate.c @@ -1,3 +1,6 @@ +/* + * interpolate.c + */ #include #include @@ -12,37 +15,53 @@ int verbosity; /* ---------------------------------------------------------------- */ static int gray_interpolate(FloatImg *s1, FloatImg *s2, FloatImg *d, float coef) { -return -1; -} -/* ---------------------------------------------------------------- */ - -int fimg_interpolate(FloatImg *s1, FloatImg *s2, FloatImg *d, float coef) -{ int picsize, idx; -if (FIMG_TYPE_RGB != s1->type) { - fprintf(stderr, "%s : bad src 1 type %d on %p\n", __func__, - s1->type, s1); - return -8; - } - -if (FIMG_TYPE_RGB != s2->type) { - fprintf(stderr, "%s : bad src 2 type %d on %p\n", __func__, - s2->type, s2); - return -8; - } -if (FIMG_TYPE_RGB != d->type) { - fprintf(stderr, "%s : bad dst type %d on %p\n", __func__, - d->type, d); - return -9; - } - -picsize = d->width * d->height * 3; +picsize = d->width * d->height; for (idx=0; idxR[idx] = (coef * s1->R[idx]) + ((1.0-coef) * s2->R[idx]); - + } + +return 0; +} +/* ---------------------------------------------------------------- */ +static int rgb_interpolate(FloatImg *s1, FloatImg *s2, FloatImg *d, float coef) +{ +int picsize, idx; + +picsize = d->width * d->height * 3; /* rude hack ? */ +for (idx=0; idxR[idx] = (coef * s1->R[idx]) + ((1.0-coef) * s2->R[idx]); + } + +return 0; +} +/* ---------------------------------------------------------------- */ +int fimg_interpolate(FloatImg *s1, FloatImg *s2, FloatImg *d, float coef) +{ +int foo; + +foo = fimg_images_compatible(s1, s2); +if (foo) { + fprintf(stderr, "compat -> %d\n", foo); + return foo; + } + +foo = fimg_images_compatible(s1, d); +if (foo) { + fprintf(stderr, "compat -> %d\n", foo); + return foo; + } + +switch (s1->type) { + case FIMG_TYPE_GRAY: + gray_interpolate(s1, s2, d, coef); break; + case FIMG_TYPE_RGB: + rgb_interpolate(s1, s2, d, coef); break; + default: + fprintf(stderr, "%s, %d is a bad type\n", __func__, s1->type); + return -18; } return 0; diff --git a/lib/t.c b/lib/t.c index 58eb451..f2a0eb2 100644 --- a/lib/t.c +++ b/lib/t.c @@ -72,56 +72,65 @@ foo = fimg_create(&dessin, W, H, 3); petit_dessin(&dessin); foo = fimg_create(&noise, W, H, 3); fimg_drand48(&noise, 0.1); - ****/ /* ---------------------------------------------------------------- */ #define W 320 #define H 240 int main(int argc, char *argv[]) { -int foo, idx; +int foo, idx, opt; float coef; FloatImg dessin, noise, result; char outname[100]; +int gray = 0; +int nb_img = 42; -verbosity = 1; +while ((opt = getopt(argc, argv, "gn:v")) != -1) { + switch(opt) { + case 'g': gray++; break; + case 'n': nb_img=atoi(optarg); break; + case 'v': verbosity++; break; + } + } + +if (verbosity) fimg_print_version(0); -fimg_print_version(1); foo = fimg_create_from_png("/home/tth/TMP/floatimg/s1.png", &dessin); if (foo) { fprintf(stderr, "s1 load err %d\n", foo); exit(1); } -fimg_describe(&dessin, "s1 dessin"); -fimg_to_gray(&dessin); +if (verbosity) fimg_describe(&dessin, "s1 dessin"); +if (gray) fimg_to_gray(&dessin); foo = fimg_create_from_png("/home/tth/TMP/floatimg/s2.png", &noise); if (foo) { fprintf(stderr, "s2 load err %d\n", foo); exit(1); } -fimg_describe(&noise, "s2 noise"); +if (verbosity) fimg_describe(&noise, "s2 noise"); +fimg_mul_cste(&noise, 0.50); +if (gray) fimg_to_gray(&noise); foo = fimg_create(&result, W, H, 3); +if (verbosity) fimg_describe(&result, "d result"); -#define NBRE 20 - -for (idx=0; idx