 
 extension ImageRep: SVGImageProtocol {
     
-    fileprivate var imageTableKey: SVGContext.ImageTableKey {
+    fileprivate var imageTableKey: SVGContext.ImageTableKey? {
         return self.originalData.map { SVGContext.ImageTableKey($0) } ?? SVGContext.ImageTableKey(AnyImage(imageRep: self, fileBacked: true))
     }
     
-    fileprivate func encode(using storageType: MediaType, properties: [ImageRep.PropertyKey : Any]) -> String? {
+    fileprivate func encode(using storageType: MediaType, properties: Any) -> String? {
         guard let mediaType = self.mediaType?.media_type_string else { return nil }
+        guard let properties = properties as? [ImageRep.PropertyKey : Any] else { return nil }
         guard let data = self.originalData else { return AnyImage(imageRep: self, fileBacked: true).encode(using: storageType, properties: properties) }
         return "data:\(mediaType);base64," + data.base64EncodedString()
     }


extension AnyImage: SVGImageProtocol {
    
    fileprivate var imageTableKey: SVGContext.ImageTableKey {
        return SVGContext.ImageTableKey(self)
    }
    
    fileprivate func encode(using storageType: MediaType, properties: [ImageRep.PropertyKey : Any]) -> String? {
        guard let mediaType = storageType.media_type_string else { return nil }
        guard let data = self.representation(using: storageType, properties: properties) else { return nil }
        return "data:\(mediaType);base64," + data.base64EncodedString()
    }

        self._draw(image: image, transform: transform, using: storageType, properties: properties)
    }
    
    public func draw(image: ImageRep, transform: SDTransform, using storageType: MediaType, properties: [ImageRep.PropertyKey : Any]) {
        self._draw(image: image, transform: transform, using: storageType, properties: properties)
    }
}

extension SVGContext {
    
    public func draw<Image : ImageProtocol>(image: Image, transform: SDTransform) {
        self.draw(image: image, transform: transform, using: .png, properties: [:])
    }
    
    public func draw(image: ImageRep, transform: SDTransform) {
        self.draw(image: image, transform: transform, using: .png, properties: [:])
    }
}

#if canImport(CoreGraphics)

extension CGImage: SVGImageProtocol {
    
    fileprivate var imageTableKey: SVGContext.ImageTableKey? {
        return SVGContext.ImageTableKey(self)
    }
    
    fileprivate func encode(using storageType: MediaType, properties: Any) -> String? {
        guard let mediaType = storageType.media_type_string else { return nil }
        guard let properties = properties as? [CGImageRep.PropertyKey : Any] else { return nil }
        guard let data = self.representation(using: storageType, properties: properties) else { return nil }
        return "data:\(mediaType);base64," + data.base64EncodedString()
    }
}

extension CGImageRep: SVGImageProtocol {
    
    fileprivate var imageTableKey: SVGContext.ImageTableKey? {
        return self.cgImage.map { SVGContext.ImageTableKey($0) }
    }
    
    fileprivate func encode(using storageType: MediaType, properties: Any) -> String? {
        return self.cgImage?.encode(using: storageType, properties: properties)
    }
}

extension SVGContext {
    
    public func draw(image: CGImageRep, transform: SDTransform, using storageType: MediaType = .png, properties: [CGImageRep.PropertyKey : Any] = [:]) {
        self._draw(image: image, transform: transform, using: storageType, properties: properties)
    }
    
    public func draw(image: CGImage, transform: SDTransform, using storageType: MediaType = .png, properties: [CGImageRep.PropertyKey : Any] = [:]) {
        self._draw(image: image, transform: transform, using: storageType, properties: properties)
    }
}

#endif

extension SVGContext {
    
