FloatImg4PythonBinding/funcs/fmorpho.c

90 lines
1.8 KiB
C
Raw Normal View History

2022-11-01 09:35:40 +01:00
/*
* FLOATIMG
* --------
* F M O R P H O
*
* nouveau 30 septembre 2022 / integration 28 octobre 2022
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
2023-01-21 00:07:21 +01:00
#include "../floatimg.h"
2022-11-01 09:35:40 +01:00
/* --------------------------------------------------------------------- !*/
static struct
{
int x, y;
} deltas[] =
{ { -1, -1, },
{ 0, -1, },
{ 1, -1, },
{ -1, 0, },
{ 0, 0, },
{ 1, 0, },
{ -1, 1, },
{ 0, 1, },
{ 1, 1 }
};
typedef struct
{
int x, y; // not used
float r, g, b;
float fgris;
int rang;
} fpixel;
static fpixel pixels[9];
/* --------------------------------------------------------------------- !*/
2022-11-02 22:11:54 +01:00
static int comparaison_fgris(const void *A, const void *B)
2022-11-01 09:35:40 +01:00
{
return ((fpixel *)A)->fgris > ((fpixel *)B)->fgris;
}
/* --------------------------------------------------------------------- !*/
/*
* this is a klugy approch, sorry.
*/
int fimg_filtre_morpho_0(FloatImg *sfimg, FloatImg *dfimg, int index)
{
int xs, ys, loop9;
int xp, yp;
2022-11-02 22:11:54 +01:00
float rgb[3];
2022-11-01 09:35:40 +01:00
2023-01-31 00:14:56 +01:00
#if DEBUG_LEVEL
2022-11-01 09:35:40 +01:00
fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, sfimg, dfimg, index);
2023-01-31 00:14:56 +01:00
#endif
2022-11-01 09:35:40 +01:00
if ( (index<0) || (index>8)) {
fprintf(stderr, " %s: bad index %d\n", __func__, index);
#if MUST_ABORT
fflush(stderr); abort();
#endif
return -1;
}
fimg_clear(dfimg);
for (ys=1; ys<sfimg->height-1; ys++) {
for (xs=1; xs<sfimg->width-1; xs++) {
for (loop9=0; loop9<9; loop9++) {
xp = xs + deltas[loop9].x;
yp = ys + deltas[loop9].y;
fimg_get_rgb(sfimg, xp, yp, rgb);
2023-01-31 00:14:56 +01:00
pixels[loop9].fgris = rgb[0] + rgb[1] + rgb[2];
2022-11-01 09:35:40 +01:00
pixels[loop9].rang = loop9;
}
2022-11-02 22:11:54 +01:00
qsort(&pixels, 9, sizeof(fpixel), comparaison_fgris);
2022-11-01 09:35:40 +01:00
rgb[0] = rgb[1] = rgb[2] = pixels[index].fgris;
fimg_put_rgb(dfimg, xs, ys, rgb);
}
}
return 0;
}
/* --------------------------------------------------------------------- !*/