Get image raw data

  1. 9 months ago

    Luiz D'Aniello

    23 Mar 2019 User since 2019

    I want to get the color for a specific pixel in the image. Something good for games, or even check where in an image a person clicked.
    it's something like:

    func getPixelColor(pos: CGPoint) -> UIColor {
    
              let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage))
              let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
    
              let pixelInfo: Int = ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4
    
              let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
              let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
              let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
              let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)
    
              return UIColor(red: r, green: g, blue: b, alpha: a)
          }  

    But i think CoreGraphics or CoreFoundation aren't ported. Is there another way?

  2. marco

    23 Mar 2019 Administrator User since 2016

    Hi @Luiz D'Aniello you would need read access or read/write access to pixel information?

  3. Luiz D'Aniello

    23 Mar 2019 User since 2019

    I think write i could use BezierPath. What i need is check the color (rgba) of a point

  4. marco

    23 Mar 2019 Administrator User since 2016

    The method you posted (as an example) would be very inefficient (it makes a full copy of the image buffer at each call). How you would expect the return values? An Int that represents RGBA with a mask? Or an array of values (probably inefficient), or what?

  5. Luiz D'Aniello

    23 Mar 2019 User since 2019

    It’s a function that I used on Swift and with Xojo. I know it’s very inefficient, but I don’t think there are one that I could get just a specific pixel.
    A return with the Int value of R,G,B and A.
    Because I can test for specific colors or Alpha channel.

  6. marco

    24 Mar 2019 Administrator User since 2016

    Hi @Luiz D'Aniello I'd like to have a more complete solution, that's the reason why we are going to introduce a new ImageBuffer/ImageSurface object (not yet sure about the exposed name) where users can get/set individual pixel values. This object will also have a way to create an Image.

    Please note that nexts version will also have CIFilters support.

  7. Luiz D'Aniello

    25 Mar 2019 User since 2019

    Hi @marco. That would be really nice.
    A function that i could get/set pixel values are good to map images. Controlling the alpha channel or a secondary image with a map of colors, i could see in which part of image a person touched.
    Waiting for next versions. ;-)

  8. marco

    5 Apr 2019 Administrator User since 2016

    @Luiz D'Aniello you can expect a lot of news next week :-)

  9. Luiz D'Aniello

    8 Apr 2019 User since 2019

    @marco can't wait to see it.

or Sign Up to reply!