From 3632dc1680700ab0b28abe2dc7de197007fd8690 Mon Sep 17 00:00:00 2001 From: tth Date: Wed, 17 Mar 2021 18:32:51 +0100 Subject: [PATCH] more clenaup, expect more bugs --- floatimg.h | 12 +++++----- funcs/contour2x2.c | 4 ++-- funcs/displacement.c | 7 ++++-- funcs/exporter.c | 11 +--------- funcs/filtrage.c | 5 ++++- funcs/fimg-fits.c | 2 -- funcs/fimg-tiff.c | 6 ++--- funcs/histogram.c | 5 +++++ funcs/misc-plots.c | 10 ++++----- funcs/rampes.c | 2 +- funcs/sfx0.c | 3 +++ funcs/t.c | 2 -- funcs/tests.c | 6 +++-- funcs/utils.c | 6 ++--- lib/contrast.c | 16 ++++---------- lib/fimg-core.c | 26 ++++++++++++---------- lib/fimg-file.c | 11 +++++----- lib/fimg-math.c | 52 ++++++++++++++++++++++++-------------------- lib/fimg-pnm.c | 4 +--- lib/fimg-timers.c | 2 +- lib/interpolate.c | 4 +++- lib/operators.c | 4 ---- lib/t.c | 35 ----------------------------- 23 files changed, 98 insertions(+), 137 deletions(-) diff --git a/floatimg.h b/floatimg.h index 05c934f3..623a4b29 100644 --- a/floatimg.h +++ b/floatimg.h @@ -1,9 +1,10 @@ /* * floatimg.h * ugly code from tTh + * http://la.buvette.org/photos/cumul */ -#define FIMG_VERSION 122 +#define FIMG_VERSION 124 /* * in memory descriptor @@ -174,12 +175,12 @@ float fimg_get_maxvalue(FloatImg *head); int fimg_get_minmax_rgb(FloatImg *head, float mmvals[6]); 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_add_cste(FloatImg *fi, float value); +int fimg_mul_cste(FloatImg *fi, float value); int fimg_ajust_from_grab(FloatImg *fi, double maxima, int notused); void fimg_drand48(FloatImg *fi, float kmul); -int fimg_count_negativ(FloatImg *fi); -int fimg_clamp_negativ(FloatImg *fi); +long fimg_count_negativ(FloatImg *fi); +long fimg_clamp_negativ(FloatImg *fi); /* various funcs modules */ int fimg_load_from_png(char *filename, FloatImg *fimg); @@ -190,6 +191,7 @@ int fimg_save_as_bmp(FloatImg *src, char *outname, int flags); int fimg_test_pattern(FloatImg *fimg, int type, double dval); int fimg_draw_something(FloatImg *fimg); +int fimg_multirandom(FloatImg *fimg, long nbpass); /* file is 'funcs/utils.c' */ void fimg_print_minmax(float minmax[6], char *titre); diff --git a/funcs/contour2x2.c b/funcs/contour2x2.c index da6d824b..694c52a2 100644 --- a/funcs/contour2x2.c +++ b/funcs/contour2x2.c @@ -1,5 +1,5 @@ /* - * classif.c + * Detection de contours */ #include @@ -26,7 +26,7 @@ int tbl[] = /* deep magic inside */ 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0 - }; + }; /* please explain */ #if DEBUG_LEVEL fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, psrc, pdst, reverse); diff --git a/funcs/displacement.c b/funcs/displacement.c index bb0cf491..1d3b40db 100644 --- a/funcs/displacement.c +++ b/funcs/displacement.c @@ -11,7 +11,6 @@ extern int verbosity; -/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */ /* nouveau 24 octobre 2020, pendant le masque-flamme coronavidique */ @@ -48,7 +47,11 @@ dltg = minmax[3] - minmax[2]; dltb = minmax[5] - minmax[4]; in = out = 0; - +/* + * BE WARNED ! + * This code doesn't work as expected, so I have to + * rewrite it, maybe when the pandemic is closed... + */ for (y=0; yheight; y++) { for (x=0; xwidth; x++) { diff --git a/funcs/exporter.c b/funcs/exporter.c index 3e8340c4..7eada001 100644 --- a/funcs/exporter.c +++ b/funcs/exporter.c @@ -26,45 +26,36 @@ fprintf(stderr, ">>> %s ( %p '%s' 0x%X )\n", __func__, filetype = format_from_extension(fname); if (verbosity > 1) { - fprintf(stderr, "file %s : type %d\n", fname, filetype); + fprintf(stderr, "file %s have type %d\n", fname, filetype); } switch(filetype) { - case FILE_TYPE_FIMG: foo = fimg_dump_to_file(pic, fname, 0); break; - case FILE_TYPE_PNM: foo = fimg_save_as_pnm(pic, fname, 0); break; - case FILE_TYPE_PNG: foo = fimg_save_as_png(pic, fname, 0); break; - case FILE_TYPE_TGA: fprintf(stderr, "%s: FILE_TYPE_TGA not implemented\n", __func__); foo = -666; break; - case FILE_TYPE_TIFF: foo = fimg_write_as_tiff(pic, fname, 0); break; - case FILE_TYPE_FITS: foo = fimg_save_R_as_fits(pic, fname, 0); break; - case FILE_TYPE_BMP: fprintf(stderr, "%s: file type BMP not implemented\n", __func__); foo = -666; - default: foo = -1789; break; - } if (foo) { diff --git a/funcs/filtrage.c b/funcs/filtrage.c index 7e7c6a4a..d3db336f 100644 --- a/funcs/filtrage.c +++ b/funcs/filtrage.c @@ -26,6 +26,10 @@ if (dst->type != FIMG_TYPE_RGB) { fprintf(stderr, "%s: dst type %d invalid\n", __func__, dst->type); return -99; } +if (fimg_images_not_compatible(src, dst)) { + fprintf(stderr, "%s: src & dst not comatibles\n", __func__); + return -98; + } /* aliasing some vars for cleaner code */ pr = src->R; pg = src->G; pb = src->B; @@ -146,7 +150,6 @@ h = img->height; w = img->width; for (idx=0; idxR[idx*w] = 0.0; img->G[idx*w] = 0.0; diff --git a/funcs/fimg-fits.c b/funcs/fimg-fits.c index 71ed9eeb..7e5a6da0 100644 --- a/funcs/fimg-fits.c +++ b/funcs/fimg-fits.c @@ -43,7 +43,6 @@ switch (plane) { return -66; } - remove(outname); /* Delete old file if it already exists */ if (fits_create_file(&fptr, outname, &status)) { fits_report_error(stderr, status); @@ -53,7 +52,6 @@ if (fits_create_file(&fptr, outname, &status)) { naxes[0] = src->width; naxes[1] = src->height; if (verbosity) fimg_describe(src, "to be saved as FITS"); - if ( fits_create_img(fptr, bitpix, naxis, naxes, &status) ) { fits_report_error(stderr, status); return -10; diff --git a/funcs/fimg-tiff.c b/funcs/fimg-tiff.c index bd66fdf4..7f084a2a 100644 --- a/funcs/fimg-tiff.c +++ b/funcs/fimg-tiff.c @@ -54,9 +54,9 @@ TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 3); // RGB TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 16); // 0->65535 TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); -TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); +TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); -sprintf(ligne, "lib FloatImg %d by tTh", FIMG_VERSION); +sprintf(ligne, "lib FloatImg v %d by tTh", FIMG_VERSION); TIFFSetField(tiff, TIFFTAG_SOFTWARE, ligne); foo = src->width * 3; @@ -66,7 +66,6 @@ if (verbosity) fprintf(stderr, "default strip size %d\n", foo); TIFFSetField(tiff, TIFFTAG_ROWSPERSTRIP, foo); for (y=0; yheight; y++) { - ptr = linebuff; idx = y * src->width; @@ -78,7 +77,6 @@ for (y=0; yheight; y++) { } TIFFWriteScanline(tiff, linebuff, y, 0); - idx += src->width; } diff --git a/funcs/histogram.c b/funcs/histogram.c index f10118cd..bd865246 100644 --- a/funcs/histogram.c +++ b/funcs/histogram.c @@ -22,6 +22,11 @@ float rgb[3], moy; fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, src, ghist, sz); #endif +if (FIMG_TYPE_RGB != src->type) { + fprintf(stderr, "%s: bad type %d of image\n", __func__, src->type); + return -97; + } + maxval = fimg_get_maxvalue(src); fprintf(stderr, "maximum is %f\n", maxval); diff --git a/funcs/misc-plots.c b/funcs/misc-plots.c index beebfe83..32c095b5 100644 --- a/funcs/misc-plots.c +++ b/funcs/misc-plots.c @@ -1,7 +1,6 @@ /* - testing some random funcs. - -*/ + * This is an eternal WIP, sorry... + */ #include #include @@ -56,7 +55,6 @@ for (x=0; xwidth; x++) { // fprintf(stderr, " %6d %f\n", x, val); } - return 0; } /* --------------------------------------------------------------------- */ @@ -88,14 +86,14 @@ for (y=1; yheight; y++) { return 0; } /* --------------------------------------------------------------------- */ -int fimg_multirandom(FloatImg *fimg) +int fimg_multirandom(FloatImg *fimg, long nbpass) { int foo, x, y; #define RI ( (rand()/7) + (rand()/9) ) #define RD ( (drand48()/7) + (drand48()/7) ) -for (foo=0; foo<100000000; foo++) +for (foo=0; foowidth; y = RI % fimg->height; diff --git a/funcs/rampes.c b/funcs/rampes.c index 491b68b7..d2079723 100644 --- a/funcs/rampes.c +++ b/funcs/rampes.c @@ -34,7 +34,7 @@ return 0; } /* --------------------------------------------------------------------- */ /* - * wtf ? ther's no way to have the black at the bottom ? + * To have the black at the bottom, use a negative dcoef */ int fimg_vdeg_a(FloatImg *img, double dcoef) { diff --git a/funcs/sfx0.c b/funcs/sfx0.c index 729b8880..7c46bd0d 100644 --- a/funcs/sfx0.c +++ b/funcs/sfx0.c @@ -33,6 +33,9 @@ for (foo=0; foo %d\n", foo); diff --git a/funcs/utils.c b/funcs/utils.c index 78aa0c1b..67fafa98 100644 --- a/funcs/utils.c +++ b/funcs/utils.c @@ -1,4 +1,6 @@ - +/* + * FloatImg from tTh - 2021 + */ #include #include @@ -15,7 +17,6 @@ fprintf(stderr, "\t\tminmax %s\n", titre); fprintf(stderr, "red\t\t%10f %10f\n", minmax[0], minmax[1]); fprintf(stderr, "green\t\t%10f %10f\n", minmax[2], minmax[3]); fprintf(stderr, "blue\t\t%10f %10f\n", minmax[4], minmax[5]); - } /* --------------------------------------------------------------------- */ int parse_WxH(char *str, int *pw, int *ph) @@ -33,7 +34,6 @@ if (2 != foo) { fprintf(stderr, "%s : arg '%s' is invalid\n", __func__, str); return foo; } - *pw = w; *ph = h; return 2; diff --git a/lib/contrast.c b/lib/contrast.c index 47e387e9..b1c9fa70 100644 --- a/lib/contrast.c +++ b/lib/contrast.c @@ -39,9 +39,7 @@ if (s->type != FIMG_TYPE_RGB) { return -4; } -if (NULL==d) { - d = s; - } +if (NULL==d) { d = s; } else { if (d->type != FIMG_TYPE_RGB) { fprintf(stderr, "%s : dst type %d invalide\n", @@ -76,9 +74,7 @@ if (s->type != FIMG_TYPE_RGB) { return -4; } -if (NULL==d) { - d = s; - } +if (NULL==d) { d = s; } else { if (d->type != FIMG_TYPE_RGB) { fprintf(stderr, "%s: dst type %d invalide\n", @@ -119,9 +115,7 @@ if (s->type != FIMG_TYPE_RGB) { return -4; } -if (NULL==d) { - d = s; - } +if (NULL==d) { d = s; } else { if (d->type != FIMG_TYPE_RGB) { fprintf(stderr, "%s : dst type %d invalide\n", @@ -155,9 +149,7 @@ if (s->type != FIMG_TYPE_RGB) { return -4; } -if (NULL==d) { - d = s; - } +if (NULL==d) { d = s; } else { if (d->type != FIMG_TYPE_RGB) { fprintf(stderr, "%s : dst type %d invalide\n", diff --git a/lib/fimg-core.c b/lib/fimg-core.c index 16d2b680..33856523 100644 --- a/lib/fimg-core.c +++ b/lib/fimg-core.c @@ -42,9 +42,9 @@ fprintf(stderr, "*** FloatImg library, alpha %d (%s, %s)\n", FIMG_VERSION, __DATE__, __TIME__); if (51 == k) { - puts("+--------------------+"); - puts("| Pastis is coming. |"); - puts("+--------------------+"); + puts("+------------------------+"); + puts("| Pastis is coming soon |"); + puts("+------------------------+"); } return 0; @@ -118,7 +118,7 @@ if ( ! fimg_type_is_valid(type) ) { /* * what to do if we've got a descriptor for an image - * already allocated ? + * already allocated ? and how to check that ? */ memset(fimg, 0, sizeof(FloatImg)); @@ -135,10 +135,11 @@ if (FIMG_TYPE_RGB == type) { } if (FIMG_TYPE_RGBA == type) { + fimg->G = (float *)plane_alloc(size); + fimg->B = (float *)plane_alloc(size); fimg->A = (float *)plane_alloc(size); } - return 0; } @@ -165,9 +166,8 @@ if (NULL == fimg->R) { return -3; } - free(fimg->R); -if (FIMG_TYPE_RGB==fimg->type) { +if (FIMG_TYPE_RGB == fimg->type) { free(fimg->G); free(fimg->B); } @@ -178,7 +178,7 @@ return 0; /* --------------------------------------------------------------------- */ int fimg_clone(FloatImg *old, FloatImg *new, int flags) { -int foo; +int foo, size; #if DEBUG_LEVEL fprintf(stderr, ">>> %s ( %p %p 0x%x )\n", __func__, old, new, flags); @@ -197,9 +197,11 @@ if (foo) { return -3; } -if (flags & 0x01) { - /* XXX copy all the pixels's datas */ - +if (flags & 0x01) { /* copy pixels values */ + size = old->width * old->height * sizeof(float); + memcpy(new->R, old->R, size); + memcpy(new->G, old->G, size); + memcpy(new->B, old->B, size); } return 0; @@ -240,7 +242,7 @@ if ( ! fimg_type_is_valid(fimg->type) ) { return -2; } -size = fimg->width * fimg->height * fimg->type * sizeof(float); +size = fimg->width * fimg->height * sizeof(float); memset(fimg->R, 0, size); return 0; diff --git a/lib/fimg-file.c b/lib/fimg-file.c index e967e043..1c872b10 100644 --- a/lib/fimg-file.c +++ b/lib/fimg-file.c @@ -40,7 +40,7 @@ fclose(fp); fprintf(stderr, " magic [%s]\n", filehead.magic); #endif -if (strncmp(filehead.magic, "FIMG", 4)) { +if (memcmp(filehead.magic, "FIMG", 4)) { fprintf(stderr, "'%s' is not a fimg file.\n", fname); return -3; } @@ -169,6 +169,7 @@ int fimg_create_from_dump(char *fname, FloatImg *head) { FILE *fp; int foo, size; +long nbread; FimgFileHead filehead; #if DEBUG_LEVEL @@ -205,10 +206,10 @@ if (foo) { } size = filehead.w * filehead.h; -foo = 0; -foo += fread(head->R, sizeof(float), size, fp); -foo += fread(head->G, sizeof(float), size, fp); -foo += fread(head->B, sizeof(float), size, fp); +nbread = 0; +nbread += fread(head->R, sizeof(float), size, fp); +nbread += fread(head->G, sizeof(float), size, fp); +nbread += fread(head->B, sizeof(float), size, fp); fclose(fp); diff --git a/lib/fimg-math.c b/lib/fimg-math.c index 6975866d..c734941a 100644 --- a/lib/fimg-math.c +++ b/lib/fimg-math.c @@ -77,11 +77,6 @@ for (idx=0; idx mmvals[5]) mmvals[5] = fval; } -#if 0 -for (foo=0; foo<6; foo++) { - fprintf(stderr, "%3d %g\n", foo, mmvals[foo]); - } -#endif return -0; } @@ -97,10 +92,10 @@ if (surface < 1) return -1; memset(accus, 0, 4*sizeof(double)); for (idx=0; idxR[idx]; + accus[0] += (double)head->R[idx]; if (head->type > 2) { /* WTF ? */ - accus[1] += head->G[idx]; - accus[2] += head->B[idx]; + accus[1] += (double)head->G[idx]; + accus[2] += (double)head->B[idx]; } } @@ -122,7 +117,6 @@ return 0; Mais c,a ne semble pas etre la bonne methode. Il faut aller voir : https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance */ - /* ---------------------------------------------------------------- */ /* * more elaborate functions are in fimg-2gray.c @@ -144,17 +138,17 @@ for (foo=0; foo<(head->width*head->height); foo++) { add += head->B[foo]; head->R[foo] = head->G[foo] = head->B[foo] = add / 3.0; } -return -1; +return 0; } /* ---------------------------------------------------------------- */ -void fimg_add_cste(FloatImg *fi, float value) +int fimg_add_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; + return -44; } nbre = fi->width * fi->height ; @@ -166,12 +160,13 @@ for (idx=0; idxG[idx] += value; fi->B[idx] += value; } +return 0; } /* ---------------------------------------------------------------- */ -int fimg_count_negativ(FloatImg *fi) +long fimg_count_negativ(FloatImg *fi) { int nbre, idx; -int count; +long count; if (fi->type != FIMG_TYPE_RGB) { fprintf(stderr, "%s : type %d invalide\n", @@ -183,19 +178,19 @@ nbre = fi->width * fi->height; count = 0; for (idx=0; idxR[idx] < 0.0) count++; - if (fi->G[idx] < 0.0) count++; - if (fi->B[idx] < 0.0) count++; + if (fi->R[idx] < 0.0) count++; + if (fi->G[idx] < 0.0) count++; + if (fi->B[idx] < 0.0) count++; } return count; } /* ---------------------------------------------------------------- */ /* nouveau 29 fevrier 2020 */ -int fimg_clamp_negativ(FloatImg *fi) +long fimg_clamp_negativ(FloatImg *fi) { int nbre, idx; -int count; +long count; if (fi->type != FIMG_TYPE_RGB) { fprintf(stderr, "%s : type %d invalide\n", @@ -203,13 +198,18 @@ if (fi->type != FIMG_TYPE_RGB) { return -1; } -nbre = fi->width * fi->height * fi->type; +nbre = fi->width * fi->height; count = 0; for (idx=0; idxR[idx] < 0.0) { - fi->R[idx] = 0.0; - count++; + fi->R[idx] = 0.0; count++; + } + if (fi->G[idx] < 0.0) { + fi->G[idx] = 0.0; count++; + } + if (fi->B[idx] < 0.0) { + fi->B[idx] = 0.0; count++; } } @@ -229,14 +229,14 @@ for (idx=0; idxtype != FIMG_TYPE_RGB) && (fi->type != FIMG_TYPE_GRAY) ) { fprintf(stderr, "%s : type %d invalide\n", __func__, fi->type); - return; + return -44; } nbre = fi->width * fi->height; @@ -255,8 +255,12 @@ if (fi->type == FIMG_TYPE_GRAY) { fi->R[idx] *= value; } } +return 0; } /* ---------------------------------------------------------------- */ +/* + * oh, please explain the usecase of this function ! + */ int fimg_ajust_from_grab(FloatImg *fi, double maxima, int notused) { double coef; diff --git a/lib/fimg-pnm.c b/lib/fimg-pnm.c index 1d60af60..cbbbd59a 100644 --- a/lib/fimg-pnm.c +++ b/lib/fimg-pnm.c @@ -190,9 +190,7 @@ switch(head->type) { break; } -fputs("\n", fp); - -fclose(fp); +fputs("\n", fp); fclose(fp); return 0; } diff --git a/lib/fimg-timers.c b/lib/fimg-timers.c index 18af9dee..bae0721f 100644 --- a/lib/fimg-timers.c +++ b/lib/fimg-timers.c @@ -1,5 +1,5 @@ /* - * timers.c + * timers.c BUGS INSIDE ? */ #include diff --git a/lib/interpolate.c b/lib/interpolate.c index b64f1114..1807d487 100644 --- a/lib/interpolate.c +++ b/lib/interpolate.c @@ -29,9 +29,11 @@ static int rgb_interpolate(FloatImg *s1, FloatImg *s2, FloatImg *d, float coef) { int picsize, idx; -picsize = d->width * d->height * 3; /* rude hack ? */ +picsize = d->width * d->height; for (idx=0; idxR[idx] = (coef * s1->R[idx]) + ((1.0-coef) * s2->R[idx]); + d->G[idx] = (coef * s1->G[idx]) + ((1.0-coef) * s2->G[idx]); + d->B[idx] = (coef * s1->B[idx]) + ((1.0-coef) * s2->B[idx]); } return 0; diff --git a/lib/operators.c b/lib/operators.c index 00bc49fa..14a5056e 100644 --- a/lib/operators.c +++ b/lib/operators.c @@ -17,7 +17,6 @@ extern int verbosity; /* must be declared around main() */ /* ---------------------------------------------------------------- */ /* * A + B -> D - * why is this func so slow ? */ int fimg_add_3(FloatImg *a, FloatImg *b, FloatImg *d) { @@ -63,9 +62,6 @@ if (FIMG_TYPE_RGB != a->type || FIMG_TYPE_RGB != b->type) { nbpixels = a->width * a->height; - /* are we cache-friendly here ? - * we need an accurate benchmarking... */ - for (idx=0; idxR[idx] += a->R[idx]; b->G[idx] += a->G[idx]; diff --git a/lib/t.c b/lib/t.c index a84b48b1..308d0261 100644 --- a/lib/t.c +++ b/lib/t.c @@ -26,44 +26,9 @@ B = fimg_timer_get(uuuh); fprintf(stderr, " %f %f\n", A, B); - return 0; } /* ---------------------------------------------------------------- */ -int essai_normalize(void) -{ -FloatImg A; -float val; -int foo; - -#if 0 -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); - -fimg_destroy(&A); - -#endif - -return 0; -} /* ---------------------------------------------------------------- */ #define WI 1024 #define HI 768