Skip to content

Instantly share code, notes, and snippets.

@omarojo
Created January 29, 2020 02:05
Show Gist options
  • Save omarojo/05592dcf1500c22ae6a5a844f242acd2 to your computer and use it in GitHub Desktop.
Save omarojo/05592dcf1500c22ae6a5a844f242acd2 to your computer and use it in GitHub Desktop.
MetalPetal Aspect Ratio Cropping
//
// G8AspectRatio.swift
// GenerateMetal-iOS
//
// Created by Omar Juarez Ortiz on 2019-12-12.
// Copyright © 2019 Generate Software Inc. All rights reserved.
//
import Foundation
import MetalPetal
public enum G8OutputAspectRatio: String{
case arOriginal = "ORI"
case ar1by1 = "1:1"
//Portrait
case ar3by4 = "3:4"
case ar9by16 = "9:16"
//Landscape
case ar4by3 = "4:3"
case ar16by9 = "16:9"
func getRatio()->CGFloat{
switch self {
case .arOriginal:
return CGFloat(0.0)
case .ar1by1:
return (CGFloat(1.0)/CGFloat(1.0))
//Portrait
case .ar3by4:
return (CGFloat(3.0)/CGFloat(4.0))
case .ar9by16:
return (CGFloat(9.0)/CGFloat(16.0))
//Landscape
case .ar4by3:
return (CGFloat(4.0)/CGFloat(3.0))
case .ar16by9:
return (CGFloat(16.0)/CGFloat(9.0))
}
}
}
public func cropTo(image: MTIImage, targetAspectRatio: G8OutputAspectRatio) -> MTIImage{
if targetAspectRatio == .arOriginal {return image}
let inputSize = CGSize(width: image.extent.size.width, height: image.extent.size.height)
let inputRatio = image.extent.size.width / image.extent.size.height
let targetRatio = targetAspectRatio.getRatio()
var offset: CGFloat
var cropFrame: CGRect = CGRect(x: 0, y: 0, width: inputSize.width, height: inputSize.height) //default
if (inputRatio > targetRatio) {
offset = (inputRatio - targetRatio) * inputSize.height / inputSize.width;
cropFrame = CGRect(x:offset/2, y:0.0, width:1.0 - offset, height:1.0);
}
else {
offset = (1/inputRatio - 1/targetRatio) * inputSize.width / inputSize.height;
cropFrame = CGRect(x:0.0, y:offset/2, width:1.0, height:1.0 - offset);
}
let cropFilter = MTICropFilter()
cropFilter.cropRegion = MTICropRegion.init(bounds: cropFrame, unit: .percentage)
cropFilter.inputImage = image
return cropFilter.outputImage!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment