Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Draw the device sticker's picture at the size it shows

Țugui Dragoș263bacd3da58
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2421f066..d1cd33c4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,14 @@
Every release of Peel is listed here, newest first. The version is the one About Peel shows, and each entry groups
its changes the way [Keep a Changelog](https://keepachangelog.com/) suggests.
+## 1.2.1 (not released yet)
+
+### Fixed
+
+#### Home
+
+- The picture of your Mac is sharp: it was drawn from a small rendition and enlarged.
+
## 1.2.0 (2026-10-24)
### Added
diff --git a/Peel/Views/Stickers/StickerStyle.swift b/Peel/Views/Stickers/StickerStyle.swift
index 788815d7..3fec48e5 100644
--- a/Peel/Views/Stickers/StickerStyle.swift
+++ b/Peel/Views/Stickers/StickerStyle.swift
@@ -75,8 +75,12 @@ struct DieCut: View {
var body: some View {
Canvas { context, size in
- // One resolved image for contour and picture: SwiftUI picks an icon's rep by scale, and reps differ.
- let picture = context.resolve(Image(nsImage: image))
+ let side = min(size.width, size.height)
+ guard side > 0, let sized = image.copy() as? NSImage else { return }
+ // SwiftUI draws an NSImage from the rendition of its own size, so the copy takes the size it shows here.
+ // One resolved image serves contour and picture, since renditions are composed differently.
+ sized.size = NSSize(width: side, height: side)
+ let picture = context.resolve(Image(nsImage: sized))
let scale = min(size.width / picture.size.width, size.height / picture.size.height)
let fitted = CGSize(width: picture.size.width * scale, height: picture.size.height * scale)
let frame = CGRect(
diff --git a/PeelTests/DieCutTests.swift b/PeelTests/DieCutTests.swift
index 4ee570f1..56f3f16d 100644
--- a/PeelTests/DieCutTests.swift
+++ b/PeelTests/DieCutTests.swift
@@ -7,13 +7,10 @@ import UniformTypeIdentifiers
struct DieCutTests {
@Test(arguments: ["Mac13,1", "Mac14,2", "VirtualMac"], [2.0, 1.0])
func theContourIsAsWideAllAroundThePictureAndNoWider(model: String, scale: Double) throws {
- let kind = try #require(
- UTType(tag: model, tagClass: UTTagClass(rawValue: "com.apple.device-model-code"), conformingTo: nil)
- )
- let picture = NSWorkspace.shared.icon(for: kind)
+ let picture = try self.picture(of: model)
- let sticker = try solidPart(of: DieCut(image: picture), scale: scale)
- let alone = try solidPart(of: Image(nsImage: picture).resizable().aspectRatio(contentMode: .fit), scale: scale)
+ let sticker = try solidPart(of: try rendered(DieCut(image: picture), padding: 8, scale: scale), scale: scale)
+ let alone = try solidPart(of: try drawnByAppKit(picture, side: 120 * scale, inset: 8 * scale), scale: scale)
let reach = [
alone.minY - sticker.minY, sticker.maxY - alone.maxY, alone.minX - sticker.minX, sticker.maxX - alone.maxX,
]
@@ -21,18 +18,72 @@ struct DieCutTests {
#expect(reach.allSatisfy { (3.5...5.5).contains($0) }, "\(reach)")
}
- private func solidPart(of view: some View, scale: Double) throws -> CGRect {
+ @Test(arguments: ["Mac15,12", "Mac13,1", "VirtualMac"], [2.0, 1.0])
+ func thePictureIsAsSharpAsAppKitDrawsIt(model: String, scale: Double) throws {
+ let picture = try self.picture(of: model)
+ let side = Int(104 * scale)
+
+ let shape = try pixels(of: try drawnByAppKit(picture, side: 104 * scale, inset: 0))
+ let drawn = detail(of: try pixels(of: try rendered(DieCut(image: picture), padding: 0, scale: scale)),
+ inside: shape, side: side)
+ let drawnByAppKit = detail(of: shape, inside: shape, side: side)
+
+ #expect(drawn >= drawnByAppKit / 2, "\(drawn) of \(drawnByAppKit)")
+ }
+
+ private func picture(of model: String) throws -> NSImage {
+ let kind = try #require(
+ UTType(tag: model, tagClass: UTTagClass(rawValue: "com.apple.device-model-code"), conformingTo: nil)
+ )
+ return NSWorkspace.shared.icon(for: kind)
+ }
+
+ private func rendered(_ view: some View, padding: CGFloat, scale: Double) throws -> CGImage {
let renderer = ImageRenderer(
- content: view.frame(width: 104, height: 104).padding(8).environment(\.colorScheme, .light)
+ content: view.frame(width: 104, height: 104).padding(padding).environment(\.colorScheme, .light)
)
renderer.scale = scale
- let image = try #require(renderer.cgImage)
- let context = try #require(CGContext(
- data: nil, width: image.width, height: image.height, bitsPerComponent: 8, bytesPerRow: image.width * 4,
- space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
+ return try #require(renderer.cgImage)
+ }
+
+ private func drawnByAppKit(_ picture: NSImage, side: Double, inset: Double) throws -> CGImage {
+ let pixels = Int(side)
+ let bitmap = try #require(NSBitmapImageRep(
+ bitmapDataPlanes: nil, pixelsWide: pixels, pixelsHigh: pixels, bitsPerSample: 8, samplesPerPixel: 4,
+ hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: pixels * 4, bitsPerPixel: 32
))
- context.draw(image, in: CGRect(x: 0, y: 0, width: image.width, height: image.height))
- let pixels = try #require(context.data).bindMemory(to: UInt8.self, capacity: image.width * image.height * 4)
+ NSGraphicsContext.saveGraphicsState()
+ NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: bitmap)
+ picture.draw(in: NSRect(x: inset, y: inset, width: side - 2 * inset, height: side - 2 * inset))
+ NSGraphicsContext.restoreGraphicsState()
+ return try #require(bitmap.cgImage)
+ }
+
+ /// The energy of the luminance's Laplacian where `shape` is opaque all around a pixel, so inside the picture and
+ /// never on the sticker's edge.
+ private func detail(of image: [UInt8], inside shape: [UInt8], side: Int) -> Double {
+ func isOpaque(_ x: Int, _ y: Int) -> Bool { shape[(y * side + x) * 4 + 3] >= 250 }
+ func isInside(_ x: Int, _ y: Int) -> Bool {
+ isOpaque(x, y) && isOpaque(x - 1, y) && isOpaque(x + 1, y) && isOpaque(x, y - 1) && isOpaque(x, y + 1)
+ }
+ func luminance(_ x: Int, _ y: Int) -> Double {
+ let pixel = (y * side + x) * 4
+ return 0.299 * Double(image[pixel]) + 0.587 * Double(image[pixel + 1]) + 0.114 * Double(image[pixel + 2])
+ }
+ var energy = 0.0, count = 0
+ for y in 1..<(side - 1) {
+ for x in 1..<(side - 1) where isInside(x, y) {
+ let laplacian = 4 * luminance(x, y) - luminance(x - 1, y) - luminance(x + 1, y)
+ - luminance(x, y - 1) - luminance(x, y + 1)
+ energy += laplacian * laplacian
+ count += 1
+ }
+ }
+ return count > 0 ? energy / Double(count) : 0
+ }
+
+ private func solidPart(of image: CGImage, scale: Double) throws -> CGRect {
+ let pixels = try pixels(of: image)
var solid = CGRect.null
for y in 0..<image.height {
for x in 0..<image.width where pixels[(y * image.width + x) * 4 + 3] >= 128 {
@@ -42,4 +93,15 @@ struct DieCutTests {
}
return solid
}
+
+ private func pixels(of image: CGImage) throws -> [UInt8] {
+ let context = try #require(CGContext(
+ data: nil, width: image.width, height: image.height, bitsPerComponent: 8, bytesPerRow: image.width * 4,
+ space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
+ ))
+ context.draw(image, in: CGRect(x: 0, y: 0, width: image.width, height: image.height))
+ let count = image.width * image.height * 4
+ let buffer = try #require(context.data).bindMemory(to: UInt8.self, capacity: count)
+ return Array(UnsafeBufferPointer(start: buffer, count: count))
+ }
}