Featured Post

XML Gallery v1.1

as3.0. xml driven. size just12.2kb. support add site logo. support swf,jpg,gif,png files. support a large number of pictures. support multiple package of pictures. support html format for image information. can fullscreen;zoom in,out ; left,right rotator;resize;etc… $20 BUY NOW IN ACTIVEDEN.NET

Read More

FLASH image processing/channel

Posted by activetofocus | Posted in Flash Technology | Posted on 20-10-2009

0

Question:
  • the images R, G, B channel respectively, separated in RGB mode, grayscale mode that.
  • pictures in grayscale mode, said.

Source:

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”>
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
private var bm:Bitmap;
private var bmd:BitmapData;

private function image_complete(evt:Event):void
{
/* Create Bitmap from Image content. */
bm = img.content as Bitmap;

/* Create new BitmapData object. */
bmd = new BitmapData(img.contentWidth, img.contentHeight);

/* Draw Bitmap into BitmapData. */
bmd.draw(bm.bitmapData);

process(imgr,getRedChannelData(bmd));
process(imgg,getGreenChannelData(bmd));
process(imgb,getBlueChannelData(bmd));

process(gimgr,getRedChannelGrayScaleData(bmd));
process(gimgg,getGreenChannelGrayScaleData(bmd));
process(gimgb,getBlueChannelGrayScaleData(bmd));

process(gimg,getGrayscaleData(bmd));
}

//Bitmap data filled with Image Control
private function process(image:Image,bitmapData:BitmapData):void
{
image.graphics.beginBitmapFill(bitmapData);
image.graphics.drawRect(0,0,bitmapData.width,bitmapData.height);
image.graphics.endFill();
}

private function image_mouseMove(evt:MouseEvent):void {
/* Get the pixel currently under the mouse cursor. */
//var color:int = bmd.getPixel(evt.localX, evt.localY);

/* Convert the color value from a number to Hex string. */
//var colorStr:String = color.toString(16).toUpperCase();

/* Set the swatch Box instance's backgroundColor style to the color under the mouse cursor. */
//swatch.setStyle("backgroundColor", color);

/* Make sure colorStr is at least 6 characters. */
//colorStr = "000000" + colorStr;

/* Make sure colorStr is at MOST 6 characters. */
//lbl.text = "#" + colorStr.substr(colorStr.length - 6);
}

//Get the red channel value of each pixel
private function getRedChannelData(source:BitmapData):BitmapData
{
var data:BitmapData = new BitmapData(source.width,source.height);
for(var i:Number = 1;i<source.width;i++)
{
for(var j:Number =1 ;j<source.height;j++)
{
data.setPixel(i,j,getRed(source.getPixel(i,j)) << 16);
}
}
return data;
}

//Get the red channel gray-scale mode, the value of each pixel
//private function getRedChannelGrayScaleData(source:BitmapData):BitmapData
{
var data:BitmapData = new BitmapData(source.width,source.height);
for(var i:Number = 1;i<source.width;i++)
{
for(var j:Number =1 ;j<source.height;j++)
{
var red:uint = getRed(source.getPixel(i,j));
data.setPixel(i,j,mergeRGB(red,red,red));
}
}
return data;
}

private function getGreenChannelData(source:BitmapData):BitmapData
{
var data:BitmapData = new BitmapData(source.width,source.height);
for(var i:Number = 1;i<source.width;i++)
{
for(var j:Number =1 ;j<source.height;j++)
{
data.setPixel(i,j,getGreen(source.getPixel(i,j)) << 8);
}
}
return data;
}

private function getGreenChannelGrayScaleData(source:BitmapData):BitmapData
{
var data:BitmapData = new BitmapData(source.width,source.height);
for(var i:Number = 1;i<source.width;i++)
{
for(var j:Number =1 ;j<source.height;j++)
{
var green:uint = getGreen(source.getPixel(i,j));
data.setPixel(i,j,mergeRGB(green,green,green));
}
}
return data;
}

private function getBlueChannelData(source:BitmapData):BitmapData
{
var data:BitmapData = new BitmapData(source.width,source.height);
for(var i:Number = 1;i<source.width;i++)
{
for(var j:Number =1 ;j<source.height;j++)
{
data.setPixel(i,j,getBlue(source.getPixel(i,j)));
}
}
return data;
}

private function getBlueChannelGrayScaleData(source:BitmapData):BitmapData
{
var data:BitmapData = new BitmapData(source.width,source.height);
for(var i:Number = 1;i<source.width;i++)
{
for(var j:Number =1 ;j<source.height;j++)
{
var blue:uint = getBlue(source.getPixel(i,j));
data.setPixel(i,j,mergeRGB(blue,blue,blue));
}
}
return data;
}

//Grayscale mode to obtain the value of each pixel
private function getGrayscaleData(source:BitmapData):BitmapData
{
var data:BitmapData = new BitmapData(source.width,source.height);
for(var i:Number = 1;i<source.width;i++)
{
for(var j:Number =1 ;j<source.height;j++)
{
var pixel:uint = source.getPixel(i,j);
var r:uint = getRed(pixel);
var g:uint = getGreen(pixel);
var b:uint = getBlue(pixel);
var gray:uint = rgb2gray(r,g,b);
data.setPixel(i,j,mergeRGB(gray,gray,gray));
}
}
return data;
}

