-
Notifications
You must be signed in to change notification settings - Fork 36
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 #35 from pedroSG94/feature/view-filter
Feature/view filter
- Loading branch information
Showing
13 changed files
with
273 additions
and
5 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
97 changes: 97 additions & 0 deletions
97
RootEncoder/Sources/RootEncoder/encoder/input/metal/Sprite.swift
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,97 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Pedro on 9/8/24. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
public class Sprite { | ||
|
||
private var positionX: Double = 0 | ||
private var positionY: Double = 0 | ||
private var scaleX: Double = 100 | ||
private var scaleY: Double = 100 | ||
private var rotation: Double = 0 | ||
|
||
public func translateTo(translation: TranslateTo) { | ||
switch translation { | ||
case .CENTER: | ||
positionX = (100 - scaleX) / 2 | ||
positionY = (100 - scaleY) / 2 | ||
case .LEFT: | ||
positionX = 0 | ||
positionY = (100 - scaleY) / 2 | ||
case .RIGHT: | ||
positionX = 100 - scaleX | ||
positionY = (100 - scaleY) / 2 | ||
case .TOP: | ||
positionX = (100 - scaleX) / 2 | ||
positionY = 0 | ||
case .BOTTOM: | ||
positionX = (100 - scaleX) / 2 | ||
positionY = 100 - scaleY | ||
case .TOP_LEFT: | ||
positionX = 0 | ||
positionY = 0 | ||
case .TOP_RIGHT: | ||
positionX = 100 - scaleX | ||
positionY = 0 | ||
case .BOTTOM_LEFT: | ||
positionX = 0 | ||
positionY = 100 - scaleY | ||
case .BOTTOM_RIGHT: | ||
positionX = 100 - scaleX | ||
positionY = 100 - scaleY | ||
} | ||
} | ||
|
||
public func reset() { | ||
positionX = 0 | ||
positionY = 0 | ||
scaleX = 100 | ||
scaleY = 100 | ||
rotation = 0 | ||
} | ||
|
||
public func getCalculatedScale(image: CGRect, filter: CGRect) -> CGSize { | ||
let filterWidth = filter.width | ||
let filterHeight = filter.height | ||
|
||
let imageWidth = image.width | ||
let imageHeight = image.height | ||
|
||
let scaleX = imageWidth / filterWidth | ||
let scaleY = imageHeight / filterHeight | ||
|
||
let resultX = scaleX / (100 / self.scaleX) | ||
let resultY = scaleY / (100 / self.scaleY) | ||
return CGSize(width: resultX, height: resultY) | ||
} | ||
|
||
public func getCalculatedPosition(image: CGRect, filter: CGRect) -> CGSize { | ||
let resultX: Double = image.width * positionX / 100 | ||
let resultY: Double = (image.height - image.height * (scaleY / 100)) - (image.height * positionY / 100) | ||
return CGSize(width: resultX, height: resultY) | ||
} | ||
|
||
public func getCalculatedRotation() -> Double { | ||
return self.rotation * .pi / 180 | ||
} | ||
|
||
public func setPosition(x: Double, y: Double) { | ||
positionX = x | ||
positionY = y | ||
} | ||
|
||
public func setScale(x: Double, y: Double) { | ||
scaleX = x | ||
scaleY = y | ||
} | ||
|
||
public func setRotation(rotation: Double) { | ||
self.rotation = rotation | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
RootEncoder/Sources/RootEncoder/encoder/input/metal/filter/UIViewFilterRender.swift
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,60 @@ | ||
// | ||
// ViewFilterRender.swift | ||
// | ||
// | ||
// Created by Pedro on 4/8/24. | ||
// | ||
|
||
import Foundation | ||
import CoreImage | ||
import SwiftUI | ||
|
||
public class ViewFilterRender: BaseFilterRender { | ||
|
||
private let view: UIView | ||
private let sprite = Sprite() | ||
|
||
public init(view: UIView) { | ||
self.view = view | ||
} | ||
|
||
public func draw(image: CIImage, orientation: CGImagePropertyOrientation) -> CIImage { | ||
let filterView = toCIImage(view: view) | ||
guard let filterView = filterView else { return image } | ||
|
||
let scale = sprite.getCalculatedScale(image: image.extent, filter: filterView.extent) | ||
let position = sprite.getCalculatedPosition(image: image.extent, filter: filterView.extent) | ||
let rotation = sprite.getCalculatedRotation() | ||
|
||
let scaled = filterView | ||
.transformed(by: CGAffineTransform(scaleX: scale.width, y: scale.height)) | ||
.transformed(by: CGAffineTransform(rotationAngle: rotation)) | ||
.transformed(by: CGAffineTransform(translationX: position.width, y: position.height)) | ||
return scaled.composited(over: image) | ||
} | ||
|
||
private func toCIImage(view: UIView) -> CIImage? { | ||
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0) | ||
view.drawHierarchy(in: view.bounds, afterScreenUpdates: false) | ||
let image = UIGraphicsGetImageFromCurrentImageContext() | ||
UIGraphicsEndImageContext() | ||
guard let image = image else { return nil } | ||
return CIImage(image: image) | ||
} | ||
|
||
public func setScale(percentX: Double, percentY: Double) { | ||
sprite.setScale(x: percentX, y: percentY) | ||
} | ||
|
||
public func setPosition(percentX: Double, percentY: Double) { | ||
sprite.setPosition(x: percentX, y: percentY) | ||
} | ||
|
||
public func translateTo(translation: TranslateTo) { | ||
sprite.translateTo(translation: translation) | ||
} | ||
|
||
public func setRotation(rotation: Double) { | ||
sprite.setRotation(rotation: rotation) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
RootEncoder/Sources/RootEncoder/encoder/utils/TranslateTo.swift
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,21 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Pedro on 9/8/24. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
public enum TranslateTo { | ||
case CENTER | ||
case LEFT | ||
case RIGHT | ||
case TOP | ||
case BOTTOM | ||
case TOP_LEFT | ||
case TOP_RIGHT | ||
case BOTTOM_LEFT | ||
case BOTTOM_RIGHT | ||
} |
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
Binary file not shown.
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,24 @@ | ||
// | ||
// FilterUIView.swift | ||
// app | ||
// | ||
// Created by Pedro on 20/9/23. | ||
// Copyright © 2023 pedroSG94. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import RootEncoder | ||
import UIKit | ||
|
||
struct FilterUIView: UIViewRepresentable { | ||
|
||
let view = Bundle.main.loadNibNamed("ViewFilter", owner: nil, options: nil)![0] as! UIView | ||
|
||
public func makeUIView(context: Context) -> UIView { | ||
view.frame = .zero | ||
return view | ||
} | ||
|
||
public func updateUIView(_ uiView: UIView, context: Context) { | ||
} | ||
} |
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,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES"> | ||
<device id="retina6_12" orientation="portrait" appearance="light"/> | ||
<dependencies> | ||
<deployment identifier="iOS"/> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22685"/> | ||
<capability name="Safe area layout guides" minToolsVersion="9.0"/> | ||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> | ||
</dependencies> | ||
<objects> | ||
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/> | ||
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/> | ||
<view contentMode="scaleToFill" id="iN0-l3-epB"> | ||
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/> | ||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> | ||
<subviews> | ||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="This is a test text" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="PAI-hx-VNq"> | ||
<rect key="frame" x="132" y="416" width="129" height="20.333333333333314"/> | ||
<fontDescription key="fontDescription" type="system" pointSize="17"/> | ||
<nil key="textColor"/> | ||
<nil key="highlightedColor"/> | ||
</label> | ||
</subviews> | ||
<viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/> | ||
<constraints> | ||
<constraint firstItem="PAI-hx-VNq" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="TUr-zJ-SjC"/> | ||
<constraint firstItem="PAI-hx-VNq" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="centerY" id="uHb-UZ-hgh"/> | ||
</constraints> | ||
<point key="canvasLocation" x="90.140845070422543" y="-11.450381679389313"/> | ||
</view> | ||
</objects> | ||
</document> |