From d867b905a80fad3f20725e5434c0ff233f11305f Mon Sep 17 00:00:00 2001 From: tth Date: Tue, 23 Feb 2021 03:49:24 +0100 Subject: [PATCH] tracking a math bug in filters3x3 --- Fonderie/crapulator.c | 16 +++++++++++----- floatimg.h | 2 +- funcs/filtrage.c | 9 ++++----- funcs/tests.c | 33 +++++++++++++++++++++++++-------- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/Fonderie/crapulator.c b/Fonderie/crapulator.c index 62fca29..d6b103b 100644 --- a/Fonderie/crapulator.c +++ b/Fonderie/crapulator.c @@ -14,6 +14,8 @@ #include "glitches.h" #include "sfx.h" +extern int verbosity; + /* -------------------------------------------------------------- */ static int trier_les_pixels(FloatImg *pimg) { @@ -76,7 +78,7 @@ return 0; static int insitu_filtre3x3(FloatImg *pimg, int typef) { FloatImg img; -int retval; +int foo, retval; FimgFilter3x3 *pfiltre; FimgFilter3x3 lowpass = { @@ -103,7 +105,7 @@ FimgFilter3x3 diagonal = { 1.0, 0.0, -1.0, 0.0, -1.0, -2.0, }, - 1.0, 0.0 + 1.0, 4.0 }; switch (typef) { @@ -124,8 +126,12 @@ if (retval) { fprintf(stderr, "%s error %d on filter\n", __func__, retval); exit(1); } -/* - * may be, here, we can check for negative values ? */ +/** may be, we can check for negative values ? */ +if (verbosity) { + foo = fimg_count_negativ(&img); + fprintf(stderr, "%s -> %d negs\n", __func__, foo); + } + fimg_killborders(&img); fimg_copy_data(&img, pimg); fimg_destroy(&img); @@ -206,7 +212,7 @@ switch (idFx) { break; case CR_liss2x2: retval = fimg_lissage_2x2(image); - (void)fimg_killborders(image); + // (void)fimg_killborders(image); break; case CR_liss3x3: /* smooth filter */ diff --git a/floatimg.h b/floatimg.h index 4b0acbc..826ae7d 100644 --- a/floatimg.h +++ b/floatimg.h @@ -3,7 +3,7 @@ * ugly code from tTh */ -#define FIMG_VERSION 118 +#define FIMG_VERSION 119 /* * in memory descriptor diff --git a/funcs/filtrage.c b/funcs/filtrage.c index 5f46c38..7e7c6a4 100644 --- a/funcs/filtrage.c +++ b/funcs/filtrage.c @@ -15,7 +15,7 @@ float *M; /* alias of filter matrix */ double dval; #if DEBUG_LEVEL -fprintf(stderr, ">>> %s ( %p %p %p)\n", __func__, src, dst, filtr); +fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, src, dst, filtr); #endif if (src->type != FIMG_TYPE_RGB) { @@ -48,7 +48,7 @@ for (y=1; y < h-1; y++) { M[7] * pr[of+w] + M[8] * pr[of+(w-1)] ; - dst->R[of] = dval; + dst->R[of] = dval + filtr->offset; dval = M[0] * pg[of-(w+1)] + M[1] * pg[of-w] + @@ -60,7 +60,7 @@ for (y=1; y < h-1; y++) { M[7] * pg[of+w] + M[8] * pg[of+(w-1)] ; - dst->G[of] = dval; + dst->G[of] = dval + filtr->offset; dval = M[0] * pb[of-(w+1)] + M[1] * pb[of-w] + @@ -72,7 +72,7 @@ for (y=1; y < h-1; y++) { M[7] * pb[of+w] + M[8] * pb[of+(w-1)] ; - dst->B[of] = dval; + dst->B[of] = dval + filtr->offset; } } @@ -189,7 +189,6 @@ if (foo) { return foo; } -/* XXX */ fimg_killborders(img); return foo; diff --git a/funcs/tests.c b/funcs/tests.c index 3d61ee4..df327ad 100644 --- a/funcs/tests.c +++ b/funcs/tests.c @@ -323,7 +323,7 @@ FimgFilter3x3 filter_a = { { 1.0, 1.0, 1.0, 1.0, -3.0, 1.0, 1.0, 1.0, 1.0 }, - 8.0, 0.0 + 9.0, 0.0 }; @@ -332,10 +332,19 @@ FimgFilter3x3 filter_b = { { -2.0, -1.0, 0.0, -1.0, 3.0, 1.0, 0.0, 1.0, 2.0 }, - 8.0, 0.0 + 8.0, 0.0 }; +FimgFilter3x3 filter_c = { + { + 2.0, 1.0, 0.0, + 1.0, 0.0, -1.0, + 0.0, -1.0, -2.0, + }, + 1.0, 8.0 + }; + if (NULL != infile) { fprintf(stderr, "%s: loading %s\n", __func__, infile); foo = fimg_create_from_dump(infile, &src); @@ -351,6 +360,8 @@ else { } // fimg_save_as_png(&src, "test.png", 0); +foo = fimg_count_negativ(&src); +fprintf(stderr, "%s: source have %d negs\n", __func__, foo); foo = fimg_clone(&src, &dst, 0); if (foo) { @@ -359,21 +370,27 @@ if (foo) { } fimg_filter_3x3(&src, &dst, &filter_a); - foo = fimg_clamp_negativ(&dst); - if (foo) { - fprintf(stderr, "clamped %d negative pixels\n", foo); + fprintf(stderr, "A clamped %d negative pixels\n", foo); } -// foo = fimg_save_as_png(&dst, "f3x3a.png", 0); +foo = fimg_save_as_png(&dst, "f3x3a.png", 0); // foo = fimg_save_as_pnm(&dst, "f3x3a.pnm", 0); fimg_filter_3x3(&src, &dst, &filter_b); foo = fimg_clamp_negativ(&dst); if (foo) { - fprintf(stderr, "clamped %d negative pixels\n", foo); + fprintf(stderr, "B clamped %d negative pixels\n", foo); } -// foo = fimg_save_as_png(&dst, "f3x3b.png", 0); +foo = fimg_save_as_png(&dst, "f3x3b.png", 0); +// foo = fimg_save_as_pnm(&dst, "f3x3a.pnm", 0); + +fimg_filter_3x3(&src, &dst, &filter_c); +foo = fimg_clamp_negativ(&dst); +if (foo) { + fprintf(stderr, "C clamped %d negative pixels\n", foo); + } +foo = fimg_save_as_png(&dst, "f3x3b.png", 0); // foo = fimg_save_as_pnm(&dst, "f3x3a.pnm", 0); fimg_destroy(&src); fimg_destroy(&dst);