Skip to content

Commit

Permalink
Fix ImageRenderer (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Apr 11, 2022
1 parent 0b9b2ae commit 444d448
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions Sources/BrightroomEngine/Engine/ImageRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,28 @@ import CoreImage
import SwiftUI
import UIKit

/// It renders an image with options
public final class ImageRenderer {

public struct Options {

public var resolution: Resolution
public var workingFormat: CIFormat
public var workingColorSpace: CGColorSpace


/// An colorspace that uses on rendering.
/// Result image would use this colorspace.
/// Nil means letting the renderer use the intrinsic colorspace of the working image.
public var workingColorSpace: CGColorSpace?

///
/// - Parameters:
/// - resolution:
/// - workingFormat:
/// - workingColorSpace:
public init(
resolution: ImageRenderer.Resolution = .full,
workingFormat: CIFormat = .ARGB8,
workingColorSpace: CGColorSpace = CGColorSpace(name: CGColorSpace.sRGB)!
workingColorSpace: CGColorSpace? = nil
) {
self.resolution = resolution
self.workingFormat = workingFormat
Expand Down Expand Up @@ -79,7 +89,6 @@ public final class ImageRenderer {
}

init(cgImage: CGImage, options: Options, engine: Engine) {
assert(cgImage.colorSpace == options.workingColorSpace)
self.cgImage = cgImage
self.options = options
self.engine = engine
Expand Down Expand Up @@ -141,7 +150,7 @@ public final class ImageRenderer {
) {
type(of: self).queue.async {
do {
let rendered = try self.render()
let rendered = try self.render(options: options)
callbackQueue.async {
completion(.success(rendered))
}
Expand All @@ -159,7 +168,7 @@ public final class ImageRenderer {
- Attension: This operation can be run background-thread.
*/
public func render(options: Options = .init()) throws -> Rendered {
if edit.drawer.isEmpty, edit.modifiers.isEmpty {
if edit.drawer.isEmpty, edit.modifiers.isEmpty, options.workingColorSpace == nil {
return try renderOnlyCropping(options: options)
} else {
return try renderRevison2(options: options)
Expand All @@ -170,6 +179,8 @@ public final class ImageRenderer {
Render for only cropping using CoreGraphics
*/
private func renderOnlyCropping(options: Options = .init()) throws -> Rendered {

assert(options.workingColorSpace == nil, "This rendering operation no supports working with specifying colorspace.")

EngineLog.debug(.renderer, "Start render in using CoreGraphics")

Expand All @@ -182,33 +193,14 @@ public final class ImageRenderer {

let sourceCGImage: CGImage = source.loadOriginalCGImage()

/*
===
===
===
*/
EngineLog.debug(
.renderer,
"Fixing colorspace \(sourceCGImage.colorSpace as Any) -> \(options.workingColorSpace)"
)

let fixedColorspace: CGImage
do {
fixedColorspace = try sourceCGImage.copy(colorSpace: options.workingColorSpace)
.unwrap(orThrow: "Failed to copy with fixing colorspace. \(sourceCGImage.colorSpace as Any), \(options.workingColorSpace)")
} catch {
EngineLog.error(.renderer, "Failed to create fixed colorspace image, \(error)")
fixedColorspace = sourceCGImage
}

/*
===
===
===
*/
EngineLog.debug(.renderer, "Fix orientation")

let orientedImage = try fixedColorspace.oriented(orientation)
let orientedImage = try sourceCGImage.oriented(orientation)

/*
===
Expand Down

0 comments on commit 444d448

Please sign in to comment.