Skip to main content
API Reference SwiftyNsdk

ImageMath

Provides affine transformation utilities for image processing....

Declaration

struct ImageMath

Summary

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

NameTypeSummary
static affineCropCGAffineTransform
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.
static affineFitCGAffineTransform
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.
static affineInvertHorizontalCGAffineTransform
Returns an (u, v) -> (u', v') transformation that represents a horizontal mirroring, i.e. (u, v) -> (1 - u, v).
static affineInvertVerticalCGAffineTransform
Returns an (u, v) -> (u', v') transformation that represents a vertical mirroring, i.e. (u, v) -> (u, 1 - v).
static affineRotationCGAffineTransform
Returns an (u, v) -> (u', v') transformation that represents a 2D affine rotation.
static affineScalingCGAffineTransform
Returns an (u, v) -> (u', v') transformation that represents a 2D affine scaling.
static affineTranslationCGAffineTransform
Returns an (u, v) -> (u', v') transformation that represents a 2D affine translation.
static deviceRotationCGAffineTransform
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 CGAffineTransform representing the rotation.
static displayTransformCGAffineTransform
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 CGAffineTransform that 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.
static reprojectionmatrix_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.
static viewRotationCGAffineTransform
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 CGAffineTransform representing the rotation.