package { import flash.display.BitmapData; public class RGBHistogram { public function calculate(bmd:BitmapData, rgb:int):Object { var ret:Object = { eBmd: new BitmapData(bmd.width, bmd.height), hBmd: null }; // Calculate histogram and eImg var histogram:Array = new Array(256); for (var i = 0; i < 360; i++) { histogram[i] = 0; } for (i = 0; i < bmd.width; i++) { for (var j = 0; j < bmd.height; j++) { var c:uint = bmd.getPixel(i, j); c = (c >> 8*(2 - rgb)) & 0x0000FF; histogram[c]++; c = (c << 8*(2 - rgb)); ret.eBmd.setPixel(i, j, c); } } ret.hBmd = toBitmapData(histogram, bmd.width); return ret; } /** * Returns a bitmap data of wx256 visualizing the histogram. */ function toBitmapData(histogram:Array, w:int):BitmapData { // Find the max so that we can normalize to the width of w var max:int = histogram[0]; for (var j:int = 1; j < 256; j++) { if (max < histogram[j]) { max = histogram[j]; } } // Visualize var normalizer:Number = w/max; var ret:BitmapData = new BitmapData(w, 256); for (j = 0; j < 256; j++) { for (var i:int = 0; i < histogram[j]*normalizer; i++) { ret.setPixel(i, j, 0x777777); } } return ret; } } }