FloatImg4PythonBinding/Fonderie/metriques.c

115 lines
2.4 KiB
C
Raw Normal View History

2020-11-10 12:07:30 +11:00
/*
* metriques.c
*/
#include <stdio.h>
#include <stdlib.h>
2020-11-10 13:58:18 +11:00
#include "../floatimg.h"
#include "metriques.h"
2020-11-10 13:58:18 +11:00
2020-11-10 12:07:30 +11:00
extern int verbosity;
2021-01-04 01:21:38 +11:00
/* -------------------------------------------------------------- */
int stat_zone(FloatImg *pimg, int geom[4], float v3[3])
{
int x, y, xe, ye;
int off;
double accus[3], divisor;
xe = geom[0] + geom[2];
ye = geom[1] + geom[3];
#if DEBUG_LEVEL
fprintf(stdout, "geom: %5d %5d %5d %5d\n",
geom[0], geom[1], geom[2], geom[3]);
#endif
accus[0] = accus[1] = accus[2] = 0.0;
for (y=geom[1]; y<ye; y++) {
for (x=geom[0]; x<xe; x++) {
off = (y*pimg->width) + x;
accus[0] += (double) pimg->R[off];
accus[1] += (double) pimg->G[off];
accus[2] += (double) pimg->B[off];
}
}
divisor = (double)(geom[2] * geom[3]); /* array of zone */
v3[0] = (float)(accus[0] / divisor);
v3[1] = (float)(accus[1] / divisor);
v3[2] = (float)(accus[2] / divisor);
return 0;
}
2020-11-10 12:07:30 +11:00
/* -------------------------------------------------------------- */
/*
* premier essai...
*/
2020-12-07 20:30:18 +11:00
int get_float_metric_a(FloatImg *pimg, float *where)
2020-11-10 12:07:30 +11:00
{
2020-12-29 10:54:15 +11:00
float means[4]; /* four values : R G B A */
int foo;
2020-11-10 12:07:30 +11:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %f %d )\n", __func__, pimg, where, whot);
#endif
foo = fimg_meanvalues(pimg, means);
if (foo) {
fprintf(stderr, "fatal error in %s\n", __func__);
exit(1);
}
*where = means[0] + means[1] + means[2];
return 0;
}
/* -------------------------------------------------------------- */
2020-12-08 08:02:08 +11:00
int get_float_metric_b(FloatImg *pimg, float *where)
{
2020-12-09 01:51:07 +11:00
int idx, size, nbre;
2020-12-08 08:02:08 +11:00
double adder;
adder = 0.0;
2020-12-09 01:51:07 +11:00
nbre = 0;
2020-12-08 08:02:08 +11:00
size = pimg->width * pimg->height;
for (idx=20; idx < size; idx+=42) {
adder += (double)pimg->R[idx];
2020-12-09 01:51:07 +11:00
nbre++;
2020-12-08 08:02:08 +11:00
}
2020-12-09 01:51:07 +11:00
*where = (float)(adder/(nbre+1));
2020-12-08 08:02:08 +11:00
return 0;
}
/* -------------------------------------------------------------- */
int get_float_metric_from_file(char *fname, float *where, int mode)
{
FloatImg image;
int foo;
float fval;
foo = fimg_create_from_dump(fname, &image);
if (foo) {
fprintf(stderr, "%s: err %d loading %s\n", __func__, foo, fname);
return foo;
}
fval = -1.0; /* sensible default value */
2020-12-11 05:19:35 +11:00
switch (mode) {
2020-12-11 07:32:53 +11:00
case 0: case 1:
2020-12-11 05:19:35 +11:00
foo = get_float_metric_a(&image, &fval);
break;
case 2:
foo = get_float_metric_b(&image, &fval);
break;
default:
fprintf(stderr, "%s: method %d invalid\n",
__func__, mode);
exit(1);
break; /* not reached */
}
*where = fval;
fimg_destroy(&image);
return 0;
2020-11-10 12:07:30 +11:00
}
/* -------------------------------------------------------------- */