embarquement de la fonderie

This commit is contained in:
Tonton Th 2020-11-02 01:25:00 +01:00
parent 364ce06130
commit d50f2145ba
11 changed files with 681 additions and 5 deletions

3
.gitignore vendored
View File

@ -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

28
Fonderie/Makefile Normal file
View File

@ -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
# ---------------------------------------------------------

42
Fonderie/crapulator.c Normal file
View File

@ -0,0 +1,42 @@
/*
* crapulator.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <floatimg.h>
#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;
}
/* -------------------------------------------------------------- */

6
Fonderie/crapulator.h Normal file
View File

@ -0,0 +1,6 @@
/*
* crapulator.h
*/
int crapulator(FloatImg *image, int notused);

252
Fonderie/fonctions.c Normal file
View File

@ -0,0 +1,252 @@
/*
* Fonctions de la Fonderie du Cumul
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Du code bien cracra / tTh / Tetalab
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <floatimg.h>
#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; idx<pfifo->nbslots; 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<nbslot; idx++) {
foo = fimg_create(&g_fifo.slots[idx], w, h, t);
if (foo) {
fprintf(stderr, "%s idx %d err%d\n", __func__, idx, foo);
return foo;
}
}
foo = fimg_create(&g_fifo.total, w, h, t);
g_fifo.next = 0;
g_fifo.magic = MAGIC_FIFO;
return 0;
}
/* -------------------------------------------------------------- */

49
Fonderie/fonctions.h Normal file
View File

@ -0,0 +1,49 @@
/*
* Machine qui fait des fils flous
*/
/* -------------------------------------------------------------- */
typedef struct {
unsigned long magic;
int nbslots; /* how many pics used ? */
FloatImg total; /* computing workspace */
FloatImg wspace; /* computing workspace */
FloatImg *slots; /* circular buffer */
int next; /* incremented with modulo */
} A_Fifo;
#define MAGIC_FIFO 0xabcd9876
typedef struct {
unsigned long magic;
int blanks; /* ???? */
float fk;
int preproc, postproc;
int step; /* for fifo export */
} ControlBloc;
#define MAGIC_CTRLB 0xabcdfefe
/* -------------------------------------------------------------- */
int export_fifo(char *fname, int postproc, int step);
int insert_picture(FloatImg *src);
int create_fifo(int nbslot, int w, int h, int t);
/* -------------------------------------------------------------- */
/*
* funcs in 'sfx.c'
*/
int brotche_rand48(FloatImg *fimg, float ratio, float mval);
int brotche_rand48_b(FloatImg *fimg, float ratio, float mval);
int kill_colors_a(FloatImg *fimg, float fval);
int colors_brotcher(FloatImg *fimg, float fval);
/* -------------------------------------------------------------- */

191
Fonderie/fonderie.c Normal file
View File

@ -0,0 +1,191 @@
/*
* FONDERIE
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <glob.h>
#include <floatimg.h>
#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<nbre; idx++) {
fimg_hdeg_a(image, 16.64);
brotche_rand48_b(image, drand48()*0.10, 1e5);
if ((foo=traite_une_image(image, pproc, 1))) {
fprintf(stderr, "%s : err %d from 'traite_une_image'\n",
__func__, foo);
break;
}
printf("\t%c\r", "ABCDEF"[idx%6]); fflush(stdout);
}
puts("");
return 0;
}
/* -------------------------------------------------------------- */
#define BLANK 30
int demarre_la_machine(char *pattern, char *outdir, int szfifo,
int outfx, int step)
{
int foo, idx;
glob_t globbuf;
char *cptr;
FloatImg input;
double fin;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %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<globbuf.gl_pathc; idx++) {
cptr = globbuf.gl_pathv[idx];
/* first step : read the current grabed picz from disk,
and put it in our private buffer */
foo = fimg_load_from_dump(cptr, &input);
if (foo) {
fprintf(stderr, "load #%d from dump -> %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;
}
/* -------------------------------------------------------------- */

101
Fonderie/sfx.c Normal file
View File

@ -0,0 +1,101 @@
/*
* SPECIAL EFFECTS
*
* Du code bien cracra / tTh / Tetalab
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <floatimg.h>
#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; foo<todo; foo++)
{
fval = (float)drand48() * mval;
x = rand() % fimg->width;
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; foo<todo; foo++)
{
fval = (float)drand48() * mval;
x = 1 + (rand() % (fimg->width-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; foo<nbpix; foo++) {
if (fimg->R[foo] > fimg->G[foo])
fimg->B[foo] = fimg->R[foo];
else
fimg->B[foo] = fimg->G[foo];
}
return 0;
}
/* -------------------------------------------------------------- */
/* -------------------------------------------------------------- */

View File

@ -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à...*

View File

@ -83,6 +83,9 @@ for (y=0; y<psrc->height-1; y++) {
}
/* kill potential NaN values in last row or last column */
fimg_killborders(pdst);
return 0;
}
/* --------------------------------------------------------------------- */

View File

@ -168,7 +168,8 @@ for (idx=0; idx<w; idx++) {
fimg_plot_rgb(img, idx, h-1, 0.0, 0.0, 0.0);
#endif
}
return -1;
return 0;
}
/* -------------------------------------------------------------------- */
int fimg_lissage_2x2(FloatImg *img)