From 86a903360c80e6bb049957a81bbb91ab9c8862a2 Mon Sep 17 00:00:00 2001 From: tTh Date: Tue, 1 Nov 2022 09:35:40 +0100 Subject: [PATCH] fimg_filtre_morpho_0 up and running --- floatimg.h | 6 +++- funcs/Makefile | 5 ++- funcs/fmorpho.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++ funcs/t.c | 21 ++++++++---- funcs/tests.c | 69 ++++++++++++++++++++++++++++++++++++-- funcs/tests.h | 1 + 6 files changed, 180 insertions(+), 10 deletions(-) create mode 100644 funcs/fmorpho.c diff --git a/floatimg.h b/floatimg.h index f20fd1e8..f8cf5dc3 100644 --- a/floatimg.h +++ b/floatimg.h @@ -20,7 +20,7 @@ * https://git.tetalab.org/tTh/FloatImg */ -#define FIMG_VERSION (204) +#define FIMG_VERSION (205) #define RELEASE_NAME ("noname") /* XXX add a test for stdint.h / uint32_t XXX */ @@ -218,6 +218,10 @@ int fimg_auto_shift_to_zero(FloatImg *s, FloatImg *d); /* funcs/falsecolors.c */ int fimg_falsecolors_0(FloatImg *src, FloatImg *dst, int k, float valf); +/* funcs/fmorpho.c */ +int fimg_filtre_morpho_0(FloatImg *sfimg, FloatImg *dfimg, int index); + + /* --> funcs/plasmas.c */ int fimg_prototype_plasma(FloatImg *img, double time, int type); diff --git a/funcs/Makefile b/funcs/Makefile index 1ce197a3..af5515e9 100644 --- a/funcs/Makefile +++ b/funcs/Makefile @@ -9,7 +9,7 @@ DEPS = ../floatimg.h Makefile OBJS = fimg-png.o fimg-tiff.o misc-plots.o filtrage.o utils.o \ fimg-libpnm.o rampes.o rectangle.o \ sfx0.o sfx1.o sfx2.o sfx3.o sfx4.o \ - falsecolors.o \ + falsecolors.o fmorpho.o \ geometry.o rotate.o fimg-openexr.o \ equalize.o fimg-fits.o saturation.o histogram.o \ fimg-dicom.o \ @@ -60,6 +60,9 @@ incrustator.o: incrustator.c $(DEPS) displacement.o: displacement.c $(DEPS) gcc $(COPT) -c $< +fmorpho.o: fmorpho.c $(DEPS) + gcc $(COPT) -c $< + fimg-png.o: fimg-png.c $(DEPS) gcc $(COPT) -c $< diff --git a/funcs/fmorpho.c b/funcs/fmorpho.c new file mode 100644 index 00000000..a3a4ce55 --- /dev/null +++ b/funcs/fmorpho.c @@ -0,0 +1,88 @@ +/* + * FLOATIMG + * -------- + + * F M O R P H O + * + * nouveau 30 septembre 2022 / integration 28 octobre 2022 + */ + +#include +#include +#include +#include +#include + +/* --------------------------------------------------------------------- !*/ +static struct + { + int x, y; + } deltas[] = +{ { -1, -1, }, + { 0, -1, }, + { 1, -1, }, + { -1, 0, }, + { 0, 0, }, + { 1, 0, }, + { -1, 1, }, + { 0, 1, }, + { 1, 1 } +}; + +typedef struct + { + int x, y; // not used + float r, g, b; + float fgris; + int rang; + } fpixel; + +static fpixel pixels[9]; + +/* --------------------------------------------------------------------- !*/ +static int comparaison(const void *A, const void *B) +{ +return ((fpixel *)A)->fgris > ((fpixel *)B)->fgris; +} +/* --------------------------------------------------------------------- !*/ +/* + * this is a klugy approch, sorry. + */ +int fimg_filtre_morpho_0(FloatImg *sfimg, FloatImg *dfimg, int index) +{ +int xs, ys, loop9; +int xp, yp; +float rgb[3], fval; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, sfimg, dfimg, index); +#endif + +if ( (index<0) || (index>8)) { + fprintf(stderr, " %s: bad index %d\n", __func__, index); +#if MUST_ABORT + fflush(stderr); abort(); +#endif + return -1; + } + +fimg_clear(dfimg); + +for (ys=1; ysheight-1; ys++) { + for (xs=1; xswidth-1; xs++) { + for (loop9=0; loop9<9; loop9++) { + xp = xs + deltas[loop9].x; + yp = ys + deltas[loop9].y; + fimg_get_rgb(sfimg, xp, yp, rgb); + pixels[loop9].fgris = rgb[0]; + pixels[loop9].rang = loop9; + } + qsort(&pixels, 9, sizeof(fpixel), comparaison); + rgb[0] = rgb[1] = rgb[2] = pixels[index].fgris; + fimg_put_rgb(dfimg, xs, ys, rgb); + } + } + +return 0; +} +/* --------------------------------------------------------------------- !*/ diff --git a/funcs/t.c b/funcs/t.c index 96bb809b..93f1140b 100644 --- a/funcs/t.c +++ b/funcs/t.c @@ -25,7 +25,8 @@ enum nCmd { Equalize=1, Rotate, Sfx0, F3x3, MIRE, Wfits, Wpng, Wtiff, Displace, ReadPNG, Plasmas, Hilight, OpenEXR, Geometrie, FileType, Mirror, KillRGB, Pixelize,SplitLevel, DecompRgbz, DecompRgbg, - Rectangle, Dicom, Fakolor0, Fakolor3 }; + Rectangle, Dicom, Fakolor0, Fakolor3, + Fmorpho0 }; typedef struct { char *name; int Cmd; @@ -62,6 +63,7 @@ Command commands[] = { { "dicom", Dicom }, { "fakolor0", Fakolor0 }, { "fakolor3", Fakolor3 }, + { "fmorpho0", Fmorpho0 }, { NULL, 0 } } ; @@ -99,13 +101,17 @@ fprintf(stderr, "options:\n"); fprintf(stderr, "\t-k 1.414\tset float value\n"); fprintf(stderr, "\t-l\t\tlist tests\n"); fprintf(stderr, "\t-o \t\toutfile\n"); +fprintf(stderr, "\t-v \t\tincrease verbosity\n"); -fprintf(stderr, "commands:\n"); -pcmd = commands; -while (pcmd->name) { - fprintf(stderr, "\t%-15s %d\n", pcmd->name, pcmd->Cmd); - pcmd++; +if (verbosity) { + fprintf(stderr, "commands:\n"); + pcmd = commands; + while (pcmd->name) { + fprintf(stderr, "\t%-15s %d\n", pcmd->name, pcmd->Cmd); + pcmd++; + } } + fprintf(stderr, "\ncompiled on "__DATE__" at "__TIME__"\n"); if (k) fimg_print_version(k); @@ -253,6 +259,9 @@ switch(opt) { case Fakolor0: foo = essai_0_fausses_couleurs(outfile, 0); break; + case Fmorpho0: + foo = essai_fmorpho_0(filename, "/tmp/fmorpho", 0); + break; default: fprintf(stderr, "'%s' is a bad command\n", command); exit(1); diff --git a/funcs/tests.c b/funcs/tests.c index 759fd0e5..25e68be0 100644 --- a/funcs/tests.c +++ b/funcs/tests.c @@ -1,4 +1,7 @@ /* + * FLOATIMG + * -------- + * tests des fonctions diverses - subroutines see also: t.c */ @@ -17,6 +20,70 @@ extern int verbosity; +#define W 1024 +#define H 768 + +/* --------------------------------------------------------------------- */ +/* + * nouveau 30 octobre 2022 --> fmorpho.c + */ + +int essai_fmorpho_0(char *infile, char *basefname, int k) +{ +FloatImg src, dst; +int idx, foo; +char fname[100]; + +fprintf(stderr, ">>> %s ( '%s' '%s' %d )\n", __func__, infile, basefname, k); + +if (k) { + fprintf(stderr, "%s k=%d must be 0\n", __func__, k); + return -6; + } + +if (NULL == infile ) { + foo = fimg_create(&src, W, H, FIMG_TYPE_RGB); + if (foo) { + fprintf(stderr, "%s: create -> %d\n",__func__, foo); + return foo; + } + foo = fimg_test_pattern(&src, 0, 254.99); + if (foo) { + fprintf(stderr, "%s: test_pattern -> %d\n",__func__, foo); + return foo; + } + } +else { + foo = fimg_create_from_dump(infile, &src); + if (foo) { + fprintf(stderr, "%s: error %d loading '%s'\n", __func__, foo, infile); + return foo; + } + } + +fimg_save_as_pnm(&src, "foo.pnm", 0); + +foo = fimg_clone(&src, &dst, 0); +if (foo) { + fprintf(stderr, "%s: clone -> %d\n",__func__, foo); + return foo; + } + +for (idx=0; idx<9; idx++) { + sprintf(fname, "%s%04d.pnm", basefname, idx); + fprintf(stderr, " ---> %s\n", fname); + foo = fimg_filtre_morpho_0(&src, &dst, idx); + if (foo) { + fprintf(stderr, "%s: got a %d at round %d\n", __func__, foo, idx); + return foo; + } + fimg_save_as_pnm(&dst, fname, 0); + } + +fimg_destroy(&src); fimg_destroy(&dst); + +return 0; +} /* --------------------------------------------------------------------- */ /* nouveau 18 mai 2022 */ int essai_0_fausses_couleurs(char *dstfile, int type) @@ -24,8 +91,6 @@ int essai_0_fausses_couleurs(char *dstfile, int type) FloatImg src, dst; int foo; -#define W 1024 -#define H 768 fprintf(stderr, "\nEssais fausses couleurs (type %d) -> '%s'\n", type, dstfile); foo = fimg_create(&src, W, H, FIMG_TYPE_RGB); diff --git a/funcs/tests.h b/funcs/tests.h index bca80982..cfbc3495 100644 --- a/funcs/tests.h +++ b/funcs/tests.h @@ -34,6 +34,7 @@ int essai_lecture_png(char *fname, char *outfile, int notused); int essai_highlights(char *inf, char *outf, int ikoef, float fkoef); int essai_openexr(char *inf, char *outf, int flags); +int essai_fmorpho_0(char *infile, char *basefname, int k); int essai_pixelize(char *infile, char *outfile);