Objective-C to Swift Conversion Issue on CGBitmapInfo

782 views Asked by At

I have this Objective-C code that takes out the opaque background of a filter. I am trying to convert it to latest Swift and having errors all over the place.

-(UIImage*)removeColorFromImage:(UIImage*)sourceImage grayLevel:(int)grayLevel
{
    int width = sourceImage.size.width * sourceImage.scale;
    int height = sourceImage.size.height * sourceImage.scale;
    CGFloat scale = sourceImage.scale;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), sourceImage.CGImage);

    unsigned int *colorData = CGBitmapContextGetData(context);

    for (int i = 0; i < width * height; i++)
    {
        unsigned int color = *colorData;

        short a = color & 0xFF;
        short r = (color >> 8) & 0xFF;
        short g = (color >> 16) & 0xFF;
        short b = (color >> 24) & 0xFF;

        if ((r == grayLevel) && (g == grayLevel) && (b == grayLevel))
        {
            a = r = g = b = 0;
            *colorData = (unsigned int)(r << 8) + ((unsigned int)(g) << 16) + ((unsigned int)(b) << 24) + ((unsigned int)(a));
        }

        colorData++;
    }

    CGImageRef output = CGBitmapContextCreateImage(context);
    UIImage* retImage = [UIImage imageWithCGImage:output scale:scale orientation:UIImageOrientationUp];

    CGImageRelease(output);
    CGContextRelease(context);

    return retImage;
}

When I convert line by line. I've gotten far as this, yet having problems converting lines:

var context = CGBitmapContextCreate(nil, width, height, 8, width * 4, colorSpace, CGBitmapInfo(CGImageAlphaInfo.PremultipliedFirst.rawValue))

EDIT** This is how the errors look: enter image description here

1

There are 1 answers

0
Jordan On

I believe the following is a correct port of the original code:

func removeColorFromImage(sourceImage: UIImage, grayLevel: UInt32) -> UIImage? {
    let scale = sourceImage.scale
    let width = Int(sourceImage.size.width * scale)
    let height = Int(sourceImage.size.height * scale)
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.AlphaInfoMask.rawValue & CGImageAlphaInfo.PremultipliedFirst.rawValue)
    let context = CGBitmapContextCreate(nil, width, height, 8, width * 4, colorSpace, bitmapInfo.rawValue)
    CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), sourceImage.CGImage)

    let uncastedData = CGBitmapContextGetData(context)
    let colorData = UnsafeMutablePointer<UInt32>(uncastedData)
    for i in 0..<width * height {
        let color = colorData[i]
        var a = color & 0xFF
        var r = (color >> 8) & 0xFF
        var g = (color >> 16) & 0xFF
        var b = (color >> 24) & 0xFF
        if ((r == grayLevel) && (g == grayLevel) && (b == grayLevel)) {
            (a, r, g, b) = (0, 0, 0, 0)
            colorData[i] = (r << 8) + (g << 16) + (b << 24) + a
        }
        colorData.advancedBy(1)
    }

    if let output = CGBitmapContextCreateImage(context) {
        return UIImage(CGImage: output, scale: scale, orientation: UIImageOrientation.Up)
    }

    return nil
}

Here is an example (original -> output):

original output

removeColorFromImage(original, grayLevel: 175)

Explaining the changes

Since Swift 2 (I believe), you must use rawValue for the last parameter of CGBitmapContextCreate:

let context = CGBitmapContextCreate(nil, width, height, 8, width * 4, colorSpace, bitmapInfo.rawValue)

Also, in your port, you weren't using the original values from the Objective-C code:

// your new code was just using CGImageAlphaInfo.PremultipliedFirst.rawValue
// to match, it should be:
let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.AlphaInfoMask.rawValue & CGImageAlphaInfo.PremultipliedFirst.rawValue)

The bit shifting you were doing in the C-style for loop was on invalid types. It's on UInt32s now. I got rid of the C-style loop warning for you as well :)

Finally, you need to use this to initialize the finalized UIImage:

UIImage(CGImage: output, scale: scale, orientation: UIImageOrientation.Up)

The class function you were trying to use is not valid.