简体   繁体   中英

Fast Bicubic or bilinear Image resize in Flash - Flex - actionscript 3

I need to resize images in actionscript maintaining the quality aka bicubic or bilinear resizing. At the moment my algorithm just loops through every single pixel and calculates the new pixel. example:

            /* Loop through the pixels of the output image, fetching the equivalent pixel from the input*/
            for (var x:int = 0; x < width; x++) {
                for (var y:int = 0; y < height; y++) {
                                                bitmapDataTemp2.setPixel(x, y, newBitmapData2.getPixelBilinear(x * xFactor, y * yFactor));
                                                //bitmapDataTemp2.setPixel(x, y, newBitmapData2.getPixelBicubic(x * xFactor, y * yFactor));
                }
            }

This is really slow and shamefully there is no multi-threading in the flash player, so I was wondering what tricks I can use to speed things up?

Many Thanks.

You should use BitmapData.setVector() instead of setPixel. Have a look at this example:

var canvas:BitmapData=new BitmapData(255,255,false,0x000000);
addChild(new Bitmap(canvas, "auto", true));
 
var size:int = canvas.width * canvas.height;
var cols:int = canvas.width;
 
var pixels:Vector.<uint> = new Vector.<uint>(size);
 
canvas.lock();
for (var i:int  = 0; i<size; i++) {
    var ox:uint= i % cols;
    var oy:uint= i / cols;
    // just an example of what you can do:
    // pixels[i] = oy <<16 | ox;
    pixels[i] = newBitmapData2.getPixelBilinear(ox * xFactor, oy * yFactor);
}
   
canvas.setVector(canvas.rect, pixels);
canvas.unlock();

You should consider inlining the newBitmapData2.getPixelBilinear function. Doing that will also significantly speed things up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM