I want to create an image that is black and white so it is 1bpp. Right now this is my code that converts an image to black and white based on a threshold:
- (UIImage *)pureBlackAndWhiteImage:(UIImage *)image value:(float)value {
unsigned char *dataBitmap = [self convertUIImageToBitmapRGBA8:image];
for (int i = 0; i < image.size.width * image.size.height * 4; i += 4) {
if ((dataBitmap[i + 0] + dataBitmap[i + 1] + dataBitmap[i + 2]) < value) {
dataBitmap[i + 0] = 0;
dataBitmap[i + 1] = 0;
dataBitmap[i + 2] = 0;
} else {
dataBitmap[i + 0] = 255;
dataBitmap[i + 1] = 255;
dataBitmap[i + 2] = 255;
}
}
image = [self convertBitmapRGBA8ToUIImage:dataBitmap withWidth:image.size.width withHeight:image.size.height];
return image;
}
However this image is still 8bpp. How can I make it so that it is 1bpp?
I don’t know what those methods are that you refer to, but the second does claim to produce an image from an RGBA data set, so why do you expect 1bpp?
Frankly, I don’t know if iOS can even support 1bpp. If you cannot determine for sure that the answer is no, you’ll probably have to experiment.
What you need to do is first render the existing RGBA image and get access to the pixels (as it appears you have done. You will then need to malloc a chunk of memory and write the image as you want – 1 bit per pixel. When you have that data you can get a CGImageRef using:
and a UIImage from that.