# HG changeset patch # User zas_ # Date 1208785157 0 # Node ID 6a2934cd0883449cc5d36152fe3cb437179d0fc9 # Parent 65475285957d75c98f36c00c06d8666efa18c047 histogram_read(): speed up calculations by 20%. diff -r 65475285957d -r 6a2934cd0883 src/histogram.c --- a/src/histogram.c Mon Apr 21 11:44:03 2008 +0000 +++ b/src/histogram.c Mon Apr 21 13:39:17 2008 +0000 @@ -104,7 +104,7 @@ gulong histogram_read(Histogram *histogram, GdkPixbuf *imgpixbuf) { - gint w, h, i, j, srs, has_alpha; + gint w, h, i, j, srs, has_alpha, step; guchar *s_pix; if (!histogram) return 0; @@ -117,25 +117,40 @@ memset(histogram->histmap, 0, sizeof(histogram->histmap)); - for (i = 0; i < h; i++) + /* code duplication is here to speed up the calculation */ + step = 3 + !!(has_alpha); + if (histogram->histogram_chan == HCHAN_MAX) { - guchar *sp = s_pix + (i * srs); /* 8bit */ - for (j = 0; j < w; j++) + for (i = 0; i < h; i++) { - histogram->histmap[sp[0] + 0 * HISTOGRAM_SIZE]++; - histogram->histmap[sp[1] + 1 * HISTOGRAM_SIZE]++; - histogram->histmap[sp[2] + 2 * HISTOGRAM_SIZE]++; - if (histogram->histogram_chan == HCHAN_MAX) + guchar *sp = s_pix + (i * srs); /* 8bit */ + for (j = 0; j < w; j++) { guchar t = sp[0]; if (sp[1]>t) t = sp[1]; if (sp[2]>t) t = sp[2]; - histogram->histmap[t + 3 * HISTOGRAM_SIZE]++; + + histogram->histmap[sp[0] + 0 * HISTOGRAM_SIZE]++; + histogram->histmap[sp[1] + 1 * HISTOGRAM_SIZE]++; + histogram->histmap[sp[2] + 2 * HISTOGRAM_SIZE]++; + histogram->histmap[t + 3 * HISTOGRAM_SIZE]++; + sp += step; } - else - histogram->histmap[3 * HISTOGRAM_SIZE + (sp[0]+sp[1]+sp[2])/3]++; - sp += 3; - if (has_alpha) sp++; + } + } + else + { + for (i = 0; i < h; i++) + { + guchar *sp = s_pix + (i * srs); /* 8bit */ + for (j = 0; j < w; j++) + { + histogram->histmap[sp[0] + 0 * HISTOGRAM_SIZE]++; + histogram->histmap[sp[1] + 1 * HISTOGRAM_SIZE]++; + histogram->histmap[sp[2] + 2 * HISTOGRAM_SIZE]++; + histogram->histmap[3 * HISTOGRAM_SIZE + (sp[0]+sp[1]+sp[2])/3]++; + sp += step; + } } }