FLASH as to achieve mosaic effect

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.