24ビットのビットマップ
24ビットのビットマップを作成してみた。
CGDataProviderCreateWithDataに渡すbitmapにはRGBAではなくRGBの24ビットの連続したデータを格納し
下記のソースのように、NSBitmapImageRepを作成すると正常に作成され、
bitsPerPixelは24になっている。
先日、試したときはbitmapにRGBAのデータを設定していたので作成したビットマップを
ファイル出力してみると画像がおかしくなっていた。
CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData(NULL, bitmap, width * height * 3, NULL); CGImageRef imageRef = CGImageCreate(width, // width height, // height 8, // bitsPerComponent 24, // bitsPerPixel width * 3, // bytesPerRow CGColorSpaceCreateDeviceRGB(), // colorspace kCGBitmapByteOrderDefault, // bitmapInfo dataProviderRef, // provider NULL, // decode NO, // shouldInterpolate kCGRenderingIntentDefault); // intent NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc]initWithCGImage:imageRef]; // このビットマップは24ビットになる
32ビットのCGContextRefから、24ビットのビットマップデータを取得するにはどうしたら良いんだろう?
下記のようにCGContextRefを作成しCGBitmapContextGetDataでビットマップデータを取得すると
32ビットのデータが取得できるが、RGBの24ビットのデータにするにはどうしたら良いのか?
unsigned char *data = (unsigned char *)malloc(width * height * sizeof(unsigned char) * 4); CGContextRef bitmapContext = CGBitmapContextCreate(data, width, height, 8, width * 4, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast); unsigned char *bitmap = (unsigned char *)CGBitmapContextGetData(bitmapContext);