Question

I want to apply negative value of RGB into image.

R: -8
G: -89
B: -76

I know that the value of RGB is in between 0 to 255 but i want to apply minus value and set offset something like that but don't know how to do.

I am using following link for reference.

http://blog.swishzone.com/?p=9606

I am using

red value = (original red value * redMultiplier) + redOffset 
green value = (original green value * greenMultiplier) + greenOffset 
blue value = (original blue value * blueMultiplier) + blueOffset

But it not working as i want.

Was it helpful?

Solution

Found one solution for change image color using negative RGB values. Following is the method without affecting alpha of image.

-(void)ColorChangeProcessing :(int )redvalue greenValue:(int)greenvalue blueValue:(int)bluevalue imageUsed : (UIImageView *)image
{

    CGContextRef ctx;
    CGImageRef imageRef = [image.image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = malloc(height * width * 4);
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);

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

    int byteIndex = (bytesPerRow * 0) + 0 * bytesPerPixel,RED = redvalue,GREEN=greenvalue,BLUE = bluevalue;


    for (int ii = 0 ; ii < width * height ; ++ii)
    {
        if( rawData[byteIndex+3] != 0 &&  (rawData[byteIndex] != '/0' || rawData[byteIndex+1] != '/0' || rawData[byteIndex+2] != '/0'  ))
        {


            if ((((rawData[byteIndex])+RED)) > 255)
            {
                rawData[byteIndex] = (char)255;
            }
            else if((((rawData[byteIndex])+RED)) >0)
            {
                rawData[byteIndex] = (char) (((rawData[byteIndex] * 1.0) + RED));
            }
            else
            {
                rawData[byteIndex] = (char)0;
            }


            if ((((rawData[byteIndex+1])+GREEN)) > 255)
            {
                rawData[byteIndex+1] = (char)255;
            }
            else if((((rawData[byteIndex+1])+GREEN))>0)
            {
                rawData[byteIndex+1] = (char) (((rawData[byteIndex+1] * 1.0) + GREEN));


            }
            else
            {
                rawData[byteIndex+1] = (char)0;
            }



            if ((((rawData[byteIndex+2])+BLUE)) > 255)
            {
                rawData[byteIndex+2] = (char)255;
            }
            else if((((rawData[byteIndex+2])+BLUE))>0)
            {
                rawData[byteIndex+2] = (char) (((rawData[byteIndex+2] * 1.0) + BLUE));


            }
            else
            {
                rawData[byteIndex+2] = (char)0;
            }
        }
        byteIndex += 4;
    }

    ctx = CGBitmapContextCreate(rawData,
                                CGImageGetWidth( imageRef ),
                                CGImageGetHeight( imageRef ),
                                8,
                                CGImageGetBytesPerRow( imageRef ),
                                CGImageGetColorSpace( imageRef ),
                                kCGImageAlphaPremultipliedLast );

    CGImageRef NewimageRef = CGBitmapContextCreateImage (ctx);
    UIImage* rawImage = [UIImage imageWithCGImage:NewimageRef];



    tempViewForProcessingColors = [[UIImageView alloc] init];
    tempViewForProcessingColors = [[arrayWithDictionary objectAtIndex:ImageCount]valueForKey:@"imageView"];

//    NSLog(@"Name: %@ --- Red: %d --- Green: %d --- Blue: %d",tempViewForProcessingColors.accessibilityLabel,RED,GREEN,BLUE);
   tempViewForProcessingColors.image = rawImage;
    //ImageCount++;
    CGContextRelease(ctx);
    free(rawData);
    CGImageRelease(NewimageRef);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top