ios 將彩色照片轉化成黑白等幾種類型

來源:互聯網
上載者:User

標籤:

-(UIImage *)changeColoursImageTograyScaleImage:(UIImage *)anImage type:(int)type

{

    CGImageRef imageRef = anImage.CGImage;

    

    size_t width                   = CGImageGetWidth(imageRef);

    size_t height                  = CGImageGetHeight(imageRef);

    size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);

    size_t bitsPerPixel           = CGImageGetBitsPerPixel(imageRef);

    size_t bytesPerRow         = CGImageGetBytesPerRow(imageRef);

    

    CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);

    CGBitmapInfo bitMapInfo      = CGImageGetBitmapInfo(imageRef);

    

    bool shouldInterPolate               = CGImageGetShouldInterpolate(imageRef);

    CGColorRenderingIntent intent   = CGImageGetRenderingIntent(imageRef);

    CGDataProviderRef dataProvider = CGImageGetDataProvider(imageRef);

    CFDataRef data                         = CGDataProviderCopyData(dataProvider);

    UInt8 *buffer                            = (UInt8  *)CFDataGetBytePtr(data);

    

    NSInteger x,y;

    for (y = 0; y < height; y++) {

        for (x = 0; x < width; x++) {

            UInt8 *tmp;

            tmp = buffer + y * bytesPerRow + x * 4;

            

            UInt8 red,green,blue;

            red     = *(tmp + 0);

            green = *(tmp + 1);

            blue   = *(tmp + 2);

            

            UInt8 brightness;

            switch (type) {

                case 0:

                    brightness = (77 * red + 28 * green + 151 * blue) / 256;

                    *(tmp + 0) = brightness;

                    *(tmp + 1) = brightness;

                    *(tmp + 1) = brightness;

                    break;

                case 1:

                    brightness = (77 * red + 28 * green + 151 * blue) / 256;

                    *(tmp + 0) = red;

                    *(tmp + 1) = green * 0.7;

                    *(tmp + 1) = blue *0.4;

                    break;

                case 2:

                    brightness = (77 * red + 28 * green + 151 * blue) / 256;

                    *(tmp + 0) = 255 - red;

                    *(tmp + 1) = 255 - green;

                    *(tmp + 1) = 255 - blue;

                    break;

                    

                default:

                    *(tmp + 0) = red;

                    *(tmp + 1) = green;

                    *(tmp + 2) = blue;

                    break;

            }

            

        }

    }

    CFDataRef effectedData = CFDataCreate(NULL, buffer, CFDataGetLength(data));

    CGDataProviderRef effectedDataProvider = CGDataProviderCreateWithCFData(effectedData);

    CGImageRef effectedCGImage = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitMapInfo, effectedDataProvider, NULL, shouldInterPolate, intent);

    UIImage *effectedImage = [[UIImage alloc]initWithCGImage:effectedCGImage];

    CGImageRelease(effectedCGImage);

    CFRelease(effectedDataProvider);

    CFRelease(effectedData);

    CFRelease(data);

    

    

    return effectedImage;

}

ios 將彩色照片轉化成黑白等幾種類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.