Convert bitmap image information into CGImage in iPhone OS 3

Posted by giftederic on Stack Overflow See other posts from Stack Overflow or by giftederic
Published on 2010-05-13T08:09:59Z Indexed on 2010/05/13 8:14 UTC
Read the original article Hit count: 386

Filed under:
|

I want to create a CGImage with the color information I already have

Here is the code for converting the CGImage to CML, CML_color is a matrix structure

- (void)CGImageReftoCML:(CGImageRef *)image destination:(CML_color &)dest{
    CML_RGBA p;
    NSUInteger width=CGImageGetWidth(image);
    NSUInteger height=CGImageGetHeight(image);
    CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();

    unsigned char *rawData=new unsigned char[height*width*4];
    NSUInteger bytesPerPixel=4;
    NSUInteger bytesPerRow=bytesPerPixel*width;
    NSUInteger bitsPerComponent=8;

    CGContextRef context=CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
    CGContextRelease(context);

    int index=0;
    for (int i=0; i<height; i++) {
        for (int j=0; j<width; j++) {
            p.red=rawData[index++];
            p.green=rawData[index++];
            p.blue=rawData[index++];
            p.alpha=rawData[index++];
            dest(i,j)=p;
        }
    }

    delete[] rawData;
}

Now I want the reverse function, which converts CML into CGImage. I know all the color and alpha information to create the image, which stored in the matrix CML, but how can I do that?

© Stack Overflow or respective owner

Related posts about iphone-sdk

Related posts about cgimage