+#endif
+
+static int
+compare_fg_counts(const void *a, const void *b)
+{
+ const FG_NODE *p = (const FG_NODE *) a;
+ const FG_NODE *q = (const FG_NODE *) b;
+ return (q->count - p->count);
+}
+
+static void
+report_colors(PICS_HEAD * pics)
+{
+ int accum;
+ double level;
+ int j;
+ int shift;
+ int total;
+ char buffer[256];
+
+ if (logfp == 0)
+ return;
+
+ qsort(pics->fgcol, (size_t) pics->colors, sizeof(FG_NODE), compare_fg_counts);
+ /*
+ * For debugging, show a (short) list of the colors used.
+ */
+ if (debugging && (pics->colors < 1000)) {
+ int digits = 0;
+ int high;
+ int wide = 4;
+ for (j = pics->colors; j != 0; j /= 10) {
+ ++digits;
+ if (j < 10)
+ ++digits;
+ }
+ if (digits > 8)
+ digits = 8;
+ logmsg("These colors were used:");
+ high = (pics->colors + wide - 1) / wide;
+ for (j = 0; j < high && j < pics->colors; ++j) {
+ int k;
+ char *s = buffer;
+ *s = '\0';
+ for (k = 0; k < wide; ++k) {
+ int n = j + (k * high);
+ size_t want = (sizeof(buffer) - (size_t) (s - buffer));
+ if (want < 100)
+ break;
+ if (n >= pics->colors)
+ break;
+ if (k) {
+ *s++ = ' ';
+ if (digits < 8) {
+ _nc_SPRINTF(s, _nc_SLIMIT(want) "%*s", 8 - digits,
+ " ");
+ s += strlen(s);
+ }
+ }
+ if (pics->fgcol[n].fgcol >= 0) {
+ _nc_SPRINTF(s, _nc_SLIMIT(want) "%3d #%06X %*d", n,
+ pics->fgcol[n].fgcol,
+ digits, pics->fgcol[n].count);
+ } else {
+ _nc_SPRINTF(s, _nc_SLIMIT(want) "%3d (empty) %*d", n,
+ digits, pics->fgcol[n].count);
+ }
+ s += strlen(s);
+ if ((s - buffer) > 100)
+ break;
+ }
+ logmsg("%s", buffer);
+ }
+ }
+
+ /*
+ * Given the list of colors sorted by the number of times they are used,
+ * log a short report showing the number of colors for 90%, 99%, 99.9%,
+ * etc.
+ */
+ logmsg("Number of colors versus number of cells");
+ total = pics->high * pics->wide;
+ accum = 0;
+ level = 0.1;
+ shift = 1;
+ for (j = 0; j < pics->colors; ++j) {
+ accum += pics->fgcol[j].count;
+ if (accum >= (total * (1.0 - level))) {
+ int after = (shift > 2) ? shift - 2 : 0;
+ logmsg("%8d colors (%.1f%%) in %d cells (%.*f%%)",
+ j + 1,
+ (100.0 * (j + 1)) / pics->colors,
+ accum,
+ after, (100.0 * accum) / total);
+ if (accum >= total)
+ break;
+ level /= 10.0;
+ shift++;
+ }
+ }
+}