-
-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uses CoreGraphics only when editing contains only cropping. (#83)
* Patch * Use CoreGraphics when crop only * Update * Fixing colorspace issue * Fix orientation
- Loading branch information
Showing
25 changed files
with
740 additions
and
326 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// | ||
// Copyright (c) 2021 Hiroshi Kimura(Muukii) <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
import CoreGraphics | ||
import ImageIO | ||
|
||
extension CGContext { | ||
|
||
@discardableResult | ||
func perform(_ drawing: (CGContext) -> Void) -> CGContext { | ||
drawing(self) | ||
return self | ||
} | ||
|
||
static func makeContext(for image: CGImage, size: CGSize? = nil) throws -> CGContext { | ||
let context = CGContext.init( | ||
data: nil, | ||
width: size.map { Int($0.width) } ?? image.width, | ||
height: size.map { Int($0.height) } ?? image.height, | ||
bitsPerComponent: image.bitsPerComponent, | ||
bytesPerRow: 0, | ||
space: try image.colorSpace.unwrap(), | ||
bitmapInfo: image.bitmapInfo.rawValue | ||
) | ||
|
||
return try context.unwrap() | ||
} | ||
|
||
fileprivate func detached(_ perform: () -> Void) { | ||
saveGState() | ||
perform() | ||
restoreGState() | ||
} | ||
} | ||
|
||
extension CGImage { | ||
|
||
var size: CGSize { | ||
return .init(width: width, height: height) | ||
} | ||
|
||
func croppedWithColorspace(to cropRect: CGRect) throws -> CGImage { | ||
|
||
let cgImage = try autoreleasepool { () -> CGImage? in | ||
|
||
let context = try CGContext.makeContext(for: self, size: cropRect.size) | ||
.perform { c in | ||
|
||
c.draw( | ||
self, | ||
in: CGRect( | ||
origin: .init( | ||
x: -cropRect.origin.x, | ||
y: -(size.height - cropRect.maxY) | ||
), | ||
size: size | ||
) | ||
) | ||
|
||
} | ||
return context.makeImage() | ||
} | ||
|
||
return try cgImage.unwrap() | ||
|
||
} | ||
|
||
func resized(maxPixelSize: CGFloat) throws -> CGImage { | ||
|
||
let cgImage = try autoreleasepool { () -> CGImage? in | ||
|
||
let targetSize = Geometry.sizeThatAspectFit( | ||
size: size, | ||
maxPixelSize: maxPixelSize | ||
) | ||
|
||
let context = try CGContext.makeContext(for: self, size: targetSize) | ||
.perform { c in | ||
c.interpolationQuality = .high | ||
c.draw(self, in: c.boundingBoxOfClipPath) | ||
} | ||
return context.makeImage() | ||
} | ||
|
||
return try cgImage.unwrap() | ||
} | ||
|
||
enum Flipping { | ||
case vertically | ||
case horizontally | ||
} | ||
|
||
func rotated(angle: CGFloat, flipping: Flipping? = nil) throws -> CGImage { | ||
guard angle != 0 else { | ||
return self | ||
} | ||
|
||
var rotatedSize: CGSize = | ||
size | ||
.applying(.init(rotationAngle: angle)) | ||
|
||
rotatedSize.width = abs(rotatedSize.width) | ||
rotatedSize.height = abs(rotatedSize.height) | ||
|
||
let cgImage = try autoreleasepool { () -> CGImage? in | ||
let rotatingContext = try CGContext.makeContext(for: self, size: rotatedSize) | ||
.perform { c in | ||
|
||
if let flipping = flipping { | ||
switch flipping { | ||
case .vertically: | ||
c.translateBy(x: 0, y: rotatedSize.height) | ||
c.scaleBy(x: 1, y: -1) | ||
case .horizontally: | ||
c.translateBy(x: rotatedSize.width, y: 0) | ||
c.scaleBy(x: -1, y: 1) | ||
} | ||
} | ||
|
||
c.translateBy(x: rotatedSize.width / 2, y: rotatedSize.height / 2) | ||
c.rotate(by: angle) | ||
c.translateBy( | ||
x: -size.width / 2, | ||
y: -size.height / 2 | ||
) | ||
|
||
c.draw(self, in: .init(origin: .zero, size: self.size)) | ||
} | ||
|
||
return rotatingContext.makeImage() | ||
} | ||
|
||
return try cgImage.unwrap() | ||
} | ||
|
||
func oriented(_ orientation: CGImagePropertyOrientation) throws -> CGImage { | ||
|
||
let angle: CGFloat | ||
|
||
switch orientation { | ||
case .down, .downMirrored: | ||
angle = CGFloat.pi | ||
case .left, .leftMirrored: | ||
angle = CGFloat.pi / 2.0 | ||
case .right, .rightMirrored: | ||
angle = CGFloat.pi / -2.0 | ||
case .up, .upMirrored: | ||
angle = 0 | ||
} | ||
|
||
let flipping: Flipping? | ||
switch orientation { | ||
case .upMirrored, .downMirrored: | ||
flipping = .horizontally | ||
case .leftMirrored, .rightMirrored: | ||
flipping = .vertically | ||
case .up, .down, .left, .right: | ||
flipping = nil | ||
} | ||
|
||
let result = try rotated(angle: angle, flipping: flipping) | ||
|
||
return result | ||
} | ||
|
||
func rotated(rotation: EditingCrop.Rotation, flipping: Flipping? = nil) | ||
throws -> CGImage | ||
{ | ||
try rotated(angle: -rotation.angle, flipping: flipping) | ||
} | ||
} |
Oops, something went wrong.