FloatImg4PythonBinding/Fonderie/crapulator.c

315 lines
6.2 KiB
C
Raw Normal View History

2020-11-02 01:25:00 +01:00
/*
* crapulator.c
*/
#include <stdio.h>
#include <stdlib.h>
2020-12-30 14:42:44 +01:00
#include <string.h>
#include <unistd.h>
2020-11-02 01:25:00 +01:00
#include <floatimg.h>
2020-11-02 14:51:48 +01:00
#include "fonctions.h"
2020-11-02 01:25:00 +01:00
#include "crapulator.h"
2020-11-16 11:12:29 +01:00
#include "glitches.h"
2020-12-02 19:55:06 +01:00
#include "sfx.h"
2020-11-02 01:25:00 +01:00
2020-11-04 10:41:21 +01:00
/* -------------------------------------------------------------- */
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;
}
2020-11-02 01:25:00 +01:00
/* -------------------------------------------------------------- */
2020-12-19 10:19:18 +01:00
static int insitu_filtre3x3(FloatImg *pimg, int typef)
2020-12-03 11:47:44 +01:00
{
FloatImg img;
int retval;
2020-12-19 10:19:18 +01:00
FimgFilter3x3 *pfiltre;
2020-12-03 11:47:44 +01:00
2020-12-19 10:19:18 +01:00
FimgFilter3x3 lowpass = {
2020-12-03 11:47:44 +01:00
{
1.0, 2.0, 1.0,
2.0, 4.0, 2.0,
1.0, 2.0, 1.0,
},
16.0, 0.0
};
2020-12-19 10:19:18 +01:00
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;
}
2020-12-03 11:47:44 +01:00
fimg_clone(pimg, &img, 0);
2020-12-19 10:19:18 +01:00
retval = fimg_filter_3x3(pimg, &img, pfiltre);
2020-12-03 11:47:44 +01:00
if (retval) {
fprintf(stderr, "%s error %d on filter\n", __func__, retval);
exit(1);
}
2020-12-07 10:29:48 +01:00
fimg_killborders(&img); /* XXX WTF OMG */
2020-12-03 11:47:44 +01:00
fimg_copy_data(&img, pimg);
fimg_destroy(&img);
return retval;
}
/* -------------------------------------------------------------- */
2020-11-03 00:00:09 +01:00
/*
2020-12-07 12:52:17 +01:00
* This is the main filter engine used both for input and
* output processing. It can be called by the filterstack
* processor.
2020-11-03 00:00:09 +01:00
*/
2020-11-20 22:25:30 +01:00
2020-12-03 11:47:44 +01:00
#define DEBUG_THIS_CRAP 0
2020-11-20 22:25:30 +01:00
2020-11-02 14:51:48 +01:00
int crapulator(FloatImg *image, int idFx, float fval)
2020-11-02 01:25:00 +01:00
{
2020-11-05 21:07:38 +01:00
int retval;
// FloatImg imgtmp;
2020-11-02 01:25:00 +01:00
#if DEBUG_LEVEL
2020-11-02 14:51:48 +01:00
fprintf(stderr, ">>> %s ( %p %d %f )\n", __func__,
image, idFx, fval);
2020-11-02 01:25:00 +01:00
#endif
2020-11-03 00:00:09 +01:00
retval = 0;
2020-11-20 22:25:30 +01:00
#if DEBUG_THIS_CRAP
2020-12-03 11:47:44 +01:00
static int count = 0;
int flag_debug = 0;
2020-11-02 14:51:48 +01:00
if (666==count) {
flag_debug = 1;
2020-11-02 22:20:54 +01:00
fprintf(stderr, "DEBUG PT 1 in %s:%d\n", __func__, __LINE__);
2020-11-03 00:00:09 +01:00
fimg_save_as_png(image, "crap_before.png", 0);
2020-11-02 14:51:48 +01:00
}
2020-11-20 22:25:30 +01:00
#endif
2020-11-02 14:51:48 +01:00
switch (idFx) {
2020-12-30 14:42:44 +01:00
case CR_none: /* DO NOTHING */
2020-11-02 14:51:48 +01:00
retval = 0; break;
2020-12-30 14:42:44 +01:00
case CR_cos01:
2020-11-02 14:51:48 +01:00
fimg_cos_01(image, image,
fimg_get_maxvalue(image));
break;
2020-12-30 14:42:44 +01:00
case CR_cos010:
2020-11-02 14:51:48 +01:00
fimg_cos_010(image, image,
fimg_get_maxvalue(image));
break;
2020-12-30 14:42:44 +01:00
case CR_fx3:
2020-11-04 10:41:21 +01:00
retval = effect_3(image);
2020-11-02 14:51:48 +01:00
break;
2020-12-30 14:42:44 +01:00
case CR_rnd48a:
2020-11-02 14:51:48 +01:00
brotche_rand48_a(image, 0.20,
fimg_get_maxvalue(image));
break;
2020-12-30 14:42:44 +01:00
case CR_rnd48b:
2020-11-02 14:51:48 +01:00
brotche_rand48_b(image, 0.10,
fimg_get_maxvalue(image)*0.8);
break;
2020-12-30 14:42:44 +01:00
case CR_killcola:
2020-12-07 12:52:17 +01:00
retval = fimg_killcolors_a(image, 0.0);
2020-11-02 14:51:48 +01:00
break;
2020-12-30 14:42:44 +01:00
case CR_colmixa:
2020-11-02 14:51:48 +01:00
retval = fimg_colors_mixer_a(image, 2.0);
break;
2020-12-30 14:42:44 +01:00
case CR_ctr2x2:
2020-11-04 10:41:21 +01:00
retval = insitu_ctr2x2(image);
2020-11-02 14:51:48 +01:00
break;
2020-12-30 14:42:44 +01:00
case CR_classtrial:
2020-11-02 22:20:54 +01:00
retval = fimg_classif_trial(image, image, 0.37, 0);
2020-11-02 14:51:48 +01:00
break;
2020-12-30 14:42:44 +01:00
case CR_binarize:
2020-11-05 21:07:38 +01:00
retval = binarize(image, 0);
break;
2020-12-30 14:42:44 +01:00
case CR_trinarize:
2020-11-10 00:50:25 +01:00
retval = trinarize(image, 0);
break;
2020-12-30 14:42:44 +01:00
case CR_liss2x2:
2020-12-02 19:55:06 +01:00
retval = fimg_lissage_2x2(image);
2020-12-10 19:19:35 +01:00
(void)fimg_killborders(image);
2020-12-02 19:55:06 +01:00
break;
2020-12-30 14:42:44 +01:00
case CR_liss3x3:
2020-12-24 10:28:56 +01:00
/* smooth filter */
2020-12-19 10:19:18 +01:00
retval = insitu_filtre3x3(image, 0);
2020-12-03 11:47:44 +01:00
break;
2020-12-30 14:42:44 +01:00
case CR_desaturate:
2020-12-20 13:19:35 +01:00
retval = fimg_desaturate(image, image, 0);
2020-12-07 12:52:17 +01:00
break;
2020-12-30 14:42:44 +01:00
case CR_killlines:
2020-12-08 15:51:07 +01:00
retval = kill_a_few_lines(image, fval,
image->height/20);
break;
2020-12-30 14:42:44 +01:00
case CR_water:
retval = bouger_les_pixels(image, 8);
2020-12-10 19:19:35 +01:00
break;
2020-12-30 14:42:44 +01:00
case CR_mirsplit:
2020-12-10 19:19:35 +01:00
retval = mirror_split(image, 0);
break;
2020-12-30 14:42:44 +01:00
case CR_updown:
2020-12-18 10:18:09 +01:00
retval = upside_down(image);
break;
2020-12-30 14:42:44 +01:00
case CR_hipass:
2020-12-24 10:28:56 +01:00
/* hipass filter */
2020-12-19 10:19:18 +01:00
retval = insitu_filtre3x3(image, 1);
break;
2020-12-30 14:42:44 +01:00
case CR_octotree:
2020-12-29 12:56:26 +01:00
retval = octotree_classif(image, 0.500, 0);
2020-12-29 00:54:15 +01:00
break;
2020-12-07 12:52:17 +01:00
/* here are the glitches */
2020-12-30 14:42:44 +01:00
case CR_bsombra: /* experiment ! */
2020-11-25 10:32:02 +01:00
retval = des_bords_sombres_a(image, 160);
2020-11-20 22:25:30 +01:00
break;
2020-12-30 14:42:44 +01:00
case CR_vsglitch:
2020-11-18 11:47:26 +01:00
/* please make this function more tweakable */
2020-12-02 19:55:06 +01:00
retval = vertical_singlitch(image, 290+rand()%45,
2020-12-30 14:42:44 +01:00
fval, 0.19, 0);
2020-11-15 11:47:37 +01:00
break;
2020-12-20 11:12:43 +01:00
2020-12-30 14:42:44 +01:00
case CR_rndblks:
2020-12-23 16:40:45 +01:00
retval = random_blocks(image, 70);
2020-12-20 11:12:43 +01:00
break;
2020-12-30 14:42:44 +01:00
case CR_message:
fprintf(stderr, "### message from pid %d, fv=%f ###\n",
getpid(), fval);
retval = 0;
break;
2020-11-02 14:51:48 +01:00
default :
fprintf(stderr, "%s : effect #%d invalid\n",
__func__, idFx);
return -77;
}
2020-11-02 01:25:00 +01:00
2020-11-20 22:25:30 +01:00
#if DEBUG_THIS_CRAP
2020-11-02 14:51:48 +01:00
if (flag_debug) {
2020-11-02 22:20:54 +01:00
fprintf(stderr, "DEBUG PT 2 in %s:%d\n", __func__, __LINE__);
2020-11-03 00:00:09 +01:00
fimg_save_as_png(image, "crap_after.png", 0);
2020-11-20 22:25:30 +01:00
flag_debug = 0;
2020-11-02 01:25:00 +01:00
}
2020-11-20 22:25:30 +01:00
count++;
2020-12-03 11:47:44 +01:00
#endif
2020-11-02 01:25:00 +01:00
2020-11-02 14:51:48 +01:00
return retval;
2020-11-02 01:25:00 +01:00
}
2020-12-19 10:19:18 +01:00
/* -------------------------------------------------------------- */
typedef struct {
int id;
char *name;
int ipar;
float fpar;
int flags;
} Crapulor;
2020-12-30 14:42:44 +01:00
/* Warning: overengeniring inside */
#include "crapstr.h" /* generated file ! */
2020-12-29 16:40:26 +01:00
#define NBCRAP (sizeof(CrapL)/sizeof(Crapulor))
void list_crapulors(char *texte)
{
int idx;
2020-12-30 14:42:44 +01:00
#define OUT stdout
fprintf(OUT, "______________. %s\n", texte);
for (idx=0; CrapL[idx].id!=-1; idx++) {
fprintf(OUT, " %-12s | %4d | %5d | %8.3f\n",
2020-12-29 16:40:26 +01:00
CrapL[idx].name,
2020-12-30 14:42:44 +01:00
CrapL[idx].id,
2020-12-29 16:40:26 +01:00
CrapL[idx].ipar,
CrapL[idx].fpar);
}
2020-12-30 14:42:44 +01:00
#undef OUT
2020-12-29 16:40:26 +01:00
}
2020-11-10 19:30:49 +01:00
/* -------------------------------------------------------------- */
char * crap_name_from_number(int num)
{
2020-12-30 14:42:44 +01:00
int idx;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %d )\n", __func__, num);
#endif
for (idx=0; CrapL[idx].id!=-1; idx++) {
if (num == CrapL[idx].id) {
return CrapL[idx].name;
}
2020-12-15 17:49:12 +01:00
}
2020-11-02 01:25:00 +01:00
2020-11-10 19:30:49 +01:00
return "???";
}
/* -------------------------------------------------------------- */
int crap_number_from_name(char *name)
{
2020-12-30 14:42:44 +01:00
int idx, foo, retval;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( '%s' )\n", __func__, name);
#endif
retval = -1; /* not found */
for (idx=0; CrapL[idx].id!=-1; idx++) {
foo = strcmp(CrapL[idx].name, name);
if (0 == foo) {
// fprintf(stderr, "found '%s' -> %d\n", name,
// CrapL[idx].id);
retval = CrapL[idx].id;
break;
}
}
2020-11-10 19:30:49 +01:00
2020-12-30 14:42:44 +01:00
return retval; /* not found */
2020-11-10 19:30:49 +01:00
}
2020-11-02 01:25:00 +01:00
/* -------------------------------------------------------------- */