From b9d963dfa26f815460539106ea8a686bccb45367 Mon Sep 17 00:00:00 2001 From: tth Date: Tue, 9 Mar 2021 11:55:48 +0100 Subject: [PATCH] big refactoring step 1, expect more bugs --- floatimg.h | 2 +- funcs/contour2x2.c | 6 +++- funcs/qsortrgb.c | 4 +++ funcs/rotate.c | 4 +-- lib/Makefile | 2 +- lib/contrast.c | 30 ++++++++++++----- lib/fimg-core.c | 84 ++++++++++++++++++++++++++-------------------- lib/fimg-file.c | 65 ++++++++++++++++++++++------------- lib/fimg-math.c | 27 ++++++++++----- lib/t.c | 16 +++++---- tools/README.md | 9 +++++ tools/fimg2text.c | 20 +++++------ tools/fimgops.c | 3 +- tools/mkfimg.c | 7 +++- 14 files changed, 179 insertions(+), 100 deletions(-) diff --git a/floatimg.h b/floatimg.h index 0a697375..05c934f3 100644 --- a/floatimg.h +++ b/floatimg.h @@ -3,7 +3,7 @@ * ugly code from tTh */ -#define FIMG_VERSION 120 +#define FIMG_VERSION 122 /* * in memory descriptor diff --git a/funcs/contour2x2.c b/funcs/contour2x2.c index 87c42359..da6d824b 100644 --- a/funcs/contour2x2.c +++ b/funcs/contour2x2.c @@ -49,8 +49,12 @@ else { } foo = fimg_meanvalues(psrc, avg); +if (foo) { + fprintf(stderr, "%s: err %d on fimg_meanvalues\n", __func__, foo); + return foo; + } if (verbosity > 1) { - fprintf(stderr, "mean values : %f %f %f\n", avg[0], avg[1], avg[2]); + fprintf(stderr, "%s: %f %f %f\n", __func__, avg[0], avg[1], avg[2]); } #define RP(ix, iy) ( psrc->R[((iy)*psrc->width)+(ix)] < avg[0] ) diff --git a/funcs/qsortrgb.c b/funcs/qsortrgb.c index 233cde48..c561a5ad 100644 --- a/funcs/qsortrgb.c +++ b/funcs/qsortrgb.c @@ -38,6 +38,10 @@ if (fimg_images_not_compatible(psrc, pdst)) { if (psrc != pdst) { /* optimize or futurbug ? */ foo = fimg_copy_data(psrc, pdst); + if (foo) { + fprintf(stderr, "%s: err %d on copy data\n", __func__, foo); + return foo; + } } szimg = pdst->width * pdst->height; diff --git a/funcs/rotate.c b/funcs/rotate.c index 1262965c..77b4816a 100644 --- a/funcs/rotate.c +++ b/funcs/rotate.c @@ -15,7 +15,7 @@ extern int verbosity; int fimg_rotate_90(FloatImg *src, FloatImg *dst, int notused) { int foo; -int x, y, j, k; +int x, y, k; float rgb[3]; #if DEBUG_LEVEL @@ -56,7 +56,7 @@ if ( (src->type != dst->type) || for (y=0; yheight; y++) { for (x=0; xwidth; x++) { fimg_get_rgb(src, x, y, rgb); - j = (dst->height - x) - 1; + // XXX ??? j = (dst->height - x) - 1; k = (dst->width - y) - 1; #if DEBUG_LEVEL > 1 fprintf(stderr, "%6d %6d\n", k, j); diff --git a/lib/Makefile b/lib/Makefile index 0f3dfeac..25170ce7 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -2,7 +2,7 @@ # building the base library # -COPT = -Wall -fpic -g -no-pie -DDEBUG_LEVEL=0 +COPT = -Wall -fpic -g -no-pie -DDEBUG_LEVEL=1 OBJS = fimg-core.o fimg-pnm.o fimg-file.o fimg-math.o \ fimg-timers.o operators.o fimg-2gray.o \ diff --git a/lib/contrast.c b/lib/contrast.c index 567fd362..47e387e9 100644 --- a/lib/contrast.c +++ b/lib/contrast.c @@ -51,11 +51,15 @@ else { } -nbre = s->width * s->height * 3; +nbre = s->width * s->height; for (idx=0; idxR[idx] / maxval; d->R[idx] = maxval * sqrt(dval); + dval = s->G[idx] / maxval; + d->G[idx] = maxval * sqrt(dval); + dval = s->B[idx] / maxval; + d->B[idx] = maxval * sqrt(dval); } return -1; @@ -67,7 +71,7 @@ int nbre, idx; double dval; if (s->type != FIMG_TYPE_RGB) { - fprintf(stderr, "%s : src type %d invalide\n", + fprintf(stderr, "%s: src type %d invalide\n", __func__, s->type); return -4; } @@ -77,18 +81,22 @@ if (NULL==d) { } else { if (d->type != FIMG_TYPE_RGB) { - fprintf(stderr, "%s : dst type %d invalide\n", + fprintf(stderr, "%s: dst type %d invalide\n", __func__, d->type); return -4; } } -nbre = s->width * s->height * 3; +nbre = s->width * s->height; for (idx=0; idxR[idx] / maxval; d->R[idx] = maxval * dval * dval; + dval = s->G[idx] / maxval; + d->G[idx] = maxval * dval * dval; + dval = s->B[idx] / maxval; + d->B[idx] = maxval * dval * dval; } return 0; @@ -122,12 +130,15 @@ else { } } -nbre = s->width * s->height * 3; +nbre = s->width * s->height; for (idx=0; idxR[idx] / maxval; - d->R[idx] = maxval * (0.5 - 0.5 * cos(3.141592654*dval)); + dval = s->G[idx] / maxval; + d->G[idx] = maxval * (0.5 - 0.5 * cos(3.141592654*dval)); + dval = s->B[idx] / maxval; + d->B[idx] = maxval * (0.5 - 0.5 * cos(3.141592654*dval)); } return 0; @@ -155,12 +166,15 @@ else { } } -nbre = s->width * s->height * 3; +nbre = s->width * s->height; for (idx=0; idxR[idx] / maxval; - d->R[idx] = maxval * (0.5 - 0.5 * cos(2*3.141592654*dval)); + dval = s->G[idx] / maxval; + d->G[idx] = maxval * (0.5 - 0.5 * cos(2*3.141592654*dval)); + dval = s->B[idx] / maxval; + d->B[idx] = maxval * (0.5 - 0.5 * cos(2*3.141592654*dval)); } return 0; diff --git a/lib/fimg-core.c b/lib/fimg-core.c index a8b606cf..5ceaae2b 100644 --- a/lib/fimg-core.c +++ b/lib/fimg-core.c @@ -19,7 +19,8 @@ int fimg_type_is_valid(int type) switch (type) { case FIMG_TYPE_GRAY: case FIMG_TYPE_RGB: - case FIMG_TYPE_RGBA: return 1; + case FIMG_TYPE_RGBA: + case FIMG_TYPE_RGBZ: return 1; } return 0; } @@ -30,13 +31,14 @@ switch (type) { case FIMG_TYPE_GRAY: return "gray"; case FIMG_TYPE_RGB: return "rgb"; case FIMG_TYPE_RGBA: return "rgba"; + case FIMG_TYPE_RGBZ: return "rgbz"; } return "???"; } /* --------------------------------------------------------------------- */ int fimg_print_version(int k) { -fprintf(stderr, "*** FloatImg library, alpha v%d (%s, %s)\n", +fprintf(stderr, "*** FloatImg library, alpha %d (%s, %s)\n", FIMG_VERSION, __DATE__, __TIME__); if (51 == k) { @@ -52,6 +54,7 @@ void fimg_print_sizeof(void) { fprintf(stderr, " sz FloatImg = %lu\n", sizeof(FloatImg)); fprintf(stderr, " sz filehead = %lu\n", sizeof(FimgFileHead)); +fprintf(stderr, " sz filter = %lu\n", sizeof(FimgFilter3x3)); } /* --------------------------------------------------------------------- */ void fimg_printhead(FloatImg *h) @@ -81,19 +84,35 @@ printf(" pixels@ %p %p %p %p\n", return 0; } /* ---------------------------------------------------------------- */ -/* - * values for the parameter 't' are defined in 'floatimg.h' - */ -int fimg_create(FloatImg *fimg, int w, int h, int t) +static float *plane_alloc(int size) { -int surface, size; float *fptr; #if DEBUG_LEVEL -fprintf(stderr, ">>> %-25s ( %p %d %d %d )\n", __func__, fimg, w, h, t); +fprintf(stderr, ">>> %s ( %d )\n", __func__, size); #endif -if ( ! fimg_type_is_valid(t) ) { +fptr = calloc(size, sizeof(float)); +if (NULL==fptr) { + fprintf(stderr, "no more memory available, ABEND\n"); + abort(); + } +return fptr; +} +/* ---------------------------------------------------------------- */ +/* + * values for the parameter 'type' are defined in 'floatimg.h' + */ +int fimg_create(FloatImg *fimg, int w, int h, int type) +{ +int size; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %p %d %d %d )\n", __func__, fimg, w, h, type); +#endif + +if ( ! fimg_type_is_valid(type) ) { + fprintf(stderr, "%s: type %d invalid\n", __func__, type); return -2; } @@ -101,35 +120,24 @@ if ( ! fimg_type_is_valid(t) ) { * what to do if we've got a descriptor for an image * already allocated ? */ - memset(fimg, 0, sizeof(FloatImg)); -surface = w * h; -size = surface * t * sizeof(float); -#if DEBUG_LEVEL > 1 -fprintf(stderr, "surface is %d pixels, need %d bytes\n", surface, size); -#endif - -fptr = (float *)malloc(size); -if (NULL==fptr) { - fprintf(stderr, "%s : no mem, exiting.\n", __func__); - exit(1); - } - -#if DEBUG_LEVEL > 1 -fprintf(stderr, " %s: got %d bytes at %p\n", __func__, size, fptr); -#endif - +size = w * h; fimg->width = w; fimg->height = h; -fimg->type = t; +fimg->type = type; -fimg->R = fptr; -if ( (t==FIMG_TYPE_RGB) || (t==FIMG_TYPE_RGBA) ) { - fimg->G = fptr + surface; - fimg->B = fptr + surface + surface; +/* the red channel is allway allocated */ +fimg->R = (float *)plane_alloc(size); + +if (FIMG_TYPE_RGB == type) { + fimg->G = (float *)plane_alloc(size); + fimg->B = (float *)plane_alloc(size); } -if ( t==FIMG_TYPE_RGBA ) fimg->A = fptr + (3 * surface); -/* ok this a really WTF fragment of code */ + +if (FIMG_TYPE_RGBA == type) { + fimg->A = (float *)plane_alloc(size); + } + return 0; } @@ -156,6 +164,8 @@ if (NULL == fimg->R) { fprintf(stderr, "%s : %p already freed ?\n", __func__, fimg); return -3; } + + free(fimg->R); memset(fimg, 0, sizeof(FloatImg)); @@ -167,7 +177,7 @@ int fimg_clone(FloatImg *old, FloatImg *new, int flags) int foo; #if DEBUG_LEVEL -fprintf(stderr, ">>> %-25s ( %p %p 0x%x)\n", __func__, old, new, flags); +fprintf(stderr, ">>> %s ( %p %p 0x%x )\n", __func__, old, new, flags); #endif if ( ! fimg_type_is_valid(old->type) ) { @@ -197,7 +207,7 @@ int size; int foo; #if DEBUG_LEVEL -fprintf(stderr, ">>> %-25s ( %p %p )\n", __func__, from, to); +fprintf(stderr, ">>> %s ( %p %p )\n", __func__, from, to); #endif foo = fimg_images_not_compatible(from, to); @@ -206,8 +216,10 @@ if (foo) { return foo; } -size = from->width * from->height * from->type * sizeof(float); +size = from->width * from->height * sizeof(float); memcpy(to->R, from->R, size); +memcpy(to->G, from->G, size); +memcpy(to->B, from->B, size); return 0; } diff --git a/lib/fimg-file.c b/lib/fimg-file.c index 32c988c5..e967e043 100644 --- a/lib/fimg-file.c +++ b/lib/fimg-file.c @@ -52,6 +52,9 @@ datas[2] = filehead.t; return 0; } /* ---------------------------------------------------------------- */ +/* + * /!\ thi func work ONLY on RGB image + */ int fimg_dump_to_file(FloatImg *fimg, char *fname, int notused) { FILE *fp; @@ -75,7 +78,7 @@ if (NULL==fp) { } memset(&filehead, 0, sizeof(filehead)); -strcpy(filehead.magic, "FIMG"); +memcpy(filehead.magic, "FIMG", 4); filehead.w = fimg->width; filehead.h = fimg->height; filehead.t = fimg->type; @@ -85,16 +88,19 @@ if (1 != foo) { fclose(fp); return -2; } -nbre = fimg->width*fimg->height*3; -#if DEBUG_LEVEL -fprintf(stderr, " %s : data at %p\n", __func__, fimg->R); -#endif +nbre = fimg->width * fimg->height; /* pixels per frame */ foo = fwrite(fimg->R, sizeof(float), nbre, fp); if (nbre != foo) { - perror(fname); - fclose(fp); - return -3; + perror(fname); fclose(fp); return -3; + } +foo = fwrite(fimg->G, sizeof(float), nbre, fp); +if (nbre != foo) { + perror(fname); fclose(fp); return -3; + } +foo = fwrite(fimg->B, sizeof(float), nbre, fp); +if (nbre != foo) { + perror(fname); fclose(fp); return -3; } fclose(fp); @@ -130,18 +136,27 @@ if (1 != foo) { if ( (filehead.w != where->width) || (filehead.h != where->height) || (filehead.t != where->type) ) { + fprintf(stderr, "%s: file '%s' incompatible\n", + __func__, fname); + fclose(fp); + return -17; + } - fprintf(stderr, "%s: file '%s' incompatible\n", __func__, fname); - fclose(fp); - return -17; - } - -nbre = filehead.w*filehead.h*filehead.t; /* ugly quirk */ +nbre = filehead.w * filehead.h; /* number of pixels per frame */ foo = fread(where->R, sizeof(float), nbre, fp); if (nbre != foo) { fprintf(stderr, "%s: err read '%s' : %d\n", __func__, fname, foo); - fclose(fp); - return -18; + fclose(fp); return -18; + } +foo = fread(where->G, sizeof(float), nbre, fp); +if (nbre != foo) { + fprintf(stderr, "%s: err read '%s' : %d\n", __func__, fname, foo); + fclose(fp); return -18; + } +foo = fread(where->B, sizeof(float), nbre, fp); +if (nbre != foo) { + fprintf(stderr, "%s: err read '%s' : %d\n", __func__, fname, foo); + fclose(fp); return -18; } fclose(fp); @@ -153,7 +168,7 @@ return 0; int fimg_create_from_dump(char *fname, FloatImg *head) { FILE *fp; -int foo; +int foo, size; FimgFileHead filehead; #if DEBUG_LEVEL @@ -173,13 +188,14 @@ if (NULL==fp) { foo = fread(&filehead, sizeof(FimgFileHead), 1, fp); if (1 != foo) { - fprintf(stderr, "%s : short read\n", fname); + fprintf(stderr, "%s: short read on '%s'\n", __func__, fname); fclose(fp); return -16; } #if DEBUG_LEVEL -fprintf(stderr, "%s : got [ %d %d %d ] from '%s'\n", __func__, - filehead.w, filehead.h, filehead.t, fname); +fprintf(stderr, "%s : got [ %dx%d %s ] from '%s'\n", __func__, + filehead.w, filehead.h, fimg_str_type(filehead.t), + fname); #endif foo = fimg_create(head, filehead.w, filehead.h, filehead.t); @@ -188,10 +204,11 @@ if (foo) { return foo; } -/* NO No no !!! don't do that !!! */ -foo = fread(head->R, sizeof(float), - filehead.w*filehead.h*filehead.t, fp); - +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); fclose(fp); diff --git a/lib/fimg-math.c b/lib/fimg-math.c index 3b3c7c65..6975866d 100644 --- a/lib/fimg-math.c +++ b/lib/fimg-math.c @@ -98,7 +98,7 @@ memset(accus, 0, 4*sizeof(double)); for (idx=0; idxR[idx]; - if (head->type > 2) { + if (head->type > 2) { /* WTF ? */ accus[1] += head->G[idx]; accus[2] += head->B[idx]; } @@ -123,8 +123,6 @@ 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 @@ -159,12 +157,14 @@ if (fi->type != FIMG_TYPE_RGB) { return; } -nbre = fi->width * fi->height * fi->type; +nbre = fi->width * fi->height ; #if DEBUG_LEVEL fprintf(stderr, "%s, nbre is %d\n", __func__, nbre); #endif for (idx=0; idxR[idx] += value; + fi->G[idx] += value; + fi->B[idx] += value; } } /* ---------------------------------------------------------------- */ @@ -179,11 +179,13 @@ 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) count++; + if (fi->G[idx] < 0.0) count++; + if (fi->B[idx] < 0.0) count++; } return count; @@ -237,12 +239,21 @@ if ( (fi->type != FIMG_TYPE_RGB) && (fi->type != FIMG_TYPE_GRAY) ) { return; } -nbre = fi->width * fi->height * fi->type; +nbre = fi->width * fi->height; #if DEBUG_LEVEL fprintf(stderr, "%s, nbre of datum is %d\n", __func__, nbre); #endif -for (idx=0; idxR[idx] *= value; +if (fi->type == FIMG_TYPE_RGB) { + for (idx=0; idxR[idx] *= value; + fi->G[idx] *= value; + fi->B[idx] *= value; + } + } +if (fi->type == FIMG_TYPE_GRAY) { + for (idx=0; idxR[idx] *= value; + } } } /* ---------------------------------------------------------------- */ diff --git a/lib/t.c b/lib/t.c index 8cfa1e9e..a84b48b1 100644 --- a/lib/t.c +++ b/lib/t.c @@ -153,6 +153,8 @@ int essai_clone_et_copy(int unused) FloatImg A, B, C; int foo; +fprintf(stderr, "-------- %s ( %d ) --------\n", __func__, unused); + foo = fimg_create(&A, TAILLE, TAILLE, FIMG_TYPE_RGB); if (foo) { fprintf(stderr, "%s err create A %d\n", __func__, foo); @@ -230,6 +232,8 @@ int foo; FloatImg dessin, copy; double maxi; +fprintf(stderr, "-------- %s ( '%s' ) --------\n", __func__, fname); + foo = fimg_create_from_dump(fname, &dessin); if (foo) { fprintf(stderr, "in %s, error %d loading '%s'\n", @@ -238,6 +242,7 @@ if (foo) { } foo = fimg_clone(&dessin, ©, 0); +fimg_save_as_pnm(&dessin, "dessin.pnm", 0); maxi = (double)fimg_get_maxvalue(&dessin); fprintf(stderr, "image source valeur maxi = %f\n", maxi); @@ -290,17 +295,14 @@ if (verbosity) { fimg_print_sizeof(); } -// foo = essai_normalize(); -// fprintf(stderr, "retour essai normalize -> %d\n", foo); - -// foo = essai_contraste("quux.fimg"); -// fprintf(stderr, "retour essai contraste -> %d\n", foo); +foo = essai_contraste("quux.fimg"); +fprintf(stderr, "retour essai contraste -> %d\n", foo); // foo = essai_clone_et_copy(0); // fprintf(stderr, "retour essai clone'n'copy -> %d\n", foo); -foo = essai_timer(0); -fprintf(stderr, "retour essai -> %d\n", foo); +// foo = essai_timer(0); +// fprintf(stderr, "retour essai timer -> %d\n", foo); return 0; } diff --git a/tools/README.md b/tools/README.md index 25958ace..04f8d1bf 100644 --- a/tools/README.md +++ b/tools/README.md @@ -1,5 +1,8 @@ # Images en virgule flottante, les outils. +Dans tous les cas, vous pouvez utiliser l'option `-h` pour avoir des +explications sur ce que vous pouvez faire. + ## mkfimg ## fimgops @@ -9,3 +12,9 @@ ## fimgstats ## fimg2pnm - fimg2png + +## fimg2text + +Nouveau de l'année 2020+1 : exfiltrer toutes des données d'une image flottante +afin de les rendre machinables. + diff --git a/tools/fimg2text.c b/tools/fimg2text.c index 76c7235e..53cd8ace 100644 --- a/tools/fimg2text.c +++ b/tools/fimg2text.c @@ -12,7 +12,7 @@ int verbosity; -/* ------------------------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ int export_as_machinable(FloatImg *src, char *fname, int steps, int flags) { FILE *fp; @@ -44,7 +44,7 @@ for (y=0; yheight; y+=steps) { return 0; } -/* ------------------------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ static int normalize(FloatImg *pimg, float vmax) { float mmv[6], maxi, coef; @@ -79,7 +79,7 @@ for (idx=0; idx bar.csv"); puts("options:"); -puts("\t-v\tincrease verbosity"); -puts("\t-n 3.14\tnormalize picture"); -puts("\t-s N\nsteps on x & y"); +puts("\t-v\t\tincrease verbosity"); +puts("\t-n 3.14\t\tnormalize picture"); +puts("\t-s N\t\tsteps on x & y"); if (verbosity) fimg_print_version(1); exit(0); } -/* ------------------------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ int main(int argc, char *argv[]) { @@ -154,7 +154,7 @@ while ((opt = getopt(argc, argv, "hn:s:v")) != -1) { } if (1 != argc-optind) { - fprintf(stderr, "error: %s need one filename\n", argv[0]); + fprintf(stderr, "error: %s need one intput filename\n", argv[0]); exit(1); } @@ -165,5 +165,5 @@ if (foo) { return 0; } -/* ------------------------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ diff --git a/tools/fimgops.c b/tools/fimgops.c index 91bc9b21..639a515a 100644 --- a/tools/fimgops.c +++ b/tools/fimgops.c @@ -65,7 +65,7 @@ static void help(int lj) puts("usage:\n\tfimgops [options] A.fimg B.fimg operator D.fimg"); puts("options:"); -puts("\t-g convert output to gray"); +// puts("\t-g convert output to gray"); puts("\t-k N.N set float value"); puts("\t-v increase verbosity"); pr_opcodes(); @@ -112,6 +112,7 @@ FloatImg srcA, srcB, dest; while ((opt = getopt(argc, argv, "hk:v")) != -1) { switch(opt) { + case 'g': break; case 'h': help(0); break; case 'k': global_fvalue = atof(optarg); break; case 'v': verbosity++; break; diff --git a/tools/mkfimg.c b/tools/mkfimg.c index 07eb6b5d..09dcae59 100644 --- a/tools/mkfimg.c +++ b/tools/mkfimg.c @@ -1,3 +1,8 @@ +/* + * making a floatimg with some random datas + * an ugly software from tTh - february 2021 + */ + #include #include #include @@ -137,7 +142,7 @@ switch(type) { foo = fimg_dump_to_file(&fimg, fname, 0); if (foo) { - fprintf(stderr, "dump fimg -> %d\n", foo); + fprintf(stderr, "dump fimg to %s -> %d\n", fname, foo); exit(1); }