【问题标题】:Negative value color apply into Image负值颜色应用于图像
【发布时间】:2013-07-08 07:00:53
【问题描述】:

我想将 RGB 的负值应用到图像中。

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

我知道 RGB 的值在 0 到 255 之间,但我想应用负值并设置类似的偏移量,但不知道该怎么做。

我正在使用以下链接作为参考。

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

我正在使用

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

但它没有按我的意愿工作。

【问题讨论】:

  • 非正值无效
  • 是的,我知道,但基于我过去发布的链接,它的范围在 -255 到 255 之间,我不知道如何使用。
  • 是的,但似乎这家伙在使用之前使数字在 0 到 255 之间的范围内
  • 如果您设置负值,它将自动将所有不同的负值视为 0,例如 [CPTColor colorWithComponentRed:(-255.0/255.0) green:(-30.0/255.0) blue:(-70.0/255.0 ) alpha:1.0]] 它会给你黑色像 [CPTColor colorWithComponentRed:(0.0/255.0) green:(0.0/255.0) blue:(0.0/255.0) alpha:1.0]]
  • 你知道上面的链接是关于 JavaScript 的吗??

标签: ios colors uiimageview colortransform


【解决方案1】:

找到了一种使用负 RGB 值更改图像颜色的解决方案。以下是不影响图片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);

}

【讨论】:

    猜你喜欢
    • 2013-09-27
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    相关资源
    最近更新 更多