forked from: 画像をトゥーンレンダリングっぽくする
トゥーンレンダリング「っぽく」なのであんまり期待しないでください。
色の階調差が激しいので本来より極端に離れた色になっている場合があります。
ちょっと暗めの人物画像に適用するのがお勧めです。
(男性や日焼けした人など)
環境によっては処理に時間がかかる場合があります。
// forked from rsakane's 画像をトゥーンレンダリングっぽくする
/*
* トゥーンレンダリング「っぽく」なのであんまり期待しないでください。
* 色の階調差が激しいので本来より極端に離れた色になっている場合があります。
*
* ちょっと暗めの人物画像に適用するのがお勧めです。
* (男性や日焼けした人など)
*
* 環境によっては処理に時間がかかる場合があります。
*/
package
{
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.geom.*;
import flash.net.*;
import flash.system.*;
import flash.text.*;
import flash.utils.*;
import com.bit101.components.*;
public class Main extends Sprite
{
private const SIZE:Number = 400;
private var loaderA:Loader;
private var loaderB:Loader;
private var input:InputText;
private var bitmap:Bitmap;
public function Main()
{
var label:Label = new Label(this, 25, 409, "IMAGE URL");
input = new InputText(this, 0, 410, "http://farm1.static.flickr.com/118/303143923_b6d36c6237.jpg");
input.width = 300;
input.x = (465 - input.width) / 2;
var button:PushButton = new PushButton(this, 230, 435, "OK", inputHandler);
button.x = (465 - button.width) / 2;
inputHandler();
}
private function inputHandler(event:Event = null):void
{
loaderA = new Loader();
loaderA.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
loaderA.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function():void { } );
loaderA.load(new URLRequest(input.text), new LoaderContext(true));
}
private function initHandler(event:Event):void
{
loaderB = new Loader();
loaderB.contentLoaderInfo.addEventListener(Event.INIT, init);
loaderB.loadBytes(loaderA.contentLoaderInfo.bytes);
}
private function init(event:Event):void
{
var loader:Loader = event.currentTarget.loader;
var scale:Number = 0.0;
if (loader.width > loader.height) scale = SIZE / loader.width;
else scale = SIZE / loader.height;
var matrix:Matrix = new Matrix();
matrix.scale(scale, scale);
var bd:BitmapData = new BitmapData(loader.width * scale, loader.height * scale, false);
bd.draw(loader, matrix);
if (bitmap) removeChild(bitmap);
bitmap = new Bitmap(bd);
bitmap.x = (465 - bitmap.width) / 2;
addChild(bitmap);
var filter:ShaderFilter = new ShaderFilter(new MedianSimpleShader());
bd.applyFilter(bd, bd.rect, new Point(), new ShaderFilter(new NewFilterShader()));
bd.applyFilter(bd, bd.rect, new Point(), filter);
bd.applyFilter(bd, bd.rect, new Point(), filter);
bd.applyFilter(bd, bd.rect, new Point(), filter);
}
}
}
import flash.display.Shader;
import flash.utils.ByteArray;
class NewFilterShader extends Shader
{
private var vec:Vector.<int> = Vector.<int>( [ 117,81,49,75,195,64,24,125,119,151,128,74,192,155,92,36,112,224,38,14,18,212,77,27,16,28,219,77,112,146,210,220,80,208,164,94,210,138,91,254,134,91,139,250,63,250,91,28,221,164,187,248,221,93,146,150,66,195,193,187,247,120,239,251,94,146,47,6,224,115,31,125,253,122,55,126,170,180,153,71,249,240,89,151,147,225,72,227,161,152,26,213,239,232,60,154,233,60,43,140,215,239,253,125,190,55,211,166,28,23,57,24,25,50,93,142,204,120,82,89,254,102,93,155,194,130,113,32,122,28,76,171,219,162,48,25,62,16,148,102,132,5,15,24,14,179,178,194,57,199,47,32,17,11,172,56,142,17,115,172,4,97,2,168,30,245,60,21,168,65,39,102,117,109,111,23,164,49,194,196,233,192,37,252,99,121,143,148,171,150,175,243,233,142,124,186,149,79,119,228,213,142,188,218,202,171,205,124,76,121,225,81,218,85,113,128,218,86,78,2,164,55,239,7,127,196,21,125,26,8,66,146,156,222,59,58,59,33,93,114,226,164,203,192,231,82,138,130,57,92,90,159,186,254,41,95,188,175,110,124,181,245,185,57,204,225,178,203,89,127,179,79,10,191,79,182,186,219,23,218,94,180,143,112,61,103,233,230,132,246,189,200,239,246,133,190,191,247,185,249,178,233,37,173,175,205,5,77,239,174,87,227,23,28,223,206,199,252,127,254,7 ] );
private var byteArr:ByteArray;
public function NewFilterShader():void
{
var len:int = vec.length;
if(!byteArr)
{
byteArr = new ByteArray();
for(var i:int=0; i<len; i++)
{
byteArr.writeByte( vec[i] );
}
byteArr.inflate();
this.byteCode = byteArr;
}
}
}
class MedianSimpleShader extends Shader
{
private var vec:Vector.<int> = Vector.<int>( [ 237,153,205,106,219,80,16,133,143,108,217,150,127,106,89,178,228,77,49,104,221,69,41,109,233,162,155,94,232,186,116,209,7,40,33,86,192,16,219,193,118,67,151,243,40,9,109,247,125,131,210,71,18,122,129,106,116,71,106,116,145,54,33,132,36,40,155,177,125,190,163,57,247,78,22,131,253,203,2,240,115,130,79,241,106,125,178,253,178,222,92,156,199,87,147,237,201,38,62,92,156,156,198,136,55,235,243,248,229,122,123,182,195,213,228,50,222,174,118,123,224,202,185,140,247,135,245,110,11,43,251,116,21,31,78,247,235,139,35,191,215,143,121,31,233,7,69,111,190,191,137,54,249,71,209,217,250,252,24,239,113,109,117,128,201,215,207,223,142,31,119,187,253,10,63,96,31,246,167,184,238,216,22,220,213,225,136,215,64,244,135,192,117,198,117,217,193,223,236,37,172,188,254,198,171,46,146,78,246,62,251,60,237,226,121,206,103,71,248,207,119,133,239,10,111,35,233,50,223,69,106,11,255,225,230,243,109,225,109,225,123,72,108,230,109,164,61,225,139,60,220,103,217,19,190,39,124,31,73,143,249,30,210,190,145,39,231,251,194,247,133,31,32,233,51,223,71,58,48,242,228,252,64,248,129,240,14,146,1,243,3,164,142,145,135,125,75,71,120,71,248,33,18,135,121,7,233,208,200,147,243,67,225,135,194,143,144,12,153,31,34,29,25,121,114,126,36,252,72,248,49,146,17,243,35,164,227,27,207,95,142,65,29,93,85,87,215,200,230,58,1,245,116,85,125,93,163,1,215,103,32,71,87,53,212,53,26,113,157,194,25,103,245,197,20,106,202,239,45,34,100,57,222,102,175,45,206,147,245,203,36,230,180,62,101,149,240,14,250,47,243,105,189,214,71,204,151,254,138,207,5,229,122,141,207,229,62,133,62,229,243,86,124,202,109,200,233,114,159,66,119,141,156,174,232,181,62,98,190,244,87,124,51,144,219,144,147,255,129,150,133,238,26,57,103,80,179,134,156,51,238,83,232,51,35,231,76,244,90,31,49,95,250,141,57,180,243,123,8,243,107,231,112,183,115,104,239,179,254,62,159,250,189,60,150,243,221,119,206,91,246,211,251,130,146,125,65,201,190,160,100,95,80,178,47,40,217,23,148,236,11,74,246,5,37,251,66,86,61,217,23,60,40,175,33,135,167,57,173,123,198,185,61,209,107,125,196,124,233,175,248,124,144,215,48,31,159,251,20,186,103,204,199,135,242,27,114,250,220,167,208,125,35,167,47,122,173,143,152,47,253,21,223,28,228,55,228,156,115,159,66,247,141,156,115,168,121,67,206,57,247,41,244,185,145,115,46,122,173,143,152,47,253,198,28,218,249,61,132,249,181,115,184,219,57,180,247,89,127,159,79,253,94,30,203,249,238,59,231,45,251,233,125,129,100,95,32,217,23,72,246,5,146,125,129,100,95,32,217,23,72,246,5,146,125,33,171,129,236,11,1,84,208,144,35,224,28,133,30,24,231,14,68,175,245,229,207,47,253,21,95,8,10,26,230,19,114,159,66,15,140,249,132,80,97,67,206,144,251,20,122,104,228,12,69,175,245,17,243,165,191,226,91,128,194,134,156,11,238,83,232,161,145,115,1,181,104,200,185,224,62,133,190,48,114,46,68,175,245,17,243,165,223,152,67,59,191,135,48,191,118,14,119,59,135,246,62,235,239,243,169,223,203,99,57,223,125,231,188,101,63,254,157,130,127,183,88,90,250,119,138,172,146,254,94,139,247,181,188,230,243,248,7 ] );
private var byteArr:ByteArray;
public function MedianSimpleShader():void
{
var len:int = vec.length;
if(!byteArr)
{
byteArr = new ByteArray();
for(var i:int=0; i<len; i++)
{
byteArr.writeByte( vec[i] );
}
byteArr.inflate();
this.byteCode = byteArr;
}
}
}