FloatImg/Fonderie/interpolator.c

342 lines
8.2 KiB
C
Raw Normal View History

/*
* INTERPOLATOR 2070
*
* +---------------------------------------+
* ! Do not use that software in real life !
* +---------------------------------------+
*
* imported in FloatImg Mon Nov 9 19:08:57 CET 2020
*
*/
#include <stdio.h>
#include <string.h>
2021-05-20 18:31:28 +11:00
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <glob.h>
#include "../floatimg.h"
#include "utilfuncs.h"
2021-04-03 04:25:20 +11:00
#include "fifo.h"
2020-11-10 13:58:18 +11:00
#include "glitches.h"
2020-11-10 10:50:25 +11:00
#include "crapulator.h"
#include "metriques.h"
2020-12-07 14:45:51 +11:00
#include "filterstack.h"
int verbosity;
/* -------------------------------------------------------------- */
/* on va essayer de trier l'ordre d'apparition des images
* selon une metrique approximative
*/
typedef struct {
int idx; /* in globbuf.gl_pathv[n] */
float value; /* from metric analyse */
} IdxValue;
2021-01-15 05:15:22 +11:00
static int negative = 0;
static int cmp_idxvalues(const void *pa, const void *pb)
{
2021-01-15 05:15:22 +11:00
if (negative)
return ( ((IdxValue *)pa)->value < ((IdxValue *)pb)->value);
else
return ( ((IdxValue *)pa)->value > ((IdxValue *)pb)->value);
}
2020-12-16 03:49:12 +11:00
int tentative_triage(glob_t *ptr_glob, IdxValue **ptr_idxval,
int method, double *p_average)
{
int idx, foo, nombre;
float metrique;
2020-12-16 03:49:12 +11:00
double average;
char *filename;
IdxValue *idxvalues;
#if DEBUG_LEVEL
2024-03-27 22:19:41 +11:00
fprintf(stderr, ">>> %s ( %p %p %d %p)\n", __func__, ptr_glob,
2024-11-12 03:13:20 +11:00
ptr_idxval, method, p_average);
#endif
2024-03-25 22:45:15 +11:00
if (0==method && verbosity) {
2024-03-27 22:19:41 +11:00
fprintf(stderr, "\tWTF? in %s, sort method was ZERO?\n", __func__);
2023-07-05 02:26:46 +11:00
}
nombre = ptr_glob->gl_pathc;
/* allocate the array for the sorting action */
idxvalues = calloc(nombre, sizeof(IdxValue));
if (NULL==idxvalues) {
fprintf(stderr, "MEMORY ERROR in %s\n", __func__);
exit(1);
}
2023-07-05 02:26:46 +11:00
// fprintf(stderr, "IdxValues array at %p\n", idxvalues);
*ptr_idxval = idxvalues;
2020-12-16 03:49:12 +11:00
average = 0.0;
/* compute all the needed values */
for (idx=0; idx<nombre; idx++) {
filename = ptr_glob->gl_pathv[idx];
2020-12-08 08:02:08 +11:00
foo = get_float_metric_from_file(filename, &metrique, method);
if (foo) {
2021-01-06 21:47:55 +11:00
fprintf(stderr, "%s: err %d get metric of '%s'\n",
__func__, foo, filename);
return -1;
}
2020-12-07 14:45:51 +11:00
if (verbosity)
2020-12-18 20:18:09 +11:00
fprintf(stderr, "%5d %s %10.3f\r",
idx, filename, metrique);
idxvalues[idx].idx = idx;
idxvalues[idx].value = metrique;
2020-12-16 03:49:12 +11:00
average += (double)metrique;
}
2020-11-15 21:47:37 +11:00
if (method) {
/* and now, we can massage all our datas */
2021-01-16 21:29:40 +11:00
fprintf(stderr, "sorting method = %d ...\n", method);
2020-11-15 21:47:37 +11:00
qsort(idxvalues, nombre, sizeof(IdxValue), cmp_idxvalues);
}
2020-12-03 23:05:44 +11:00
if (verbosity > 1) {
2020-11-16 21:12:29 +11:00
for (idx=0; idx<nombre; idx++) {
2020-12-07 20:11:54 +11:00
printf("%5d %9.3f %5d\n", idx,
2020-11-16 21:12:29 +11:00
idxvalues[idx].value, idxvalues[idx].idx);
fflush(stdout);
}
}
2020-12-16 03:49:12 +11:00
average /= (double)nombre;
*p_average = average;
2023-07-05 02:26:46 +11:00
fprintf(stderr, "\naverage of ??? is %f\n", average);
2020-12-16 03:49:12 +11:00
return 0;
}
/* -------------------------------------------------------------- */
/*
* This is the mega working loop
*/
2020-11-15 21:47:37 +11:00
int interpolator(char *pattern, char *outdir, int Nsteps,
int infx, int outfx, int sort)
{
2020-11-10 13:58:18 +11:00
FloatImg A, B, Out, *pFirst, *pSecond;
glob_t globbuf;
int foo, idx, ipng, w, h, step;
int curpix;
int iarray[3];
char *cptr, line[200];
2022-07-06 19:27:55 +11:00
float coef;
2020-12-16 03:49:12 +11:00
double meanmetric;
2021-01-06 21:47:55 +11:00
IdxValue *idx_values; /* gni? */
2023-07-05 02:26:46 +11:00
fprintf(stderr, " interpolate from '%s' to '%s' with %d steps.\n",
pattern, outdir, Nsteps);
2021-01-16 21:29:40 +11:00
if (negative) fprintf(stderr, "%s: negative ON\n", __func__);
2021-01-15 05:15:22 +11:00
memset(&globbuf, 0, sizeof(glob_t));
foo = glob(pattern, 0, NULL, &globbuf);
2023-07-05 02:26:46 +11:00
fprintf(stderr, " globbing '%s' -> %d, %d files found\n",
2020-12-07 14:45:51 +11:00
pattern, foo, (int)globbuf.gl_pathc);
if (0 == globbuf.gl_pathc) {
2024-11-12 03:13:20 +11:00
fprintf(stderr, "%s : no file found, ABEND\n", __func__);
exit(1);
}
idx_values = NULL;
2020-12-16 03:49:12 +11:00
foo = tentative_triage(&globbuf, &idx_values, sort, &meanmetric);
2020-12-07 14:45:51 +11:00
if (foo) {
fprintf(stderr, "sort of %p -> %d\n\n", idx_values, foo);
return foo;
}
foo = fimg_fileinfos(globbuf.gl_pathv[0], iarray);
2020-11-11 00:00:22 +11:00
if (FIMG_TYPE_RGB != iarray[2]) {
fprintf(stderr, "can work only on RGB fimg picture, was %d\n",
iarray[2]);
2020-12-16 03:49:12 +11:00
exit(1); /* BLAM! */
2020-11-11 00:00:22 +11:00
}
2020-12-16 03:49:12 +11:00
w = iarray[0], h = iarray[1];
2024-03-25 22:45:15 +11:00
if (verbosity) fprintf(stderr, "first image size : %dx%d\n", w, h);
2021-03-18 03:06:25 +11:00
fimg_create(&A, w, h, 3); pFirst = &A; fimg_clear(&A);
fimg_create(&B, w, h, 3); pSecond = &B; fimg_clear(&B);
fimg_create(&Out, w, h, 3);
ipng = 0;
for (idx=0; idx<globbuf.gl_pathc; idx++) {
curpix = idx_values[idx].idx;
cptr = globbuf.gl_pathv[curpix]; /* aliasing filename */
/* read the next file in B */
2020-12-07 14:45:51 +11:00
fprintf(stderr, "%5d / %5d %s\r", idx,
(int)globbuf.gl_pathc, cptr);
foo = fimg_load_from_dump(cptr, &B);
if (foo) {
2024-03-31 10:15:47 +11:00
fprintf(stderr, "\n%s: load %s from dump -> %d\n", __func__,
cptr, foo);
continue;
}
2021-01-04 06:34:10 +11:00
/* here was the input filter */
foo = filterstack_run(0, &B, 0);
2020-11-10 10:50:25 +11:00
if (foo) {
2020-11-21 08:25:30 +11:00
fprintf(stderr, "%s: input fx fail %d\n", __func__, foo);
2020-11-10 10:50:25 +11:00
exit(1);
}
for (step=0; step<Nsteps; step++) {
coef = (float)step / (float)Nsteps;
fimg_interpolate(pSecond, pFirst, &Out, coef);
2020-12-13 04:45:44 +11:00
foo = filterstack_run(1, &Out, 0);
2020-12-07 14:45:51 +11:00
if (foo) {
2020-12-13 04:45:44 +11:00
fprintf(stderr, "run filt stack--> %d\n", foo);
2020-12-07 14:45:51 +11:00
return foo;
}
sprintf(line, "%s/%05d.png", outdir, ipng);
foo = fimg_save_as_png(&Out, line, 0);
if (foo) {
fprintf(stderr, "err saving %s\n", line);
return -8;
}
ipng++;
}
#if 1
/* temporary hack : move datas */
fimg_copy_data(&B, &A);
#else
/* swap pointers to the two picz */
pTmp = pSecond;
pSecond = pFirst;
pFirst = pTmp;
2020-11-10 10:50:25 +11:00
/* XXX THIS CODE DON'T WORK !!! */
#endif
}
fprintf(stderr, "\ngenerated %d png files\n", ipng);
return 0;
}
/* -------------------------------------------------------------- */
2020-11-10 13:58:18 +11:00
void help(void)
{
2020-12-03 23:05:44 +11:00
puts("\tINTERPOLATOR");
puts("usage:\n\tinterpolator [options] <inglob> <outdir> <nbsteep>");
2020-11-10 13:58:18 +11:00
/* may be we can make options incoherent, like
* the options of 'fonderie' software ?
*/
2020-12-03 23:05:44 +11:00
puts("options:");
2021-01-04 06:34:10 +11:00
puts("\t-E i:bla:k\tinput filter chain");
puts("\t-F name:j\toutput filter chain");
2021-01-15 05:15:22 +11:00
puts("\t-n\t\tmake negative");
puts("\t-S nn\t\tmysterious sort");
2021-01-04 06:34:10 +11:00
puts("\t-L\t\tlist available filters");
puts("\t-v\t\tincrease verbosity");
2024-11-12 03:15:53 +11:00
if (verbosity) { puts(""); fimg_print_version(2); puts(""); }
2020-11-10 13:58:18 +11:00
exit(0);
}
/* -------------------------------------------------------------- */
int main (int argc, char *argv[])
{
int foo;
int nbrsteps = 9;
2020-11-10 13:58:18 +11:00
int opt;
2021-01-06 21:47:55 +11:00
// int inFx = 0;
// int outFx = 0;
2020-11-15 21:47:37 +11:00
int sort = 0;
char *InFchain = "0";
char *OutFchain = "0";
char *out_dir = "p8/";
2023-07-07 21:25:47 +11:00
fprintf(stderr, "*** %s\n\tcompiled on %s %s\n", argv[0],
__DATE__, __TIME__);
2024-03-25 22:45:15 +11:00
if (verbosity) fimg_print_version(2);
2020-12-31 10:46:12 +11:00
#if DEBUG_LEVEL
2020-12-16 03:49:12 +11:00
/* this is for the debug off calling shellscript */
for (foo=0; foo<argc; foo++)
fprintf(stderr, "%5d %s\n", foo, argv[foo]);
2020-12-31 10:46:12 +11:00
#endif
2020-12-16 03:49:12 +11:00
2021-01-15 05:15:22 +11:00
while ((opt = getopt(argc, argv, "E:F:hLnS:v")) != -1) {
2020-11-10 13:58:18 +11:00
switch(opt) {
case 'E': InFchain = optarg; break;
case 'F': OutFchain = optarg; break;
2020-11-11 00:00:22 +11:00
case 'h': help(); break;
2020-12-31 00:42:44 +11:00
case 'L':
list_crapulors("available filters");
exit(0);
2020-11-15 21:47:37 +11:00
case 'S': sort = atoi(optarg); break;
2020-11-10 13:58:18 +11:00
case 'v': verbosity++; break;
2021-01-15 05:15:22 +11:00
case 'n': negative = 1; break;
default: exit(1);
2020-11-10 13:58:18 +11:00
}
}
2021-01-04 06:34:10 +11:00
#if DEBUG_LEVEL
2020-12-16 03:49:12 +11:00
fprintf(stderr, "%s: argc = %d, optind = %d\n", argv[0], argc, optind);
2021-01-04 06:34:10 +11:00
#endif
2020-11-10 13:58:18 +11:00
2020-11-15 21:47:37 +11:00
if (3 != (argc-optind)) {
fprintf(stderr, "args: [options] <inglob> <outdir> <nbsteep>\n");
exit(1);
}
out_dir = argv[optind+1];
/*
* check for the export directory
*/
foo = check_if_export_dir_is_valid(out_dir, 0);
if ( !foo ) {
fprintf(stderr, "** export dir (%s) not valid\n", out_dir);
fprintf(stderr, "** may be you have to create it ?\n");
exit(1);
}
foo = parse_filter_chain(0, InFchain);
2020-12-07 14:45:51 +11:00
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);
2020-12-07 14:45:51 +11:00
exit(1);
}
2021-02-26 20:14:33 +11:00
if (verbosity) {
fprintf(stderr, "\tinput glob '%s'\n", argv[optind]);
fprintf(stderr, "\toutput dir '%s'\n", argv[optind+1]);
fprintf(stderr, "\tsrc filter '%s'\n", InFchain);
fprintf(stderr, "\tout filter '%s'\n", OutFchain);
2023-07-05 02:26:46 +11:00
fprintf(stderr, "\tsort %d\n", sort);
2021-02-26 20:14:33 +11:00
}
2020-12-16 03:49:12 +11:00
if (verbosity > 1) {
2021-05-12 22:51:46 +11:00
fputs("=========================\n", stderr);
2020-12-13 04:45:44 +11:00
filterstack_list(0, __FILE__);
filterstack_list(1, __FILE__);
2021-05-12 22:51:46 +11:00
fputs("=========================\n", stderr);
2020-12-13 04:45:44 +11:00
}
2020-12-07 14:45:51 +11:00
2020-11-15 21:47:37 +11:00
nbrsteps = atoi(argv[optind+2]);
foo = interpolator(argv[optind], argv[optind+1], nbrsteps,
2021-01-06 21:47:55 +11:00
0, 0, sort);
fprintf(stderr, "interpolator give us a %d score\n", foo);
return 0;
}
/* -------------------------------------------------------------- */