iPhone上的像素操作

时间:2011-11-28 07:38:06

标签: iphone pixel-manipulation

我正在开发一个应用程序,它必须以下列方式使用像素操作:在点(x,y)周围我必须缩小或扩展半径为R的圆中的像素,但我想保留UIImage的矩形框架。我通过以下方式获取原始数据(我在网上找到了这个):

CGImageRef imageRef = image.CGImage;

NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CFDataRef dataref = CopyImagePixels(imageRef);

unsigned char *rawData = CFDataGetBytePtr(dataref);

所以,我在rawData中有像素数据。

int byteIndex = 0;
for (int ii = 0 ; ii < width * height ; ++ii)
{
    //now rawData[baseIndex] stands for red
    //rawData[baseIndex + 1] stands for green
    //rawData[baseIndex + 2] stands for blue

    byteIndex += 4;
}

现在,我如何知道当前像素是否在原点(x,y)和半径R的范围内? 我如何缩小/扩展这些像素?

由于

1 个答案:

答案 0 :(得分:1)

使用pythagorus'定理 - 2 + b 2 = c 2

c 2 很容易:

CGFloat maxDistance = R*R;

对于每个像素,给定它的位置(xp,yp) - 你可以在循环中保持 - 得到距离原点的距离的平方并与maxDistance进行比较:

int byteIndex = 0;
CGFloat xp = 0;
CGFloat yp = 0;

for (int ii = 0 ; ii < width * height ; ++ii)
{
    CGFloat xDist = xp - x;
    CGFloat yDist = yp - y;

    CGFloat dist = sqrt((xDist * xDist + yDist * yDist)) // Squares will be positive for comparison below...

    if (dist <= maxDistance) {
        ...
    }

    byteIndex += 4;
    if (++xp > width) {
        xp = 0;
        yp++;
    }
}

我不知道你的意思是凝聚像素,但我相信你现在可以解决你需要做的事情......