我有一个应用程序需要在图像过滤器上做一些工作,所以我刚开始研究过滤器,我实现了一些过滤器,比如黑白、Hud、苔藓、亮度、对比度等。
这里有几个我面临的问题要实现的过滤器
- 怀旧
- 复古
- 旧
原始图像
怀旧
复古
旧
如果有人能指导我正确的方向,那就太好了。
提前致谢。
最佳答案
This线程非常有用关于 iphone 中的图像过滤的信息...
编辑
这是我为制作复古图像所做的工作(在我的案例中,复古图像要求略有不同..它的边缘没有阴影)
//first increase contrast a bit...
float contrastValue = 1.45;
CGImageRef originalImage = [myImage CGImage];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(NULL,CGImageGetWidth(originalImage),CGImageGetHeight(originalImage),8,CGImageGetWidth(originalImage)*4,colorSpace,kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGBitmapContextGetWidth(bitmapContext), CGBitmapContextGetHeight(bitmapContext)), originalImage);
UInt8* data = CGBitmapContextGetData(bitmapContext);
int numComponents = 4;
int bytesInContext = CGBitmapContextGetHeight(bitmapContext) * CGBitmapContextGetBytesPerRow(bitmapContext);
double redIn, greenIn, blueIn;
for (int i = 0; i < bytesInContext; i += numComponents) {
redIn = (double)data[i]/255.0;
greenIn = (double)data[i+1]/255.0;
blueIn = (double)data[i+2]/255.0;
redIn -= 0.5;
redIn *= contrastValue;
redIn += 0.5;
redIn *= 255.0;
if (redIn < 0) {
redIn = 0;
}
if (redIn > 255) {
redIn = 255;
}
greenIn -= 0.5;
greenIn *= contrastValue;
greenIn += 0.5;
greenIn *= 255.0;
if (greenIn < 0) {
greenIn = 0;
}
if (greenIn > 255) {
greenIn = 255;
}
blueIn -= 0.5;
blueIn *= contrastValue;
blueIn += 0.5;
blueIn *= 255.0;
if (blueIn < 0) {
blueIn = 0;
}
if (blueIn > 255) {
blueIn = 255;
}
data[i] = (redIn);
data[i+1] = (greenIn);
data[i+2] = (blueIn);
}
CGImageRef outImage = CGBitmapContextCreateImage(bitmapContext);
myImage = [UIImage imageWithCGImage:outImage];
CGImageRelease(outImage);
//Then blend it with a yellowish color
UIGraphicsBeginImageContext(myImage.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, myImage.size.width, myImage.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSaveGState(ctx);
CGContextClipToMask(ctx, area, myImage.CGImage);
UIColor *color = [UIColor colorWithRed:248.0/255.0 green:254.0/255.0 blue:186.0/255.0 alpha:1.0]; //[UIColor colorWithRed:248.0/255.0 green:254.0/255.0 blue:186.0/255.0 alpha:1.0]
[color set];
CGContextFillRect(ctx, area);
CGContextRestoreGState(ctx);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextDrawImage(ctx, area, myImage.CGImage);
myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(myImage.size);
ctx = UIGraphicsGetCurrentContext();
area = CGRectMake(0, 0, myImage.size.width, myImage.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSaveGState(ctx);
CGContextClipToMask(ctx, area, myImage.CGImage);
color = [UIColor colorWithRed:27.0/255.0 green:50.0/255.0 blue:224.0/255.0 alpha:0.2]; //[UIColor colorWithRed:248.0/255.0 green:254.0/255.0 blue:186.0/255.0 alpha:1.0]
[color set];
CGContextFillRect(ctx, area);
CGContextRestoreGState(ctx);
CGContextSetBlendMode(ctx, kCGBlendModeLighten);
CGContextDrawImage(ctx, area, myImage.CGImage);
processedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
这可能不适合你..请根据你自己的要求玩这个..从内存中复制,可能有错误..
关于ios - 图像过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7885089/