StfnoPad
StfnoPad

Reputation: 1027

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.

Upvotes: 1

Views: 1513

Answers (1)

Zevan
Zevan

Reputation: 10235

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.

Upvotes: 1

Related Questions