Johann Burgess
Johann Burgess

Reputation: 606

Swift - Reading .jpg image for pixel color returning incorrect values

I'm trying to get the UIColor of each pixel from a jpg image in swift, the following code successfully gets each pixel color however the results are confusing me:

func renderLandscape() {

    var landscapeImage = UIImage(named: "test")

    for var xPos = 0 as CGFloat; xPos < landscapeImage?.size.width; xPos++ {
        for var yPos = 0 as CGFloat; yPos < landscapeImage?.size.height; yPos++ {
            var currentColor = landscapeImage?.getPixelColor(CGPointMake(xPos, yPos))
            NSLog("Color: %@", currentColor!)
        }
    }
}

I've created an extension of UIImage here:

extension UIImage {
    func getPixelColor(pos: CGPoint) -> UIColor {

        var pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage))
        var data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

        var pixelInfo: Int = ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4

        //NSLog("Data: %@", data.encode())

        var r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
        var g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
        var b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
        var a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)

        return UIColor(red: r, green: g, blue: b, alpha: a)
    }  
}

Obviously the output is extremely long however here is the first 41 lines (the first column of the image):

2015-05-12 22:21:58.793 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.793 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.795 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.795 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.795 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.797 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.798 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.798 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.798 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.798 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.798 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.799 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.799 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.799 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.799 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.799 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.814 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.814 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.814 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.814 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.814 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.814 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.815 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.815 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.815 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.815 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0.00392157 0 1
2015-05-12 22:21:58.815 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0.00392157 0 1
2015-05-12 22:21:58.830 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0.0470588 0 1
2015-05-12 22:21:58.831 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0.141176 0 1
2015-05-12 22:21:58.831 Game[6542:220967] Color: UIDeviceRGBColorSpace 0.627451 0.694118 0.317647 1
2015-05-12 22:21:58.832 Game[6542:220967] Color: UIDeviceRGBColorSpace 0.756863 0.121569 0 1
2015-05-12 22:21:58.832 Game[6542:220967] Color: UIDeviceRGBColorSpace 0.996078 0 0 1
2015-05-12 22:21:58.832 Game[6542:220967] Color: UIDeviceRGBColorSpace 0.996078 0.00392157 0 1

The fist 32 lines are UIDeviceRGBColorSpace 0 0 0 1, which is correct (Black) however after that there should be four more black pixels a green (#2aff00) and three red (#ff0000) except there are 8 non black pixels, each with a different color value.

Here is the image which was created in Photoshop:

enter image description here

Why aren't any of the red colors the same in RGB and why are there 8 non black pixels?

Thanks in advance, Johann

Upvotes: 0

Views: 663

Answers (1)

Johann Burgess
Johann Burgess

Reputation: 606

Okay so after a fair bit of struggling I tried saving my image as a PNG file. Fixed everything.

According to a friend JPG images kind of blur between the pixel colours where as PNG will keep each pixel a solid colour. Using PNG for this type of work is much more reliable. - Excuse me for my non-technical talk.

Thank you.

Upvotes: 1

Related Questions