ヒストグラム表示Flash
勉強のために、PhotoXP 1.xで使っているヒストグラム表示Java AppletをActionScript 3.0で書き直して見ました。FlashはJavaに比べて起動が速くていいですね。
(R, G, B, L部分をクリックするとグラフをOn/Offできます。)
Histogram.as
package {
import flash.display.*;
import flash.net.*;
import flash.text.*;
import flash.events.*;
import mx.utils.ObjectUtil;
public class Histogram extends Sprite {
public static const GRAPH_WIDTH:int = 256;
public static const GRAPH_HEIGHT:int = 125;
public static const LABEL_WIDTH:int = 13;
public static const LABEL_HEIGHT:int = 17;
public static const LABEL_FONT_SIZE:int = 12;
public static const RED:int = 0;
public static const GREEN:int = 1;
public static const BLUE:int = 2;
public static const GRAYSCALE:int = 3;
public static const HISTOGRAM_COLOR:Array = [0xff0000, 0x00ff00, 0x0000ff, 0xffffff];
public static const LABEL_TEXTS:Array = ["R", "G", "B", "L"];
private var loader:Loader;
private var bitmap:Bitmap;
private var shapes:Array = new Array(4);
private var labels:Array = new Array(4);
public function Histogram() {
var flashVars:Object = LoaderInfo(loaderInfo).parameters;
var imgsrc:String = flashVars["imgsrc"];
if (imgsrc == null) {
imgsrc = "image3.jpg";
}
this.opaqueBackground = 0x000000;
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderIOError);
loader.load(new URLRequest(imgsrc));
}
public function loaderIOError(event:IOErrorEvent):void {
trace("ERROR:"+event);
}
public function loaderComplete(event:Event):void {
var loader:Loader = Loader(event.target.loader);
var image:Bitmap = Bitmap(loader.content);
var bitmap_data:BitmapData = image.bitmapData;
var i:int, j:int;
var x:int, y:int;
var border:Shape = new Shape();
this.addChild(border);
var histograms:Array = new Array(256*4);
for (i = 0; i < histograms.length; i++) {
histograms[i] = 0.0;
}
for (y = 0; y < bitmap_data.height; y++) {
for (x = 0; x < bitmap_data.width; x++) {
var pixel:int, r:int, g:int, b:int;
pixel = bitmap_data.getPixel(x, y);
r = (pixel >> 16) & 0xff;
g = (pixel >> 8) & 0xff;
b = (pixel) & 0xff;
histograms[256*RED + r]++;
histograms[256*GREEN + g]++;
histograms[256*BLUE + b]++;
var grayscale:int = Math.round(r*0.299 + g*0.587 + b*0.114);
histograms[256*GRAYSCALE + grayscale]++;
}
}
var max:int = (bitmap_data.width*bitmap_data.height)/68;
var gr:Graphics = border.graphics;
gr.lineStyle(1, 0x808080, 1.0);
gr.drawRect(0, 0, GRAPH_WIDTH - 1, GRAPH_HEIGHT - 1);
var step:int = GRAPH_WIDTH/4;
for (i = 1; i < 4; i++) {
gr.moveTo(i*step, 0);
gr.lineTo(i*step, GRAPH_HEIGHT - 1);
}
for (i = 0; i < 4; i++) {
shapes[i] = new Shape();
shapes[i].blendMode = BlendMode.ADD;
this.addChild(shapes[i]);
gr = shapes[i].graphics;
if (i != GRAYSCALE) {
gr.beginFill(HISTOGRAM_COLOR[i], 0.8);
gr.moveTo(0, GRAPH_HEIGHT - 1);
} else {
gr.lineStyle(2, HISTOGRAM_COLOR[i], 0.8);
gr.moveTo(0, GRAPH_HEIGHT - 1 -(histograms[256*i+0]*GRAPH_HEIGHT)/max);
}
for (j = 0; j < 256; j++) {
gr.lineTo(j, GRAPH_HEIGHT - 1 -(histograms[256*i+j]*GRAPH_HEIGHT)/max);
}
if (i != GRAYSCALE) {
gr.lineTo(255, GRAPH_HEIGHT - 1);
gr.endFill();
}
}
for (i = 0; i < 4; i++) {
labels[i] = new ToggleLabel(LABEL_TEXTS[i], HISTOGRAM_COLOR[i], 0x000000);
labels[i].x = (GRAPH_WIDTH-(LABEL_WIDTH*3*1.75+LABEL_WIDTH))/2 + LABEL_WIDTH*1.75*i;
labels[i].y = GRAPH_HEIGHT + 1;
labels[i].toggle();
labels[i].addEventListener(MouseEvent.MOUSE_DOWN, labelClicked);
addChild(labels[i]);
}
}
public function labelClicked(e:Event):void {
for (var i:int = 0; i < 4; i++) {
if (e.target == labels[i].rect) {
labels[i].toggle();
shapes[i].alpha = labels[i].state ? 0.0 : 1.0;
}
}
}
}
}
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.text.*;
class ToggleLabel extends Sprite {
public var state:Boolean = true;
public var fgcolor:int, bgcolor:int;
public var text:TextField = new TextField();
public var rect:Sprite = new Sprite();
public function ToggleLabel(label:String, fgcolor:int, bgcolor:int) {
this.fgcolor = fgcolor;
this.bgcolor = bgcolor;
text = new TextField();
text.text = label;
text.autoSize =TextFieldAutoSize.LEFT;
text.selectable = false;
var tf:TextFormat = new TextFormat();
tf.font = "_sans";
tf.size = Histogram.LABEL_FONT_SIZE;
tf.color = fgcolor;
text.setTextFormat(tf);
addChild(text);
rect = new Sprite();
rect.graphics.beginFill(fgcolor, 0.8);
rect.graphics.drawRoundRect(0, 0, Histogram.LABEL_WIDTH, Histogram.LABEL_HEIGHT, 2);
addChild(rect);
}
public function toggle():void {
state = !state;
rect.alpha = state ? 0.0 : 1.0;
}
}
参考:
