1、黑白化
if (红色>120 || 绿色>120 ||蓝>120 ){
变成黑色
}
else{
变成白色
}
- (UIImage *)whiteBlackImage{
CGImageRef imImage = self.CGImage;
CFDataRef mDataRef = CGDataProviderCopyData(CGImageGetDataProvider(imImage));
UInt8 *mPixelBuf = (UInt8 *)CFDataGetBytePtr(mDataRef);
CFIndex length = CFDataGetLength(mDataRef);
for(int i=0;i<length;i+=4){
int r = i;
int g = i+1;
int b = i+2;
int red1 = mPixelBuf[r];
int green1 = mPixelBuf[g];
int blue1 = mPixelBuf[b];
if (red1 < 120 || green1 < 120 || blue1 < 120) {
mPixelBuf[r] = 0;
mPixelBuf[g] = 0;
mPixelBuf[b] = 0;
}
else{
mPixelBuf[r] = 255;
mPixelBuf[g] = 255;
mPixelBuf[b] = 255;
}
}
CGContextRef ctx = CGBitmapContextCreate(mPixelBuf,
CGImageGetWidth(imImage),
CGImageGetHeight(imImage),
CGImageGetBitsPerComponent(imImage),
CGImageGetBytesPerRow(imImage),
CGImageGetColorSpace(imImage),
CGImageGetBitmapInfo(imImage)
);
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CFRelease(mDataRef);
return finalImage;
}
2、海报化
素像点颜色红绿蓝都乘以Oxe0
/**
* 海报化
*/
- (UIImage *)CBMPImage{
CGImageRef imImage = self.CGImage;
CFDataRef mDataRef = CGDataProviderCopyData(CGImageGetDataProvider(imImage));
UInt8 *mPixelBuf = (UInt8 *)CFDataGetBytePtr(mDataRef);
CFIndex length = CFDataGetLength(mDataRef);
for(int i=0;i<length;i+=4){
int r = i;
int g = i+1;
int b = i+2;
int red1 = mPixelBuf[r];
int green1 = mPixelBuf[g];
int blue1 = mPixelBuf[b];
mPixelBuf[r] = red1&0xe0;
mPixelBuf[g] = green1&0xe0;
mPixelBuf[b] = blue1&0xe0;
}
CGContextRef ctx = CGBitmapContextCreate(mPixelBuf,
CGImageGetWidth(imImage),
CGImageGetHeight(imImage),
CGImageGetBitsPerComponent(imImage),
CGImageGetBytesPerRow(imImage),
CGImageGetColorSpace(imImage),
CGImageGetBitmapInfo(imImage)
);
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CFRelease(mDataRef);
return finalImage;
}
三、高斯模糊
/**
* 高斯模糊
*/
- (UIImage *)GaussianSmoothImage{
CGImageRef imImage = self.CGImage;
CFDataRef mDataRef = CGDataProviderCopyData(CGImageGetDataProvider(imImage));
CFDataRef m_OutDataRef = CGDataProviderCopyData(CGImageGetDataProvider(imImage));
UInt8 *mPixelBuf = (UInt8 *)CFDataGetBytePtr(mDataRef);
UInt8 *mOutPixelBuf = (UInt8 *) CFDataGetBytePtr(m_OutDataRef);
//CFIndex length = CFDataGetLength(mDataRef);
int width = (int)CGImageGetWidth(imImage);
int height = (int)CGImageGetHeight(imImage);
int templates[25] = {
1, 4, 7, 4, 1,
4, 16, 26, 16, 4,
7, 26, 41, 26, 7,
4, 16, 26, 16, 4,
1, 4, 7, 4, 1 };
int rSum,gSum,bSum;
int index=0;
for(int i= 2;i<height-2;i++){
for(int j= 2;j<width-2;j++){
rSum=0;
gSum=0;
bSum=0;
index=0;
for(int zi=i-2;zi<i+3;zi++){
for(int zj=j-2;zj<j+3;zj++){
int s = templates[index++];
int tindex = zi*height*4+zj*4;
rSum += mPixelBuf[tindex]*s;
gSum += mPixelBuf[tindex+1]*s;
bSum += mPixelBuf[tindex+2]*s;
}
}
rSum/=273;
gSum/=273;
bSum/=273;
rSum = rSum>255?255:rSum;
gSum = gSum>255?255:gSum;
bSum = bSum>255?255:bSum;
index = i*4*width+j*4;
mOutPixelBuf[index]=rSum;
mOutPixelBuf[index+1]=gSum;
mOutPixelBuf[index+2]=bSum;
}
}
CGContextRef ctx = CGBitmapContextCreate(mOutPixelBuf,
CGImageGetWidth(imImage),
CGImageGetHeight(imImage),
CGImageGetBitsPerComponent(imImage),
CGImageGetBytesPerRow(imImage),
CGImageGetColorSpace(imImage),
CGImageGetBitmapInfo(imImage)
);
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CFRelease(mDataRef);
return finalImage;
}