title: CIAreaHistogram inputScale factor
body: <p>I'm building an application that uses the <code>CIAreaHistogram</code> Core Image filter. I use an <code>inputCount</code> value (number of buckets) of 10 for testing, and an <code>inputScale</code> value of 1.</p>
  
  <p>I get the <code>CIImage</code> for the histogram itself, which I then run through a custom kernel (see end of post) to set alpha values to 1 (since otherwise the alpha value from the histogram calculations is premultiplied) and then convert it to an <code>NSBitmapImageRep</code>.</p>
  
  <p>I then scan through the image rep's buffer and print the RGB values (skipping the alpha values). However, when I do this, the sum of the R, G, and B values across the 10 do not necessarily add up to 255.</p>
  
  <p>For example, with a fully black image, I apply the histogram, then the custom kernel, and get the following output:</p>
  
  <pre><code>RGB: 255 255 255
  RGB: 0 0 0
  RGB: 0 0 0
  RGB: 0 0 0
  RGB: 0 0 0
  RGB: 0 0 0
  RGB: 0 0 0
  RGB: 0 0 0
  RGB: 0 0 0
  RGB: 0 0 0
  </code></pre>
  
  <p>This is as I expect, since all pixels are black, so everything is in the first bucket. However, if I run the same algorithm with a color image, I get the following:</p>
  
  <pre><code>RGB: 98 76 81
  RGB: 164 97 87
  RGB: 136 161 69
  RGB: 100 156 135
  RGB: 80 85 185
  RGB: 43 34 45
  RGB: 31 19 8
  RGB: 19 7 3
  RGB: 12 5 2
  RGB: 16 11 11
  </code></pre>
  
  <p>Add up the values for R, G, and B - they don't add up to 255. This causes problems because I need to compare two of these histograms, and my algorithm expects the sums to be between 0 and 255. I could obviously scale these values, but I want to avoid that extra step for performance reasons.</p>
  
  <p>I noticed something else interesting that might give some clue as to why this is happening. In my custom kernel, I simply set the alpha value to 1. I tried a second kernel (see end of post) that sets all pixels to red. Clearly, green and blue values are zero. However, I get this result when checking the values from the bitmap rep:</p>
  
  <pre><code>RGB: 255 43 25
  </code></pre>
  
  <p>But I just set G and B to zero! This seems to be part of the problem, which indicates color management. But since I explicitly set the values in the kernel, there's only one block of code where this can be happening - the conversion to an NSBitmapImageRep from the CIImage from the filter:</p>
  
  <pre><code>NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCIImage:kernelOutput];
  unsigned char *buf = [bitmapRep bitmapData];
  </code></pre>
  
  <p>Once I set the pixels to RGB 255 0 0, then execute those lines, then read the buffer, the RGB values are all 255 43 25. I have further tried setting the color space of the original CGImageRef on which the entire workflow is based to <code>kCGColorSpaceGenericRGB</code>, thinking the color profile may be carrying through, but to no avail.</p>
  
  <p>Can anyone tell me why a <code>CIFilter</code> kernel would behave this way, and how I could solve it?</p>
  
  <p>As mentioned before, here are copies of the CIFilter kernel functions I use. First, the one that sets alpha to 1:</p>
  
  <pre><code>kernel vec4 adjustHistogram(sampler src)
  {
      vec4 pix = sample(src, destCoord());
      pix.a = 1.0;
      return pix;
  }
  </code></pre>
  
  <p>And next, the one that sets all pixels to RGB 255 0 0 but that ends up 255 43 25 once it converts to <code>NSBitmapImageRep</code>:</p>
  
  <pre><code>kernel vec4 adjustHistogram(sampler src)
  {
      vec4 pix = sample(src, destCoord());
      pix.r = 1.0; pix.g = 0.0; pix.b = 0.0;
      pix.a = 1.0;
      return pix;
  }
  </code></pre>
  
  <p>Thanks in advance for your help.</p>