Skip to content

Commit

Permalink
Make it available for Swift 5.0
Browse files Browse the repository at this point in the history
Modifications in last commit made it unavailable for Swift 5.0.
  • Loading branch information
koher committed Apr 26, 2020
1 parent c45a8a3 commit 3345065
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Sources/SwiftImage/CoreGraphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,14 @@ extension ImageSlice: _CGImageDirectlyConvertible where Pixel: _CGDirectPixel {
@inlinable
public func withCGImage<R>(_ body: (CGImage) throws -> R) rethrows -> R {
let length = image.width * self.height * MemoryLayout<Pixel>.size
let width = self.width
let xRange = self.xRange
let yRange = self.yRange
let imageWidth = self.image.width

var slice = self
return try slice.image.pixels.withUnsafeMutableBufferPointer { pixelsPointer in
let bytes: UnsafeMutablePointer<Pixel> = pixelsPointer.baseAddress! + (slice.yRange.lowerBound * slice.image.width + slice.xRange.lowerBound)
let bytes: UnsafeMutablePointer<Pixel> = pixelsPointer.baseAddress! + (yRange.lowerBound * imageWidth + xRange.lowerBound)
let provider: CGDataProvider = CGDataProvider(data: Data(
bytesNoCopy: bytes,
count: length,
Expand Down Expand Up @@ -588,19 +592,22 @@ extension ImageSlice: _CGImageDirectlyConvertible where Pixel: _CGDirectPixel {
public mutating func withCGContext(coordinates: CGContextCoordinates = .natural, _ body: (CGContext) throws -> Void) rethrows {
let width = self.width
let height = self.height
let xRange = self.xRange
let yRange = self.yRange
let imageWidth = self.image.width

precondition(width >= 0)
precondition(height >= 0)

try self.image.pixels.withUnsafeMutableBufferPointer { pixelsPointer in
let data: UnsafeMutablePointer<Pixel> = pixelsPointer.baseAddress! + (yRange.lowerBound * self.image.width + xRange.lowerBound)
let data: UnsafeMutablePointer<Pixel> = pixelsPointer.baseAddress! + (yRange.lowerBound * imageWidth + xRange.lowerBound)

let context = CGContext(
data: data,
width: width,
height: height,
bitsPerComponent: MemoryLayout<Pixel._EZ_PixelDirectChannel>.size * 8,
bytesPerRow: MemoryLayout<Pixel>.size * self.image.width,
bytesPerRow: MemoryLayout<Pixel>.size * imageWidth,
space: Pixel._ez_cgColorSpace,
bitmapInfo: Pixel._ez_cgBitmapInfo.rawValue
)!
Expand Down

0 comments on commit 3345065

Please sign in to comment.