Search code examples
iosswiftcore-graphics

Can't get bytes for a non-square image


I use a method introduced in this post, modified for Swift:

func getRaster() -> [UIColor] {
    let result = NSMutableArray()
    
    let img = self.CGImage
    let width = CGImageGetWidth(img)
    let height = CGImageGetHeight(img)
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    
    var rawData = [UInt8](count: width * height * 4, repeatedValue: 0)
    let bytesPerPixel = 4
    let bytesPerRow = bytesPerPixel * width
    let bytesPerComponent = 8
    
    let bitmapInfo = CGImageAlphaInfo.PremultipliedLast.rawValue | CGBitmapInfo.ByteOrder32Big.rawValue
    let context = CGBitmapContextCreate(&rawData, width, height, bytesPerComponent, bytesPerRow, colorSpace, bitmapInfo)
    
    CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), img);
    for x in 0..<width {
        for y in 0..<height {
            let byteIndex = (bytesPerRow * x) + y * bytesPerPixel
            
            let red   = CGFloat(rawData[byteIndex]    ) / 255.0
            let green = CGFloat(rawData[byteIndex + 1]) / 255.0
            let blue  = CGFloat(rawData[byteIndex + 2]) / 255.0
            let alpha = CGFloat(rawData[byteIndex + 3]) / 255.0
            
            let color = UIColor(red: red, green: green, blue: blue, alpha: alpha)
            result.addObject(color)
        }
    }
    
    return (result as NSArray) as! [UIColor]
}

But the problem is that it only ever succeeds if the image is square (i.e. 32x32 sprite), whenever I try to get a "raster" of an image, which dimensions are not equal, I get an "Index out of range" fatal error for red on x = 16 and y = 0 (for image of size h: 16, w: 32). What could be a solution for this problem?

Thank you in advance!


Solution

  • It was pretty easy to solve, actually, here's the problem part:

    for x in 0..<width {
        for y in 0..<height {
            let byteIndex = (bytesPerRow * x) + y * bytesPerPixel
    

    And here's how it's supposed to be:

    for y in 0..<height {
            for x in 0..<width {
                let byteIndex = (bytesPerRow * y) + x * bytesPerPixel
    

    I do not think this needs any further explanation.