FloatImg4PythonBinding/funcs/classif.c

104 lines
2.4 KiB
C
Raw Normal View History

2020-10-04 22:05:28 +11:00
/*
* classif.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "../floatimg.h"
2020-10-16 20:48:54 +11:00
extern int verbosity;
2020-10-04 22:05:28 +11:00
/* --------------------------------------------------------------------- */
/* nouveau 2 octobre 2020, juste avant sonoptic de la pluie craignos */
2020-10-06 21:48:17 +11:00
int fimg_classif_trial(FloatImg *psrc, FloatImg *pdst, float fval, int notused)
2020-10-04 22:05:28 +11:00
{
float minmax[6], delta[3], baryc[3];
float range, dist, rgb[3], dr, dg, db;
int x, y, on, off;
#if DEBUG_LEVEL
2020-10-14 19:32:29 +11:00
fprintf(stderr, ">>> %s ( %p %p %f %d )\n", __func__,
psrc, pdst, fval, notused);
2020-10-04 22:05:28 +11:00
#endif
2020-10-14 19:32:29 +11:00
if (FIMG_TYPE_RGB != psrc->type) {
fprintf(stderr, "%s: bad src type %d\n", __func__, psrc->type);
return -7;
}
if (fimg_images_not_compatible(psrc, pdst)) {
fprintf(stderr, "%s: bad dst type %d\n", __func__, pdst->type);
return -8;
}
2020-10-04 22:05:28 +11:00
/* calculer les amplitudes RGB de l'image source */
fimg_get_minmax_rgb(psrc, minmax);
delta[0] = minmax[1] - minmax[0];
delta[1] = minmax[3] - minmax[2];
delta[2] = minmax[5] - minmax[4];
/* chercher le plus petit des deltas */
range = delta[0]; if (delta[1]<range) range=delta[1];
if (delta[2]<range) range=delta[2];
/* convertir le diametre en rayon (magic inside) */
2020-10-06 21:48:17 +11:00
range *= fval;
2020-10-04 22:05:28 +11:00
2020-10-06 21:48:17 +11:00
if (verbosity ) fprintf(stderr, "deltas : %f %f %f / %f\n",
delta[0], delta[1], delta[2], range);
2020-10-04 22:05:28 +11:00
/* calcul du "barycentre" chromatique */
baryc[0] = (minmax[1] + minmax[0]) / 2;
baryc[1] = (minmax[3] + minmax[2]) / 2;
baryc[2] = (minmax[5] + minmax[4]) / 2;
2020-10-06 21:48:17 +11:00
if (verbosity) fprintf(stderr, "barycs : %f %f %f\n",
baryc[0], baryc[1], baryc[2]);
2020-10-04 22:05:28 +11:00
on = off = 0;
for (y=0; y<psrc->height; y++) {
for (x=0; x<psrc->width; x++) {
fimg_get_rgb(psrc, x, y, rgb);
/* calcul de la distance chromatique */
dr = rgb[0] - baryc[0];
dg = rgb[1] - baryc[1];
db = rgb[2] - baryc[2];
dist = sqrtf( (dr*dr) + (dg*dg) + (db*db) );
#if DEBUG_LEVEL
if (x==y) fprintf(stderr, "%4d %4d %f\n", x, y, dist);
#endif
2020-10-06 21:48:17 +11:00
/* action !!! */
2020-10-04 22:05:28 +11:00
if (dist > range) {
2020-10-06 21:48:17 +11:00
/* make our pixel gray-level */
rgb[0] = rgb[1] = rgb[2] = 0.0;
// (rgb[0] + rgb[1] + rgb[2]) / 3.0;
2020-10-05 18:16:20 +11:00
on++;
2020-10-04 22:05:28 +11:00
}
else {
2020-10-06 21:48:17 +11:00
/* the easy part : do nothing */
2020-10-05 18:16:20 +11:00
off++;
2020-10-04 22:05:28 +11:00
}
fimg_put_rgb(pdst, x, y, rgb);
/* MUST BE MORE CREATIVE HERE !!! */
}
}
fprintf(stderr, "on %d off %d\n", on, off);
return 0;
}
/* --------------------------------------------------------------------- */