FloatImg/tools/fimgfx.c

368 lines
8.6 KiB
C
Raw Normal View History

2019-11-29 17:06:40 +01:00
/*
FIMGFX
2021-05-17 22:38:56 +02:00
* some functions here come from 'funcs/contrast.c'
*
2019-11-29 17:06:40 +01:00
*/
#include <stdio.h>
#include <string.h>
2021-05-20 09:31:28 +02:00
#include <stdint.h>
2019-11-29 17:06:40 +01:00
#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-10-09 01:26:07 +02:00
Fx_rot90, Fx_cmixa, Fx_desat, Fx_ctr2x2, Fx_norm,
2021-05-17 22:38:56 +02:00
Fx_classtrial, Fx_mirror, Fx_shift0, Fx_trimul,
2021-11-02 16:01:11 +01:00
Fx_xper, Fx_binarize, Fx_trinarize, Fx_hilight_R,
2022-11-01 21:18:09 +01:00
Fx_absolute, Fx_clamp, Fx_fakol0, Fx_fakol3,
2024-03-20 20:07:48 +01:00
Fx_morph0, Fx_morph4, Fx_morph8, Fx_thermocol };
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 },
2021-04-23 13:20:20 +02:00
// { "halfsz0", Fx_halfsz0, 0, 1 },
// { "rot90", Fx_rot90, 0, 0 },
{ "cmixa", Fx_cmixa, 0, 1 },
2020-03-26 09:12:25 +01:00
{ "xper", Fx_xper, 0, 1 },
2020-10-07 15:26:34 +02:00
{ "desat", Fx_desat, 0, 1 },
{ "ctr2x2", Fx_ctr2x2, 0, 1 },
2021-04-26 11:43:42 +02:00
{ "mirror", Fx_mirror, 0, 1 },
2021-04-28 00:21:45 +02:00
{ "shift0", Fx_shift0, 0, 1 },
2021-05-17 22:38:56 +02:00
{ "trimul", Fx_trimul, 0, 1 },
2021-04-23 13:20:20 +02:00
// { "norm", Fx_norm, 0, 1 },
2021-04-20 17:46:54 +02:00
{ "classtrial", Fx_classtrial, 0, 1 },
2021-04-20 12:06:19 +02:00
{ "binarize", Fx_binarize, 0, 1 },
{ "trinarize", Fx_trinarize, 0, 1 },
2021-03-25 11:11:16 +01:00
{ "hilightr", Fx_hilight_R, 0, 1 },
2021-11-02 19:27:19 +01:00
{ "abs", Fx_absolute, 0, 1 },
2021-11-26 23:10:29 +01:00
{ "clamp", Fx_clamp, 0, 1 },
2022-07-06 10:27:55 +02:00
{ "fakol0", Fx_fakol0, 0, 1 },
2022-11-01 21:18:09 +01:00
{ "morph0", Fx_morph0, 0, 1 },
{ "morph4", Fx_morph4, 0, 1 },
{ "morph8", Fx_morph8, 0, 1 },
2024-03-20 20:07:48 +01:00
{ "thermocol", Fx_thermocol, 0, 1 },
2021-03-25 11:11:16 +01:00
{ NULL, 0, 0, 0 }
2019-11-29 17:06:40 +01:00
};
2021-04-23 13:20:20 +02:00
/* --------------------------------------------------------------------- */
static void list_of_effects(void)
{
Fx *fx;
2021-05-17 22:38:56 +02:00
/* this list must be on just ONE columns */
2021-04-23 13:20:20 +02:00
for (fx=fx_list; fx->name; fx++) {
printf("%s\n", fx->name);
}
}
2019-11-29 17:06:40 +01:00
/* --------------------------------------------------------------------- */
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 */
}
/* --------------------------------------------------------------------- */
2020-10-04 14:00:44 +02:00
/*
* this is the mutant function
*/
2020-02-17 01:27:33 +01:00
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_crump_hard(S, D, kf, 0);
2020-02-17 01:27:33 +01:00
if (foo) {
fprintf(stderr, "%s error %d experiment %p\n", __func__,
2020-10-04 14:00:44 +02:00
foo, S);
2020-02-17 01:27:33 +01:00
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;
2021-04-20 12:06:19 +02:00
int foo;
2019-11-29 17:06:40 +01:00
2021-05-17 22:38:56 +02: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");
2021-04-23 13:20:20 +02:00
puts("\t-l\tlist effects");
2020-01-10 14:18:38 +01:00
puts("\t-v\tincrease verbosity");
2020-01-03 18:21:43 +01:00
puts("effects:");
printf("\t");
2021-04-20 12:06:19 +02:00
foo = 0;
for (fx=fx_list; fx->name; fx++) {
2021-04-20 12:06:19 +02:00
foo += printf("%s ", fx->name);
if (foo > 55) {
printf("\n\t");
foo = 0;
}
}
2021-05-17 22:38:56 +02:00
puts("\n");
2022-01-31 20:26:45 +01:00
if (lvl) fimg_print_version(1);
2019-11-29 17:06:40 +01:00
exit(0);
}
/* --------------------------------------------------------------------- */
2021-03-25 11:11:16 +01:00
int do_an_effect(char *srcfname, int fxidx, char *dstfname)
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__,
2021-03-25 11:11:16 +01:00
srcfname, action, dstfname);
2019-11-29 17:06:40 +01:00
#endif
2021-03-25 11:11:16 +01:00
foo = fimg_create_from_dump(srcfname, &src);
2019-11-29 17:06:40 +01:00
if (foo) {
2021-03-25 11:11:16 +01:00
fprintf(stderr, "err load '%s' : %d\n", srcfname, foo);
2019-11-29 17:06:40 +01:00
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 {
2022-01-31 20:26:45 +01:00
fprintf(stderr, "%s: flags & 1 ???\n", __func__); /* XXX */
2020-03-26 09:12:25 +01:00
memset(&dest, 0, sizeof(dest));
2021-04-20 17:46:54 +02:00
return -888;
2020-03-26 09:12:25 +01:00
}
action = fx_list[fxidx].id;
2019-11-29 17:06:40 +01:00
2022-07-06 10:27:55 +02:00
if (verbosity > 1) {
2022-06-06 22:17:15 +02:00
fprintf(stderr, "%s: fxidx=%d action=%d\n", __func__,
fxidx, action);
}
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;
case Fx_cmixa:
fimg_copy_data(&src, &dest);
foo = fimg_colors_mixer_a(&dest, 2.0); break;
2020-03-26 09:12:25 +01:00
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");
2021-04-05 19:26:10 +02:00
fprintf(stderr, "see 'fimghalfsize.c'. \n");
2020-02-16 16:21:27 +01:00
return -3;
2021-04-20 17:46:54 +02:00
case Fx_classtrial:
fprintf(stderr, "classif trial with %f fvalue\n",
global_fvalue);
foo = fimg_classif_trial(&src, &dest, global_fvalue, 0);
2021-05-29 23:29:13 +02:00
break;
2020-10-07 15:26:34 +02:00
case Fx_desat:
fimg_copy_data(&src, &dest);
foo = fimg_mix_rgb_gray(&dest, global_fvalue);
2021-05-29 23:29:13 +02:00
break;
2021-04-26 11:43:42 +02:00
case Fx_mirror:
foo = fimg_mirror(&src, &dest, 0);
2021-05-29 23:29:13 +02:00
break;
2021-04-28 00:21:45 +02:00
case Fx_shift0:
2021-05-17 22:38:56 +02:00
// fprintf(stderr, "Krkrk %d\n", action);
2021-04-28 00:21:45 +02:00
foo = fimg_auto_shift_to_zero(&src, &dest);
2022-07-06 10:27:55 +02:00
if (foo) {
fprintf(stderr, "*** shiftzero -> %d\n", foo);
}
2021-05-29 23:29:13 +02:00
break;
2021-05-17 22:38:56 +02:00
case Fx_trimul:
2022-07-06 10:27:55 +02:00
// fprintf(stderr, "trimul %d\n", action);
2021-05-17 22:38:56 +02:00
foo = fimg_sfx_triplemul(&src, &dest, 0);
2021-05-29 23:29:13 +02:00
break;
2020-10-07 15:26:34 +02:00
case Fx_ctr2x2:
foo = fimg_contour_2x2(&src, &dest, 0);
2021-05-29 23:29:13 +02:00
break;
2021-03-25 11:11:16 +01:00
case Fx_binarize:
fimg_copy_data(&src, &dest);
foo = fimg_binarize(&dest, 0);
2021-05-29 23:29:13 +02:00
break;
2021-04-20 12:06:19 +02:00
case Fx_trinarize:
fimg_copy_data(&src, &dest);
foo = fimg_trinarize(&dest, 0);
2021-05-29 23:29:13 +02:00
break;
2021-03-25 11:11:16 +01:00
case Fx_hilight_R:
foo = fimg_highlight_color(&src, &dest, 'R', 1.333);
2021-05-29 23:29:13 +02:00
break;
2021-11-02 16:01:11 +01:00
case Fx_absolute:
fimg_copy_data(&src, &dest);
foo = fimg_absolute(&dest);
break;
2021-11-26 23:10:29 +01:00
case Fx_clamp:
fimg_copy_data(&src, &dest);
foo = fimg_clamp_negativ(&dest);
/* this func return the count of
negative pixels killed */
if (foo > 0) foo = 0;
break;
2022-01-31 20:26:45 +01:00
2022-07-06 10:27:55 +02:00
case Fx_fakol0:
/***********************/
/* DO SOMETHING HERE ! */
/***********************/
foo = fimg_falsecolors_0(&src, &dest, 0, 1.0);
break;
2022-01-31 20:26:45 +01:00
2022-11-01 21:18:09 +01:00
case Fx_morph0:
foo = fimg_filtre_morpho_0(&src, &dest, 0); break;
case Fx_morph4:
foo = fimg_filtre_morpho_0(&src, &dest, 4); break;
case Fx_morph8:
foo = fimg_filtre_morpho_0(&src, &dest, 8); break;
2024-03-20 20:07:48 +01:00
case Fx_thermocol:
foo = fimg_auto_thermique(&src, &dest, 0); break;
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;
}
2022-05-17 08:30:20 +02:00
foo = fimg_export_picture(&dest, dstfname, 0);
// foo = fimg_dump_to_file(&dest, dstfname, 0);
2019-11-29 17:06:40 +01:00
if (foo) {
2021-04-23 13:20:20 +02:00
fprintf(stderr, "dumping datas to file '%s' give us a %d\n",
dstfname, foo);
2019-11-29 17:06:40 +01:00
return foo;
}
2021-04-20 12:06:19 +02:00
fimg_destroy(&src);
if (dest.type) {
fimg_destroy(&dest);
}
2019-11-29 17:06:40 +01:00
return 0;
}
/* --------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int foo, opt, action;
int nba;
char *operator;
char *srcname = "";
char *dstname = "out.fimg";
2022-01-31 20:26:45 +01:00
while ((opt = getopt(argc, argv, "hk:lvx")) != -1) {
2019-11-29 17:06:40 +01:00
switch(opt) {
case 'h': help(0); break;
case 'k': global_fvalue = atof(optarg); break;
2022-01-31 20:26:45 +01:00
case 'l': list_of_effects(); exit(0);
2019-11-29 17:06:40 +01:00
case 'v': verbosity++; break;
2022-01-31 20:26:45 +01:00
case 'x': break;
2023-05-29 09:06:57 +02:00
default: exit(1);
2019-11-29 17:06:40 +01:00
}
}
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);
}
2020-06-24 12:37:02 +02:00
if (verbosity>1) fprintf(stderr, "*** fimgfx *** %s %s\n", __DATE__, __TIME__);
2022-06-06 22:17:15 +02:00
if (verbosity>2) {
for (foo=0; foo<argc; foo++)
fprintf(stderr, "\t\t%5d\t%s\n", foo, argv[foo]);
}
2020-06-24 12:37:02 +02:00
2019-11-29 17:06:40 +01:00
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);
}
2021-11-26 23:10:29 +01:00
if (verbosity > 1) {
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];
2021-11-26 23:10:29 +01:00
if (verbosity > 1) fprintf(stderr, "Fx %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;
}
/* --------------------------------------------------------------------- */