0

画像の明るさを変更する機能(openGLを使用せずに)を使用していますが、これはうまく機能します。

別の関数を使用して画像をグレースケールに変換しますが、これもうまく機能します。

しかし、それらを組み合わせると、グレースケール画像に明るさ関数を適用すると、画像に縞模様ができ、背景が透明 (アルファ 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