//Get the red channel value of the pixel
private function getRed(rgb:uint):uint
{
if(!isHex24(rgb))
{
throw new Error("rgb value is not correct!");
}
var mask:uint = 0xff0000;
var red:uint = (rgb & mask) >> 16;

return red;
}

//When the pixel color value that contains alpha channel value, get the red //channel value of the pixel
private function getRed32(argb:uint):uint
{
if(!isHex32(argb))
{
throw new Error("rgb value is not correct!");
}

var mask:uint = 0x00ff0000;
var red:uint = (argb & mask) >> 16;

return red;
}

private function getGreen(rgb:uint):uint
{
if(!isHex24(rgb))
{
throw new Error("rgb value is not correct!");
}

var mask:uint = 0x00ff00;
var green:uint = (rgb & mask) >> 8;

return green;
}

private function getGreen32(argb:uint):uint
{
if(!isHex32(argb))
{
throw new Error("rgb value is not correct!");
}

var mask:uint = 0x0000ff00;
var green:uint = (argb & mask) >> 8;

return green;
}

private function getBlue(rgb:uint):uint
{
if(!isHex24(rgb))
{
throw new Error("rgb value is not correct!");
}

var mask:uint = 0x0000ff;
var blue:uint = (rgb & mask);

return blue;
}

private function getBlue32(argb:uint):uint
{
if(!isHex32(argb))
{
throw new Error("rgb value is not correct!");
}

var mask:uint = 0x000000ff;
var blue:uint = (argb & mask);

return blue;
}

//To obtain the pixel alpha values
private function getAlpha(argb:uint):uint
{
if(!isHex32(argb))
{
throw new Error("rgb value is not correct!");
}

var mask:uint = 0xff000000;
var alpha:uint = (argb & mask) >>> 24;

return alpha;
}

private function mergeRGB(red:uint,green:uint,blue:uint):uint
{
return (red<<16)|(green<<8)|blue;
}

private function mergeARGB(alpha:uint,red:uint,green:uint,blue:uint):uint
{
return (alpha << 24)|(red<<16)|(green<<8)|blue;
}

//To determine whether the 32-bit hexadecimal number 16
private function isHex32(hex:uint):Boolean
{
return hex.toString(16).length == 8;
}
//To determine whether the 24-bit 16 hex numbers
private function isHex24(hex:uint):Boolean
{
return hex.toString(16).length == 6;
}

//Online excerpts algorithm is used to convert RGB images to grayscale //images, according to pixel conversion.
private function rgb2gray(R:Number,G:Number,B:Number):Number
{
var x:Number;
var y:Number;
var z:Number;
var gray:Number;

var r:Number = R/255.0;
var g:Number = G/255.0;
var b:Number = B/255.0;

r = Math.pow((r+0.055)/1.055,2.4);
g = Math.pow((g+0.055)/1.055,2.4);
b = Math.pow((b+0.055)/1.055,2.4);

y = r*0.222+g*0.717+b*0.061;
x = y*0.964;
z = y * 0.825;

gray = 3.134 * x -1.617*y -0.490*z;
gray = Math.pow(gray,1/2.4)*1.055-0.055;
gray = gray*255;

//gray = (1-(gray/255))*100;

return gray;

//return R*0.3+G*0.59+B*0.11;
}
]]>
</mx:Script>
<mx:Zoom id=”zoom” />
<mx:VBox id=”container” height=”100%” width=”100%”>
<mx:Label text=”origin”/>
<mx:Image id=”img” source=”images/test.gif” completeEffect=”{zoom}” complete=”image_complete(event);” mouseMove=”image_mouseMove(event)”/>

<mx:Label text=”channel”/>
<mx:HBox>
<mx:VBox>
<mx:Label text=”red”/>
<mx:Image id=”imgr” width=”{img.width}” height=”{img.height}” mouseMove=”image_mouseMove(event)”/>
</mx:VBox>

<mx:VBox>
<mx:Label text=”green”/>
<mx:Image id=”imgg” width=”{img.width}” height=”{img.height}” mouseMove=”image_mouseMove(event)”/>
</mx:VBox>

<mx:VBox>
<mx:Label text=”blue”/>
<mx:Image id=”imgb” width=”{img.width}” height=”{img.height}” mouseMove=”image_mouseMove(event)”/>
</mx:VBox>

</mx:HBox>

<mx:Label text=”grayscale channel”/>
<mx:HBox>
<mx:VBox>
<mx:Label text=”red”/>
<mx:Image id=”gimgr” width=”{img.width}” height=”{img.height}” mouseMove=”image_mouseMove(event)”/>
</mx:VBox>

<mx:VBox>
<mx:Label text=”green”/>
<mx:Image id=”gimgg” width=”{img.width}” height=”{img.height}” mouseMove=”image_mouseMove(event)”/>
</mx:VBox>

<mx:VBox>
<mx:Label text=”blue”/>
<mx:Image id=”gimgb” width=”{img.width}” height=”{img.height}” mouseMove=”image_mouseMove(event)”/>
</mx:VBox>
</mx:HBox>

<mx:Label text=”grayscale mode”/>
<mx:Image id=”gimg” width=”{img.width}” height=”{img.height}” mouseMove=”image_mouseMove(event)”/>

<mx:HBox>
<mx:Box id=”swatch” width=”{lbl.height}” height=”{lbl.height}”/>
<mx:Label id=”lbl” width=”100″/>
</mx:HBox>
</mx:VBox>
</mx:Application>

Write a comment

Powered by WP Hashcash