简体   繁体   English

如何通过使用原始ARGB数据创建颜色位图(CGBitmapContextCreate)

[英]How to create color bitmap (CGBitmapContextCreate) by using raw ARGB data

I am getting an image but I am losing 1 byte. 我正在获取图像,但丢失了1个字节。 My resulting image: 我得到的图像:

在此处输入图片说明

I want to create color/rgba bitmap using argb raw (void*)data and I have its width and height. 我想使用argb原始(void*)data创建color / rgba位图,并且具有宽度和高度。 In backend(c++) I have decoded rgb to argb by using following method and then giving input as (void*)pData , 在后端(c ++)中,我已使用以下方法将rgb解码为argb ,然后将输入作为(void*)pData

void decode_rgb_to_argb(U8Data r, U8Data g, U8Data b, U32Data argb, u_int elements)
{
 assert(argb);
 assert(r);
 assert(g);
 assert(b);
 assert(elements);     

 unsigned char*p=NULL;

 for(u_int i=0;i<elements;i++)
 {
      p=(unsigned char*)(argb+i);
      *p=b[i];
      p++;
      *p=g[i];
      p++;
      *p=r[i];
      p++;
      *p=0;
 }
}
pData = decode_rgb_to_argb;

//I am handling in ios

-(uiimage*) createBitmap:(void*)pData pWidth:(u_int)pWidth pHeight:(u_int)pHeight{

   // Here i want to write ppm file using pData to check wether 4byte/3byte.
   NSData* data = [NSData dataWithBytes:pData  length:pWidth*pHeight];

     char* myBuffer = (char*)pData;
    char* rgba = (char*)malloc(pWidth*pHeight*4);

    for(int i=0; i < pWidth*pHeight; i++) {
    rgba[4*i] = myBuffer[3*i];
    rgba[4*i+1] = myBuffer[3*i+1];
    rgba[4*i+2] = myBuffer[3*i+2];
    rgba[4*i+3] = 255; //or 0
   }


size_t bitsPerComponent = 8;
size_t bytesPerRow      = pWidth*4;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef bitmapContext = CGBitmapContextCreate(
 // Here i have given my raw data(pData) and rgba buffer
                                                   (u_char*)pData,
                                                   pWidth,
                                                   pHeight,
                                                   bitsPerComponent,
                                                   bytesPerRow,
                                                   colorSpace,
   //Here i have used kCGImageAlphaFirst because i am getting data as ARGB,but
   //bitmap is not creating.If i use kCGImageAlphaNoneSkipLast, i am getting expected
   //image but i'm loosing one byte(alpha).
                                                   kCGImageAlhaInfo       
                                                   );

CFRelease(colorSpace);

CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
UIImage *result = [[[UIImage imageWithCGImage:cgImage] retain] autorelease];

CGColorSpaceRelease(colorSpace);
CGImageRelease(cgImage);
CGContextRelease(bitmapContext);
return result;

}

How do I write a ppm file and how do I create a bitmap from my raw (void*)Pdata to a color image in ios? 如何编写ppm文件以及如何将原始(void*)Pdata到iOS中的彩色图像创建位图?

If you need an uiimage with rgb Then you can use this function. 如果您需要带有rgb的uiimage,则可以使用此功能。

- (UIImage *) createImageWithRGBColor :(UIColor *)color ofSize : (CGSize)Size {
    UIView *keyview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Size.width, Size.height)];
    [keyview setBackgroundColor:color];
    CGRect rect = [keyview bounds];
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [keyview.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

And call like this: 并这样调用:

UIImage *image = [self createImageWithRGBColor:[UIColor colorWithRed:.25f green:.40f blue:.8f alpha:1.0] ofSize:CGSizeMake(100, 100)];

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

相关问题 如何为使用 CGBitmapContextCreate() 创建的图形 canvas 设置背景颜色? - How to set a background color for the drawing canvas created with CGBitmapContextCreate()? 如何从RGBA图像创建ARGB图像 - how to create ARGB Image From RGBA Image 使用CGBitmapContextCreate()和基础位图内存管理创建的CGContextRef - CGContextRef created with CGBitmapContextCreate() & underlying bitmap memory management 如何在不预乘alpha的情况下获取真实的RGBA或ARGB颜色值? - How to get the real RGBA or ARGB color values without premultiplied alpha? CGBitmapContext创建无效的数据字节/行 - CGBitmapContextCreate invalid data bytes/row CGBitmapContextCreate:无效的数据字节/行 - CGBitmapContextCreate: invalid data bytes/row iOS CGBitmapContextCreate是否复制数据? - iOS Does CGBitmapContextCreate copy data? Swift 2&3:如何使用CGBitmapContextCreate - Swift 2&3: How to Use CGBitmapContextCreate 如何在 iOS swift 上创建 ARGB8888 二进制图像 - How to create an ARGB8888 Binary image on iOS swift 是否可以确定UIGraphicsBeginImageContext使用CGBitmapContextCreate来创建图形上下文? - Is it certain that UIGraphicsBeginImageContext use CGBitmapContextCreate to create the graphics context?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM