SEEEEEAAAAAA10000000

Image Buffer Template

Jul 16th, 2020
1,302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.45 KB | None | 0 0
  1. import CoreGraphics
  2. import CoreImage
  3.  
  4. public struct ImageBufferMaker {
  5.    
  6.     let diagonal: SIMD2<Int>
  7.  
  8.     public init(diagonal: SIMD2<Int>) {
  9.         self.diagonal = diagonal
  10.     }
  11.    
  12.     func makeBitmapContext() -> CGContext? {
  13.         let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
  14.         let context = CGContext(data: nil,
  15.                                 width: diagonal.x,
  16.                                 height: diagonal.y,
  17.                                 bitsPerComponent: 8,
  18.                                 bytesPerRow: 4 * diagonal.x,
  19.                                 space: CGColorSpaceCreateDeviceRGB(),
  20.                                 bitmapInfo: bitmapInfo.rawValue)
  21.         return context
  22.     }
  23.    
  24.     public func makeImage() -> CGImage? {
  25.         let context = makeBitmapContext()!
  26.         context.setFillColor(CGColor.white)
  27.         context.fill(CGRect(x: 0, y: 0, width: diagonal.x, height: diagonal.y))
  28.         context.setFillColor(CGColor.black)
  29.         let center = diagonal / 2
  30.         context.addArc(center: CGPoint(x: center.x, y: center.y),
  31.                        radius: CGFloat(center.x),
  32.                        startAngle: CGFloat(0),
  33.                        endAngle: CGFloat(Double.pi * 2),
  34.                        clockwise: false)
  35.         context.fillPath(using: CGPathFillRule.winding)
  36.         let image = context.makeImage()
  37.         return image
  38.     }
  39.    
  40.     public func makeBuffer() -> [UInt8] {
  41.         let context = CIContext()
  42.         var option = [CIImageOption : Any]()
  43.         option[.colorSpace] = CGColorSpaceCreateDeviceRGB()
  44.         var ciimage = CIImage(cgImage: makeImage()!, options: option)
  45.         ciimage = transformScale(image: ciimage, factor: 0.9)
  46.         let dimension = ciimage.extent.width * ciimage.extent.height
  47.         var bitmap = [UInt8](repeating: 0, count: 4 * Int(dimension)) // 4 'инта по 8 бит' на пиксель
  48.         context.render(ciimage,
  49.                        toBitmap: &bitmap,
  50.                        rowBytes: 4 * Int(ciimage.extent.width),
  51.                        bounds: ciimage.extent,
  52.                        format: CIFormat.RGBA8,
  53.                        colorSpace: CGColorSpaceCreateDeviceRGB()
  54.         )
  55.         return bitmap
  56.     }
  57.    
  58.     public func imageProcessing(image: CGImage) -> CGImage {
  59.         let context = CIContext()
  60.         var option = [CIImageOption : Any]()
  61.         option[.colorSpace] = CGColorSpaceCreateDeviceRGB()
  62.         var ciimage = CIImage(cgImage: makeImage()!, options: option)
  63.         ciimage = transformScale(image: ciimage, factor: 0.9)
  64.         return context.createCGImage(ciimage, from: ciimage.extent)!
  65.     }
  66.    
  67.     // transforms, filters
  68.    
  69.     func transformScaleAndRotate(image: CIImage) -> CIImage {
  70.         let transformScale = CGAffineTransform(scaleX: -1, y: 1)
  71.         let transformRotate = CGAffineTransform(rotationAngle: CGFloat(exactly: Double.pi / 2)!)
  72.         let transformed = image.transformed(by: transformScale).transformed(by: transformRotate)
  73.         return transformed
  74.     }
  75.    
  76.     func transformScale(image: CIImage, factor: CGFloat) -> CIImage {
  77.         let transformScale = CGAffineTransform(scaleX: factor, y: factor)
  78.         let transformed = image.transformed(by: transformScale)
  79.         return transformed
  80.     }
  81.    
  82.     // debug draw
  83.     func draw(to context: CGContext) {
  84.         let rect = CGRect(x: 0, y: 0, width: diagonal.x, height: diagonal.y)
  85.         var image = makeImage()!
  86.         image = imageProcessing(image: image)
  87.         context.draw(image, in: rect, byTiling: false)
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment