質問

I am working on an application, which has to use pixel manipulation in the following way: around a point(x,y) I have to condense or expand the pixels in a circle of radius R, but I want to keep the rectangular frame of the UIImage. I get the raw data n the following way (I found this on the web):

CGImageRef imageRef = image.CGImage;

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

unsigned char *rawData = CFDataGetBytePtr(dataref);

So, I have in rawData the pixel data.

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;
}

Now, how do I know if the current pixel is within the circle of origin(x,y) and of radius R? And how do I condense/expand these pixels?

Thanks

役に立ちましたか?

解決

Use pythagorus' theorem - a2 + b2 = c2

c2 is easy:

CGFloat maxDistance = R*R;

For each pixel, given its position (xp, yp) - which you can maintain in your loop - get the square of the distance from the origin and compare to 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++;
    }
}

I don't know what you mean be condensing the pixels, but I'm sure that you can work out what you need to do now...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top