Skip to content

Commit

Permalink
Merge pull request #60 from valeriyvan/refactor/EnergyFunction
Browse files Browse the repository at this point in the history
Move EnergyFunction into separate file
  • Loading branch information
valeriyvan authored Oct 8, 2023
2 parents f9d02c1 + 0c713ed commit 684b7da
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 49 deletions.
49 changes: 0 additions & 49 deletions Sources/geometrize/Core.swift
Original file line number Diff line number Diff line change
@@ -1,54 +1,5 @@
import Foundation

// The core functions for Geometrize.

/// Type alias for a function that calculates a measure of the improvement adding
/// the scanlines of a shape provides - lower energy is better.
/// - Parameters:
/// - lines The scanlines of the shape.
/// - alpha The alpha of the scanlines.
/// - target The target bitmap.
/// - current The current bitmap.
/// - buffer The buffer bitmap.
/// - score The score.
/// - Returns: The energy measure.
public typealias EnergyFunction = (
_ lines: [Scanline],
_ alpha: UInt8,
_ target: Bitmap,
_ curent: Bitmap,
_ buffer: inout Bitmap,
_ score: Double
) -> Double

/// The default/built-in energy function that calculates a measure of the improvement adding
/// the scanlines of a shape provides - lower energy is better.
/// - Parameters:
/// - lines: The scanlines of the shape.
/// - alpha: The alpha of the scanlines.
/// - target: The target bitmap.
/// - current: The current bitmap.
/// - buffer: The buffer bitmap.
/// - score: The score.
/// - Returns: The energy measure.
public func defaultEnergyFunction( // swiftlint:disable:this function_parameter_count
_ lines: [Scanline],
_ alpha: UInt8,
_ target: Bitmap,
_ current: Bitmap,
_ buffer: inout Bitmap,
_ score: Double
) -> Double {
// Calculate best color for areas covered by the scanlines
let color: Rgba = computeColor(target: target, current: current, lines: lines, alpha: UInt8(alpha))
// Copy area covered by scanlines to buffer bitmap
buffer.copy(lines: lines, source: current)
// Blend scanlines into the buffer using the color calculated earlier
buffer.draw(lines: lines, color: color)
// Get error measure between areas of current and modified buffers covered by scanlines
return current.differencePartial(with: buffer, target: target, score: score, mask: lines)
}

/// Calculates the color of the scanlines.
/// - Parameters:
/// - target: The target image.
Expand Down
48 changes: 48 additions & 0 deletions Sources/geometrize/EnergyFunction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Foundation

/// Type alias for a function that calculates a measure of the improvement adding
/// the scanlines of a shape provides - lower energy is better.
/// - Parameters:
/// - lines The scanlines of the shape.
/// - alpha The alpha of the scanlines.
/// - target The target bitmap.
/// - current The current bitmap.
/// - buffer The buffer bitmap.
/// - score The score.
/// - Returns: The energy measure.
public typealias EnergyFunction = (
_ lines: [Scanline],
_ alpha: UInt8,
_ target: Bitmap,
_ curent: Bitmap,
_ buffer: inout Bitmap,
_ score: Double
) -> Double

/// The default/built-in energy function that calculates a measure of the improvement adding
/// the scanlines of a shape provides - lower energy is better.
/// - Parameters:
/// - lines: The scanlines of the shape.
/// - alpha: The alpha of the scanlines.
/// - target: The target bitmap.
/// - current: The current bitmap.
/// - buffer: The buffer bitmap.
/// - score: The score.
/// - Returns: The energy measure.
public func defaultEnergyFunction( // swiftlint:disable:this function_parameter_count
_ lines: [Scanline],
_ alpha: UInt8,
_ target: Bitmap,
_ current: Bitmap,
_ buffer: inout Bitmap,
_ score: Double
) -> Double {
// Calculate best color for areas covered by the scanlines
let color: Rgba = computeColor(target: target, current: current, lines: lines, alpha: UInt8(alpha))
// Copy area covered by scanlines to buffer bitmap
buffer.copy(lines: lines, source: current)
// Blend scanlines into the buffer using the color calculated earlier
buffer.draw(lines: lines, color: color)
// Get error measure between areas of current and modified buffers covered by scanlines
return current.differencePartial(with: buffer, target: target, score: score, mask: lines)
}

0 comments on commit 684b7da

Please sign in to comment.