迭代在ActionScript位图图像的每个像素 [英] Iterating each pixel of a Bitmap image in ActionScript

查看:120
本文介绍了迭代在ActionScript位图图像的每个像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能重复位图图像的每个像素?最后,我想要实现的是,我需要得到一个位图图像的每个像素的坐标值,并改变那些像素根据自己的坐标值的颜色。在我看来,我需要使用的getPixels()方法,但我还是不明白到底是什么我应该做的。

Is it possible to iterate each pixel of a bitmap image? Eventually what I'm trying to achieve is that I need to get the coordinate values of each pixel of a bitmap image and change the color of those pixels according to their coordinate values. As I see it, I need to use the getPixels() method but I still did not understand exactly what I should do.

推荐答案

如果像你说的,你根据自己的x和y只设置像​​素,你既不需要与getPixel(),也没有的getPixels()!

If, as you say, you are only setting the pixels based on their x and y, you need neither getPixel() nor getPixels()!

myBitmapData.lock();

for( var j:int = 0; j < myBitmapData.height; j++ )
{
    for( var i:int = 0; i < myBitmapData.width; i++ )
    {
        var alpha:uint = 0xFF000000;  // Alpha is always 100%
        var red:uint = 0x00FF0000 * ( i / myBitmapData.width );  // Set red based on x
        var green:uint = 0x0000FF00 * ( j / myBitmapData.height );  // Set green based on y
        var newColor:uint = alpha + red + green;  // Add the components

        // Set the new pixel value (setPixel32() includes alpha, e.g. 0xFFFF0000 => alpha=FF, red=FF, green=00, blue=00)
        myBitmapData.setPixel32( i, j, newColor );
    }
}

myBitmapData.unlock();

不过,如果你想读的像素目前的价值,让我加入速度的竞争。

If, however, you want to read the pixels' current value, let me join the speed competition.

除了前面的答案,这里有更多的速度增长!

In addition to earlier answers, here's much more speed increase!

相反,无数的电话,以与getPixel(),你可以使用的getPixels()来获取像素数据的字节数组。

Instead of numerous calls to getPixel(), you can use getPixels() to get a byteArray of the pixel data.

myBitmapData.lock();

var numPixels:int = myBitmapData.width * myBitmapData.height;
var pixels:ByteArray = myBitmapData.getPixels( new Rectangle( 0, 0, myBitmapData.width, myBitmapData.height ) );
for( var i:int = 0; i < numPixels; i++ )
{
    // Read the color data
    var color:uint = pixels.readUnsignedInt();

    // Change it if you like

    // Write it to the pixel (setPixel32() includes alpha, e.g. 0xFFFF0000 => alpha=FF, red=FF, green=00, blue=00)
    var theX:int = i % myBitmapData.width;
    myBitmapData.setPixel32( theX, ( i - theX ) / myBitmapData.width, color );
}

myBitmapData.unlock();

这篇关于迭代在ActionScript位图图像的每个像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