85 lines
1.7 KiB
C
85 lines
1.7 KiB
C
/*
|
|
* FLOATIMG
|
|
* calculer un histogramme et l'afficher
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#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;
|
|
|
|
#if DEBUG_LEVEL
|
|
fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, src, ghist, sz);
|
|
#endif
|
|
|
|
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;
|
|
}
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
#define NSLICES 1000
|
|
|
|
int fimg_essai_histo(FloatImg *src, char *outpic, int k)
|
|
{
|
|
long histo[NSLICES];
|
|
int foo;
|
|
FILE *pipe;
|
|
|
|
fprintf(stderr, ">>> %s ( %p '%s' %d )\n", __func__, src, outpic, k);
|
|
|
|
memset(histo, 0, NSLICES*sizeof(long));
|
|
|
|
foo = fimg_calcul_histo(src, histo, NSLICES);
|
|
|
|
// for (foo=0; foo<NSLICES; foo++) {
|
|
// printf("%7d %ld\n", foo, histo[foo]);
|
|
// }
|
|
|
|
pipe = popen("gnuplot", "w");
|
|
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");
|
|
|
|
for (foo=0; foo<NSLICES; foo++) {
|
|
fprintf(pipe, "%d %ld\n", foo, histo[foo]);
|
|
}
|
|
|
|
fclose(pipe);
|
|
|
|
|
|
return -1;
|
|
}
|
|
/* --------------------------------------------------------------------- */
|