1
votes

ios-converting-uiimage-to-rgba8-bitmaps-and-back is a very good article that I found which describes how we can deal with bitmap buffer and UIImages. This article deals with RGBA32/ RGBA8 bitmap images. The bitmap images are created with char * buffer with size width * height * 4. ie, each pixel information will have 4 bytes of data, 1 byte each for storing red, green, blue and alpha respectively. On creating the bitmap image the bitmap info 'kCGImageAlphaPremultipliedLast' is given. CGColorSpaceCreateDeviceRGB() is used for converting back the bitmapbuffer to UIImage. By changing the bitmap info we can also deal with RGBA 24 images. I need to deal with RGBA 5551 bitmap images. red, green and blue colors are given 5 bits to represent the respetive colors and 1 bit for storing alpha value. If we are creating such a bitmap, how we can allocate buffer for a char * bitmap. Is it possible to convert into a UIImage data type?. Any help will be appreciated.

2
Get a copy of Programming with Quartz by David Gelphman and Bunny Laden. The closest format is probably: kCGImageAlphaNoneSkipFirst`, this is a XRGB with 5 bits for each color (XRGB 1555). Perhaps you could set the leading bit to your Alpha value.zaph
@Zaph I have converted the char buffer using the XRGB, I am getting the UIImage object and perfectly rendered using an ImageView. But the char buffer I get is of type RGB 5551. There is a mismatch in actual image colour and the one I have converted using XRGB (kCGImageAlphaNoneSkipFirst bitmap info). Could you advise any solution like converting 5551 rgb buffer to 1555 rg buffer or to any other type that supports in iOS sdk.Govind
Swap the bits in the buffer if that is what is needed, just use the "C" bit shift, and and or operations.zaph

2 Answers

0
votes

Here the BITS_PER_COMPONENT is 5 and BITS_PER_COMPONENT is 16. With this code I have successfully. kCGBitmapByteOrder16Little indicates the byte order of the char buffer.

size_t bufferLength = width * height * 2;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);
size_t bitsPerComponent = BITS_PER_COMPONENT;
size_t bitsPerPixel = BITS_PER_PIXEL;
size_t bytesPerRow = 2 * width;

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
if(colorSpaceRef == NULL) {
    NSLog(@"Error allocating color space");
    CGDataProviderRelease(provider);
    return nil;
}
CGBitmapInfo bitmapInfo = kCGBitmapByteOrder16Little | kCGImageAlphaNoneSkipFirst;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGImageRef iref = CGImageCreate(width,
                                height,
                                bitsPerComponent,
                                bitsPerPixel,
                                bytesPerRow,
                                colorSpaceRef,
                                bitmapInfo,
                                provider,   // data provider
                                NULL,       // decode
                                YES,            // should interpolate
                                renderingIntent);
uint32_t* pixels = (uint32_t*)malloc(bufferLength);

if(pixels == NULL) {
    NSLog(@"Error: Memory not allocated for bitmap");
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpaceRef);
    CGImageRelease(iref);
    return nil;
}

CGContextRef context = CGBitmapContextCreate(pixels,
                                             width,
                                             height,
                                             bitsPerComponent,
                                             bytesPerRow,
                                             colorSpaceRef,
                                             bitmapInfo);
0
votes

you need to create 16 bit image, so provide bpp as 16, bpc as 5, below will be the code:

size_t width = CGImageGetWidth(screenShotImageRef);
size_t height = CGImageGetHeight(screenShotImageRef);
size_t bytesPerRow = width * (bpc == 5 ? 2 : 4);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(buffer(provide buffer where you want to write image into), width, height, bpc, bytesPerRow, colorSpace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrderDefault);
CGContextDrawImage(bitmapContext, CGRectMake(0, 0, width, height), screenShotImageRef);
CGContextRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);