简体   繁体   English

将UIImage保存为8位灰度位图

[英]Save UIImage as 8-bit Grayscale Bitmap

I am trying to output a UIImage to an 8-bit grayscale bitmap, but I'm not very familiar with image manipulation, so I am at a loss on how to do this. 我正在尝试将UIImage输出为8位灰度位图,但是我对图像处理不是很熟悉,因此我对如何执行此操作感到困惑。 I followed this stackoverflow post ( Objective C - save UIImage as a BMP file ) to create a UIImage category which can successfully generated a RGB 32-bit image, but I have been unable to manipulate the code to get it to output it in 8-bit grayscale. 我按照这篇stackoverflow帖子( 目标C-将UIImage保存为BMP文件 )创建了一个UIImage类别,该类别可以成功生成RGB 32位图像,但是我无法操纵代码以使其在8中输出。有点灰度。 I am trying to get the raw bytes for this image in order to pass on to a provided C library. 我试图获取此图像的原始字节,以便传递给提供的C库。

Does it matter what kind of UIImage I am trying to generate the bitmap for? 我尝试为哪种UIImage生成位图有关系吗? Can it be any number of bits per pixel, bits per component, and alpha setting? 每个像素的位数,每个组件的位数和alpha设置可以是任意数量吗? I have tried using this code on the 8-bit UIImage without alpha and a 32 bit with alpha, and both didn't create a bitmap that could be opened by OS X. 我曾尝试在没有alpha的8位UIImage和带有alpha的32位上使用此代码,但是两者都未创建OS X可以打开的位图。

I hope someone can help! 我希望有人能帮帮忙! I've been banging my head for a few days! 几天来我一直在头!

- (NSData *)bitmapData
{
    NSData          *bitmapData = nil;
    CGImageRef      image = self.CGImage;
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    UInt8           *rawData;

    size_t bitsPerPixel = 8;
    size_t bitsPerComponent = 8;
    size_t bytesPerPixel = bitsPerPixel / bitsPerComponent;

    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);

    size_t bytesPerRow = width * bytesPerPixel;
    size_t bufferLength = bytesPerRow * height;

    colorSpace = CGColorSpaceCreateDeviceGray();

    if (colorSpace)
    {
        // Allocate memory for raw image data
        rawData = (UInt8 *)calloc(bufferLength, sizeof(UInt8));

        if (rawData)
        {
            CGBitmapInfo bitmapInfo = (CGBitmapInfo)kCGImageAlphaNone;
            context = CGBitmapContextCreate(rawData,
                                            width,
                                            height,
                                            bitsPerComponent,
                                            bytesPerRow,
                                            colorSpace,
                                            bitmapInfo);

            if (context)
            {
                CGRect rect = CGRectMake(0, 0, width, height);

                CGContextTranslateCTM(context, 0, height);
                CGContextScaleCTM(context, 1.0, -1.0);
                CGContextDrawImage(context, rect, image);

                bitmapData = [NSData dataWithBytes:rawData length:bufferLength];

                CGContextRelease(context);
            }

            free(rawData);
        }

        CGColorSpaceRelease(colorSpace);
    }

    return bitmapData;
}

- (NSData *)bitmapFileHeaderData
{
    CGImageRef image = self.CGImage;
    UInt32     width = (UInt32)CGImageGetWidth(image);
    UInt32     height = (UInt32)CGImageGetHeight(image);

    t_bitmap_header header;

    header.fileType = 0x4D42;
    header.fileSize = (height * width) + 54;
    header.reserved1 = 0;
    header.reserved2 = 0;
    header.bitmapOffset = 54;
    header.headerSize = 40;
    header.width = width;
    header.height = height;
    header.colorPlanes = 1;
    header.bitsPerPixel = 8;
    header.compression = 0;
    header.bitmapSize = height * width;
    header.horizontalResolution = 0;
    header.verticalResolution = 0;
    header.colorsUsed = 0;
    header.colorsImportant = 0;

    return [NSData dataWithBytes:&header length:sizeof(t_bitmap_header)];
}

- (NSData *)bitmapDataWithFileHeader
{
    NSMutableData *data = [NSMutableData dataWithData:[self bitmapFileHeaderData]];
    [data appendData:[self bitmapData]];

    return [NSData dataWithData:data];
}

If you have a UIImage with no alpha channel, you can convert it to grayscale like this: 如果您的UIImage没有alpha通道,则可以将其转换为灰度,如下所示:

- (UIImage *)grayscaleImage:(UIImage *)image {
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaNone);
    CGContextDrawImage(context, imageRect, [image CGImage]);
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *grayscaleImage = [UIImage imageWithCGImage:imageRef];
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    CFRelease(imageRef);
    return grayscaleImage;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM