FloatImg/Fonderie/crapulator.c
2020-12-19 10:19:18 +01:00

281 lines
5.4 KiB
C

/*
* crapulator.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <floatimg.h>
#include "fonctions.h"
#include "crapulator.h"
#include "glitches.h"
#include "sfx.h"
/* -------------------------------------------------------------- */
static int effect_3(FloatImg *image)
{
float value;
int foo;
value = fimg_get_maxvalue(image);
fimg_mul_cste(image, -1.0);
fimg_add_cste(image, value);
foo = fimg_count_negativ(image);
if (foo) {
fimg_dump_to_file(image, "err.fimg", 0);
fprintf(stderr, "%s negativ %d\n",
__func__, foo);
return -78;
}
return 0;
}
/* -------------------------------------------------------------- */
static int insitu_ctr2x2(FloatImg *pimg)
{
FloatImg img;
int retval;
fimg_clone(pimg, &img, 0);
retval = fimg_contour_2x2(pimg, &img, 0);
if (retval) {
fprintf(stderr, "%s : err contour %d\n",
__func__, retval);
exit(1);
}
fimg_copy_data(&img, pimg);
fimg_destroy(&img);
return 0;
}
/* -------------------------------------------------------------- */
static int insitu_filtre3x3(FloatImg *pimg, int typef)
{
FloatImg img;
int retval;
FimgFilter3x3 *pfiltre;
FimgFilter3x3 lowpass = {
{
1.0, 2.0, 1.0,
2.0, 4.0, 2.0,
1.0, 2.0, 1.0,
},
16.0, 0.0
};
FimgFilter3x3 hipass = {
{
-1.0, -1.0, -1.0,
-1.0, 9.0, -1.0,
-1.0, -1.0, -1.0,
},
1.0, 0.0
};
switch (typef) {
case 0: pfiltre = &lowpass; break;
case 1: pfiltre = &hipass; break;
default:
fprintf(stderr, "%s: bad filter number %d\n",
__func__, typef);
return -6;
break;
}
fimg_clone(pimg, &img, 0);
retval = fimg_filter_3x3(pimg, &img, pfiltre);
if (retval) {
fprintf(stderr, "%s error %d on filter\n", __func__, retval);
exit(1);
}
fimg_killborders(&img); /* XXX WTF OMG */
fimg_copy_data(&img, pimg);
fimg_destroy(&img);
return retval;
}
/* -------------------------------------------------------------- */
static int desaturate(FloatImg *pimg)
{
FloatImg img;
int retval;
fimg_clone(pimg, &img, 0);
retval = fimg_desaturate(pimg, &img, 0);
if (retval) {
fprintf(stderr, "%s : err desaturate %d\n",
__func__, retval);
exit(1);
}
fimg_copy_data(&img, pimg);
fimg_destroy(&img);
return 0;
}
/* -------------------------------------------------------------- */
/*
* This is the main filter engine used both for input and
* output processing. It can be called by the filterstack
* processor.
*/
#define DEBUG_THIS_CRAP 0
int crapulator(FloatImg *image, int idFx, float fval)
{
int retval;
// FloatImg imgtmp;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %d %f )\n", __func__,
image, idFx, fval);
#endif
retval = 0;
#if DEBUG_THIS_CRAP
static int count = 0;
int flag_debug = 0;
if (666==count) {
flag_debug = 1;
fprintf(stderr, "DEBUG PT 1 in %s:%d\n", __func__, __LINE__);
fimg_save_as_png(image, "crap_before.png", 0);
}
#endif
switch (idFx) {
case 0: /* DO NOTHING */
retval = 0; break;
case 1:
fimg_cos_01(image, image,
fimg_get_maxvalue(image));
break;
case 2:
fimg_cos_010(image, image,
fimg_get_maxvalue(image));
break;
case 3:
retval = effect_3(image);
break;
case 4:
brotche_rand48_a(image, 0.20,
fimg_get_maxvalue(image));
break;
case 5:
brotche_rand48_b(image, 0.10,
fimg_get_maxvalue(image)*0.8);
break;
case 6:
retval = fimg_killcolors_a(image, 0.0);
break;
case 7:
retval = fimg_colors_mixer_a(image, 2.0);
break;
case 8:
retval = insitu_ctr2x2(image);
break;
case 9:
retval = fimg_classif_trial(image, image, 0.37, 0);
break;
case 10:
retval = binarize(image, 0);
break;
case 11:
retval = trinarize(image, 0);
break;
case 12:
retval = fimg_lissage_2x2(image);
(void)fimg_killborders(image);
break;
case 13:
retval = insitu_filtre3x3(image, 0);
break;
case 14:
retval = desaturate(image);
break;
case 15:
retval = kill_a_few_lines(image, fval,
image->height/20);
break;
case 16:
retval = bouger_les_pixels(image, 8);
break;
case 17:
retval = mirror_split(image, 0);
break;
case 18:
retval = upside_down(image);
break;
case 19:
retval = insitu_filtre3x3(image, 1);
break;
/* here are the glitches */
case 24: /* experiment ! */
retval = des_bords_sombres_a(image, 160);
break;
case 25:
/* please make this function more tweakable */
retval = vertical_singlitch(image, 290+rand()%45,
fval, 0.19, 0);
break;
default :
fprintf(stderr, "%s : effect #%d invalid\n",
__func__, idFx);
return -77;
}
#if DEBUG_THIS_CRAP
if (flag_debug) {
fprintf(stderr, "DEBUG PT 2 in %s:%d\n", __func__, __LINE__);
fimg_save_as_png(image, "crap_after.png", 0);
flag_debug = 0;
}
count++;
#endif
return retval;
}
/* -------------------------------------------------------------- */
typedef struct {
int id;
char *name;
int ipar;
float fpar;
int flags;
} Crapulor;
/* -------------------------------------------------------------- */
char * crap_name_from_number(int num)
{
switch (num) {
case 0: return "none";
case 1: return "cos01";
case 2: return "cos010";
case 6: return "killcola";
case 7: return "colmixa";
case 8: return "ctr2x2";
case 9: return "classif0";
case 10: return "binariz";
case 11: return "trinariz";
case 13: return "fltr3x3";
case 14: return "desatur";
case 18: return "updown";
}
return "???";
}
/* -------------------------------------------------------------- */
int crap_number_from_name(char *name)
{
return -1;
}
/* -------------------------------------------------------------- */