diff options
author | Navan Chauhan <navanchauhan@gmail.com> | 2025-03-28 12:22:58 -0600 |
---|---|---|
committer | Navan Chauhan <navanchauhan@gmail.com> | 2025-03-28 12:22:58 -0600 |
commit | bb018b2d346134c3f399b2424a9f5908d7ecf455 (patch) | |
tree | e4bdb08e1eaf88a36466bef3ca929af04d6b7aec /iTexSnip | |
parent | d5e7bc49ce1ce7e8005089164815df3550dfffee (diff) |
disable trimming white borders
Diffstat (limited to 'iTexSnip')
-rw-r--r-- | iTexSnip/Utils/ImageUtils.swift | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/iTexSnip/Utils/ImageUtils.swift b/iTexSnip/Utils/ImageUtils.swift index eead8e6..3a93492 100644 --- a/iTexSnip/Utils/ImageUtils.swift +++ b/iTexSnip/Utils/ImageUtils.swift @@ -94,7 +94,8 @@ func trimWhiteBorder(image: CIImage) -> CIImage? { return image.cropped(to: croppedRect) } func addWhiteBorder(to image: CIImage, maxSize: CGFloat) -> CIImage { - let randomPadding = (0..<4).map { _ in CGFloat(arc4random_uniform(UInt32(maxSize))) } + let clampedMaxSize = max(0, min(maxSize, CGFloat(UInt32.max))) + let randomPadding = (0..<4).map { _ in CGFloat(arc4random_uniform(UInt32(clampedMaxSize))) } var xPadding = randomPadding[0] + randomPadding[2] var yPadding = randomPadding[1] + randomPadding[3] @@ -127,11 +128,39 @@ func padding(images: [CIImage], requiredSize: CGFloat) -> [CIImage] { } } +func resizeIfLarger(image: CIImage, maxSize: CGFloat) -> CIImage { + let width = image.extent.width + let height = image.extent.height + + if width <= maxSize && height <= maxSize { + return image + } + + let aspectRatio = width / height + var newWidth: CGFloat + var newHeight: CGFloat + + if aspectRatio > 1 { + newWidth = maxSize + newHeight = maxSize / aspectRatio + } else { + newHeight = maxSize + newWidth = maxSize * aspectRatio + } + + let scaleX = newWidth / width + let scaleY = newHeight / height + let transform = CGAffineTransform(scaleX: scaleX, y: scaleY) + return image.transformed(by: transform) +} + func inferenceTransform(images: [NSImage]) -> [CIImage] { let ciImages = images.compactMap { nsImageToCIImage($0) } - - let trimmedImages = ciImages.compactMap { trimWhiteBorder(image: $0) } - let paddedImages = padding(images: trimmedImages, requiredSize: FIXED_IMG_SIZE) + // let trimmedImages = ciImages.compactMap { trimWhiteBorder(image: $0) } + // let resizedImages = trimmedImages.map { resizeIfLarger(image: $0, maxSize: FIXED_IMG_SIZE) } + // let paddedImages = padding(images: trimmedImages, requiredSize: FIXED_IMG_SIZE) + let resizedImages = ciImages.map { resizeIfLarger(image: $0, maxSize: FIXED_IMG_SIZE) } + let paddedImages = padding(images: resizedImages, requiredSize: FIXED_IMG_SIZE) return paddedImages } |