-
-
Notifications
You must be signed in to change notification settings - Fork 213
generate the image in swift and remove dependency on imagemagic and graphicsmagick #100
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
Open
gsabran
wants to merge
7
commits into
sindresorhus:main
Choose a base branch
from
gsabran:gui--coregraphicx
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1090197
generate the image in swift
gsabran 30a13c6
nits
gsabran fd15924
nit
gsabran 4b5be0b
address review
gsabran 5f15a15
fix tests
gsabran 8cbf75c
add snapshot tests for generated icon
gsabran adf5a6f
serial tests
gsabran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| node_modules | ||
| yarn.lock | ||
| .build |
This file contains hidden or 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,11 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Build the Swift package for composing icons | ||
| echo "Building Swift package..." | ||
| cd imageComposition | ||
| swift build --configuration release --product compose-icon | ||
|
|
||
| # Copy the built executable to the root directory for easy access | ||
| cp .build/release/compose-icon ../compose-icon | ||
|
|
||
| echo "Swift executable built successfully: compose-icon" |
Binary file not shown.
This file contains hidden or 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
7 changes: 7 additions & 0 deletions
7
imageComposition/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+26.2 KB
...pm/xcode/package.xcworkspace/xcuserdata/guigui.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
14 changes: 14 additions & 0 deletions
14
...mposition/.swiftpm/xcode/xcuserdata/guigui.xcuserdatad/xcschemes/xcschememanagement.plist
This file contains hidden or 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,14 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>SchemeUserState</key> | ||
| <dict> | ||
| <key>ComposeIcon.xcscheme_^#shared#^_</key> | ||
| <dict> | ||
| <key>orderHint</key> | ||
| <integer>0</integer> | ||
| </dict> | ||
| </dict> | ||
| </dict> | ||
| </plist> |
This file contains hidden or 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,22 @@ | ||
| // swift-tools-version: 5.7 | ||
|
|
||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
| name: "ComposeIcon", | ||
| platforms: [ | ||
| .macOS(.v11) | ||
| ], | ||
| products: [ | ||
| .executable( | ||
| name: "compose-icon", | ||
| targets: ["ComposeIcon"] | ||
| ) | ||
| ], | ||
| targets: [ | ||
| .executableTarget( | ||
| name: "ComposeIcon", | ||
| dependencies: [] | ||
| ) | ||
| ] | ||
| ) |
This file contains hidden or 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,165 @@ | ||
| import CoreGraphics | ||
| import CoreImage | ||
| import Foundation | ||
| import CoreImage.CIFilterBuiltins | ||
| import ImageIO | ||
| import UniformTypeIdentifiers | ||
|
|
||
| func cgContext(width: Int, height: Int) -> CGContext? { | ||
| CGContext( | ||
| data: nil, | ||
| width: width, | ||
| height: height, | ||
| bitsPerComponent: 8, | ||
| bytesPerRow: 0, | ||
| space: CGColorSpaceCreateDeviceRGB(), | ||
| bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue | ||
| ) | ||
| } | ||
|
|
||
| func perspectiveTransform(image: CGImage, width: Int, height: Int) -> CGImage? { | ||
| // Apply perspective transformation directly to the image | ||
| let ciImage = CIImage(cgImage: image) | ||
| let filter = CIFilter.perspectiveTransform() | ||
|
|
||
| let w = CGFloat(width) | ||
| let h = CGFloat(height) | ||
|
|
||
| // From original JS transformation: top gets narrower by 8% on each side | ||
| // CIFilter uses bottom-left origin | ||
| filter.setValue(ciImage, forKey: kCIInputImageKey) | ||
| filter.setValue(CIVector(x: w * 0.08, y: h), forKey: "inputTopLeft") // Top-left: inset 8% | ||
| filter.setValue(CIVector(x: w * 0.92, y: h), forKey: "inputTopRight") // Top-right: inset to 92% | ||
| filter.setValue(CIVector(x: 0, y: 0), forKey: "inputBottomLeft") // Bottom-left: no change | ||
| filter.setValue(CIVector(x: w, y: 0), forKey: "inputBottomRight") // Bottom-right: no change | ||
|
|
||
| guard let outputImage = filter.outputImage else { return nil } | ||
|
|
||
| // Create context for the final image | ||
| guard let ctx = cgContext(width: width, height: height) else { return nil } | ||
|
|
||
| ctx.clear(CGRect(x: 0, y: 0, width: width, height: height)) | ||
|
|
||
| let ciContext = CIContext() | ||
| guard let finalCGImage = ciContext.createCGImage(outputImage, from: outputImage.extent) else { return nil } | ||
|
|
||
| // Crop to original size if needed | ||
| if finalCGImage.width == width && finalCGImage.height == height { | ||
| return finalCGImage | ||
| } else { | ||
| let sourceRect = CGRect(x: 0, y: 0, width: width, height: height) | ||
| return finalCGImage.cropping(to: sourceRect) | ||
| } | ||
| } | ||
|
|
||
| func resizeImage(image: CGImage, width: Int, height: Int) -> CGImage? { | ||
| guard let ctx = cgContext(width: width, height: height) else { return nil } | ||
|
|
||
| ctx.clear(CGRect(x: 0, y: 0, width: width, height: height)) | ||
| ctx.draw(image, in: CGRect(x: 0, y: 0, width: width, height: height)) | ||
|
|
||
| return ctx.makeImage() | ||
| } | ||
|
|
||
| func compositeImages(baseImage: CGImage, overlayImage: CGImage, offsetY: CGFloat) -> CGImage? { | ||
| let width = baseImage.width | ||
| let height = baseImage.height | ||
|
|
||
| guard let ctx = cgContext(width: width, height: height) else { return nil } | ||
|
|
||
| // Draw base image | ||
| ctx.draw(baseImage, in: CGRect(x: 0, y: 0, width: width, height: height)) | ||
|
|
||
| // Calculate centered position with offset (matching GraphicsMagick's gravity('Center').geometry('+0-offset')) | ||
| let overlayWidth = overlayImage.width | ||
| let overlayHeight = overlayImage.height | ||
|
|
||
| // Center horizontally and vertically, then apply upward offset | ||
| // CoreGraphics uses bottom-left origin, so we need to flip Y coordinate | ||
| let x = CGFloat(width - overlayWidth) / 2.0 | ||
| let centerY = CGFloat(height - overlayHeight) / 2.0 | ||
| let y = centerY + offsetY // In CoreGraphics, positive Y moves up from bottom-left origin | ||
|
|
||
|
|
||
| // Draw overlay image | ||
| ctx.draw(overlayImage, in: CGRect(x: x, y: y, width: CGFloat(overlayWidth), height: CGFloat(overlayHeight))) | ||
|
|
||
| return ctx.makeImage() | ||
| } | ||
|
|
||
| func loadImage(from path: String) -> CGImage? { | ||
| guard let imageSource = CGImageSourceCreateWithURL(URL(fileURLWithPath: path) as CFURL, nil) else { | ||
| return nil | ||
| } | ||
|
|
||
| return CGImageSourceCreateImageAtIndex(imageSource, 0, nil) | ||
| } | ||
|
|
||
| func saveImage(_ image: CGImage, to path: String) -> Bool { | ||
| guard let destination = CGImageDestinationCreateWithURL(URL(fileURLWithPath: path) as CFURL, UTType.png.identifier as CFString, 1, nil) else { | ||
| return false | ||
| } | ||
|
|
||
| CGImageDestinationAddImage(destination, image, nil) | ||
| return CGImageDestinationFinalize(destination) | ||
| } | ||
|
|
||
| // Main program | ||
| guard CommandLine.arguments.count == 4 else { | ||
| print("Usage: compose-icon <app-icon-path> <mount-icon-path> <output-path>") | ||
| exit(1) | ||
| } | ||
|
|
||
| let appIconPath = CommandLine.arguments[1] | ||
| let mountIconPath = CommandLine.arguments[2] | ||
| let outputPath = CommandLine.arguments[3] | ||
|
|
||
| guard let appImage = loadImage(from: appIconPath), | ||
| let mountImage = loadImage(from: mountIconPath) else { | ||
| print("Error: Could not load input images") | ||
| exit(1) | ||
| } | ||
|
|
||
| let appIconSize = (width: appImage.width, height: appImage.height) | ||
| let mountIconSize = (width: mountImage.width, height: mountImage.height) | ||
|
|
||
| // Apply perspective transformation | ||
| guard let transformedAppImage = perspectiveTransform( | ||
| image: appImage, | ||
| width: appIconSize.width, | ||
| height: appIconSize.height | ||
| ) else { | ||
| print("Error: Could not apply perspective transformation") | ||
| exit(1) | ||
| } | ||
|
|
||
| // Resize app icon to fit inside mount icon (from JS: width / 1.58, height / 1.82) | ||
| let resizedWidth = Int(Double(mountIconSize.width) / 1.58) | ||
| let resizedHeight = Int(Double(mountIconSize.height) / 1.82) | ||
|
|
||
| guard let resizedAppImage = resizeImage( | ||
| image: transformedAppImage, | ||
| width: resizedWidth, | ||
| height: resizedHeight | ||
| ) else { | ||
| print("Error: Could not resize app image") | ||
| exit(1) | ||
| } | ||
|
|
||
| // Composite images with offset (from JS: mountIconSize.height * 0.063) | ||
| let offsetY = CGFloat(mountIconSize.height) * 0.063 | ||
|
|
||
| guard let composedImage = compositeImages( | ||
| baseImage: mountImage, | ||
| overlayImage: resizedAppImage, | ||
| offsetY: offsetY | ||
| ) else { | ||
| print("Error: Could not composite images") | ||
| exit(1) | ||
| } | ||
|
|
||
| // Save result | ||
| guard saveImage(composedImage, to: outputPath) else { | ||
| print("Error: Could not save output image") | ||
| exit(1) | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of using the typed filter is to use the typed properties.