<commit_before>import Foundation
import ImageIO

//TODO: Copy/Paste documentation from ImageIO

public final class ImageDestination {
  let imageDestination: CGImageDestination
  public init?(data: NSMutableData, UTI: String, imageCount: Int) {
    guard let imageDestination = CGImageDestinationCreateWithData(data as CFMutableData, UTI as CFString, imageCount, nil)
      else { return nil }
    self.imageDestination = imageDestination
  }
  
  public init?(url: NSURL, UTI: String, imageCount: Int) {
    guard let imageDestination = CGImageDestinationCreateWithURL(url as CFURL, UTI as CFString, imageCount, nil)
      else { return nil }
    self.imageDestination = imageDestination
  }
  
  //TODO: Implement CGImageDestinationCreateWithDataConsumer
  
  public enum Property {
    case LossyCompressionQuality(Double)
    case MaximumCompressionQuality
    case LosslessCompressionQuality
    case BackgroundColor(CGColor)
    case ImageProperty(key: String, value: AnyObject)
  }
}

public extension ImageDestination.Property {
  public var imageIOProperty: (key: String, value: AnyObject) {
    switch self {
    case .LossyCompressionQuality(let quality):
      return (key: kCGImageDestinationLossyCompressionQuality as String, value: quality)
    case .MaximumCompressionQuality:
      return (key: kCGImageDestinationLossyCompressionQuality as String, value: 0.0)
    case .LosslessCompressionQuality:
      return (key: kCGImageDestinationLossyCompressionQuality as String, value: 1.0)
    case .BackgroundColor(let color):
      return (key: kCGImageDestinationBackgroundColor as String, value: color)
    case .ImageProperty(let key, let value):
      return (key: key, value: value)
    }
  }
}

#if swift(>=3.0)
  public extension Sequence where Iterator.Element == ImageDestination.Property {
    func rawProperties() -> CFDictionary {
      var dictionary: [String: AnyObject] = [:]
      for property in self {
        let (key, value) = property.imageIOProperty
        dictionary[key] = value
      }
      
      return dictionary as CFDictionary
    }
  }
#else
  public extension SequenceType where Generator.Element == ImageDestination.Property {
    func rawProperties() -> CFDictionary {
      var dictionary: [String: AnyObject] = [:]
      for property in self {
        let (key, value) = property.imageIOProperty
        dictionary[key] = value
      }
      
      return dictionary as CFDictionary
    }
  }
#endif

//MARK: - Adding Images
public extension ImageDestination {
  public func addImage(image: CGImage, properties: [Property]? = nil) {
    CGImageDestinationAddImage(imageDestination, image, properties?.rawProperties())
  }
  
  public func addImage(fromSource source: ImageSource, sourceImageIndex: Int, properties: [Property]? = nil) {
    CGImageDestinationAddImageFromSource(imageDestination, source.imageSource, sourceImageIndex, properties?.rawProperties())
  }
}

//MARK: - Getting Type Identifiers
public extension ImageDestination {
  public static func supportedUTIs() -> [String] {
    let identifiers = CGImageDestinationCopyTypeIdentifiers() as [AnyObject] as! [String]
    return identifiers
  }
  
  public static func supportsUTI(UTI: String) -> Bool {
    return supportedUTIs().contains(UTI)
  }
  
  public static var typeID: CFTypeID {
    return CGImageDestinationGetTypeID()
  }
}

//MARK: - Settings Properties
public extension ImageDestination {
  public func setProperties(properties: [Property]?) {
    CGImageDestinationSetProperties(imageDestination, properties?.rawProperties())
  }
}

//MARK: - Finalizing an Image Destination
public extension ImageDestination {
  public func finalize() -> Bool {
    return CGImageDestinationFinalize(imageDestination)
  }
}

<commit_msg>Add CGImageDestinationCreateWithDataConsumer wrapper<commit_after>import Foundation
import ImageIO

//TODO: Copy/Paste documentation from ImageIO

