                 
                 guard let _iccData = _image.colorSpace.iccData else { return encode(image: AnyImage(image: _image, colorSpace: .genericGamma22Gray), properties: properties) }
                 
-                var _pixels = Data(count: _image.pixels.count)
-                
-                _pixels.withUnsafeMutableBufferPointer {
-                    
-                    guard var destination = $0.baseAddress else { return }
-                    
-                    _image.pixels.withUnsafeBufferPointer {
-                        
-                        guard var source = $0.baseAddress else { return }
-                        
-                        for _ in 0..<$0.count {
-                            
-                            let w = UInt16(source.pointee.w)
-                            let a = UInt16(source.pointee.a)
-                            
-                            destination.pointee = UInt8((a * w + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
-                            
-                            destination += 1
-                            source += 1
-                        }
-                    }
-                }
-                
-                pixels = _pixels
+                pixels = _image.pixels.map(JPEGEncoder._encodeGray).data
                 bytesPerRow = _image.width
                 format = JCS_GRAYSCALE
                 components = 1

            
            guard let _iccData = image.colorSpace.iccData else { return encode(image: AnyImage(image: image, colorSpace: .genericGamma22Gray), properties: properties) }
            
            var _pixels = Data(count: image.pixels.count)
            
            _pixels.withUnsafeMutableBufferPointer {
                
                guard var destination = $0.baseAddress else { return }
                
                image.pixels.withUnsafeBufferPointer {
                    
                    guard var source = $0.baseAddress else { return }
                    
                    for _ in 0..<$0.count {
                        
                        let w = UInt16(source.pointee.w)
                        let a = UInt16(source.pointee.a)
                        
                        destination.pointee = UInt8((a * w + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                        
                        destination += 1
                        source += 1
                    }
                }
            }
            
            pixels = _pixels
            bytesPerRow = image.width
            format = JCS_GRAYSCALE
            components = 1
            iccData = _iccData
            
        case var image as Image<ARGB32ColorPixel>:
            
            guard let _iccData = image.colorSpace.iccData else { return encode(image: AnyImage(image: image, colorSpace: .sRGB), properties: properties) }
            
            image.withUnsafeMutableBufferPointer {
                
                guard var pixels = $0.baseAddress else { return }
                
                for _ in 0..<$0.count {
                    
                    let r = UInt16(pixels.pointee.r)
                    let g = UInt16(pixels.pointee.g)
                    let b = UInt16(pixels.pointee.b)
                    let a = UInt16(pixels.pointee.a)
                    
                    pixels.pointee.r = UInt8((a * r + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                    pixels.pointee.g = UInt8((a * g + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                    pixels.pointee.b = UInt8((a * b + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                    
                    pixels += 1
                }
            }
            
            pixels = image.pixels.data
            bytesPerRow = 4 * image.width
            format = JCS_EXT_XRGB
            components = 4
            iccData = _iccData
            
        case var image as Image<RGBA32ColorPixel>:
            
            guard let _iccData = image.colorSpace.iccData else { return encode(image: AnyImage(image: image, colorSpace: .sRGB), properties: properties) }
            
            image.withUnsafeMutableBufferPointer {
                
                guard var pixels = $0.baseAddress else { return }
                
                for _ in 0..<$0.count {
                    
                    let r = UInt16(pixels.pointee.r)
                    let g = UInt16(pixels.pointee.g)
                    let b = UInt16(pixels.pointee.b)
                    let a = UInt16(pixels.pointee.a)
                    
                    pixels.pointee.r = UInt8((a * r + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                    pixels.pointee.g = UInt8((a * g + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                    pixels.pointee.b = UInt8((a * b + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                    
                    pixels += 1
                }
            }
            
            pixels = image.pixels.data
            bytesPerRow = 4 * image.width
            format = JCS_EXT_RGBX
            components = 4
            iccData = _iccData
            
        case var image as Image<ABGR32ColorPixel>:
            
            guard let _iccData = image.colorSpace.iccData else { return encode(image: AnyImage(image: image, colorSpace: .sRGB), properties: properties) }
            
            image.withUnsafeMutableBufferPointer {
                
                guard var pixels = $0.baseAddress else { return }
                
                for _ in 0..<$0.count {
                    
                    let r = UInt16(pixels.pointee.r)
                    let g = UInt16(pixels.pointee.g)
                    let b = UInt16(pixels.pointee.b)
                    let a = UInt16(pixels.pointee.a)
                    
                    pixels.pointee.r = UInt8((a * r + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                    pixels.pointee.g = UInt8((a * g + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                    pixels.pointee.b = UInt8((a * b + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                    
                    pixels += 1
                }
            }
            
            pixels = image.pixels.data
            bytesPerRow = 4 * image.width
            format = JCS_EXT_XBGR
            components = 4
            iccData = _iccData
            
        case var image as Image<BGRA32ColorPixel>:
            
            guard let _iccData = image.colorSpace.iccData else { return encode(image: AnyImage(image: image, colorSpace: .sRGB), properties: properties) }
            
            image.withUnsafeMutableBufferPointer {
                
                guard var pixels = $0.baseAddress else { return }
                
                for _ in 0..<$0.count {
                    
                    let r = UInt16(pixels.pointee.r)
                    let g = UInt16(pixels.pointee.g)
                    let b = UInt16(pixels.pointee.b)
                    let a = UInt16(pixels.pointee.a)
                    
                    pixels.pointee.r = UInt8((a * r + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                    pixels.pointee.g = UInt8((a * g + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                    pixels.pointee.b = UInt8((a * b + 0xFF * (0xFF - a) + 0x7F) / 0xFF)
                    
                    pixels += 1
                }
            }
            
            pixels = image.pixels.data
            bytesPerRow = 4 * image.width
            format = JCS_EXT_BGRX
            components = 4

                
            } else {
                
                let _image = Image<RGBA32ColorPixel>(image) ?? Image<RGBA32ColorPixel>(image: image, colorSpace: .sRGB)
                
                guard let _iccData = _image.colorSpace.iccData else { return encode(image: AnyImage(image: _image, colorSpace: .sRGB), properties: properties) }
                
                pixels = _image.pixels.map(JPEGEncoder._encodeRGB).data
                bytesPerRow = 4 * _image.width
                format = JCS_EXT_RGBX
                components = 4
