モザイクがうまくいかない

by undo
♥0 | Line 164 | Modified 2009-10-26 20:46:04 | MIT License
play

ActionScript3 source code

/**
 * Copyright undo ( http://wonderfl.net/user/undo )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/77Pr
 */

<?xml version = "1.0" encoding = "utf-8"?>
<!-- まだ途中 色が255超えることがあるんだけどなんでだろう -->
<mx:Application xmlns:mx = "http://www.adobe.com/2006/mxml" layout = "absolute" applicationComplete = "init();">
	<mx:Script>
		<![CDATA[

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.net.FileFilter;
private var fr:FileReference = new FileReference();
private var ba:ByteArray = new ByteArray();
private var mosaic:ByteArray = new ByteArray();
private var rl:Loader = new Loader();
private var bmd:BitmapData;
private var bm:Bitmap = new Bitmap();
private var pbmd:BitmapData;

private function init():void
{
	//this.preview.addChild(this.pl);
	//this.image.addChild(this.rl);
	this.image.addChild(bm);
}

private function onBrowseBtnClick():void
{
	//画像ファイルを参照
	this.fr.addEventListener(Event.SELECT, onFileSelect);
	this.fr.browse([new FileFilter("Image", "*.jpg")]);
}

private function onFileSelect(evt:Event):void
{
	this.fr.removeEventListener(Event.SELECT, onFileSelect);
	this.fr.addEventListener(Event.COMPLETE, onFileComplete);
	this.fr.load();
}

private function onFileComplete(evt:Event):void
{
	//画像をByteArrayに格納、表示
	this.fr.removeEventListener(Event.COMPLETE, onFileComplete);
	this.ba = this.fr.data;
	this.rl.contentLoaderInfo.addEventListener(Event.COMPLETE, onPreviewLoadComp);
	this.rl.loadBytes(this.ba);
}

private function onPreviewLoadComp(evt:Event):void
{
	this.rl.contentLoaderInfo.removeEventListener(Event.COMPLETE, onPreviewLoadComp);
	this.bmd = new BitmapData(this.rl.contentLoaderInfo.width, this.rl.contentLoaderInfo.height);
	this.bmd.draw(rl);
	this.bm.bitmapData = this.bmd;
	this.bm.scaleX = this.bm.scaleY = Math.min(this.width / this.bm.width, this.height / this.bm.height);
	trace(this.bmd.width, this.bm.height);
}

private function onMosaicBtnClick():void
{
	if (this.ba.length > 0)
	{
		startMosaic();
	}
	else
	{
		//画像を参照してね
	}
}



//
//画像をMosaicする
//
private function startMosaic():void
{
	this.mosaic = null;
	this.mosaic = new ByteArray();
	this.pbmd = this.bmd.clone();
	this.bm.bitmapData = this.pbmd;

	var t:int = Math.floor(Number(this.time.text));
	var blockX:int = Math.floor(this.rl.contentLoaderInfo.width / t);
	var blockY:int = Math.floor(this.rl.contentLoaderInfo.height / t);
	var newColor:uint = 0;
	var newAlpha:uint = 0;
	var newRed:uint = 0;
	var newGreen:uint = 0;
	var newBlue:uint = 0;

	//まず端は無視してブロック処理
	for (var i:int = 0; i < blockX; i++)
	{
		for (var j:int = 0; j < blockY; j++)
		{

			for (var k:int = 0; k < t; k++)
			{
				for (var l:int = 0; l < t; l++)
				{
					newColor = this.pbmd.getPixel32(l + i * t, k + j * t);
					newAlpha += newColor >> 24 & 0xff;
					newRed += newColor >> 16 & 0xff;
					newGreen += newColor >> 8 & 0xff;
					newBlue += newColor & 0xff;
				}
			}
			newAlpha = uint(Math.floor(newAlpha / (t * t)));
			newRed = uint(Math.floor(newRed / (t * t)));
			newGreen = uint(Math.floor(newGreen / (t * t)));
			newBlue = uint(Math.floor(newBlue / (t * t)));

			//tが少ないと256になることがあるけどなんで!?
			if (newAlpha > 255)
			{
				newAlpha = 255;
			}
			if (newRed > 255)
			{
				newRed = 255;
			}
			if (newGreen > 255)
			{
				newGreen = 255;
			}
			if (newBlue > 255)
			{
				newBlue = 255;
			}

			newColor = (newAlpha << 24 | newRed << 16 | newGreen << 8 | newBlue);

			this.pbmd.fillRect(new Rectangle(i * t, j * t, t, t), newColor);

		}
	}

	//残った部分を処理
	var leftWidth:Number = this.rl.contentLoaderInfo.width - Math.floor(this.rl.contentLoaderInfo.width / t);
	var leftHeight:Number = this.rl.contentLoaderInfo.height - Math.floor(this.rl.contentLoaderInfo.height / t);
	for (var m:int = 0; m < blockX; m++)
	{
		for (var n:int = 0; n < t; n++)
		{
			for (var o:int = 0; o < leftHeight; o++)
			{
				
			}
		}
	}

	for (var p:int = 0; p < blockY; p++)
	{
		for (var q:int = 0; q < t; q++)
		{
			for (var r:int = 0; r < leftWidth; r++)
			{
				//ここでモザイク
			}
		}
	}

	for (var s:int = 0; s < leftWidth; s++)
	{
		for (var u:int = 0; u < leftHeight; u++)
		{
			//ここでモザイク
		}
	}


	//ByteArrayを画像としてLoaderにロード
	//this.rl.contentLoaderInfo.addEventListener(Event.COMPLETE, onMosaicLoadComp);
	//this.rl.loadBytes(this.mosaic);
}

private function onMosaicLoadComp(evt:Event):void
{
	this.rl.removeEventListener(Event.COMPLETE, onMosaicLoadComp);
	this.rl.scaleX = this.rl.scaleY = 1;
	this.rl.scaleX = this.rl.scaleY = Math.min(this.width / this.rl.width, this.height / this.rl.height);
}


private function onSaveBtnClick():void
{
	//画像を保存
	if (this.mosaic.length > 0)
	{
		this.fr.save(this.mosaic, "mosaic.jpg");
	}
	else
	{
		//glitchしてね。
	}
}
			
		]]>
	</mx:Script>

	<mx:Image id = "image" />

	<mx:HBox paddingTop = "10" paddingLeft = "10">
		<mx:Button id = "browseBtn" label = "参照" click = "onBrowseBtnClick();" />
		<mx:TextInput id = "time" text = "10" width = "50" />
		<mx:Label text = "←処理量" />
		<mx:Button id = "createBtn" label = "モザイク!" click = "onMosaicBtnClick();" />
		<mx:Button id = "saveBtn" label = "保存" click = "onSaveBtnClick();" />
	</mx:HBox>

</mx:Application>

Forked