This the case,
I wanna use iOS6+ to convert a PNG image ( or JPEG ) to 4-Bits Bitmap grayscale image, that 4-Bits bitmap only need to support 16 gray colors.
How can I do ?
Thanks in advance.
This the case,
I wanna use iOS6+ to convert a PNG image ( or JPEG ) to 4-Bits Bitmap grayscale image, that 4-Bits bitmap only need to support 16 gray colors.
How can I do ?
Thanks in advance.
You can convert it to an 8-bit grayscale image (see Convert UIImage to 8 bits, or see the link that Spynet shared which includes other permutations), but I was unable to adapt that to create 4-bit grayscale bitmap (CGBitmapContextCreate
complains that it's not a valid parameter combination). It seems like it should be theoretically possible (there are 4-bit formats listed under CVPixelBuffer
pixel format types), but I couldn't get it to work this this technique.
I found the solution to convert the Image to 4 Bits grayscale, code like this.
-(UIImage *)grayscaleImageAt4Bits:(UIImage *)_image
{
CGImageRef imageRef = [_image CGImage];
int width = CGImageGetWidth(imageRef);
int height = CGImageGetHeight(imageRef);
NSInteger _bitsPerComponent = 8;
NSInteger _bytesPerPixel = 1;
NSInteger _bytesPerRow = _bytesPerPixel * width;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
uint8_t *sourceData = malloc(height * width * _bytesPerPixel);
CGContextRef context = CGBitmapContextCreate(sourceData,
width,
height,
_bitsPerComponent,
_bytesPerRow,
colorSpace,
kCGImageAlphaNone);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
const int _redIndex = 0;
const int _greenIndex = 1;
const int _blueIndex = 2;
int _byteIndex = 0;
int _bitIndex = 0;
uint8_t *_newImageBits = malloc( height * width * _bytesPerPixel / 2 );
for(int j=0;j<height;j++)
{
for(int k=0;k<width;k++)
{
UInt8 _perPixel = _newImageBits[_byteIndex];
UInt8 *rgbPixel = (UInt8 *) &sourceData[j * width + k];
int _red = rgbPixel[_redIndex];
int _green = rgbPixel[_greenIndex];
int _blue = rgbPixel[_blueIndex];
int _pixelGrayValue = (_green + _red + _blue) / 16;
UInt8 _pixelByte = (UInt8)_pixelGrayValue;
_perPixel |= (_pixelByte<<4);
_newImageBits[_byteIndex] = _perPixel;
_bitIndex += 4;
if(_bitIndex > 7)
{
_byteIndex++;
_bitIndex = 0;
}
}
}
free(sourceData);
//Direct fetch Bytes of 4-Bits, then you can use this Bytes ( sourceData ) to do something.
sourceData = _newImageBits;
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage *_grayImage = [UIImage imageWithCGImage:cgImage];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
//Convert to NSData type, then you can use this NSData ( _sourceImageData ) to do something.
NSData *_sourceImageData = [NSData dataWithBytes:sourceData length:(width * height * _bytesPerPixel)];
free(sourceData);
return _grayImage;
}
And iOS only support the least 8 Bits color to display, so the 4-Bits data only transport to texture of openGL for using.