#187 Code Reduction in bwhisteq-2

Untested
closed-accepted
Sean Morrison
None
5
2015-01-26
2013-06-07
zero_level
No

This patch deals in code reduction. It reduces redundancy in the file bwhisteq.c in src/util. The following is description of removal of redundancy

while ((n = fread(buf, sizeof(*buf), 512, fp)) > 0) {
bp = &buf[0];
for (i = 0; i < n; i++)
    bin[ *bp++ ]++;
}

havg = 0.0;
for (i = 0; i < BINSIZE; i++)
havg += bin[ i ];
havg /= (double)BINSIZE;

is replaced with

havg = 0.0;
while ((n = fread(buf, sizeof(*buf), 512, fp)) > 0) {
bp = &buf[0];
for (i = 0; i < n; i++) 
    bin[ *bp++ ]++;
    havg += n;
}
havg /= (double)BINSIZE;

If you have trouble understanding this please contact
zero_level at #brlcad

1 Attachments

Discussion

  • Erik Greenwald
    Erik Greenwald
    2013-06-18

    This patch is not correct. Adding n to havg is not summing the bin values, n holds the length of the buffer just read.

     
  • zero_level
    zero_level
    2013-06-18

    havg after this statement
    havg /= (double)BINSIZE;
    contains the ratio of total pixels(sum of bin values) and the number of bins.

    Since these are bw files.
    thus each byte corresponds to a pixel.
    Hence

     
    Last edit: zero_level 2013-06-18
  • zero_level
    zero_level
    2013-06-29

    Discussion on this patch beween me(zero_level) and sean(brlcad) happened on IRC http://ibot.rikers.org/%23brlcad/20130618.html.gz.
    We have tested this patch on artificially created image and natural image and compared the result with matlab.

    The result were similar images. We also quantized the difference by using the pixdiff and matlab substraction. It turns out that differences of 1 were seen in some pixels due to floating point error.

    I believe this patch is ready to be applied.

     
    Last edit: zero_level 2013-06-30
  • Sean Morrison
    Sean Morrison
    2013-07-13

    Patch applied, thank you! It was a surprisingly complicated patch to evaluate, but it looks like it checks out. Comparing the output to Matlab's function was an awesome thing to do.

    The patch file itself was almost perfect, just be careful to not introduce spaces at the ends of lines (vim is bad about that).

     
  • Sean Morrison
    Sean Morrison
    2013-07-13

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,4 +1,3 @@
    -
     This patch deals in code reduction. It reduces redundancy in the file bwhisteq.c in src/util. The following is description of removal of redundancy
    
         while ((n = fread(buf, sizeof(*buf), 512, fp)) > 0) {
    
    • status: open --> closed-accepted