FloatImg/extras/Fonderie/filterstack.c

243 lines
5.4 KiB
C
Raw Normal View History

2020-12-04 07:56:45 +11:00
/*
* filterstack.c
*/
#include <stdio.h>
#include <stdlib.h>
2020-12-07 14:45:51 +11:00
#include <string.h>
2020-12-31 00:42:44 +11:00
#include <ctype.h>
2020-12-19 22:49:06 +11:00
#include <alloca.h>
2020-12-04 07:56:45 +11:00
#include "../floatimg.h"
2020-12-04 08:05:21 +11:00
#include "crapulator.h"
2020-12-04 07:56:45 +11:00
#include "filterstack.h"
2020-12-05 05:55:34 +11:00
// #undef DEBUG_LEVEL
// #define DEBUG_LEVEL 1
2020-12-04 07:56:45 +11:00
/* -------------------------------------------------------------- */
2020-12-07 14:45:51 +11:00
extern int verbosity;
2020-12-11 05:19:35 +11:00
static FilterStack f_stacks[NUMBER_OF_STACK];
2020-12-04 07:56:45 +11:00
/* -------------------------------------------------------------- */
int filterstack_init(int numid, int notused)
2020-12-04 07:56:45 +11:00
{
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %d %d )\n", __func__, numid, notused);
2020-12-04 07:56:45 +11:00
#endif
2020-12-11 05:19:35 +11:00
if (numid < 0 || numid > NUMBER_OF_STACK) {
2020-12-16 03:49:12 +11:00
fprintf(stderr, "%s: slot number %d is invalid\n", __func__, numid);
2020-12-04 07:56:45 +11:00
exit(1);
}
2020-12-11 05:19:35 +11:00
memset(&f_stacks[numid], 0, sizeof(FilterSlot));
2020-12-04 07:56:45 +11:00
return 0;
}
/* -------------------------------------------------------------- */
2020-12-11 05:19:35 +11:00
int filterstack_add(int numid, int code, int ival, float fval)
2020-12-04 07:56:45 +11:00
{
2020-12-11 05:19:35 +11:00
int idxsl;
2020-12-04 07:56:45 +11:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %d %d %f )\n", __func__, code, ival, fval);
#endif
2020-12-11 05:19:35 +11:00
if (numid < 0 || numid > NUMBER_OF_STACK) {
2020-12-16 03:49:12 +11:00
fprintf(stderr, "%s: slot number %d is invalid\n", __func__, numid);
2020-12-04 07:56:45 +11:00
exit(1);
}
2020-12-11 05:19:35 +11:00
if (f_stacks[numid].count == FILTER_BY_STACK) {
2020-12-04 07:56:45 +11:00
fprintf(stderr, "%s: stack is full\n", __func__);
return -1;
}
2020-12-11 05:19:35 +11:00
idxsl = f_stacks[numid].count; /* aliasing */
f_stacks[numid].slots[idxsl].numero = code;
f_stacks[numid].slots[idxsl].ival = ival;
f_stacks[numid].slots[idxsl].fval = fval;
2020-12-04 07:56:45 +11:00
2020-12-11 05:19:35 +11:00
f_stacks[numid].count++;
2020-12-04 07:56:45 +11:00
return 0;
}
/* -------------------------------------------------------------- */
2020-12-11 05:19:35 +11:00
int filterstack_list(int numid, const char *txt)
2020-12-04 07:56:45 +11:00
{
int idx;
2020-12-11 05:19:35 +11:00
if (numid < 0 || numid > NUMBER_OF_STACK) {
2020-12-16 03:49:12 +11:00
fprintf(stderr, "%s: slot number %d is invalid\n", __func__, numid);
2020-12-07 14:45:51 +11:00
exit(1);
}
2020-12-24 20:28:56 +11:00
fprintf(stderr, "---- %2d ---- %-20s -----------\n", numid, txt);
2020-12-11 05:19:35 +11:00
// fprintf(stderr, "stack at %p, size %d, current %d\n",
// f_slots, nbre_filters, idx_slot);
2020-12-18 20:18:09 +11:00
fprintf(stderr, "idx ___ fx# _ name ________ ival _ fval ___\n");
2020-12-11 05:19:35 +11:00
for (idx=0; idx<f_stacks[numid].count; idx++) {
2020-12-04 07:56:45 +11:00
2020-12-09 01:51:07 +11:00
fprintf(stderr, "%3d %3d %-10s %3d %f\n", idx,
2020-12-11 05:19:35 +11:00
f_stacks[numid].slots[idx].numero,
crap_name_from_number(f_stacks[numid].slots[idx].numero),
f_stacks[numid].slots[idx].ival,
f_stacks[numid].slots[idx].fval);
2020-12-04 07:56:45 +11:00
}
return 0;
}
/* -------------------------------------------------------------- */
2020-12-11 05:19:35 +11:00
int filterstack_run(int numid, FloatImg *target, int notused)
2020-12-04 07:56:45 +11:00
{
2020-12-11 05:19:35 +11:00
int idx, foo, eff;
float fv;
2020-12-04 07:56:45 +11:00
#if DEBUG_LEVEL
2020-12-07 14:45:51 +11:00
fprintf(stderr, ">>> %s ( %p %d )\n", __func__, target, notused);
2020-12-04 07:56:45 +11:00
#endif
2020-12-11 05:19:35 +11:00
if (numid < 0 || numid > NUMBER_OF_STACK) {
2020-12-16 03:49:12 +11:00
fprintf(stderr, "%s: slot number %d is invalid\n", __func__, numid);
2020-12-07 14:45:51 +11:00
exit(1);
}
2020-12-13 04:45:44 +11:00
if (0==f_stacks[numid].count) {
fprintf(stderr, "%s: stack %d empty ?\n", __func__, numid);
return -11;
}
2020-12-11 05:19:35 +11:00
for (idx=0; idx<f_stacks[numid].count; idx++) {
eff = f_stacks[numid].slots[idx].numero;
fv = f_stacks[numid].slots[idx].fval;
2020-12-04 07:56:45 +11:00
2020-12-07 14:45:51 +11:00
if (verbosity > 1)
2021-05-10 08:30:58 +11:00
fprintf(stderr, "stack %d idx %d : effect %2d on %p\n",
2021-04-02 13:16:26 +11:00
numid, idx, eff, target);
2020-12-11 05:19:35 +11:00
foo = crapulator(target, eff, fv);
2020-12-04 07:56:45 +11:00
if (foo) {
2020-12-18 20:18:09 +11:00
fprintf(stderr,
2021-01-04 01:21:38 +11:00
"crapulator give me error %d on effect %d (%s)\n",
foo, eff,
crap_name_from_number(eff));
2020-12-04 07:56:45 +11:00
return foo;
}
}
return 0;
}
/* -------------------------------------------------------------- */
2020-12-11 05:19:35 +11:00
int load_stack_from_file(int numid, char *fname, int notused)
2020-12-05 05:55:34 +11:00
{
2020-12-07 14:45:51 +11:00
FILE *fp;
// int a, b;
// float f;
// char line[100];
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( '%s' %d )\n", __func__, fname, notused);
#endif
2020-12-11 05:19:35 +11:00
if (numid < 0 || numid > NUMBER_OF_STACK) {
2020-12-16 03:49:12 +11:00
fprintf(stderr, "%s: slot number %d is invalid\n", __func__, numid);
2020-12-07 14:45:51 +11:00
exit(1);
}
if (NULL==(fp=fopen(fname, "r"))) {
perror(fname);
return -1;
}
/*
* here was dragons
*/
/* hadoc parser ? */
fclose(fp);
2020-12-05 05:55:34 +11:00
return -1;
}
/* -------------------------------------------------------------- */
2020-12-11 05:19:35 +11:00
int parse_filter_chain(int numid, char *argument)
2020-12-07 14:45:51 +11:00
{
2020-12-19 22:49:06 +11:00
char *cptr, *tmparg;
2020-12-07 14:45:51 +11:00
int value, foo;
2020-12-11 05:19:35 +11:00
if (numid < 0 || numid > NUMBER_OF_STACK) {
2020-12-16 03:49:12 +11:00
fprintf(stderr, "%s: slot number %d is invalid\n", __func__, numid);
2020-12-11 05:19:35 +11:00
exit(1);
}
2020-12-31 00:42:44 +11:00
#if DEBUG_LEVEL
2020-12-07 14:45:51 +11:00
fprintf(stderr, "\n%s: arg = '%s'\n", __func__, argument);
2020-12-31 00:42:44 +11:00
#endif
2020-12-07 14:45:51 +11:00
2020-12-11 05:19:35 +11:00
foo = filterstack_init(numid, 8);
2020-12-07 14:45:51 +11:00
if (foo) {
fprintf(stderr, "%s: filterstack init --> %d\n", __func__, foo);
return foo;
}
2020-12-19 22:49:06 +11:00
/* BUG ?
If the 'argument' string is coming from a default value (as defined
here in main), strtok make a nice segfault. so I make a copy of that
string...
*/
tmparg = alloca(strlen(argument) + 1);
2020-12-23 08:39:00 +11:00
if (NULL==tmparg) {
fprintf(stderr, "memory panic in %s:%s\n", __FILE__, __func__);
exit(1);
}
2020-12-19 22:49:06 +11:00
strcpy(tmparg, argument);
2020-12-07 14:45:51 +11:00
for (;;) {
2020-12-19 22:49:06 +11:00
cptr = strtok(tmparg, ":");
2020-12-07 14:45:51 +11:00
// fprintf(stderr, "cptr %p\n", cptr);
if (NULL==cptr) break;
2020-12-19 22:49:06 +11:00
tmparg = NULL; /* for the next pass of strtok */
2020-12-07 14:45:51 +11:00
// fprintf(stderr, " parsing '%s'\n", cptr);
2020-12-24 20:28:56 +11:00
2020-12-31 00:42:44 +11:00
if (isalpha(*cptr)) {
value = crap_number_from_name(cptr);
// fprintf(stderr, "%s: '%s' -> %d\n", __func__,
// cptr, value);
2020-12-31 10:46:12 +11:00
if (value < 0) {
fprintf(stderr, "%s: '%s' not found\n",
__func__, cptr);
return -1;
}
2020-12-31 00:42:44 +11:00
foo = filterstack_add(numid, value, 1, 1.0);
continue;
}
2020-12-24 20:28:56 +11:00
if ('@' == cptr[0]) {
fprintf(stderr, "%s: got indirect '%s'\n", __func__,
cptr+1);
continue;
}
2020-12-07 14:45:51 +11:00
if (1 == sscanf(cptr, "%d", &value)) {
2020-12-11 05:19:35 +11:00
foo = filterstack_add(numid, value, 1, 1.0);
2020-12-07 14:45:51 +11:00
if (foo) {
fprintf(stderr, "%s: err %d add\n",
__func__, foo);
}
2021-01-15 00:15:42 +11:00
continue;
2020-12-07 14:45:51 +11:00
}
}
2021-01-14 02:09:27 +11:00
if (verbosity > 1) filterstack_list(numid, __func__);
2020-12-07 14:45:51 +11:00
return 0;
}
/* ----------------------------------------------------------- */
2020-12-23 08:39:00 +11:00