Leave That Thing Alone Blog

ColdFusion Histogram and Color Histogram CFC

This ColdFusion component creates black/white and color image histograms. The component will return histogram array(s) or will write PNG images of the histogram. View the project page for more details and to download the CFC and examples.

Example ColdFusion color histogram

Unlike my last example of how to create a histogram, this CFC uses standard Java API currently available to ColdFusion and has no need for external libraries.

To give a brief outline of how this component works for a 3 color histogram

  • JPG image is buffered (jpegCodec - decodeAsBufferedImage)
  • A holder array[3][256] is created to hold R-G-B values
  • Each pixel of image data is looped over (bufferedJPG.getRaster().getDataBuffer().getData())
  • The hex value of each (R, G, and B) pixel value is separated. example for red: BitSHRN(bitAnd(p, InputBaseN("ff0000",16)),16)
  • As each pixel is looped over the separated RBG values are incremented in the appropriate array cell
  • Once the histogram array has been populated an PNG histogram graph image is drawn (using java.awt.Graphics2D)
  • For each of the three colors in the array a line is drawn from 0-255 and the height is determined by the value of that array cell

A color histogram is a representation of an image derived by counting the 'color' of each pixel. So while this CFC makes neat looking graphs it also creates an array of the colors so you can determine the frequency of colors in an image.

This CFC is very beta, please read the CFHistogram project page for more details. If you have comments, suggestions, or better ways to do this please let me know.

Comments

Kevin Hoyt's Gravatar Very nice! I was looking for just such a beast a few weeks ago.
# Posted By Kevin Hoyt | 10/20/06 8:01 AM
Rob Cameron's Gravatar Dude I love your method calls. No factories or any of that crap, just straight up getMyDamnHistogram(). You are awesome.

Cool plugin, too. :) How did you figure out the Java stuff? Are you a Java dev at heart and CF is your guilty pleasure?
# Posted By Rob Cameron | 10/20/06 9:13 AM