-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move EnergyFunction into separate file
- Loading branch information
1 parent
f9d02c1
commit 0c713ed
Showing
2 changed files
with
48 additions
and
49 deletions.
There are no files selected for viewing
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,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) | ||
} |