Animation effect:
Click here to download the source file
In general, there are 2 ways to achieve mosaic, using all of the image to traverse the pixel, for a certain range of pixels to fill the same color, but the color location of the sampling, there are selection center point, also have the selection of all pixel average, considering the efficiency of flash, this is to select the center point, you can save a lot of Cycle
Also pay attention to the multiple of stride 4, otherwise. See for yourself. Haha ( Moonlight Note: In fact, with a multiple of 2 is OK )
Import Flash.display.BitmapData;
The scene is called an IMG MC OK
var oldbmp:bitmapdata = new BitmapData (img._width, img._height);
Oldbmp.draw (IMG);
var newbmp:bitmapdata = mosaic (oldbmp, 12);
var mc = createemptymovieclip ("MC", 1);
Mc.attachbitmap (Newbmp, getnexthightestdepth ());
mc._x = Img._width;
function mosaic (Bmp:bitmapdata, Stride:number): BitmapData {
var starttime = Gettimer ();
var newbmp:bitmapdata = new BitmapData (bmp.width, bmp.height);
var w = bmp.width/stride + 1;
var h = bmp.height/stride + 1;
var Edgew = bmp.width% Stride; Marginal stride part of the
var Edgeh = bmp.height% Stride;
var centerw = (stride-1)/2; The center point of the first lattice
var centerh = CenterY; The first lattice is always square.
var tmpx, Tmpy; The true pixel position in the loop, the front W, H has been removed.
var blockw, Blockh; Mosaic lattice
var i =-1;
while (++i < W) {
TMPX = i * STRIDE;
if (i = = w-1) {
BLOCKW = Edgew;
} else {
BLOCKW = Stride;
}
Centerw = (blockW-1)/2;
var j =-1;
while (++j < h) {
Tmpy = J * STRIDE;
if (i = = h-1) {
Blockh = Edgeh;
} else {
Blockh = Stride;
}
Centerh = (blockH-1)/2;
var color = bmp.getpixel (tmpx + centerw, Tmpy + centerh);
var m =-1;
while (++m < blockw) {
var n =-1
while (++n < Blockh) {
Newbmp.setpixel (tmpx + M, tmpy + N,color);
}
}
}
}
Trace ("Cost:" + (Gettimer ()-starttime));
return newbmp;
}
The above code is compiled with FLASH8.