简体   繁体   English

负值颜色应用于图像

[英]Negative value color apply into Image

I want to apply negative value of RGB into image. 我想将RGB的负值应用到图像中。

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. 我知道RGB的值在0到255之间,但是我想应用减值并设置偏移量之类的方法,但是不知道该怎么做。

I am using following link for reference. 我正在使用以下链接作为参考。

http://blog.swishzone.com/?p=9606 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. 但是它没有按我的意愿工作。

Found one solution for change image color using negative RGB values. 找到了一种使用负RGB值更改图像颜色的解决方案。 Following is the method without affecting alpha of image. 以下是不影响图像Alpha值的方法。

-(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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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