0

我使用一个函数来改变图片的亮度(不使用openGL),效果很好。

我使用另一个函数将我的图像转换为灰度,这也很有效。

但是当我组合它们时,当我在灰度图像上应用我的亮度函数时,我在图像上得到了一些条纹,当背景是透明的(alpha 0)时,它被黑色背景取代。你有什么想法吗?

请在下面找到我的灰度函数和亮度函数:

// ## Brightness without OpenGL call
+(UIImage *) changeImageBrightness:(UIImage *)aInputImage withFactor:(float)aFactor
{
    CGImageRef img=aInputImage.CGImage;
    CFDataRef dataref = CGDataProviderCopyData(
                  CGImageGetDataProvider(aInputImage.CGImage));

    int length=CFDataGetLength(dataref);
    UInt8 *data=(UInt8 *)CFDataGetBytePtr(dataref);

    // Perform operation on pixels
    for(int index=0;index<length;index+=4) {
        // Go For BRIGHTNESS
        for(int i=0;i<3;i++) {
            if(data[index+i]+aFactor<0) {
                data[index+i]=0;
            } else {
                if(data[index+i]+aFactor>255) {
                    data[index+i]=255;
                } else {
                    data[index+i]+=aFactor;
                }
            }
        }
    }

    // .. Take image attributes
    size_t width=CGImageGetWidth(img);
    size_t height=CGImageGetHeight(img);
    size_t bitsPerComponent=CGImageGetBitsPerComponent(img);
    size_t bitsPerPixel=CGImageGetBitsPerPixel(img);
    size_t bytesPerRow=CGImageGetBytesPerRow(img);

    // .. Do the pixel manupulation
    CGColorSpaceRef colorspace=CGImageGetColorSpace(img);
    CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(img);
    CFDataRef newData=CFDataCreate(NULL,data,length);
    CGDataProviderRef provider=CGDataProviderCreateWithCFData(newData);

    // .. Get the Image out of this raw data
    CGImageRef newImg = CGImageCreate(width, height, bitsPerComponent,
                bitsPerPixel, bytesPerRow, colorspace, bitmapInfo, provider,
                                      NULL, true, kCGRenderingIntentDefault);

    // .. Prepare the image from raw data
    UIImage* rawImage = [[UIImage alloc] initWithCGImage:newImg];

    // .. done with all,so release the references
    CFRelease(newData);
    CGImageRelease(newImg);
    CGDataProviderRelease(provider);
    CFRelease(dataref);
   // return rawImage.CGImage;
    UIImage *imageApresFiltreEtRotationCGI = [UIImage
                        imageWithCGImage:rawImage.CGImage];
    return imageApresFiltreEtRotationCGI;
}

+ (UIImage *)convertImageToGrayScale:(UIImage *)image
{
    // Create image rectangle with current image width/height
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
    // Grayscale color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    // Create bitmap content with current image size and grayscale colorspace
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width,
                 image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
    // Draw image into current context, with specified rectangle
    // using previously defined context (with grayscale colorspace)
    CGContextDrawImage(context, imageRect, [image CGImage]);
    /* changes start here */
    // Create bitmap image info from pixel data in current context
    CGImageRef grayImage = CGBitmapContextCreateImage(context);
    // release the colorspace and graphics context
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    // make a new alpha-only graphics context
    context = CGBitmapContextCreate(nil, image.size.width,
                 image.size.height, 8, 0, nil, kCGImageAlphaOnly);
    // draw image into context with no colorspace
    CGContextDrawImage(context, imageRect, [image CGImage]);
    // create alpha bitmap mask from current context
    CGImageRef mask = CGBitmapContextCreateImage(context);
    // release graphics context
    CGContextRelease(context);
    // make UIImage from grayscale image with alpha mask
    UIImage *grayScaleImage = [UIImage imageWithCGImage:
             CGImageCreateWithMask(grayImage, mask) scale:image.scale
                                       orientation:image.imageOrientation];
    // release the CG images
    CGImageRelease(grayImage);
    CGImageRelease(mask);
    // return the new grayscale image
    return grayScaleImage;
    /* changes end here */
}

这是我得到的图片。

http://ch4v.atopiq.net/background-php/IMG_0143.JPG

在我进行灰度/比例和光线转换之前,婴儿背后的背景是透明的。

4

0 回答 0