Skip to content
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
11 changes: 7 additions & 4 deletions Sources/Graphite2D/ColorModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ public func smallFract(fixed: Int32) -> UInt16 {

private let one = 0x10000

// Two fractions' product, or one shifted up to be divided, needs more than the
// 32 bits an Int has on some machines.

/// A product of fractions, to the nearest.
private func times(_ a: Int, _ b: Int) -> Int { (a * b + 0x8000) >> 16 }
private func times(_ a: Int, _ b: Int) -> Int { Int((Int64(a) * Int64(b) + 0x8000) >> 16) }

/// A quotient as a fraction, to the nearest.
private func over(_ a: Int, _ b: Int) -> Int { ((a << 16) + b / 2) / b }
private func over(_ a: Int, _ b: Int) -> Int { Int(((Int64(a) << 16) + Int64(b / 2)) / Int64(b)) }

private func clamped(_ value: Int) -> UInt16 { UInt16(max(0, min(value, 0xFFFF))) }

Expand All @@ -63,8 +66,8 @@ extension RGBColor {
let (sixth, difference) = largest == Int(red) ? (0, Int(green) - Int(blue))
: largest == Int(green) ? (2, Int(blue) - Int(red)) : (4, Int(red) - Int(green))
// Down to a fraction of a sixth, then toward zero to one of the circle.
let part = difference << 16
let floored = part >= 0 ? part / delta : -((-part + delta - 1) / delta)
let part = Int64(difference) << 16, whole = Int64(delta)
let floored = Int(part >= 0 ? part / whole : -((-part + whole - 1) / whole))
return UInt16(truncatingIfNeeded: (sixth * one + floored) / 6)
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/Graphite2D/CopyBits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ public extension Port {
/// The source coordinate a destination pixel's center falls on.
private func scale(_ value: Int16, from origin: Int16, _ length: Int, to sourceOrigin: Int16, _ sourceLength: Int) -> Int16 {
guard length != sourceLength else { return sourceOrigin &+ (value &- origin) }
let offset = (2 * (Int(value) - Int(origin)) + 1) * sourceLength / (2 * length)
return Int16(clamping: Int(sourceOrigin) + offset)
// The product can pass 32 bits.
let offset = (2 * (Int64(value) - Int64(origin)) + 1) * Int64(sourceLength) / (2 * Int64(length))
return Int16(clamping: Int64(sourceOrigin) + offset)
}

/// The source coordinates a destination coordinate takes. A bitmap shrunk to
Expand Down
8 changes: 5 additions & 3 deletions Sources/Graphite2D/Mapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

import GraphiteCore

/// value × numerator ÷ denominator, rounded half away from zero.
/// value × numerator ÷ denominator, rounded half away from zero. The product
/// can pass 32 bits; a result far outside a coordinate's range is held there,
/// for the caller to clamp.
private func proportion(_ value: Int, _ numerator: Int, _ denominator: Int) -> Int {
let product = value * numerator
let magnitude = (2 * abs(product) + abs(denominator)) / (2 * abs(denominator))
let product = Int64(value) * Int64(numerator), divisor = abs(Int64(denominator))
let magnitude = Int(min((2 * abs(product) + divisor) / (2 * divisor), 0x10_0000))
return (product < 0) != (denominator < 0) ? -magnitude : magnitude
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/Graphite2D/Pattern.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ public struct TransferMode: RawRepresentable, Hashable, Sendable {
)
}
switch base {
case .blend: return each { source, destination, weight in (source * weight + destination * (0xFFFF - weight)) / 0xFFFF }
case .blend:
// A component times a weight can pass 32 bits.
return each { source, destination, weight in
Int((Int64(source) * Int64(weight) + Int64(destination) * Int64(0xFFFF - weight)) / 0xFFFF)
}
case .addPin: return each { source, destination, pin in min(source + destination, pin) }
case .addOver: return each { source, destination, _ in (source + destination) & 0xFFFF }
case .subPin: return each { source, destination, pin in max(destination - source, 0xFFFF - pin) }
Expand Down
3 changes: 2 additions & 1 deletion Sources/Graphite2D/Picture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ public struct Picture {

private func scale(_ value: Int16, _ from: Int16, _ fromLength: Int, _ to: Int16, _ toLength: Int) -> Int16 {
guard fromLength != 0 else { return to }
return Int16(clamping: Int(to) + (Int(value) - Int(from)) * toLength / fromLength)
// The product can pass 32 bits.
return Int16(clamping: Int64(to) + (Int64(value) - Int64(from)) * Int64(toLength) / Int64(fromLength))
}

func point(_ point: Point) -> Point {
Expand Down
5 changes: 3 additions & 2 deletions Sources/Graphite2D/PointAngle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ extension Rect {
} else if dv == 0 {
half = 90
} else {
let across = dh * height, down = dv * width
let slope = min((abs(across) << 16) / abs(down), Int(Int32.max))
// Either product, and the shifted one, can pass 32 bits.
let across = Int64(dh) * Int64(height), down = Int64(dv) * Int64(width)
let slope = Int(min((abs(across) << 16) / abs(down), Int64(Int32.max)))
let degrees = slopes.firstIndex { slope < $0 + margin } ?? 90
half = (across < 0) != (down < 0) ? degrees : 180 - degrees
}
Expand Down
64 changes: 64 additions & 0 deletions Tests/Graphite2DTests/WideArithmeticTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Testing
import GraphiteCore
import Graphite2D

// Arithmetic whose intermediate values pass 32 bits, which an Int is only on
// some machines: there these trapped or came out wrong.
@Suite struct WideArithmeticTests {

@Test func convertsBrightColors() {
let white = RGBColor(red: 0xFFFF, green: 0xFFFF, blue: 0xFFFF)
#expect(RGBColor(HSLColor(hue: 0, saturation: 0xFFFF, lightness: 0xFFFF)) == RGBColor(red: 0xFFFF, green: 0xFFFE, blue: 0xFFFE))
#expect(RGBColor(HSVColor(hue: 0, saturation: 0, value: 0xFFFF)) == white)
let red = RGBColor(red: 0xFFFF, green: 0, blue: 0)
#expect(red.hsl == HSLColor(hue: 0, saturation: 0xFFFF, lightness: 0x7FFF))
#expect(red.hsv == HSVColor(hue: 0, saturation: 0xFFFF, value: 0xFFFF))
// A hue before red's, and one past it by most of a sixth.
#expect(RGBColor(red: 0xFFFF, green: 0, blue: 0x8000).hsv.hue == 0xEAAB)
#expect(RGBColor(red: 0, green: 0, blue: 0xFFFF).hsv.hue == 0xAAAA)
}

// The values Mac OS 9.2.2 gave (Utilities-macos-9.2.2-ppc.txt), and the
// corners of the largest rectangle.
@Test func findsAnglesInLargeRectangles() {
let tall = Rect(top: 0, left: 0, bottom: 300, right: 100)
#expect(tall.angle(to: Point(v: 50, h: -150)) == 279)
let huge = Rect(top: -16000, left: -16000, bottom: 16000, right: 16000)
#expect(huge.angle(to: Point(v: -100, h: -200)) == 296)
let largest = Rect(top: -32767, left: -32767, bottom: 32767, right: 32767)
#expect(largest.angle(to: Point(v: -32767, h: 32767)) == 45)
#expect(largest.angle(to: Point(v: 32767, h: -32767)) == 225)
}

@Test func blendsBrightColors() {
let color = TransferMode.blend.combine(isForeground: true, drawn: .white, existing: .white,
fore: .black, back: .white,
opColor: RGBColor(red: 0xFFFF, green: 0x8000, blue: 0))
#expect(color == .white)
}

@Test func mapsBetweenLargeRectangles() {
let source = Rect(top: 0, left: 0, bottom: 32767, right: 32767)
let destination = Rect(top: -32767, left: -32767, bottom: 32767, right: 32767)
let point = Point(v: 32767, h: 16384).mapped(from: source, to: destination)
#expect(point == Point(v: 32767, h: 1))
#expect(Point(v: 32767, h: 32767).scaled(from: source, to: destination) == Point(v: 32767, h: 32767))
// Far past a coordinate's range.
let one = Rect(top: 0, left: 0, bottom: 1, right: 1)
#expect(Point(v: 32767, h: -32767).mapped(from: one, to: destination) == Point(v: 32767, h: -32768))
}

@Test func scalesFarPastThePort() throws {
let source = try GraphicsWorld(bounds: Rect(top: 0, left: 0, bottom: 4, right: 4), depth: 32)
source.port.eraseAll()
source.port.perform(.paint, rect: Rect(top: 1, left: 1, bottom: 2, right: 2))
let destination = try GraphicsWorld(bounds: Rect(top: 0, left: 0, bottom: 4, right: 4), depth: 32)
destination.port.eraseAll()
// Barely stretched, between rectangles far larger than either port.
destination.port.copyBits(from: source.pixels,
sourceRect: Rect(top: -32000, left: -32000, bottom: 32000, right: 32000),
destinationRect: Rect(top: -32000, left: -32000, bottom: 32001, right: 32001))
#expect(destination.pixels.color(at: Point(v: 2, h: 2)) == .black)
#expect(destination.pixels.color(at: Point(v: 3, h: 3)) == .white)
}
}
Loading