colors

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

commit 6b73dd60c1bf9ce6dc80a3a423d24a74de4c922e
parent 7a2875c1d2797a593ca8749b4bd87835b63c6383
Author: lostd <lostd@2f30.org>
Date:   Thu Jun  4 23:49:38 +0100

Make the default cluster selection fixed

Diffstat:
README | 2--
colors.1 | 3+++
colors.c | 27+++++++++++++++++++++++----
3 files changed, 26 insertions(+), 6 deletions(-)
diff --git a/README b/README @@ -7,8 +7,6 @@ file it outputs a colormap[1] to stdout. # ./colors input.png | ./scripts/tohtml.sh > output.html -Running the tool multiple times will produce slightly different -colormaps. [0] http://saturn.2f30.org/colors/input.png [1] http://saturn.2f30.org/colors/output.html diff --git a/colors.1 b/colors.1 @@ -7,6 +7,7 @@ .Sh SYNOPSIS .Nm colors .Op Fl e +.Op Fl r .Op Fl n Ar clusters .Ar file .Sh DESCRIPTION @@ -17,6 +18,8 @@ from pictures. .Bl -tag -width Ds .It Fl e Print empty clusters as well. +.It Fl r +Select initial clusters randomly. .It Fl n Ar clusters .El Set the number of clusters. It defaults to 8. diff --git a/colors.c b/colors.c @@ -29,6 +29,7 @@ size_t nclusters = 8; TAILQ_HEAD(points, point) points; size_t npoints; int eflag; +int rflag; int distance(struct point *p1, struct point *p2) @@ -71,7 +72,17 @@ adjclusters(struct cluster *c, size_t n) } void -initcluster(struct cluster *c) +initcluster_fixed(struct cluster *c, int i) +{ + TAILQ_INIT(&c->members); + c->nmembers = 0; + c->center.x = i * (256 / nclusters); + c->center.y = i * (256 / nclusters); + c->center.z = i * (256 / nclusters); +} + +void +initcluster_rand(struct cluster *c, int unused) { struct point *p; int i, sel; @@ -86,6 +97,8 @@ initcluster(struct cluster *c) c->center = *p; } +void (*initcluster)(struct cluster *c, int i) = initcluster_fixed; + void initclusters(struct cluster *c, size_t n) { @@ -95,7 +108,7 @@ initclusters(struct cluster *c, size_t n) if (!clusters) err(1, "malloc"); for (i = 0; i < n; i++) - initcluster(&clusters[i]); + initcluster(&clusters[i], i); } void @@ -195,7 +208,7 @@ printclusters(void) void usage(void) { - fprintf(stderr, "usage: %s [-e] [-n clusters] file\n", argv0); + fprintf(stderr, "usage: %s [-er] [-n clusters] file\n", argv0); exit(1); } @@ -208,6 +221,9 @@ main(int argc, char *argv[]) case 'e': eflag = 1; break; + case 'r': + rflag = 1; + break; case 'n': errno = 0; nclusters = strtol(EARGF(usage()), &e, 10); @@ -221,7 +237,10 @@ main(int argc, char *argv[]) if (argc != 1) usage(); - srand(time(NULL)); + if (rflag) { + srand(time(NULL)); + initcluster = initcluster_rand; + } TAILQ_INIT(&points); parseimg(argv[0], fillpoints); initclusters(clusters, nclusters);