From d50f2145ba5a08ad57a63afcf5245678e5579ee4 Mon Sep 17 00:00:00 2001 From: Tonton Th Date: Mon, 2 Nov 2020 01:25:00 +0100 Subject: [PATCH] embarquement de la fonderie --- .gitignore | 3 + Fonderie/Makefile | 28 +++++ Fonderie/crapulator.c | 42 +++++++ Fonderie/crapulator.h | 6 + Fonderie/fonctions.c | 252 ++++++++++++++++++++++++++++++++++++++++++ Fonderie/fonctions.h | 49 ++++++++ Fonderie/fonderie.c | 191 ++++++++++++++++++++++++++++++++ Fonderie/sfx.c | 101 +++++++++++++++++ README.md | 8 +- funcs/contour2x2.c | 3 + funcs/filtrage.c | 3 +- 11 files changed, 681 insertions(+), 5 deletions(-) create mode 100644 Fonderie/Makefile create mode 100644 Fonderie/crapulator.c create mode 100644 Fonderie/crapulator.h create mode 100644 Fonderie/fonctions.c create mode 100644 Fonderie/fonctions.h create mode 100644 Fonderie/fonderie.c create mode 100644 Fonderie/sfx.c diff --git a/.gitignore b/.gitignore index 8ba49abc..a95a764e 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ doc/*.idx doc/*.ilg doc/*.ind doc/co*.tex + funcs/t funcs/*.o funcs/*.png @@ -61,4 +62,6 @@ tools/fimgfx tools/*.png tools/*.tiff +Fonderie/*.o +Fonderie/fonderie diff --git a/Fonderie/Makefile b/Fonderie/Makefile new file mode 100644 index 00000000..155459ac --- /dev/null +++ b/Fonderie/Makefile @@ -0,0 +1,28 @@ + + +COPT = -g -no-pie -Wall -O3 -DDEBUG_LEVEL=0 -Werror=parentheses +LIBS = -lfloatimg -lpnglite -lm +OBJS = fonctions.o sfx.o crapulator.o +DEPS = fonctions.h crapulator.h + +fonderie: fonderie.c fonctions.h ${OBJS} Makefile + gcc ${COPT} $< ${OBJS} ${LIBS} -o $@ + +crapulator.o: crapulator.c ${DEPS} + gcc ${COPT} -c $< + +fonctions.o: fonctions.c fonctions.h Makefile + gcc ${COPT} -c $< + +sfx.o: sfx.c fonctions.h Makefile + gcc ${COPT} -c $< + +# --------------------------------------------------------- + +TOTAR = *.c *.h Makefile \ + *.sh + + + +# --------------------------------------------------------- + diff --git a/Fonderie/crapulator.c b/Fonderie/crapulator.c new file mode 100644 index 00000000..d1fee9da --- /dev/null +++ b/Fonderie/crapulator.c @@ -0,0 +1,42 @@ +/* + * crapulator.c + */ + +#include +#include + +#include + +#include "crapulator.h" + +/* -------------------------------------------------------------- */ + +int crapulator(FloatImg *image, int notused) +{ +int retval; +FloatImg imgtmp; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %p %d )\n", __func__, image, notused); +#endif + +/* contour 2x2 don't work inplace */ +fimg_clone(image, &imgtmp, 0); + +/* so we work on a copy */ +retval = fimg_contour_2x2(image, &imgtmp, 0); +if (retval) { + fprintf(stderr, "%s : err contour %d\n", + __func__, retval); + exit(1); + } + +/* and put back porcessed pixels */ +fimg_copy_data(&imgtmp, image); +fimg_destroy(&imgtmp); + +return 0; +} + +/* -------------------------------------------------------------- */ + diff --git a/Fonderie/crapulator.h b/Fonderie/crapulator.h new file mode 100644 index 00000000..85a7f90e --- /dev/null +++ b/Fonderie/crapulator.h @@ -0,0 +1,6 @@ +/* + * crapulator.h + */ + +int crapulator(FloatImg *image, int notused); + diff --git a/Fonderie/fonctions.c b/Fonderie/fonctions.c new file mode 100644 index 00000000..b1e3ff4a --- /dev/null +++ b/Fonderie/fonctions.c @@ -0,0 +1,252 @@ +/* + * Fonctions de la Fonderie du Cumul + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Du code bien cracra / tTh / Tetalab + */ +#include +#include +#include +#include + +#include + +#include "fonctions.h" + +/* -------------------------------------------------------------- */ +/* global vars from main + */ +extern int verbosity; + +/* private vars of this module - it was very dirty + */ +static A_Fifo g_fifo; + +/* -------------------------------------------------------------- */ + +int faire_la_somme(A_Fifo *pfifo, FloatImg *destination, int step) +{ +int idx, foo; +FloatImg *pdest; + +if (NULL==destination) { + pdest = &(pfifo->total); } +else { + pdest = destination; } + +fimg_clear(pdest); +for (idx=0; idxnbslots; idx += step) { + foo = fimg_add_2(&(pfifo->slots[idx]), pdest); + if (foo) + { + fprintf(stderr, "%s: err %d on add_2\n", __func__, foo); + abort(); + } + } + +return 0; +} +/* -------------------------------------------------------------- */ +int post_process_picture(FloatImg *image, int numFx, float fval) +{ +int retval = 0; +int foo; +float value; +FloatImg imgtmp; +static int count, flag_debug; + +if (666==count) { + flag_debug = 1; + fprintf(stderr, "DEBUG POINT 1 in %s\n", __func__); + fimg_save_as_png(image, "source.png", 0); + } + +switch (numFx) { + case 0: /* DO NOTHING */ break; + + case 1: + fimg_cos_01(image, image, + fimg_get_maxvalue(image)); + break; + case 2: + fimg_cos_010(image, image, + fimg_get_maxvalue(image)); + break; + case 3: + value = fimg_get_maxvalue(image); + fimg_mul_cste(image, -1.0); + fimg_add_cste(image, value); + foo = fimg_count_negativ(image); + if (foo) { + fimg_dump_to_file(image, "err.fimg", 0); + fprintf(stderr, "%s negativ %d\n", __func__, foo); + return -78; + } + break; + case 4: + brotche_rand48(image, 0.20, + fimg_get_maxvalue(image)); + break; + case 5: + brotche_rand48_b(image, 0.10, + fimg_get_maxvalue(image)*0.8); + break; + case 6: + kill_colors_a(image, 0.0); + break; + + case 7: + retval = fimg_colors_mixer_a(image, 2.0); + break; + + case 8: + fimg_clone(image, &imgtmp, 0); + fimg_clear(&imgtmp); + if (flag_debug) { + fprintf(stderr, "DEBUG A contour 2x2\n"); + fimg_save_as_png(image, "before.png", 0); + fimg_dump_to_file(image, "before.fimg", 0); + } + retval = fimg_contour_2x2(image, &imgtmp, 0); + if (retval) { + fprintf(stderr, "%s : err contour %d\n", + __func__, retval); + exit(1); + } + if (flag_debug) { + fprintf(stderr, "DEBUG B contour 2x2\n"); + // fimg_save_as_png(&imgtmp, "contour.png", 0); + fimg_dump_to_file(&imgtmp, "contour.fimg", 0); + } + fimg_copy_data(&imgtmp, image); + fimg_destroy(&imgtmp); + break; + + case 9: + retval = fimg_classif_trial(image, image, 0.42, 0); + if (retval) { + fprintf(stderr, "err %d in classif\n", retval); + exit(1); + } + break; + + default : + fprintf(stderr, "%s : postproc #%d invalid\n", + __func__, numFx); + return -77; + } + +if (flag_debug) { + fprintf(stderr, "DEBUG POINT 2 in %s\n", __func__); + fimg_save_as_png(image, "after.png", 0); + } + +count++; flag_debug = 0; + +return retval; +} +/* -------------------------------------------------------------- */ +/* + * this func save the fifo content as + * - float FIMG + * - 16 bits PNM + * - 8 bits PNG + */ +int export_fifo(char *fname, int postproc, int step) +{ +int foo, type; + +foo = faire_la_somme(&g_fifo, NULL, step); + +/* BEGIN GRUIK CODE */ +extern int convert_to_gray; +/* END OF THE KLUGE */ +if (convert_to_gray) { + fimg_to_gray(&g_fifo.total); + // fprintf(stderr, "%p gray-washed\n", &fifo.total); + } + +foo = post_process_picture(&g_fifo.total, postproc, 0.0); +if (foo) { + fprintf(stderr, "ERR post process picture -> %d\n", foo); + return foo; + } + +type = format_from_extension(fname); +switch (type) { + case FILE_TYPE_PNG: + foo = fimg_save_as_png(&g_fifo.total, fname, 0); + break; + case FILE_TYPE_PNM: + foo = fimg_save_as_pnm(&g_fifo.total, fname, 0); + break; + case FILE_TYPE_FIMG: + foo = fimg_dump_to_file(&g_fifo.total, fname, 0); + break; + default: + fprintf(stderr, "%s : type of '%s' unknow\n", + __func__, fname); + foo = 888; + break; + } + +if (foo) { + fprintf(stderr, "ERR EXPORT '%s' is %d\n", fname, foo); + exit(3); + } + +return 0; +} +/* -------------------------------------------------------------- */ +int insert_picture(FloatImg *src) +{ +FloatImg *dst; +int nbre; + +/* + * this is the where we can insert the 'input filter' + */ + +dst = &g_fifo.slots[g_fifo.next]; + +nbre = dst->width * dst->height * dst->type; +memcpy(dst->R, src->R, nbre*sizeof(float)); + +g_fifo.next++, g_fifo.next %= g_fifo.nbslots; +// maybe we can write : +// (++fifo.next) %= fifo.nbslots; + +if (verbosity > 2) fprintf(stderr, "%s : next slot %d\n", + __func__, g_fifo.next); + +return 0; +} +/* -------------------------------------------------------------- */ +int create_fifo(int nbslot, int w, int h, int t) +{ +int foo, idx; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %d %dx%d %d )\n", __func__, + nbslot, w, h, t); +#endif + +memset(&g_fifo, 0, sizeof(A_Fifo)); + +g_fifo.nbslots = nbslot; + +g_fifo.slots = calloc(nbslot, sizeof(FloatImg)); +if (NULL==g_fifo.slots) abort(); +for (idx=0; idx +#include +#include +#include +#include +#include + +#include "fonctions.h" +#include "crapulator.h" + + +int verbosity; + +int convert_to_gray; + +/* -------------------------------------------------------------- */ +int traite_une_image(FloatImg *image, int proc, int step) +{ +static int numero; +int foo; +char ligne[200]; + +/* here, we put the picz in the fifo machinery */ +foo = insert_picture(image); +if (foo) { + fprintf(stderr, "%s : err %d on insert\n", __func__, foo); + return foo; + } + +sprintf(ligne, "p8/%05d.png", numero); +foo = export_fifo(ligne, proc, step); +if (foo) { + fprintf(stderr, "%s : err %d on export\n", __func__, foo); + return foo; + } + +numero++; /* VERY IMPORTANT :) */ + +return 0; +} +/* -------------------------------------------------------------- */ +int insert_blank(FloatImg *image, int nbre, int pproc) +{ +int idx, foo; + +fimg_clear(image); + +for (idx=0; idx>> %s ( '%s' -> '%s' %d )\n", __func__, + pattern, outdir, szfifo); +#endif + +fprintf(stderr, "\tstep is %d\n", step); + +(void)fimg_timer_set(0); + +if (outfx) fprintf(stderr, "\tout fx #%d\n", outfx); +else fprintf(stderr, "\tno out fx\n"); + +foo = create_fifo(szfifo, 640, 480, FIMG_TYPE_RGB); +fprintf(stderr, "init fifo (%d slots) --> %d\n", szfifo, foo); + +fimg_create(&input, 640, 480, 3); + +/* XXX inject a few stange pics in the fifo */ +insert_blank(&input, BLANK, outfx); + +memset(&globbuf, 0, sizeof(glob_t)); +foo = glob(pattern, 0, NULL, &globbuf); +fprintf(stderr, "glob '%s' -> %d, %ld files found\n", pattern, foo, + globbuf.gl_pathc); + +for (idx=0; idx %d\n", idx, foo); + continue; + } + + /* ========================= */ + /* FSCKING INPUT FILTER HERE */ + + foo = crapulator(&input, idx); + if (foo) { + fprintf(stderr, "%s crapulator -> %d\n", __func__, foo); + exit(1); + } + + foo = traite_une_image(&input, outfx, step); + if (foo) { + fprintf(stderr, "traitement %s -> %d WTF?\n", cptr, foo); + break; + } + fprintf(stderr, "\t%5d\r", idx); + } + +fputs("\n", stderr); + +insert_blank(&input, BLANK, outfx); + +fin = fimg_timer_get(0); +if (idx) { + fprintf(stderr, "\nelapsed %.2f seconds, %.2f s/pic\n", fin, fin/idx); + } +else { + fprintf(stderr, "\nelapsed %.2f seconds\n", fin); + } + +return 8; +} +/* -------------------------------------------------------------- */ +int main (int argc, char *argv[]) +{ +int foo, opt; +int fifosize = 10; +char *in_pattern = "capture/?????.fimg"; +char *out_dir = "p8"; +int out_effect = 0; +int steps = 1; + +// float maxlevel = 2500.0; + +fprintf(stderr, "*** %s : compiled by tTh, %s %s\n", argv[0], + __DATE__, __TIME__); +fimg_print_version(2); + +while ((opt = getopt(argc, argv, "gI:O:s:T:vx:")) != -1) { + switch(opt) { + + case 'g': convert_to_gray = 1; + break; + case 'I': in_pattern = optarg; + break; + case 'O': out_dir = optarg; + break; + case 'T': fifosize = atoi(optarg); + break; + case 'v': verbosity++; + break; + case 'x': out_effect = atoi(optarg); + break; + case 's': steps = atoi(optarg); + break; + } + } + +foo = demarre_la_machine(in_pattern, out_dir, fifosize, out_effect, steps); +fprintf(stderr, "retour du big-run de la machine -> %d\n", foo); + +return 0; +} +/* -------------------------------------------------------------- */ diff --git a/Fonderie/sfx.c b/Fonderie/sfx.c new file mode 100644 index 00000000..04df13fd --- /dev/null +++ b/Fonderie/sfx.c @@ -0,0 +1,101 @@ +/* + * SPECIAL EFFECTS + * + * Du code bien cracra / tTh / Tetalab + */ +#include +#include +#include +#include + +#include + +#include "fonctions.h" + +/* -------------------------------------------------------------- */ +/* global vars from main + */ +extern int verbosity; + +/* -------------------------------------------------------------- */ + +int brotche_rand48(FloatImg *fimg, float ratio, float mval) +{ +int nbpix, todo, foo; +int x, y; +float fval; + +nbpix = fimg->width * fimg->height; +todo = (int)((float)nbpix * ratio); + +if (verbosity > 1) + fprintf(stderr, "ratio %f nbpix %d todo %d\n", ratio, nbpix, todo); + +for (foo=0; foowidth; + y = rand() % fimg->height; + fimg_plot_rgb(fimg, x, y, fval, fval, fval); + } + +return 0; +} +/* -------------------------------------------------------------- */ +int brotche_rand48_b(FloatImg *fimg, float ratio, float mval) +{ +int nbpix, todo, foo; +int x, y; +float fval; + +nbpix = fimg->width * fimg->height; +todo = (int)((float)nbpix * ratio); + +if (verbosity > 1) { + fprintf(stderr, "ratio %f nbpix %d todo %d\n", + ratio, nbpix, todo); + } + +for (foo=0; foowidth-2)); + y = rand() % fimg->height; + fimg_plot_rgb(fimg, x-1, y, fval, 0.0, 0.0); + fimg_plot_rgb(fimg, x , y, 0.0, 0.0, fval); + fimg_plot_rgb(fimg, x+1, y, 0.0, fval, 0.0); + } + +return 0; +} +/* -------------------------------------------------------------- */ +/* + * OMG ! a Color Graphic Adaptor emulator :) + */ +int kill_colors_a(FloatImg *fimg, float fval) +{ +int nbpix, foo; + +if (FIMG_TYPE_RGB != fimg->type) { + fprintf(stderr, "%s: bad src type %d on %p\n", __func__, + fimg->type, fimg); + return -8; + } + +nbpix = fimg->width * fimg->height; + +for (foo=0; fooR[foo] > fimg->G[foo]) + fimg->B[foo] = fimg->R[foo]; + else + fimg->B[foo] = fimg->G[foo]; + } + +return 0; +} +/* -------------------------------------------------------------- */ +/* -------------------------------------------------------------- */ + + + diff --git a/README.md b/README.md index f8232f40..816880c4 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,8 @@ le canal IRC #tetalab sur le réseau de Par ailleurs, d'autres expérimentations sont [en cours](http://la.buvette.org/photos/cumul/fonderie/vidz.html#interpolator) sur le traitement et l'assemblage de ces images floues dans le but de faire -des films flous. Je sais aussi qu'un autre _diehard coder_ travaille sur la -parallélisation de certaines opérations. +des films flous. + +Un logiciel à base de [moyenne mobile](fonderie/) est en bonne voie. + *En avant vers l'infini, et au-delà...* - - diff --git a/funcs/contour2x2.c b/funcs/contour2x2.c index 1569e2e4..87c42359 100644 --- a/funcs/contour2x2.c +++ b/funcs/contour2x2.c @@ -83,6 +83,9 @@ for (y=0; yheight-1; y++) { } +/* kill potential NaN values in last row or last column */ + fimg_killborders(pdst); + return 0; } /* --------------------------------------------------------------------- */ diff --git a/funcs/filtrage.c b/funcs/filtrage.c index caec0c95..a1fbe59a 100644 --- a/funcs/filtrage.c +++ b/funcs/filtrage.c @@ -168,7 +168,8 @@ for (idx=0; idx