SEEEEEAAAAAA10000000

Compact Image Buffer Maker

Mar 17th, 2021 (edited)
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.03 KB | None | 0 0
  1. import CoreGraphics
  2. import CoreImage
  3.  
  4. public struct ImageBufferMaker {
  5.    
  6.     let image: CIImage
  7.    
  8.     public init(image: CIImage) {
  9.         self.image = image
  10.     }
  11.    
  12.     public func makeBuffer() -> [UInt8] {
  13.         let context = CIContext()
  14.         let ciimage = image
  15.         let dimension = ciimage.extent.width * ciimage.extent.height
  16.         var bitmap = [UInt8](repeating: 0, count: 4 * Int(dimension)) // 4 'инта по 8 бит' на пиксель
  17.         context.render(ciimage,
  18.                        toBitmap: &bitmap,
  19.                        rowBytes: 4 * Int(ciimage.extent.width),
  20.                        bounds: ciimage.extent,
  21.                        format: CIFormat.RGBA8,
  22.                        colorSpace: CGColorSpaceCreateDeviceRGB()
  23.         )
  24.         return bitmap
  25.     }
  26. }
  27.  
  28. // Usage
  29. let text = "/Users/yourname/Desktop/image.jpg"
  30. let url = URL(fileURLWithPath: text)
  31. let photo = CIImage(contentsOf: url)!
  32. let buffer = ImageBufferMaker(image: photo).makeBuffer()
  33. print("buffer: \(buffer)")
Advertisement
Add Comment
Please, Sign In to add comment