画像の誤差拡散2値化(カラー)
画像を誤差拡散法で2値化します
/**
* Copyright TheCoolMuseum ( http://wonderfl.net/user/TheCoolMuseum )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/yEgx
*/
// forked from TheCoolMuseum's 画像の誤差拡散2値化
// forked from TheCoolMuseum's 画像のWAVファイル化 誤差拡散2値化付
// forked from TheCoolMuseum's forked from: 画像のWAVファイル化
// forked from TheCoolMuseum's 画像のWAVファイル化
// forked from TheCoolMuseum's ローカルファイルの読み込み・保存
// 画像を誤差拡散法で2値化します
package {
import flash.utils.IDataInput;
import flash.display.InterpolationMethod;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.ByteArray;
import flash.utils.Endian;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.net.FileReference;
import flash.net.FileFilter;
public class FlashTest extends Sprite {
private var fileLoader:FileReference;
private var fileSaver:FileReference;
private var imageLoader:Loader;
private var image:Bitmap = new Bitmap();
private var uIx:Array = [0,2,1,3,0,2,1,3,0,2,1,3,0,2,1,3];
private var vIx:Array = [0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3];
public function makeBtn(name:String):TextField{
var btn:TextField = new TextField();
btn.text = name;
btn.autoSize = TextFieldAutoSize.LEFT;
btn.background = true;
btn.border = true;
btn.selectable = false;
return btn;
}
public function FlashTest() {
image.y=20;
addChild(image);
var loadBtn:TextField = makeBtn("LOAD");
addChild(loadBtn);
loadBtn.addEventListener(MouseEvent.CLICK, onClickLoadBtn);
}
private function onClickLoadBtn(e:Event):void{
load();
}
private function load():void{
var fileFilter:FileFilter = new FileFilter("Images (.png .jpg .gif)", "*.png;*.jpg;*.gif");
fileLoader = new FileReference();
fileLoader.addEventListener(Event.SELECT, onSelectLoadFile);
fileLoader.addEventListener(Event.COMPLETE, onCompleteLoadFile);
fileLoader.browse([fileFilter]);
}
private function onSelectLoadFile(e:Event):void{
fileLoader.load();
}
private function onCompleteLoadFile(e:Event):void{
imageLoader = new Loader();
imageLoader.loadBytes(fileLoader.data);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteLoadImage);
}
private function onCompleteLoadImage(e:Event):void{
var width:int = imageLoader.width;
var height:int = imageLoader.height;
var bitmapData:BitmapData = new BitmapData(width, height);
bitmapData.draw(imageLoader);
//addChild(new Bitmap(bitmapData));
image.bitmapData = errorDiffusion(bitmapData);
}
private function errorDiffusion(img:BitmapData):BitmapData{
var width:int = img.width;
var height:int = img.height;
var array:ByteArray = img.getPixels(img.rect);
var l:int=array.length;
for(var y:int=0; y<height; y++){
var pos:int = y*width*4;
for(var x:int=0; x<width; x++){
var c:int = array[pos+1];
var r:int = c>127 ? 255 : 0;
var re:int = c-r;
var r1:int = re*5/16;
var r2:int = re*3/16;
c = array[pos+2];
var g:int = c>127 ? 255 : 0;
var ge:int = c-g;
var g1:int = ge*5/16;
var g2:int = ge*3/16;
c = array[pos+3];
var b:int = c>127 ? 255 : 0;
var be:int = c-b;
var b1:int = be*5/16;
var b2:int = be*3/16;
array[pos] = 0xff;
array[pos+1] = r;
array[pos+2] = g;
array[pos+3] = b;
if(y<height-1){
array[pos+width*4+1] = clip8bit(array[pos+width*4+1]+r1);
array[pos+width*4+2] = clip8bit(array[pos+width*4+2]+g1);
array[pos+width*4+3] = clip8bit(array[pos+width*4+3]+b1);
if(x<width-1){
array[pos+5] = clip8bit(array[pos+5] + r1);
array[pos+6] = clip8bit(array[pos+6] + g1);
array[pos+7] = clip8bit(array[pos+7] + b1);
array[pos+width*4+5] = clip8bit(array[pos+width*4+5] + r2);
array[pos+width*4+6] = clip8bit(array[pos+width*4+6] + g2);
array[pos+width*4+7] = clip8bit(array[pos+width*4+7] + b2);
}
if(x>0){
array[pos+width*4-3] = clip8bit(array[pos+width*4-3]+r2);
array[pos+width*4-2] = clip8bit(array[pos+width*4-2]+g2);
array[pos+width*4-1] = clip8bit(array[pos+width*4-1]+b2);
}
}else{
if(x<width-1){
array[pos+5] = clip8bit(array[pos+5] + r1);
array[pos+6] = clip8bit(array[pos+6] + g1);
array[pos+7] = clip8bit(array[pos+7] + b1);
}
}
pos+=4;
}
}
array.position=0;
img.setPixels(img.rect, array);
return img;
}
private function clip8bit(n:int):int{
return Math.min(255,Math.max(0,n));
}
}
}