如何访问和处理JPEG图像像素? [英] How do I access and manipulate JPEG image pixels?

查看:203
本文介绍了如何访问和处理JPEG图像像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jpg文件.我需要将其转换为像素数据,然后更改某些像素的颜色.我是这样的:

i have a jpg file. I need to convert it to pixel data and then change color of some pixel. I do it like this:

    NSString *string = [[NSBundle mainBundle] pathForResource:@"pic" ofType:@"jpg"];
    NSData *data = [NSData dataWithContentsOfFile:string];
    unsigned char *bytesArray = dataI.bytes;
    NSUInteger byteslenght = data.length;
   //--------pixel to array
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:byteslenght];
    for (int i = 0; i<byteslenght; i++) {
        [array addObject:[NSNumber numberWithUnsignedChar:bytesArray[i]]];
    }

从95到154,我尝试更改像素的颜色.

Here i try to change color of pixels since 95 till 154.

NSNumber *number = [NSNumber numberWithInt:200];
    for (int i=95; i<155; i++) {
        [array replaceObjectAtIndex:i withObject:number];
    }

但是当我将数组转换为图像时,我得到了模糊的图片.我不明白为什么我对某些像素没有影响,为什么我对图片总有影响?

But when i convert array to image i got a blurred picture. I don't understand why i don't have an influence on some pixels and why i have influence on picture in total?

推荐答案

访问像素级数据的过程比您的问题可能要复杂一些,因为正如Martin所指出的那样,JPEG可以是压缩图像格式. Apple在技术问答中QA1509 .

The process of accessing pixel-level data is a little more complicated than your question might suggest, because, as Martin pointed out, JPEG can be a compressed image format. Apple discusses the approved technique for getting pixel data in Technical Q&A QA1509.

底线,要获取UIImage的未压缩像素数据,您将:

Bottom line, to get the uncompressed pixel data for a UIImage, you would:

  1. 获取UIImageCGImage.

通过CGImageGetDataProvider获取该CGImageRef的数据提供程序.

Get the data provider for that CGImageRef via CGImageGetDataProvider.

通过CGDataProviderCopyData获取与该数据提供者关联的二进制数据.

Get the binary data associated with that data provider via CGDataProviderCopyData.

提取有关图像的一些信息,因此您知道如何解释该缓冲区.

Extract some of the information about the image, so you know how to interpret that buffer.

因此:

UIImage *image = ...

CGImageRef imageRef = image.CGImage;                                     // get the CGImageRef
NSAssert(imageRef, @"Unable to get CGImageRef");

CGDataProviderRef provider = CGImageGetDataProvider(imageRef);           // get the data provider
NSAssert(provider, @"Unable to get provider");

NSData *data = CFBridgingRelease(CGDataProviderCopyData(provider));      // get copy of the data
NSAssert(data, @"Unable to copy image data");

NSInteger       bitsPerComponent = CGImageGetBitsPerComponent(imageRef); // some other interesting details about image
NSInteger       bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
NSInteger       bitsPerPixel     = CGImageGetBitsPerPixel(imageRef);
CGBitmapInfo    bitmapInfo       = CGImageGetBitmapInfo(imageRef);
NSInteger       bytesPerRow      = CGImageGetBytesPerRow(imageRef);
NSInteger       width            = CGImageGetWidth(imageRef);
NSInteger       height           = CGImageGetHeight(imageRef);
CGColorSpaceRef colorspace       = CGImageGetColorSpace(imageRef);

鉴于您要对此进行操作,您大概需要一些可变的像素缓冲区.最简单的方法是在该NSData对象中创建一个mutableCopy并在该对象中进行操作,但是在这些情况下,我倾向于使用C语言,创建一个void *outputBuffer,然后将原始像素复制到其中并进行操作使用传统的C数组技术.

Given that you want to manipulate this, you presumably want some mutable pixel buffer. The easiest approach would be to make a mutableCopy of that NSData object and manipulate it there, but in these cases, I tend to fall back to C, creating a void *outputBuffer, into which I copy the original pixels and manipulate using traditional C array techniques.

要创建缓冲区:

void *outputBuffer = malloc(width * height * bitsPerPixel / 8);
NSAssert(outputBuffer, @"Unable to allocate buffer");

有关如何操作它的确切详细信息,您必须查看bitmapInfo(它将告诉您是RGBA还是ARGB;是浮点还是整数)和bitsPerComponent(它将告诉您是否是RGBA或ARGB).每个组件8位或16位等).例如,一种非常常见的JPEG格式是每个分量8位,即RGBA(即红色,绿色,蓝色和alpha,按此顺序)四个分量.但是您确实需要检查我们从CGImageRef中提取的各种属性以确保.请参阅

For the precise details on how to manipulate it, you have to look at bitmapInfo (which will tell you whether it's RGBA or ARGB; whether it's floating point or integer) and bitsPerComponent (which will tell you whether it's 8 or 16 bits per component, etc.). For example, a very common JPEG format is 8 bits per component, four components, RGBA (i.e. red, green, blue, and alpha, in that order). But you really need to check those various properties we extracted from the CGImageRef to make sure. See the discussion in the Quartz 2D Programming Guide - Bitmap Images and Image Masks for more information. I personally find "Figure 11-2" to be especially illuminating.

下一个逻辑问题是当您完成处理像素数据时,如何为此创建一个UIImage.简而言之,您已颠倒了上述过程,例如创建一个数据提供程序,创建一个CGImageRef,然后创建一个UIImage:

The next logical question is when you're done manipulating the pixel data, how to create a UIImage for that. In short, you'd reverse the above process, e.g. create a data provider, create a CGImageRef, and then create a UIImage:

CGDataProviderRef outputProvider = CGDataProviderCreateWithData(NULL, outputBuffer, sizeof(outputBuffer), releaseData);

CGImageRef outputImageRef = CGImageCreate(width,
                                          height,
                                          bitsPerComponent,
                                          bitsPerPixel,
                                          bytesPerRow,
                                          colorspace,
                                          bitmapInfo,
                                          outputProvider,
                                          NULL,
                                          NO,
                                          kCGRenderingIntentDefault);

UIImage *outputImage = [UIImage imageWithCGImage:outputImageRef];

CGImageRelease(outputImageRef);
CGDataProviderRelease(outputProvider);

其中releaseData是C函数,可以简单地调用free与数据提供者关联的像素缓冲区:

Where releaseData is a C function that simply calls free the pixel buffer associated with the data provider:

void releaseData(void *info, const void *data, size_t size)
{
    free((void *)data);
}

这篇关于如何访问和处理JPEG图像像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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