简体   繁体   中英

how to change color of part of UIImage?

I have one image which is in grayscale and I am applying it's original color in some part of that image and I have achieved it. Now I want to change color of that part in which I have applied original color in image

I have this: Original Image

I want to convert in this: Result Image

CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);

NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;

NSUInteger bytesCount = height * width * bytesPerPixel;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char *)calloc(bytesCount, sizeof(unsigned char));
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

unsigned char *outputData = (unsigned char *)calloc(bytesCount, sizeof(unsigned char));

NSUInteger byteIndex = 0;
for (NSUInteger i=0; i<bytesCount / bytesPerPixel; ++i) {
    CGFloat red = (CGFloat)rawData[byteIndex];
    CGFloat green = (CGFloat)rawData[byteIndex+1];
    CGFloat blue = (CGFloat)rawData[byteIndex+2];
    CGFloat alpha = (CGFloat)rawData[byteIndex+3];

    BOOL grayscale = red == green == blue;

    if (!grayscale) {
        // test for near values
        CGFloat diff = MAX(ABS(red-green), MAX(ABS(red-blue), ABS(green-blue)));

        static CGFloat allowedDifference = 100; // in range of 0-255

        if (diff > allowedDifference) {

//                CGFloat redTemp = 236;
//                red = green;
//                green = redTemp;
            red = 236.0;
            green = 17.0;
            blue = 17.0;

        }
    }

    outputData[byteIndex] = red;
    outputData[byteIndex+1] = green;
    outputData[byteIndex+2] = blue;
    outputData[byteIndex+3] = alpha;

    byteIndex += bytesPerPixel;
}

free(rawData);


CGDataProviderRef outputDataProvider = CGDataProviderCreateWithData(NULL,
                                                                    outputData,
                                                                    bytesCount,
                                                                    NULL);
free(outputData);

CGImageRef outputImageRef = CGImageCreate(width,
                                          height,
                                          bitsPerComponent,
                                          bytesPerPixel * 8,
                                          bytesPerRow,
                                          colorSpace,
                                          kCGBitmapByteOrderDefault,
                                          outputDataProvider,
                                          NULL,NO,
                                          kCGRenderingIntentDefault);


CGColorSpaceRelease(colorSpace);
CGDataProviderRelease(outputDataProvider);

UIImage *outputImage = [UIImage imageWithCGImage:outputImageRef];
CGImageRelease(outputImageRef);

I tried bitmapcontext and everything, but not getting desired result.

Does anyone have idea ?

You can try grabbing pixel data from an image by using CGBitmapContextCreate to create a color space, then draw an image to it via CGContextDrawImage.

Secondly, you will receive an array of bytes of one dimension. Like this: [r1, g1, b1, a1, r2, g2, b2, a2, ...] where r,g,b,a - color components, 1,2 - nu. of pixel.

After this, you can iterate over the array and compare each pixel's color components. Since you should skip grayscale pixels, you need to compare rgb params and they theoretically must be equal, but you can also support some little errors in few digits +-.

And if concrete pixel is not grayscale, just swap red and green bytes.

Should be the way to go.

Updated with example:

UIImage *image = [UIImage imageNamed:@"qfjsc.png"];

CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);

NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;

NSUInteger bytesCount = height * width * bytesPerPixel;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char *)calloc(bytesCount, sizeof(unsigned char));
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

unsigned char *outputData = (unsigned char *)calloc(bytesCount, sizeof(unsigned char));

NSUInteger byteIndex = 0;
for (NSUInteger i=0; i<bytesCount / bytesPerPixel; ++i) {
    CGFloat red = (CGFloat)rawData[byteIndex];
    CGFloat green = (CGFloat)rawData[byteIndex+1];
    CGFloat blue = (CGFloat)rawData[byteIndex+2];
    CGFloat alpha = (CGFloat)rawData[byteIndex+3];

    BOOL grayscale = red == green == blue;

    if (!grayscale) {
        // test for near values
        CGFloat diff = MAX(ABS(red-green), MAX(ABS(red-blue), ABS(green-blue)));

        static CGFloat allowedDifference = 50.0; // in range of 0-255

        if (diff > allowedDifference) {

            CGFloat redTemp = red;
            red = green;
            green = redTemp;

        }
    }

    outputData[byteIndex] = red;
    outputData[byteIndex+1] = green;
    outputData[byteIndex+2] = blue;
    outputData[byteIndex+3] = alpha;

    byteIndex += bytesPerPixel;
}

free(rawData);


CGDataProviderRef outputDataProvider = CGDataProviderCreateWithData(NULL,
                                                                    outputData,
                                                                    bytesCount,
                                                                    NULL);
free(outputData);

CGImageRef outputImageRef = CGImageCreate(width,
                                          height,
                                          bitsPerComponent,
                                          bytesPerPixel * 8,
                                          bytesPerRow,
                                          colorSpace,
                                          kCGBitmapByteOrderDefault,
                                          outputDataProvider,
                                          NULL,NO,
                                          kCGRenderingIntentDefault);


CGColorSpaceRelease(colorSpace);
CGDataProviderRelease(outputDataProvider);

UIImage *outputImage = [UIImage imageWithCGImage:outputImageRef];
CGImageRelease(outputImageRef);

Note the static allowed difference variable. It allows you to skip almost non grayscale pixels, but which are in RGB color space and almost grayscale by its nature.

Here are examples:

  1. Allowed difference = 0 允许差异 = 0
  2. Allowed difference = 50 允许差异 = 50

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM