Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move EnergyFunction into separate file #60

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}