2020-11-10 02:07:30 +01:00
|
|
|
/*
|
|
|
|
* metriques.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2020-11-12 23:36:06 +01:00
|
|
|
#include <stdlib.h>
|
2021-05-29 09:05:02 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2020-11-10 03:58:18 +01:00
|
|
|
#include "../floatimg.h"
|
2020-11-12 23:36:06 +01:00
|
|
|
#include "metriques.h"
|
2020-11-10 03:58:18 +01:00
|
|
|
|
2020-11-10 02:07:30 +01:00
|
|
|
extern int verbosity;
|
|
|
|
|
2021-01-03 15:21:38 +01:00
|
|
|
/* -------------------------------------------------------------- */
|
2021-05-10 00:05:52 +02:00
|
|
|
/* usage --> sfx.c:trinitron */
|
2021-01-03 15:21:38 +01:00
|
|
|
int stat_zone(FloatImg *pimg, int geom[4], float v3[3])
|
|
|
|
{
|
|
|
|
int x, y, xe, ye;
|
|
|
|
int off;
|
|
|
|
double accus[3], divisor;
|
|
|
|
|
|
|
|
#if DEBUG_LEVEL
|
|
|
|
fprintf(stdout, "geom: %5d %5d %5d %5d\n",
|
|
|
|
geom[0], geom[1], geom[2], geom[3]);
|
|
|
|
#endif
|
|
|
|
|
2021-02-24 16:50:14 +01:00
|
|
|
xe = geom[0] + geom[2];
|
|
|
|
ye = geom[1] + geom[3];
|
|
|
|
|
2021-01-03 15:21:38 +01:00
|
|
|
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 02:07:30 +01:00
|
|
|
/* -------------------------------------------------------------- */
|
2020-11-12 23:36:06 +01:00
|
|
|
/*
|
2021-05-10 00:05:52 +02:00
|
|
|
* premier essai : moyenne de toutes les composantes
|
|
|
|
* de tous les pixels.
|
|
|
|
*
|
|
|
|
* Question: pourquoi pas le retour en double precision ?
|
2020-11-12 23:36:06 +01:00
|
|
|
*/
|
2021-05-10 00:05:52 +02:00
|
|
|
int get_float_metric_avg(FloatImg *pimg, float *where)
|
2020-11-10 02:07:30 +01:00
|
|
|
{
|
2020-12-29 00:54:15 +01:00
|
|
|
float means[4]; /* four values : R G B A */
|
2020-11-12 23:36:06 +01:00
|
|
|
int foo;
|
2020-11-10 02:07:30 +01:00
|
|
|
|
|
|
|
#if DEBUG_LEVEL
|
2021-03-17 11:34:11 +01:00
|
|
|
fprintf(stderr, ">>> %s ( %p %p )\n", __func__, pimg, where);
|
2020-11-10 02:07:30 +01:00
|
|
|
#endif
|
|
|
|
|
2020-11-12 23:36:06 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
/* -------------------------------------------------------------- */
|
2021-05-10 00:05:52 +02:00
|
|
|
/* echantillonage des pixels rouges */
|
|
|
|
int get_float_metric_iRed(FloatImg *pimg, float *where)
|
2020-12-07 22:02:08 +01:00
|
|
|
{
|
2020-12-08 15:51:07 +01:00
|
|
|
int idx, size, nbre;
|
2020-12-07 22:02:08 +01:00
|
|
|
double adder;
|
|
|
|
|
|
|
|
adder = 0.0;
|
2020-12-08 15:51:07 +01:00
|
|
|
nbre = 0;
|
2020-12-07 22:02:08 +01:00
|
|
|
size = pimg->width * pimg->height;
|
|
|
|
for (idx=20; idx < size; idx+=42) {
|
|
|
|
adder += (double)pimg->R[idx];
|
2020-12-08 15:51:07 +01:00
|
|
|
nbre++;
|
2020-12-07 22:02:08 +01:00
|
|
|
}
|
2020-12-08 15:51:07 +01:00
|
|
|
*where = (float)(adder/(nbre+1));
|
2020-12-07 22:02:08 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* -------------------------------------------------------------- */
|
2022-02-04 23:39:52 +01:00
|
|
|
/*
|
|
|
|
* LR mean left/right
|
|
|
|
*/
|
2021-05-10 00:05:52 +02:00
|
|
|
int get_float_metric_LR(FloatImg *pimg, float *where)
|
|
|
|
{
|
|
|
|
int coords[4], foo;
|
|
|
|
float valL[3], valR[3];
|
|
|
|
|
|
|
|
coords[0] = 0; // X
|
|
|
|
coords[1] = 0; // Y
|
|
|
|
coords[2] = pimg->width / 2; // W
|
|
|
|
coords[3] = pimg->height; // H
|
|
|
|
|
|
|
|
foo = stat_zone(pimg, coords, valL);
|
2022-02-04 23:39:52 +01:00
|
|
|
if (foo) {
|
|
|
|
fprintf(stderr, "err %d stat zone in %s\n", foo, __func__);
|
|
|
|
return foo;
|
|
|
|
}
|
2021-05-10 00:05:52 +02:00
|
|
|
coords[1] = pimg->width / 2;
|
|
|
|
foo = stat_zone(pimg, coords, valR);
|
2022-02-04 23:39:52 +01:00
|
|
|
if (foo) {
|
|
|
|
fprintf(stderr, "err %d stat zone in %s\n", foo, __func__);
|
|
|
|
return foo;
|
|
|
|
}
|
2021-05-10 00:05:52 +02:00
|
|
|
|
|
|
|
*where = valL[1] - valR[1];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* -------------------------------------------------------------- */
|
|
|
|
/*
|
|
|
|
* et voici le grand dispactheur
|
|
|
|
*/
|
2020-12-07 22:02:08 +01:00
|
|
|
int get_float_metric_from_file(char *fname, float *where, int mode)
|
2020-11-12 23:36:06 +01:00
|
|
|
{
|
|
|
|
FloatImg image;
|
|
|
|
int foo;
|
|
|
|
float fval;
|
|
|
|
|
2021-04-24 00:16:23 +02:00
|
|
|
#if DEBUG_LEVEL
|
|
|
|
fprintf(stderr, ">>> %s ( '%s' %p %d )\n", __func__, fname, where, mode);
|
|
|
|
#endif
|
|
|
|
|
2020-11-12 23:36:06 +01:00
|
|
|
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-10 19:19:35 +01:00
|
|
|
switch (mode) {
|
2020-12-10 21:32:53 +01:00
|
|
|
case 0: case 1:
|
2021-05-10 00:05:52 +02:00
|
|
|
foo = get_float_metric_avg(&image, &fval);
|
2020-12-10 19:19:35 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2021-05-10 00:05:52 +02:00
|
|
|
foo = get_float_metric_iRed(&image, &fval);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
foo = get_float_metric_LR(&image, &fval);
|
2020-12-10 19:19:35 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: method %d invalid\n",
|
|
|
|
__func__, mode);
|
|
|
|
exit(1);
|
|
|
|
break; /* not reached */
|
|
|
|
}
|
|
|
|
|
2020-11-12 23:36:06 +01:00
|
|
|
*where = fval;
|
|
|
|
|
|
|
|
fimg_destroy(&image);
|
|
|
|
|
|
|
|
return 0;
|
2020-11-10 02:07:30 +01:00
|
|
|
}
|
|
|
|
/* -------------------------------------------------------------- */
|