深入了解 NSImage 的像素数据

发布于 2024-09-10 18:12:49 字数 1118 浏览 10 评论 0原文

我正在编写对黑白图像进行操作的应用程序。我通过将 NSImage 对象传递到我的方法中,然后从 NSImage 生成 NSBitmapImageRep 来完成此操作。一切正常,但速度很慢。这是我的代码:

- (NSImage *)skeletonization: (NSImage *)image
{
    int x = 0, y = 0;
    NSUInteger pixelVariable = 0;

    NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];

    [myHelpText setIntValue:[bitmapImageRep pixelsWide]];
    [myHelpText2 setIntValue:[bitmapImageRep pixelsHigh]];

    NSColor *black = [NSColor blackColor];
    NSColor *white = [NSColor whiteColor];
    [myColor set];
    [myColor2 set];

    for (x=0; x<=[bitmapImageRep pixelsWide]; x++) {
        for (y=0; y<=[bitmapImageRep pixelsHigh]; y++) {
            // This is only to see if it's working
            [bitmapImageRep setColor:myColor atX:x y:y];
        }
    }

    [myColor release];
    [myColor2 release];

    NSImage *producedImage = [[NSImage alloc] init];
    [producedImage addRepresentation:bitmapImageRep];
    [bitmapImageRep release];

    return [producedImage autorelease];
}

所以我尝试使用 CIImage 但我不知道如何通过 (x,y) 坐标进入每个像素。这真的很重要。

I'm writing application that operates on black&white images. I'm doing it by passing a NSImage object into my method and then making NSBitmapImageRep from NSImage. All works but quite slow. Here's my code:

- (NSImage *)skeletonization: (NSImage *)image
{
    int x = 0, y = 0;
    NSUInteger pixelVariable = 0;

    NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];

    [myHelpText setIntValue:[bitmapImageRep pixelsWide]];
    [myHelpText2 setIntValue:[bitmapImageRep pixelsHigh]];

    NSColor *black = [NSColor blackColor];
    NSColor *white = [NSColor whiteColor];
    [myColor set];
    [myColor2 set];

    for (x=0; x<=[bitmapImageRep pixelsWide]; x++) {
        for (y=0; y<=[bitmapImageRep pixelsHigh]; y++) {
            // This is only to see if it's working
            [bitmapImageRep setColor:myColor atX:x y:y];
        }
    }

    [myColor release];
    [myColor2 release];

    NSImage *producedImage = [[NSImage alloc] init];
    [producedImage addRepresentation:bitmapImageRep];
    [bitmapImageRep release];

    return [producedImage autorelease];
}

So I tried to use CIImage but I don't know how to get into each pixel by (x,y) coordinates. That is really important.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮云壑 2024-09-17 18:12:49

使用 NSImage 中的 representations 数组属性来获取 NSBitmapImageRep。它应该比将图像序列化为 TIFF 然后再返回要快。

使用 NSBitmapImageRep 的 bitmapData 属性直接访问图像字节。

例如,

unsigned char black = 0;
unsigned char white = 255;

NSBitmapImageRep* bitmapImageRep = [[image representations] firstObject];
// you will need to do checks here to determine the pixelformat of your bitmap data
unsigned char* imageData = [bitmapImageRep bitmapData];

int rowBytes = [bitmapImageRep bytesPerRow];
int bpp = [bitmapImageRep bitsPerPixel] / 8;

for (x=0; x<[bitmapImageRep pixelsWide]; x++) {  // don't use <= 
    for (y=0; y<[bitmapImageRep pixelsHigh]; y++) {

       *(imageData + y * rowBytes + x * bpp ) = black; // Red
       *(imageData + y * rowBytes + x * bpp +1) = black;  // Green
       *(imageData + y * rowBytes + x * bpp +2) = black;  // Blue
       *(imageData + y * rowBytes + x * bpp +3) = 255;  // Alpha
    }
}

在使用数据之前,您需要知道图像中使用的像素格式,查看 NSBitmapImageRep 的 bitsPerPixel 属性来帮助确定您的图像是否采用 RGBA 格式。

您可以使用灰度图像、RGB 图像、或者可能是 CMYK。或者先将图像转换为您想要的图像。或者以不同的方式处理循环中的数据。

Use the representations array property from NSImage, to get your NSBitmapImageRep. It should be faster than serializing your image to a TIFF and then back.

Use the bitmapData property of the NSBitmapImageRep to access the image bytes directly.

eg

unsigned char black = 0;
unsigned char white = 255;

NSBitmapImageRep* bitmapImageRep = [[image representations] firstObject];
// you will need to do checks here to determine the pixelformat of your bitmap data
unsigned char* imageData = [bitmapImageRep bitmapData];

int rowBytes = [bitmapImageRep bytesPerRow];
int bpp = [bitmapImageRep bitsPerPixel] / 8;

for (x=0; x<[bitmapImageRep pixelsWide]; x++) {  // don't use <= 
    for (y=0; y<[bitmapImageRep pixelsHigh]; y++) {

       *(imageData + y * rowBytes + x * bpp ) = black; // Red
       *(imageData + y * rowBytes + x * bpp +1) = black;  // Green
       *(imageData + y * rowBytes + x * bpp +2) = black;  // Blue
       *(imageData + y * rowBytes + x * bpp +3) = 255;  // Alpha
    }
}

You will need to know what pixel format you are using in your images before you can go playing with its data, look at the bitsPerPixel property of NSBitmapImageRep to help determine if your image is in RGBA format.

You could be working with a gray scale image, or an RGB image, or possibly CMYK. And either convert the image to what you want first. Or handle the data in the loop differently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文