FloatImg/funcs/histogram.c

97 lines
2.0 KiB
C
Raw Normal View History

2020-09-03 02:00:32 +02:00
/*
* FLOATIMG
* calculer un histogramme et l'afficher
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2021-05-20 09:31:28 +02:00
#include <stdint.h>
2020-09-03 02:00:32 +02:00
#include "../floatimg.h"
extern int verbosity;
/* --------------------------------------------------------------------- */
int fimg_calcul_histo(FloatImg *src, long *ghist, int sz)
{
float maxval;
int x, y, idx;
float rgb[3], moy;
2020-09-04 06:18:59 +02:00
#if DEBUG_LEVEL
2020-09-03 02:00:32 +02:00
fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, src, ghist, sz);
2020-09-04 06:18:59 +02:00
#endif
2020-09-03 02:00:32 +02:00
2021-03-17 18:32:51 +01:00
if (FIMG_TYPE_RGB != src->type) {
fprintf(stderr, "%s: bad type %d of image\n", __func__, src->type);
return -97;
}
2020-09-03 02:00:32 +02:00
maxval = fimg_get_maxvalue(src);
fprintf(stderr, "maximum is %f\n", maxval);
for (y=0; y<src->height; y++) {
for(x=0; x<src->width; x++) {
fimg_get_rgb(src, x, y, rgb);
moy = (rgb[0]+rgb[1]+rgb[2]) / 3.0;
/* ok, here the had math part ... */
idx = (int)( (moy*sz) / maxval);
/* sanity check */
if (idx<0 || idx>=sz) {
fprintf(stderr, "idx = %d, error\n", idx);
abort();
}
ghist[idx]++;
}
}
return -66;
}
/* --------------------------------------------------------------------- */
2020-09-07 13:13:03 +02:00
int fimg_essai_histo(FloatImg *src, char *outpic, int nbslices)
2020-09-03 02:00:32 +02:00
{
2020-09-07 13:13:03 +02:00
long *histo;
2020-09-03 02:00:32 +02:00
int foo;
FILE *pipe;
2020-09-07 13:13:03 +02:00
fprintf(stderr, ">>> %s ( %p '%s' %d )\n", __func__, src, outpic, nbslices);
2020-09-03 02:00:32 +02:00
2020-09-07 13:13:03 +02:00
if (NULL==(histo=calloc(nbslices, sizeof(long)))) {
fprintf(stderr, "OUT OF MEMORY\n");
abort();
}
2020-09-03 02:00:32 +02:00
2020-09-07 13:13:03 +02:00
foo = fimg_calcul_histo(src, histo, nbslices);
2020-09-03 02:00:32 +02:00
2020-09-04 06:18:59 +02:00
// for (foo=0; foo<NSLICES; foo++) {
// printf("%7d %ld\n", foo, histo[foo]);
// }
pipe = popen("gnuplot", "w");
2020-09-07 13:13:03 +02:00
if (NULL==pipe) {
fprintf(stderr, "%s: error running gnuplot\n", __func__);
return -17;
}
2020-09-04 06:18:59 +02:00
fprintf(pipe, "set term png size 1024,512\n");
fprintf(pipe, "set grid\n");
fprintf(pipe, "set output \"%s\"\n", outpic);
fprintf(pipe, "plot '/dev/stdin' with lines\n");
2020-09-07 13:13:03 +02:00
for (foo=0; foo<nbslices; foo++) {
2020-09-04 06:18:59 +02:00
fprintf(pipe, "%d %ld\n", foo, histo[foo]);
2020-09-03 02:00:32 +02:00
}
2020-09-07 13:13:03 +02:00
pclose(pipe); // and not fclose (see man page)
2020-09-03 02:00:32 +02:00
2020-09-07 13:13:03 +02:00
free(histo);
2020-09-03 02:00:32 +02:00
2020-09-07 13:13:03 +02:00
return 0;
2020-09-03 02:00:32 +02:00
}
/* --------------------------------------------------------------------- */