STRUCT
ImageMath
public struct ImageMath
Provides affine transformation utilities for image processing.
All affine matrices returned by this class operate in normalized coordinates, where image space is mapped to the [0, 1] range in both axes with origin at top-left.
Methods
reprojection(aspect:fovRadians:zNear:zFar:referenceView:targetView:backProjectionDistance:)
public static func reprojection(aspect: Float, fovRadians: Float, zNear: Float, zFar: Float,
referenceView: matrix_float4x4, targetView: matrix_float4x4, backProjectionDistance: Float = 0.9
) -> matrix_float3x3
Returns a 3×3 homography matrix as matrix_float3x3 that reprojects
image coordinates from a reference camera view into a target camera view.
The image coordinates are expected to be normalized [0..1].
- Parameters:
- aspect: The aspect ratio of the image (width / height).
- fovRadians: The vertical field of view in radians.
- zNear: Near clipping plane distance.
- zFar: Far clipping plane distance.
- referenceView: The reference camera view.
- targetView: The target camera view.
- backProjectionDistance: Depth at which to reproject, normalized between clipping planes near (0) and far (1). Recommended: 0.9.
- Returns: A 3×3 homography matrix in column-major order.
Parameters
| Name | Description |
|---|---|
| aspect | The aspect ratio of the image (width / height). |
| fovRadians | The vertical field of view in radians. |
| zNear | Near clipping plane distance. |
| zFar | Far clipping plane distance. |
| referenceView | The reference camera view. |
| targetView | The target camera view. |
| backProjectionDistance | Depth at which to reproject, normalized between clipping planes near (0) and far (1). Recommended: 0.9. |
displayTransform(for:viewportSize:imageSize:)
public static func displayTransform(for orientation: UIInterfaceOrientation,
viewportSize: CGSize,
imageSize: CGSize) -> CGAffineTransform
Returns an affine transform that maps normalized image coordinates into a coordinate space suitable for rendering the camera image in the given viewport and orientation.
This method replicates ARKit’s displayTransform(for:viewportSize:)
by computing a transform that accounts for the camera image’s
aspect ratio, the device’s interface orientation, and the desired
viewport size.
- Parameters:
- orientation: The current interface orientation of the device’s UI.
- viewportSize: The size of the viewport in which the image will be rendered.
- imageSize: The dimensions of the camera image.
- Returns: A
CGAffineTransformthat converts normalized image coordinates (with origin at top-left, ranging from 0.0 to 1.0) into the coordinate space of the viewport, accounting for orientation and aspect ratio.
Parameters
| Name | Description |
|---|---|
| orientation | The current interface orientation of the device’s UI. |
| viewportSize | The size of the viewport in which the image will be rendered. |
| imageSize | The dimensions of the camera image. |
viewRotation(fromOrientation:toOrientation:)
public static func viewRotation(fromOrientation: UIInterfaceOrientation, toOrientation: UIInterfaceOrientation) -> CGAffineTransform
Returns an affine transform that rotates between two interface orientations around the center.
This method models UI rotation (opposite to physical device rotation).
For example, rotating from .landscapeRight to .portrait results in a counter-clockwise transform.
- Parameters:
- fromOrientation: The starting interface orientation.
- toOrientation: The target interface orientation.
- Returns: A
CGAffineTransformrepresenting the rotation.
Parameters
| Name | Description |
|---|---|
| fromOrientation | The starting interface orientation. |
| toOrientation | The target interface orientation. |
deviceRotation(fromOrientation:toOrientation:)
public static func deviceRotation(fromOrientation: UIInterfaceOrientation, toOrientation: UIInterfaceOrientation) -> CGAffineTransform
Returns an affine transform that rotates between two interface orientations around the center.
This method models physical device rotation.
For example, rotating from .landscapeRight to .portrait results in a clockwise transform.
- Parameters:
- fromOrientation: The starting interface orientation.
- toOrientation: The target interface orientation.
- Returns: A
CGAffineTransformrepresenting the rotation.
Parameters
| Name | Description |
|---|---|
| fromOrientation | The starting interface orientation. |
| toOrientation | The target interface orientation. |
affineCrop(source:target:)
public static func affineCrop(source: CGSize, target: CGSize) -> CGAffineTransform
Returns an affine transformation that crops the source size to match the aspect ratio of the target size.
- Parameters:
- source: The size of the source container.
- target: The size of the target container.
- Returns: An affine transformation matrix that crops the source to fit the target aspect ratio.
Parameters
| Name | Description |
|---|---|
| source | The size of the source container. |
| target | The size of the target container. |
affineFit(source:sourceOrientation:target:targetOrientation:)
public static func affineFit(source: CGSize, sourceOrientation: UIInterfaceOrientation,
target: CGSize, targetOrientation: UIInterfaceOrientation) -> CGAffineTransform
Returns an affine transformation that maps normalized coordinates in the source frame to normalized coordinates in the target frame.
-
Note: In portrait, this function aligns the top and bottom edges to the target.
-
Parameters:
- source: The size of the source container.
- sourceOrientation: The orientation of the source container.
- target: The size of the target container.
- targetOrientation: The orientation of the target container.
-
Returns: An affine transformation matrix that fits the source into the target.
Parameters
| Name | Description |
|---|---|
| source | The size of the source container. |
| sourceOrientation | The orientation of the source container. |
| target | The size of the target container. |
| targetOrientation | The orientation of the target container. |
affineRotation(radians:)
public static func affineRotation(radians: Float) -> CGAffineTransform
Returns an (u, v) -> (u', v') transformation that represents a 2D affine rotation.
affineTranslation(tx:ty:)
public static func affineTranslation(tx: Float, ty: Float) -> CGAffineTransform
Returns an (u, v) -> (u', v') transformation that represents a 2D affine translation.
affineScaling(sx:sy:)
public static func affineScaling(sx: Float, sy: Float) -> CGAffineTransform
Returns an (u, v) -> (u', v') transformation that represents a 2D affine scaling.
affineInvertHorizontal()
public static func affineInvertHorizontal() -> CGAffineTransform
Returns an (u, v) -> (u', v') transformation that represents a horizontal mirroring, i.e. (u, v) -> (1 - u, v).
affineInvertVertical()
public static func affineInvertVertical() -> CGAffineTransform
Returns an (u, v) -> (u', v') transformation that represents a vertical mirroring, i.e. (u, v) -> (u, 1 - v).