-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from jasonjmcghee/fix-external-monitor-bugs
fix external monitor bugs, mainly by calling CGDisplayCreateImage without passing the display frame
- Loading branch information
Showing
6 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,42 @@ | ||
// | ||
// ImageResizer.swift | ||
// rem | ||
// | ||
// Created by Jason McGhee on 1/17/24. | ||
// | ||
|
||
import Foundation | ||
import Cocoa | ||
|
||
class ImageResizer { | ||
private var context: CGContext | ||
private let targetWidth: CGFloat | ||
private let targetHeight: CGFloat | ||
|
||
init(targetWidth: Int, targetHeight: Int) { | ||
self.targetWidth = CGFloat(targetWidth) | ||
self.targetHeight = CGFloat(targetHeight) | ||
|
||
let colorSpace = CGColorSpaceCreateDeviceRGB() | ||
let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue | ||
context = CGContext(data: nil, width: targetWidth, height: targetHeight, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo)! | ||
} | ||
|
||
func resizeAndPad(image: CGImage) -> CGImage? { | ||
let widthScaleRatio = targetWidth / CGFloat(image.width) | ||
let heightScaleRatio = targetHeight / CGFloat(image.height) | ||
let scaleFactor = min(widthScaleRatio, heightScaleRatio) | ||
|
||
let scaledWidth = CGFloat(image.width) * scaleFactor | ||
let scaledHeight = CGFloat(image.height) * scaleFactor | ||
let imageRect = CGRect(x: (targetWidth - scaledWidth) / 2, y: (targetHeight - scaledHeight) / 2, width: scaledWidth, height: scaledHeight) | ||
|
||
context.clear(CGRect(x: 0, y: 0, width: targetWidth, height: targetHeight)) | ||
context.setFillColor(NSColor.black.cgColor) | ||
context.fill(CGRect(x: 0, y: 0, width: targetWidth, height: targetHeight)) | ||
context.interpolationQuality = .high | ||
context.draw(image, in: imageRect) | ||
|
||
return context.makeImage() | ||
} | ||
} |
This file contains 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
This file contains 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