FloatImg/Fonderie/fonderie.c

279 lines
6.6 KiB
C
Raw Normal View History

2020-11-02 11:25:00 +11:00
/*
* FONDERIE
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <glob.h>
2021-01-26 03:18:44 +11:00
#include "../floatimg.h"
2020-11-02 11:25:00 +11:00
#include "fonctions.h"
2020-11-15 21:47:37 +11:00
#include "glitches.h"
2020-11-02 11:25:00 +11:00
#include "crapulator.h"
2020-12-16 03:49:12 +11:00
#include "filterstack.h"
2020-11-02 11:25:00 +11:00
int verbosity;
/* -------------------------------------------------------------- */
int traite_une_image(FloatImg *image, int proc, int step, char *outd)
2020-11-02 11:25:00 +11:00
{
static int numero;
int foo;
char ligne[200];
/* here, we put the picz in the fifo machinery */
foo = insert_picture(image);
if (foo) {
2020-11-03 00:51:48 +11:00
fprintf(stderr, "%s: err %d on insert\n", __func__, foo);
2020-11-02 11:25:00 +11:00
return foo;
}
sprintf(ligne, "%s/%05d.png", outd, numero);
2020-11-02 11:25:00 +11:00
foo = export_fifo(ligne, proc, step);
if (foo) {
2020-11-03 00:51:48 +11:00
fprintf(stderr, "%s: err %d on export\n", __func__, foo);
2020-11-02 11:25:00 +11:00
return foo;
}
numero++; /* VERY IMPORTANT :) */
return 0;
}
/* -------------------------------------------------------------- */
int insert_blank(FloatImg *image, int nbre, int pproc,
float fval, char *outd)
2020-11-02 11:25:00 +11:00
{
int idx, foo;
2021-02-23 00:44:02 +11:00
int preloaded = 0;
FloatImg blank;
char *fname;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %d %d %f '%s' )\n", __func__,
image, nbre, pproc, fval, outd);
#endif
if (NULL != (fname=getenv("BLANK_FIMG"))) {
2021-02-23 01:43:29 +11:00
fprintf(stderr,"%s: try to load %s\n", __func__, fname);
2021-02-23 00:44:02 +11:00
fimg_clone(image, &blank, 1);
foo = fimg_load_from_dump(fname, &blank);
if (foo) {
fprintf(stderr, "epic fail %d on %s\n", foo, fname);
exit(1);
}
if (verbosity) fimg_describe(&blank, "Blank pic");
preloaded = 1;
}
2020-11-02 11:25:00 +11:00
fimg_clear(image);
for (idx=0; idx<nbre; idx++) {
2021-02-23 00:44:02 +11:00
if (preloaded) {
fimg_copy_data(&blank, image);
}
else {
fimg_hdeg_a(image, fval);
}
if ((foo=traite_une_image(image, pproc, 1, outd))) {
2020-11-02 11:25:00 +11:00
fprintf(stderr, "%s : err %d from 'traite_une_image'\n",
__func__, foo);
break;
}
2021-02-23 00:44:02 +11:00
printf("\t%c\r", "ABCDEFGH"[idx%8]); fflush(stdout);
2020-11-02 11:25:00 +11:00
}
puts("");
2021-02-23 00:44:02 +11:00
if (preloaded) {
fimg_destroy(&blank);
}
2020-11-02 11:25:00 +11:00
return 0;
}
/* -------------------------------------------------------------- */
int demarre_la_machine(char *pattern, char *outdir, int szfifo,
2020-12-02 01:26:15 +11:00
int infx, int outfx, int step, int blk)
2020-11-02 11:25:00 +11:00
{
int foo, idx, width, height;
2020-11-02 11:25:00 +11:00
glob_t globbuf;
char *cptr;
FloatImg input;
double fin;
2020-11-06 07:07:38 +11:00
float maxvalue;
int datas[3];
2020-11-02 11:25:00 +11:00
#if DEBUG_LEVEL
2020-11-06 07:07:38 +11:00
fprintf(stderr, ">>> %s ( '%s' -> '%s' %d eff:%d:%d)\n", __func__,
pattern, outdir, szfifo, infx, outfx);
2020-11-02 11:25:00 +11:00
#endif
2020-11-04 20:42:02 +11:00
if (1 != step) fprintf(stderr, "\tstep is %d\n", step);
2020-11-02 11:25:00 +11:00
(void)fimg_timer_set(0);
memset(&globbuf, 0, sizeof(glob_t));
foo = glob(pattern, 0, NULL, &globbuf);
2020-11-21 08:25:30 +11:00
if (foo) {
2020-12-07 20:00:54 +11:00
fprintf(stderr, "glob (%s) failure %d\n", pattern, foo);
2020-11-21 08:25:30 +11:00
exit(1);
}
2020-12-06 21:13:06 +11:00
fprintf(stderr, "glob '%s' -> %d, %d files found\n", pattern, foo,
(int)globbuf.gl_pathc);
/* get the size of the inputs images */
foo = fimg_fileinfos(globbuf.gl_pathv[0], datas);
2020-11-06 07:07:38 +11:00
width = datas[0]; height = datas[1];
2020-12-03 21:47:44 +11:00
fprintf(stderr, "first image size %dx%d\n", width, height);
2020-11-06 07:07:38 +11:00
fimg_create(&input, width, height, 3);
/* get the maximum value of the first pic */
foo = fimg_load_from_dump(globbuf.gl_pathv[0], &input);
if (foo) {
fprintf(stderr, "%s: err %d loading %s\n",
__func__, foo, globbuf.gl_pathv[0]);
exit(1);
}
maxvalue = fimg_get_maxvalue(&input);
2020-12-03 21:47:44 +11:00
fprintf(stderr, "first image maxvalue %f\n", maxvalue);
foo = create_fifo(szfifo, width, height, FIMG_TYPE_RGB);
2020-11-02 11:25:00 +11:00
fprintf(stderr, "init fifo (%d slots) --> %d\n", szfifo, foo);
2020-11-06 07:07:38 +11:00
/* XXX inject a few strange pics in the fifo */
2020-12-02 01:26:15 +11:00
insert_blank(&input, blk, outfx, maxvalue, outdir);
2020-11-02 11:25:00 +11:00
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;
}
2020-11-16 06:15:56 +11:00
/* fscking input filter here */
2020-12-16 03:49:12 +11:00
if (infx) {
foo = crapulator(&input, infx, maxvalue);
}
else {
foo = filterstack_run(0, &input, 0);
}
2020-11-02 11:25:00 +11:00
if (foo) {
2020-12-16 03:49:12 +11:00
fprintf(stderr, "%s: input filter -> %d\n", __func__, foo);
2020-11-02 11:25:00 +11:00
exit(1);
}
foo = traite_une_image(&input, outfx, step, outdir);
2020-11-02 11:25:00 +11:00
if (foo) {
fprintf(stderr, "traitement %s -> %d WTF?\n", cptr, foo);
break;
}
2020-12-06 21:13:06 +11:00
fprintf(stderr, "\t%5d / %5d\r", idx, (int)globbuf.gl_pathc);
2020-11-02 11:25:00 +11:00
}
fputs("\n", stderr);
2020-12-02 01:26:15 +11:00
insert_blank(&input, blk, outfx, maxvalue, outdir);
2020-11-06 07:07:38 +11:00
/*
* PLEASE, FLUSH THE FIFO !
*/
2020-11-02 11:25:00 +11:00
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);
}
2020-11-10 13:58:18 +11:00
return 8; /* why 9 ? */
2020-11-02 11:25:00 +11:00
}
/* -------------------------------------------------------------- */
2020-11-03 00:51:48 +11:00
void help(void)
{
puts("\tFONDERIE\noptions:");
2020-12-31 00:42:44 +11:00
puts("\t-E\tinput:filter:chain");
puts("\t-F\toutput:filter:chain");
2021-01-14 02:09:27 +11:00
// puts("\t-g\tconvert to gray");
2020-11-03 00:51:48 +11:00
puts("\t-I\tinput glob pattern");
2020-12-31 00:42:44 +11:00
puts("\t-L\tlist available filters");
2020-11-03 00:51:48 +11:00
puts("\t-O\toutput directory");
puts("\t-T\tfifo size");
puts("\t-v\tincrease verbosity");
exit(0);
}
/* -------------------------------------------------------------- */
2020-11-02 11:25:00 +11:00
int main (int argc, char *argv[])
{
int foo, opt;
int fifosize = 10;
char *in_pattern = "capture/?????.fimg";
char *out_dir = "p8";
2021-01-06 21:47:55 +11:00
// OBSOLETE int in_effect = 0;
// OBSOLETE int out_effect = 0;
2020-11-02 11:25:00 +11:00
int steps = 1;
2020-12-02 01:26:15 +11:00
int blanks = 20;
2020-12-16 03:49:12 +11:00
char *InFchain = "0";
char *OutFchain = "0";
2020-11-02 11:25:00 +11:00
2021-01-04 00:23:01 +11:00
fprintf(stderr, "%s\n\tcompiled %s, %s, pid %d\n",
2020-11-03 08:20:54 +11:00
argv[0], __DATE__, __TIME__, getpid());
2020-11-02 11:25:00 +11:00
fimg_print_version(2);
2020-12-31 00:42:44 +11:00
while ((opt = getopt(argc, argv, "B:E:F:ghI:LO:s:T:vw:x:")) != -1) {
2020-11-02 11:25:00 +11:00
switch(opt) {
2020-12-16 03:49:12 +11:00
case 'E': InFchain = optarg; break;
case 'F': OutFchain = optarg; break;
2020-12-02 01:26:15 +11:00
case 'B': blanks = atoi(optarg);
break;
2021-01-14 02:09:27 +11:00
case 'g': // convert_to_gray = 1;
2020-11-02 11:25:00 +11:00
break;
2020-11-03 00:51:48 +11:00
case 'h': help();
break;
2020-11-02 11:25:00 +11:00
case 'I': in_pattern = optarg;
break;
2020-12-31 00:42:44 +11:00
case 'L':
list_crapulors("available filters");
exit(0);
2020-11-02 11:25:00 +11:00
case 'O': out_dir = optarg;
break;
case 'T': fifosize = atoi(optarg);
break;
case 'v': verbosity++;
break;
2021-01-06 21:47:55 +11:00
// OBSOLETE case 'w': in_effect = atoi(optarg);
// OBSOLETE break;
// OBSOLETE case 'x': out_effect = atoi(optarg);
// OBSOLETE break;
2020-11-02 11:25:00 +11:00
case 's': steps = atoi(optarg);
break;
}
}
if (verbosity) {
2020-12-07 20:00:54 +11:00
fprintf(stderr, "input glob '%s'\n", in_pattern);
fprintf(stderr, "output dir '%s'\n", out_dir);
}
2020-12-16 03:49:12 +11:00
foo = parse_filter_chain(0, InFchain);
if (foo) {
fprintf(stderr, "err %d parsing '%s'\n", foo, InFchain);
exit(1);
}
foo = parse_filter_chain(1, OutFchain);
if (foo) {
fprintf(stderr, "err %d parsing '%s'\n", foo, OutFchain);
exit(1);
}
2021-01-06 21:47:55 +11:00
foo = demarre_la_machine(in_pattern, out_dir, fifosize, 0,
0, steps, blanks);
2020-11-02 11:25:00 +11:00
fprintf(stderr, "retour du big-run de la machine -> %d\n", foo);
return 0;
}
/* -------------------------------------------------------------- */