FloatImg4PythonBinding/tools/fimgfx.c

237 lines
5.0 KiB
C
Raw Normal View History

2019-11-29 17:06:40 +01:00
/*
FIMGFX
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "../floatimg.h"
/* --------------------------------------------------------------------- */
int verbosity;
float global_fvalue;
typedef struct {
char *name;
int id;
int nbarg;
2020-03-26 09:12:25 +01:00
int flags;
2019-11-29 17:06:40 +01:00
} Fx;
2020-02-17 01:27:33 +01:00
enum fxid { Fx_cos01=5, Fx_cos010, Fx_pow2, Fx_sqrt, Fx_gray0, Fx_halfsz0,
2020-03-26 09:12:25 +01:00
Fx_rot90,
2020-02-17 01:27:33 +01:00
Fx_xper };
2019-11-29 17:06:40 +01:00
Fx fx_list[] = {
2020-03-26 09:12:25 +01:00
{ "cos01", Fx_cos01, 0, 1 },
{ "cos010", Fx_cos010, 0, 1 },
{ "pow2", Fx_pow2, 0, 1 },
{ "sqrt", Fx_sqrt, 0, 1 },
{ "gray0", Fx_gray0, 0, 1 },
{ "halfsz0", Fx_halfsz0, 0, 1 },
{ "rot90", Fx_rot90, 0, 0 },
{ "xper", Fx_xper, 0, 1 },
2019-11-29 17:06:40 +01:00
{ NULL, 0, 0 }
};
/* --------------------------------------------------------------------- */
2020-03-26 09:12:25 +01:00
int lookup_fxidx(char *txt)
2019-11-29 17:06:40 +01:00
{
Fx *fx;
int n;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( '%s' )\n", __func__, txt);
#endif
for (n=0, fx=fx_list; fx->name; fx++, n++) {
2020-02-17 01:27:33 +01:00
#if DEBUG_LEVEL > 1
2020-03-26 09:12:25 +01:00
fprintf(stderr, " -> %3d %s\n", n, fx->name);
2019-11-29 17:06:40 +01:00
#endif
if (!strcmp(fx->name, txt)) {
2020-03-26 09:12:25 +01:00
return n;
2019-11-29 17:06:40 +01:00
}
}
2020-02-17 01:27:33 +01:00
return -1; /* NOT FOUND */
}
/* --------------------------------------------------------------------- */
int do_experiment(FloatImg *S, FloatImg *D, float kf)
{
int foo;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %f )\n", __func__, S, D, kf);
#endif
foo = fimg_copy_data(S, D);
if (foo) {
fprintf(stderr, "%s err %d copy data %p -> %p\n", __func__,
foo, S, D);
return -98;
}
foo = fimg_killcolors_a(D, kf);
if (foo) {
fprintf(stderr, "%s err %d killcolors %p %f\n", __func__,
foo, S, kf);
return -98;
}
2019-11-29 17:06:40 +01:00
2020-02-17 01:27:33 +01:00
return 0;
2019-11-29 17:06:40 +01:00
}
/* --------------------------------------------------------------------- */
static void help(int lvl)
{
Fx *fx;
2019-11-29 17:06:40 +01:00
2020-02-19 22:42:52 +01:00
printf("-- fimg special effects -- %s %s --\n", __DATE__, __TIME__);
2020-01-03 18:21:43 +01:00
puts("usage:");
2020-02-19 22:32:24 +01:00
puts("\tfimgfx [options] <effect> source.fimg resultat.fimg");
2020-01-03 18:21:43 +01:00
2020-01-10 14:18:38 +01:00
puts("options:");
puts("\t-k N.N\tset the floating value");
puts("\t-v\tincrease verbosity");
2020-01-03 18:21:43 +01:00
puts("effects:");
printf("\t");
for (fx=fx_list; fx->name; fx++) {
printf("%s ", fx->name);
}
puts("");
2020-02-19 22:32:24 +01:00
fimg_print_version(1);
2019-11-29 17:06:40 +01:00
exit(0);
}
/* --------------------------------------------------------------------- */
2020-03-26 09:12:25 +01:00
int do_an_effect(char *sf, int fxidx, char *df)
2019-11-29 17:06:40 +01:00
{
FloatImg src, dest;
2020-03-26 09:12:25 +01:00
int foo, action;
2019-11-29 17:06:40 +01:00
double maxval;
2019-11-29 19:55:52 +01:00
#if DEBUG_LEVEL
2019-11-29 17:06:40 +01:00
fprintf(stderr, ">>> %s ( '%s' %d '%s' )\n", __func__,
2020-03-26 09:12:25 +01:00
sf, action, df);
2019-11-29 17:06:40 +01:00
#endif
foo = fimg_create_from_dump(sf, &src);
if (foo) {
fprintf(stderr, "err load '%s' : %d\n", sf, foo);
return foo;
}
maxval = (double)fimg_get_maxvalue(&src);
2020-03-26 09:12:25 +01:00
if (fx_list[fxidx].flags & 1) {
foo = fimg_clone(&src, &dest, 0);
if (foo) {
fprintf(stderr, "err clone %p : %d\n", &src, foo);
return foo;
}
2019-11-29 17:06:40 +01:00
}
2020-03-26 09:12:25 +01:00
else {
/* XXX */
memset(&dest, 0, sizeof(dest));
}
action = fx_list[fxidx].id;
2019-11-29 17:06:40 +01:00
2020-03-26 09:12:25 +01:00
switch (action) {
2019-11-29 17:06:40 +01:00
case Fx_cos01:
fimg_cos_01(&src, &dest, maxval); break;
case Fx_cos010:
fimg_cos_010(&src, &dest, maxval); break;
2019-11-29 17:06:40 +01:00
case Fx_pow2:
fimg_power_2(&src, &dest, maxval); break;
case Fx_sqrt:
fimg_square_root(&src, &dest, maxval); break;
2020-01-10 14:18:38 +01:00
case Fx_gray0: /* new 2020 01 10 */
fimg_to_gray(&src); fimg_copy_data(&src, &dest);
break;
2020-02-17 01:27:33 +01:00
case Fx_xper:
do_experiment(&src, &dest, maxval); break;
2020-03-26 09:12:25 +01:00
case Fx_rot90:
foo = fimg_rotate_90(&src, &dest, 0); break;
2020-02-16 16:21:27 +01:00
case Fx_halfsz0:
2020-03-26 09:12:25 +01:00
fprintf(stderr, "halfsize was not implemented\n");
2020-02-16 16:21:27 +01:00
return -3;
2019-11-29 17:06:40 +01:00
default:
2020-02-19 22:32:24 +01:00
fprintf(stderr, "%s %s : %d is bad action\n",
2020-03-26 09:12:25 +01:00
__FILE__, __func__, action);
2019-11-29 17:06:40 +01:00
break;
}
foo = fimg_dump_to_file(&dest, df, 0);
if (foo) {
fprintf(stderr, "dumping datas to file give us a %d\n", foo);
return foo;
}
return 0;
}
/* --------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int foo, opt, action;
int nba;
char *operator;
char *srcname = "";
char *dstname = "out.fimg";
while ((opt = getopt(argc, argv, "hk:v")) != -1) {
switch(opt) {
case 'h': help(0); break;
case 'k': global_fvalue = atof(optarg); break;
case 'v': verbosity++; break;
}
}
2019-11-29 19:55:52 +01:00
#if DEBUG_LEVEL
2019-11-29 17:06:40 +01:00
fprintf(stderr, "argc %d optind %d\n", argc, optind);
for (foo=0; foo<argc; foo++)
fprintf(stderr, "%3d %c %s\n", foo, foo==optind?'*':' ', argv[foo]);
#endif
if (3 > argc-optind) {
fprintf(stderr, "%s need some arguments...\n", argv[0]);
exit(1);
}
operator = argv[optind];
2020-03-26 09:12:25 +01:00
action = lookup_fxidx(operator);
2019-11-29 17:06:40 +01:00
if (action < 0) {
fprintf(stderr, "garbage found in opcode field : %s\n", operator);
exit(1);
}
if (verbosity) {
2019-11-29 19:55:52 +01:00
fprintf(stderr, " global fvalue %f\n", global_fvalue);
fprintf(stderr, " action %d\n", action);
fprintf(stderr, " verbosity %d\n", verbosity);
2019-11-29 17:06:40 +01:00
}
if ((nba=fx_list[action].nbarg)) {
fprintf(stderr, "action '%s' need %d arg\n", operator, nba);
}
srcname = argv[optind+1];
dstname = argv[optind+2];
2019-11-29 19:55:52 +01:00
if (verbosity) fprintf(stderr, "%s ==> %s\n", srcname, dstname);
2019-11-29 17:06:40 +01:00
foo = do_an_effect(srcname, action, dstname);
2019-11-29 19:55:52 +01:00
if (foo) {
fprintf(stderr, "do an effect -> %d\n", foo);
}
2019-11-29 17:06:40 +01:00
return 0;
}
/* --------------------------------------------------------------------- */