我想用我已经拥有的颜色信息创建一个 CGImage
这里是CGImage转CML的代码,CML_color是一个矩阵结构
- (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=(unsigned char*)malloc(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;
}
现在我想要反向函数,它将 CML 转换为 CGImage。我知道创建图像的所有颜色和 alpha 信息,这些信息存储在矩阵 CML 中,但我该怎么做呢?