概要
範囲を指定して画像にモザイクをかけるコードを書いてみましょう。
モザイク
内容はほとんどリンク先記事と同じなので、先に上の記事を参照してください。
内容はほとんどリンク先記事と同じなので、先に上の記事を参照してください。
プログラム
private function pixelization(bd:BitmapData, size:int = 10, rect:Rectangle = null):BitmapData
{
var dest:BitmapData = bd.clone();
var color:ColorRGB = new ColorRGB();
if (rect == null) rect = new Rectangle();
if (rect.width == 0) rect.width = bd.width - rect.x;
if (rect.height == 0) rect.height = bd.height - rect.y;
for (var y:int = rect.y; y < rect.y + rect.height; y += size)
{
for (var x:int = rect.x; x < rect.x + rect.width; x += size)
{
var count:int = 0;
var r:int = 0;
var g:int = 0;
var b:int = 0;
for (var yy:int = 0; yy < size; yy++)
{
for (var xx:int = 0; xx < size; xx++)
{
if (x + xx < 0 || bd.width <= x + xx ||
y + yy < 0 || bd.height <= y + yy) continue;
count++;
color.value = bd.getPixel(x + xx, y + yy);
r += color.r;
g += color.g;
b += color.b;
}
}
color.r = r / count;
color.g = g / count;
color.b = b / count;
for (yy = 0; yy < size; yy++)
{
for (xx = 0; xx < size; xx++)
{
if (x + xx < 0 || bd.width <= x + xx ||
y + yy < 0 || bd.height <= y + yy) continue;
dest.setPixel(x + xx, y + yy, color.value);
}
}
}
}
return dest;
}
全体のコードはこのようになります。
private function pixelization(bd:BitmapData, size:int = 10, rect:Rectangle = null):BitmapData
{
var dest:BitmapData = bd.clone();
var color:ColorRGB = new ColorRGB();
if (rect == null) rect = new Rectangle();
if (rect.width == 0) rect.width = bd.width - rect.x;
if (rect.height == 0) rect.height = bd.height - rect.y;
for (var y:int = rect.y; y < rect.y + rect.height; y += size)
{
for (var x:int = rect.x; x < rect.x + rect.width; x += size)
{
前のコードと比べて、変更したのは前半部分だけです。
private function pixelization(bd:BitmapData, size:int = 10, rect:Rectangle = null):BitmapData
第三引数にRectangleクラスを指定できるようになりました。
Rectangleクラスについては、Rectangleクラスを使用するを参照してください。
Rectangleクラスについては、Rectangleクラスを使用するを参照してください。
例えば、new Rectangle(100, 100, 250, 300)を指定すると、(100, 100)~(350, 400)までの範囲にモザイクがかかります。
Rectangleは別に指定しなくても構いません。その場合は今までどおり画像全てにモザイクがかかるようになっています。
if (rect == null) rect = new Rectangle();
if (rect.width == 0) rect.width = bd.width - rect.x;
if (rect.height == 0) rect.height = bd.height - rect.y;
Rectangleが指定されなかったらnullが入っているので、newでオブジェクトを作っておきましょう。
widthとheightが指定されなかったら、(x, y)から画像右端、下段までの長さをwidthとheightに入れておきます。
widthとheightが指定されなかったら、(x, y)から画像右端、下段までの長さをwidthとheightに入れておきます。
for (var y:int = rect.y; y < rect.y + rect.height; y += size)
{
for (var x:int = rect.x; x < rect.x + rect.width; x += size)
{
外側二重ループのx, yに指定するのはモザイクをかける(x, y)地点でしたね。
なので、rect.x, rect.yでxyを初期化して、ループの終了条件をrect.y + rect.height, rect.x + rect.widthにしておきます。
なので、rect.x, rect.yでxyを初期化して、ループの終了条件をrect.y + rect.height, rect.x + rect.widthにしておきます。
使用方法
addChild(new Bitmap(pixelization(bd, 20, new Rectangle(40, 40, 110, 110))));
(40, 40)~(150, 150)の範囲にモザイクをかけます。
addChild(new Bitmap(pixelization(bd, 20, new Rectangle(40, 40))));
widthとheightが指定されていないので、(40, 40)~(160, 160)の範囲にモザイクがかかります。
addChild(new Bitmap(pixelization(bd, 20)));
Rectangleを指定していません。
この場合は画像全体にモザイクがかかります。
この場合は画像全体にモザイクがかかります。
検証用コード
package
{
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.system.Security;
import frocessing.color.ColorRGB;
public class Main extends Sprite
{
private const WIDTH:int = 465; // 幅
private const HEIGHT:int = 465; // 高さ
public function Main()
{
stage.scaleMode = "noScale";
Security.loadPolicyFile("http://farm3.static.flickr.com/crossdomain.xml");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
loader.load(new URLRequest("http://farm3.static.flickr.com/2205/2532926223_6b2745185b.jpg"), new LoaderContext(true)); // 読み込みたい画像URL
}
private function initHandler(event:Event):void
{
var loader:Loader = event.currentTarget.loader;
var matrix:Matrix = new Matrix();
matrix.scale(WIDTH / loader.width, HEIGHT / loader.height); // 設定したいサイズ / 元サイズ
var bd:BitmapData = new BitmapData(WIDTH, HEIGHT); // 設定したいサイズ
bd.draw(loader, matrix);
addChild(new Bitmap(pixelization(bd, 20, new Rectangle(100, 100, 300, 300))));
}
private function pixelization(bd:BitmapData, size:int = 10, rect:Rectangle = null):BitmapData
{
var dest:BitmapData = bd.clone();
var color:ColorRGB = new ColorRGB();
if (rect == null) rect = new Rectangle();
if (rect.width == 0) rect.width = bd.width - rect.x;
if (rect.height == 0) rect.height = bd.height - rect.y;
for (var y:int = rect.y; y < rect.y + rect.height; y += size)
{
for (var x:int = rect.x; x < rect.x + rect.width; x += size)
{
var count:int = 0;
var r:int = 0;
var g:int = 0;
var b:int = 0;
for (var yy:int = 0; yy < size; yy++)
{
for (var xx:int = 0; xx < size; xx++)
{
if (x + xx < 0 || bd.width <= x + xx ||
y + yy < 0 || bd.height <= y + yy) continue;
count++;
color.value = bd.getPixel(x + xx, y + yy);
r += color.r;
g += color.g;
b += color.b;
}
}
color.r = r / count;
color.g = g / count;
color.b = b / count;
for (yy = 0; yy < size; yy++)
{
for (xx = 0; xx < size; xx++)
{
if (x + xx < 0 || bd.width <= x + xx ||
y + yy < 0 || bd.height <= y + yy) continue;
dest.setPixel(x + xx, y + yy, color.value);
}
}
}
}
return dest;
}
}
}
このwikiの更新情報RSS