colors

extract colors from pictures
git clone git://git.2f30.org/colors.git
Log | Files | Refs | README | LICENSE

commit b945d8df203f585a48e1e83d51c98be53ea9f99d
parent e13e5e6a5b36694048d5ed3d29d9e3f32f069a49
Author: sin <sin@2f30.org>
Date:   Thu Jun 11 15:01:14 +0100

Add verbose mode

Diffstat:
colors.1 | 4+++-
colors.c | 34+++++++++++++++++++++++++++++++++-
2 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/colors.1 b/colors.1 @@ -6,7 +6,7 @@ .Nd extract colors from pictures .Sh SYNOPSIS .Nm colors -.Op Fl er +.Op Fl erv .Op Fl h | Fl p .Op Fl n Ar clusters .Ar file @@ -21,6 +21,8 @@ steps. Print empty clusters as well. .It Fl r Randomize cluster selection. +.It Fl v +Be verbose. .It Fl h Select initial clusters from the hue domain. .It Fl p diff --git a/colors.c b/colors.c @@ -34,11 +34,13 @@ struct cluster *clusters; size_t nclusters = 8; RB_HEAD(pointtree, point) pointhead; size_t npoints; +size_t niters; int eflag; int rflag; int hflag; int pflag; +int vflag; int distance(struct point *p1, struct point *p2) @@ -209,6 +211,7 @@ process(void) while (!done) { done = 1; + niters++; RB_FOREACH(p, pointtree, &pointhead) { for (i = 0; i < nclusters; i++) dists[i] = distance(p, &clusters[i].center); @@ -279,9 +282,33 @@ printclusters(void) } void +printstatistics(void) +{ + struct point *p; + size_t ntotalpoints = 0; + size_t navgcluster = 0; + int i; + + RB_FOREACH(p, pointtree, &pointhead) + ntotalpoints += p->freq; + + for (i = 0; i < nclusters; i++) + TAILQ_FOREACH(p, &clusters[i].pointhead, e) + navgcluster++; + navgcluster /= nclusters; + + fprintf(stderr, "Total number of points: %zu\n", ntotalpoints); + fprintf(stderr, "Number of unique points: %zu\n", npoints); + fprintf(stderr, "Number of clusters: %zu\n", nclusters); + fprintf(stderr, "Average number of unique points per cluster: %zu\n", + navgcluster); + fprintf(stderr, "Number of iterations to converge: %zu\n", niters); +} + +void usage(void) { - fprintf(stderr, "usage: %s [-er] [-h | -p] [-n clusters] file\n", argv0); + fprintf(stderr, "usage: %s [-erv] [-h | -p] [-n clusters] file\n", argv0); exit(1); } @@ -297,6 +324,9 @@ main(int argc, char *argv[]) case 'r': rflag = 1; break; + case 'v': + vflag = 1; + break; case 'h': hflag = 1; pflag = 0; @@ -341,5 +371,7 @@ main(int argc, char *argv[]) initclusters(clusters, nclusters); process(); printclusters(); + if (vflag) + printstatistics(); return 0; }