From 4bfc291959d6182ca2b463d4ea71b8172500a3dd Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 3 Dec 2026 15:53:56 -0500 Subject: [PATCH 1/7] Convert colors without overflowing a 32-bit Int --- Sources/Graphite2D/ColorModels.swift | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sources/Graphite2D/ColorModels.swift b/Sources/Graphite2D/ColorModels.swift index eb620bb..4e0639a 100644 --- a/Sources/Graphite2D/ColorModels.swift +++ b/Sources/Graphite2D/ColorModels.swift @@ -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))) } @@ -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) } From ce431948c4186ad05ab30594d064ea72a33f9ed7 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 3 Dec 2026 15:53:56 -0500 Subject: [PATCH 2/7] Find angles without overflowing a 32-bit Int --- Sources/Graphite2D/PointAngle.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/Graphite2D/PointAngle.swift b/Sources/Graphite2D/PointAngle.swift index c050bcf..940e447 100644 --- a/Sources/Graphite2D/PointAngle.swift +++ b/Sources/Graphite2D/PointAngle.swift @@ -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 } From 03c82e9be4f0e45b075a91820c2c042eb4eef10b Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 3 Dec 2026 15:53:56 -0500 Subject: [PATCH 3/7] Blend colors without overflowing a 32-bit Int --- Sources/Graphite2D/Pattern.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/Graphite2D/Pattern.swift b/Sources/Graphite2D/Pattern.swift index dd3e4a0..ef0cd60 100644 --- a/Sources/Graphite2D/Pattern.swift +++ b/Sources/Graphite2D/Pattern.swift @@ -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) } From 4e331587ae2fccdcc48dcf8b7ca9055c4f74bf59 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 3 Dec 2026 15:53:56 -0500 Subject: [PATCH 4/7] Map points without overflowing a 32-bit Int --- Sources/Graphite2D/Mapping.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/Graphite2D/Mapping.swift b/Sources/Graphite2D/Mapping.swift index d621859..b938048 100644 --- a/Sources/Graphite2D/Mapping.swift +++ b/Sources/Graphite2D/Mapping.swift @@ -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 } From 7ea25a0b2586453c1ac76c6f1ad99d84c816070d Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 3 Dec 2026 15:53:56 -0500 Subject: [PATCH 5/7] Scale CopyBits without overflowing a 32-bit Int --- Sources/Graphite2D/CopyBits.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/Graphite2D/CopyBits.swift b/Sources/Graphite2D/CopyBits.swift index a2bf343..df256aa 100644 --- a/Sources/Graphite2D/CopyBits.swift +++ b/Sources/Graphite2D/CopyBits.swift @@ -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 From 6cef4b23fabda52db53221e38661d4ed627797c5 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 3 Dec 2026 15:53:56 -0500 Subject: [PATCH 6/7] Scale pictures without overflowing a 32-bit Int --- Sources/Graphite2D/Picture.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/Graphite2D/Picture.swift b/Sources/Graphite2D/Picture.swift index 713d1c8..314f05c 100644 --- a/Sources/Graphite2D/Picture.swift +++ b/Sources/Graphite2D/Picture.swift @@ -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 { From 004e76acb96e008e8d96a02b799f11ac49e94ae1 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 3 Dec 2026 15:53:56 -0500 Subject: [PATCH 7/7] Test arithmetic that passes 32 bits --- .../Graphite2DTests/WideArithmeticTests.swift | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Tests/Graphite2DTests/WideArithmeticTests.swift diff --git a/Tests/Graphite2DTests/WideArithmeticTests.swift b/Tests/Graphite2DTests/WideArithmeticTests.swift new file mode 100644 index 0000000..9b3ea4d --- /dev/null +++ b/Tests/Graphite2DTests/WideArithmeticTests.swift @@ -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) + } +}