Skip to content

Commit

Permalink
Uses CoreGraphics only when editing contains only cropping. (#83)
Browse files Browse the repository at this point in the history
* Patch

* Use CoreGraphics when crop only

* Update

* Fixing colorspace issue

* Fix orientation
  • Loading branch information
muukii committed Mar 30, 2021
1 parent 3560de5 commit 96ee654
Show file tree
Hide file tree
Showing 25 changed files with 740 additions and 326 deletions.
140 changes: 76 additions & 64 deletions Brightroom.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

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.
2 changes: 1 addition & 1 deletion Sources/BrightroomEngine/ColorCube/ColorCubeHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ColorCubeHelper {
throw ColorCubeHelperError.failedToCreateCGImageSource
}

let cgImage = ImageTool.loadOriginalCGImage(from: imageSource)!
let cgImage = ImageTool.loadOriginalCGImage(from: imageSource, fixesOrientation: false)!

let pixels = cgImage.width * cgImage.height
let channels = 4
Expand Down
61 changes: 38 additions & 23 deletions Sources/BrightroomEngine/DataSource/ImageSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,37 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import UIKit

import CoreImage
import UIKit
import Verge

#if canImport(UIKit)
import UIKit
import UIKit
#endif

#if canImport(Photos)
import Photos
import Photos
#endif


/**
An object that provides an image-data from multiple backing storage.
*/
/// An object that provides an image-data from multiple backing storage.
public final class ImageSource: Equatable {

private struct Closures {
let readImageSize: () -> CGSize
let loadOriginalCGImage: () -> CGImage
let loadThumbnailCGImage: (CGFloat) -> CGImage
let makeCIImage: () -> CIImage
}

public static func == (lhs: ImageSource, rhs: ImageSource) -> Bool {
lhs === rhs
}

private let closures: Closures

public init(image: UIImage) {

self.closures = .init(
readImageSize: {
image.size.applying(.init(scaleX: image.scale, y: image.scale))
Expand All @@ -68,43 +65,61 @@ public final class ImageSource: Equatable {
}
)
}

public init(cgImageSource: CGImageSource) {
self.closures = .init(
readImageSize: {
ImageTool.readImageSize(from: cgImageSource)!
},
loadOriginalCGImage: {
ImageTool.loadOriginalCGImage(from: cgImageSource)!
ImageTool.loadOriginalCGImage(from: cgImageSource, fixesOrientation: false)!
},
loadThumbnailCGImage: { (maxPixelSize) -> CGImage in
ImageTool.loadThumbnailCGImage(from: cgImageSource, maxPixelSize: maxPixelSize)!
ImageTool.loadThumbnailCGImage(
from: cgImageSource,
maxPixelSize: maxPixelSize,
fixesOrientation: false
)!
},
makeCIImage: {
if #available(iOS 13.0, *) {
return CIImage(cgImageSource: cgImageSource, index: 0, options: [:])
} else {
return CIImage(cgImage: ImageTool.loadOriginalCGImage(from: cgImageSource)!)
return CIImage(cgImage: ImageTool.loadOriginalCGImage(from: cgImageSource, fixesOrientation: false)!)
}
}
)
}

public func readImageSize() -> CGSize {
closures.readImageSize()
}


/**
Creates an instance of CGImage full-resolution.
- Attention: The image is not orientated.
*/
public func loadOriginalCGImage() -> CGImage {
closures.loadOriginalCGImage()
}


/**
Creates an instance of CGImage resized to maximum pixel size.
- Attention: The image is not orientated.
*/
public func loadThumbnailCGImage(maxPixelSize: CGFloat) -> CGImage {
closures.loadThumbnailCGImage(maxPixelSize)
}

public func makeCIImage() -> CIImage {

/**
Creates an instance of CIImage that backed full-resolution image.
- Attention: The image is not orientated.
*/
public func makeOriginalCIImage() -> CIImage {
closures.makeCIImage()
}

}

}
189 changes: 189 additions & 0 deletions Sources/BrightroomEngine/Engine/CoreGraphics+.swift
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)
}
}
Loading

0 comments on commit 96ee654

Please sign in to comment.