public final class ImageDestination {
  let imageDestination: CGImageDestination
  public init?(data: NSMutableData, UTI: String, imageCount: Int) {
    guard let imageDestination = CGImageDestinationCreateWithData(data as CFMutableData, UTI as CFString, imageCount, nil)
      else { return nil }
    self.imageDestination = imageDestination
  }
  
  public init?(url: NSURL, UTI: String, imageCount: Int) {
    guard let imageDestination = CGImageDestinationCreateWithURL(url as CFURL, UTI as CFString, imageCount, nil)
      else { return nil }
    self.imageDestination = imageDestination
  }
  
  public init?(dataConsumer: CGDataConsumer, imageType: String, imageCount: Int, options: [Property]? = nil) {
    guard let imageDestination = CGImageDestinationCreateWithDataConsumer(dataConsumer, imageType, imageCount, options?.rawProperties())
      else { return nil }
    self.imageDestination = imageDestination
  }
  
  public enum Property {
    case LossyCompressionQuality(Double)
    case MaximumCompressionQuality
    case LosslessCompressionQuality
    case BackgroundColor(CGColor)
    case ImageProperty(key: String, value: AnyObject)
  }
}

public extension ImageDestination.Property {
  public var imageIOProperty: (key: String, value: AnyObject) {
    switch self {
    case .LossyCompressionQuality(let quality):
      return (key: kCGImageDestinationLossyCompressionQuality as String, value: quality)
    case .MaximumCompressionQuality:
      return (key: kCGImageDestinationLossyCompressionQuality as String, value: 0.0)
    case .LosslessCompressionQuality:
      return (key: kCGImageDestinationLossyCompressionQuality as String, value: 1.0)
    case .BackgroundColor(let color):
      return (key: kCGImageDestinationBackgroundColor as String, value: color)
    case .ImageProperty(let key, let value):
      return (key: key, value: value)
    }
  }
}

#if swift(>=3.0)
  public extension Sequence where Iterator.Element == ImageDestination.Property {
    func rawProperties() -> CFDictionary {
      var dictionary: [String: AnyObject] = [:]
      for property in self {
        let (key, value) = property.imageIOProperty
        dictionary[key] = value
      }
      
      return dictionary as CFDictionary
    }
  }
#else
  public extension SequenceType where Generator.Element == ImageDestination.Property {
    func rawProperties() -> CFDictionary {
      var dictionary: [String: AnyObject] = [:]
      for property in self {
        let (key, value) = property.imageIOProperty
        dictionary[key] = value
      }
      
      return dictionary as CFDictionary
    }
  }
#endif

//MARK: - Adding Images
public extension ImageDestination {
  public func addImage(image: CGImage, properties: [Property]? = nil) {
    CGImageDestinationAddImage(imageDestination, image, properties?.rawProperties())
  }
  
  public func addImage(fromSource source: ImageSource, sourceImageIndex: Int, properties: [Property]? = nil) {
    CGImageDestinationAddImageFromSource(imageDestination, source.imageSource, sourceImageIndex, properties?.rawProperties())
  }
}

//MARK: - Getting Type Identifiers
public extension ImageDestination {
  public static func supportedUTIs() -> [String] {
    let identifiers = CGImageDestinationCopyTypeIdentifiers() as [AnyObject] as! [String]
    return identifiers
  }
  
  public static func supportsUTI(UTI: String) -> Bool {
    return supportedUTIs().contains(UTI)
  }
  
  public static var typeID: CFTypeID {
    return CGImageDestinationGetTypeID()
  }
}

//MARK: - Settings Properties
public extension ImageDestination {
  public func setProperties(properties: [Property]?) {
    CGImageDestinationSetProperties(imageDestination, properties?.rawProperties())
  }
}

//MARK: - Finalizing an Image Destination
public extension ImageDestination {
  public func finalize() -> Bool {
    return CGImageDestinationFinalize(imageDestination)
  }
}


{'fraction_non_alphanumeric': 0.08315182575272262, 'fraction_numerical': 0.0015374759769378604, 'mean_word_length': 4.091976516634051, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7195604', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2151, 'n_words': 603}