美文网首页
图片滤镜 之 CoreGraphics

图片滤镜 之 CoreGraphics

作者: 小凡凡520 | 来源:发表于2019-10-08 09:22 被阅读0次
    一、将UIImage转化为像素数据
    #pragma mark - 将UIImage转化为像素数据
    - (unsigned char *)convertUIImageToData:(UIImage *)image{
        CGSize size = image.size;
        
        void *data = malloc(size.width * size.height * 4);
        
        CGImageRef imageRef = [image CGImage];
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();//RGBA
        CGContextRef contextRef = CGBitmapContextCreate(data, size.width, size.height, 8, 4 * size.width, colorSpaceRef, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        CGContextDrawImage(contextRef, CGRectMake(0, 0, size.width, size.height), imageRef);
        
        //mrc
        CGColorSpaceRelease(colorSpaceRef);
        CGContextRelease(contextRef);
        
        return (unsigned char *)data;
    }
    
    
    二、对像素进行灰度处理
    #pragma mark - 对像素进行灰度处理
    - (unsigned char *)grayImageWithData:(unsigned char *)imageData originalImage:(UIImage *)originalImage{
        CGFloat width = originalImage.size.width;
        CGFloat height = originalImage.size.height;
        unsigned char *resultData = malloc(width * height * sizeof(unsigned char) * 4);
        
        memset(resultData, 0, sizeof(unsigned char) * 4);
        
        for (int h = 0; h < height; h++) {
            for (int w = 0; w < width; w++) {
                unsigned int imageIndex = h*width + w;
                
                unsigned char bitMapRed = *(imageData + imageIndex*4);
                unsigned char bitMapGreen = *(imageData + imageIndex*4 + 1);
                unsigned char bitMapBlue = *(imageData + imageIndex*4 + 2);
                
                int bitMap = bitMapRed * 77 / 255 + bitMapGreen * 151 / 255 + bitMapBlue * 88 / 255;
                unsigned char newBitMap = (bitMap > 255) ? 255 : bitMap;
                memset(resultData + imageIndex*4, newBitMap, 1);
                memset(resultData + imageIndex*4 + 1, newBitMap, 1);
                memset(resultData + imageIndex*4 + 2, newBitMap, 1);
                
            }
        }
        
        return resultData;
    }
    
    三、对像素进行颜色反转处理
    #pragma mark - 对像素进行颜色反转处理
    - (unsigned char *)reColorImageWithData:(unsigned char *)imageData originalImage:(UIImage *)originalImage{
        CGFloat width = originalImage.size.width;
        CGFloat height = originalImage.size.height;
        unsigned char *resultData = malloc(width * height * sizeof(unsigned char) * 4);
        
        memset(resultData, 0, sizeof(unsigned char) * 4);
        
        for (int h = 0; h < height; h++) {
            for (int w = 0; w < width; w++) {
                unsigned int imageIndex = h*width + w;
                
                unsigned char bitMapRed = *(imageData + imageIndex*4);
                unsigned char bitMapGreen = *(imageData + imageIndex*4 + 1);
                unsigned char bitMapBlue = *(imageData + imageIndex*4 + 2);
                unsigned char bitMapRedRe = 255 - bitMapRed;
                unsigned char bitMapGreenRe = 255 - bitMapGreen;
                unsigned char bitMapBlueRe = 255 - bitMapBlue;
                
                memset(resultData + imageIndex*4, bitMapRedRe, 1);
                memset(resultData + imageIndex*4 + 1, bitMapGreenRe, 1);
                memset(resultData + imageIndex*4 + 2, bitMapBlueRe, 1);
                
            }
        }
        
        return resultData;
    }
    
    
    四、将像素数据转化为UIImage
    #pragma mark - 将像素数据转化为UIImage
    - (UIImage *)convertDataToUIImage:(unsigned char *)imageData image:(UIImage *)imageSource{
        CGFloat width = imageSource.size.width;
        CGFloat height = imageSource.size.height;
        CGFloat dataLength = width * height * 4;
        
        CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData(NULL, imageData, dataLength, NULL);
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        
        CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4*width, colorSpaceRef, kCGBitmapByteOrderDefault, dataProviderRef, NULL, NO, kCGRenderingIntentDefault);
        UIImage *image = [UIImage imageWithCGImage:imageRef];
        
        //mrc
        CGImageRelease(imageRef);
        CGDataProviderRelease(dataProviderRef);
        CGColorSpaceRelease(colorSpaceRef);
        
        return image;
    }
    
    -(UIImage *)filterImage:(UIImage *)CGImage{
        
        CGImageRef imageRef = CGImage.CGImage;
        // 1 个字节 = 8bit  每行有 17152 每行有17152*8 位
        size_t width   = CGImageGetWidth(imageRef);
        size_t height  = CGImageGetHeight(imageRef);
        size_t bits    = CGImageGetBitsPerComponent(imageRef); // 8
        size_t bitsPerrow = CGImageGetBytesPerRow(imageRef); // width * bits
        
        // 颜色空间
        CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
        // AlphaInfo: RGBA  AGBR  RGB  :AlphaInfo 信息
        CGImageAlphaInfo alpInfo =  CGImageGetAlphaInfo(imageRef);
        
        // bitmap的数据
        CGDataProviderRef providerRef = CGImageGetDataProvider(imageRef);
        CFDataRef bitmapData = CGDataProviderCopyData(providerRef);
        
        NSInteger pixLength = CFDataGetLength(bitmapData);
        // 像素byte数组
        Byte *pixbuf = CFDataGetMutableBytePtr((CFMutableDataRef)bitmapData);
        
        // RGBA 为一个单元
        for (int i = 0; i < pixLength; i+=4) {
            
            [self eocImageFiletPixBuf:pixbuf offset:i];
        }
        
        // 准备绘制图片了
        // bitmap 生成一个上下文  再通过上下文生成图片
        CGContextRef contextR = CGBitmapContextCreate(pixbuf, width, height, bits, bitsPerrow, colorSpace, alpInfo);
        
        CGImageRef filterImageRef = CGBitmapContextCreateImage(contextR);
        
        UIImage *filterImage =  [UIImage imageWithCGImage:filterImageRef];
        
        return filterImage;
    }
    
    // RGBA 为一个单元  彩色照变黑白照
    - (void)eocImageFiletPixBuf:(Byte*)pixBuf offset:(int)offset{
        
        int offsetR = offset;
        int offsetG = offset + 1;
        int offsetB = offset + 2;
        // int offsetA = offset + 3;
        
        int red = pixBuf[offsetR];
        int gre = pixBuf[offsetG];
        int blu = pixBuf[offsetB];
        // int alp = pixBuf[offsetA];
        
        int gray = (red + gre + blu)/3;
        
        pixBuf[offsetR] = gray;
        pixBuf[offsetG] = gray;
        pixBuf[offsetB] = gray;
        
    }
    

    相关文章

      网友评论

          本文标题:图片滤镜 之 CoreGraphics

          本文链接:https://www.haomeiwen.com/subject/hdhnuctx.html