iOS-如何将png图像转换为4位位图灰度?
这是事实, 我想使用iOS6+将PNG图像或JPEG转换为4位位图灰度图像,4位位图只需要支持16种灰度 我该怎么办iOS-如何将png图像转换为4位位图灰度?,ios,bitmap,grayscale,Ios,Bitmap,Grayscale,这是事实, 我想使用iOS6+将PNG图像或JPEG转换为4位位图灰度图像,4位位图只需要支持16种灰度 我该怎么办 提前感谢。您可以将其转换为8位灰度图像查看,或查看其中包含其他排列,但我无法将其调整为创建4位灰度位图CGBitmapContextCreate,并抱怨它不是有效的参数组合。从理论上讲,CVPixelBuffer下可能会列出4位格式,但我无法让它使用这种技术。我找到了将图像转换为4位灰度的解决方案,代码如下 -(UIImage *)grayscaleImageAt4Bits:(U
提前感谢。您可以将其转换为8位灰度图像查看,或查看其中包含其他排列,但我无法将其调整为创建4位灰度位图CGBitmapContextCreate,并抱怨它不是有效的参数组合。从理论上讲,CVPixelBuffer下可能会列出4位格式,但我无法让它使用这种技术。我找到了将图像转换为4位灰度的解决方案,代码如下
-(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;
}
而iOS只支持至少8位的颜色显示,所以4位的数据只传输到openGL的纹理中使用 谢谢,我会检查代码是否有意义。谢谢你的回答,我会检查。iOS 8现在发出警告:从枚举类型“enum CGImageAlphaInfo”隐式转换为不同的枚举类型“CGBitmapInfo”又名“enum CGBitmapInfo”