objc2_metal_performance_shaders/generated/MPSImage/
MPSImageEDLines.rs

1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5use objc2::__framework_prelude::*;
6use objc2_foundation::*;
7use objc2_metal::*;
8
9use crate::*;
10
11extern_class!(
12    /// The MPSImageEDLInes class implements the EDLines line segmenting algorithm using edge-drawing (ED)
13    /// described here
14    /// https://ieeexplore.ieee.org/document/6116138
15    ///
16    /// The EDLInes algorithm consists of 5 steps, the first 4 of which describe the ED algorithm:
17    /// 1. Blur the source image using a Gaussian blur with a sigma parameter
18    /// 2. Use horizontal and vertical Sobel filters to find a gradient magnitude and
19    /// direction.
20    /// G = sqrt(Sx^2 + Sy^2)
21    /// G_ang = arctan(Sy / Sx)
22    /// 3. Compute anchor points, points with a high probability of being edge pixels.
23    /// Anchor points are local maxima, in the gradient image that lie on row and column
24    /// multiples of the detailRatio. This parameter effectively downsamples the gradient
25    /// image, and directly influences the density of anchor points. A larger detailRatio results
26    /// in fewer fine grained details, leaving long, main lines.
27    /// 4. Anchor points are traced in a forward and backward direction along the gradient direction, until
28    /// the gradient falls below some gradientThreshold parameter or the edge of the image is reached.
29    /// The paths traced become an edge map of the image.
30    /// 5. Points in the edges are fit to a line), and extended along the edge until the line error crosses a
31    /// lineErrorThreshold. Lines which are beyond a minimum length are labelled line segments and
32    /// will be outputs of the algorithm.
33    ///
34    /// See also [Apple's documentation](https://developer.apple.com/documentation/metalperformanceshaders/mpsimageedlines?language=objc)
35    #[unsafe(super(MPSKernel, NSObject))]
36    #[derive(Debug, PartialEq, Eq, Hash)]
37    #[cfg(all(feature = "MPSCore", feature = "MPSKernel"))]
38    pub struct MPSImageEDLines;
39);
40
41#[cfg(all(feature = "MPSCore", feature = "MPSKernel"))]
42extern_conformance!(
43    unsafe impl NSCoding for MPSImageEDLines {}
44);
45
46#[cfg(all(feature = "MPSCore", feature = "MPSKernel"))]
47extern_conformance!(
48    unsafe impl NSCopying for MPSImageEDLines {}
49);
50
51#[cfg(all(feature = "MPSCore", feature = "MPSKernel"))]
52unsafe impl CopyingHelper for MPSImageEDLines {
53    type Result = Self;
54}
55
56#[cfg(all(feature = "MPSCore", feature = "MPSKernel"))]
57extern_conformance!(
58    unsafe impl NSObjectProtocol for MPSImageEDLines {}
59);
60
61#[cfg(all(feature = "MPSCore", feature = "MPSKernel"))]
62extern_conformance!(
63    unsafe impl NSSecureCoding for MPSImageEDLines {}
64);
65
66#[cfg(all(feature = "MPSCore", feature = "MPSKernel"))]
67impl MPSImageEDLines {
68    extern_methods!(
69        /// Initialize an EDLines kernel on a given device with specified parameters.
70        ///
71        /// Parameter `device`: The device the filter will run on
72        ///
73        /// Parameter `gaussianSigma`: The standard deviation of gaussian blur filter.
74        /// Gaussian weight, centered at 0, at integer grid i is given as
75        ///
76        /// ```text
77        ///                           w(i) = 1/sqrt(2*pi*sigma) * exp(-i^2/(2*sigma^2))
78        /// ```
79        ///
80        /// If we take cut off at 1% of w(0) (max weight) beyond which weights
81        /// are considered 0, we have
82        ///
83        /// ```text
84        ///                           ceil (sqrt(-log(0.01)*2)*sigma) ~ ceil(3.7*sigma)
85        /// ```
86        ///
87        /// as rough estimate of filter width
88        ///
89        /// Parameter `minLineLength`: The minimum length of output line segments.
90        ///
91        /// Parameter `maxLines`: The maximum amount of lines for the EDLines algorithm to output. The size of the
92        /// endpointBuffer supplied at encode must be >= maxLines * 4 * sizeof(unsigned short) + sizeof(uint32_t).
93        ///
94        /// Parameter `detailRatio`: The detailRatio to use in the EDLines algorithm, which
95        /// inversely effects the number of anchor points
96        ///
97        /// Parameter `gradientThreshold`: Any pixel with a gradient below the gradientThreshold will
98        /// not be considerd an edge
99        ///
100        /// Parameter `lineErrorThreshold`: The limit of how much error a line segment can have relative
101        /// to the edge it represents
102        ///
103        /// Parameter `mergeLocalityThreshold`: Determines how many pixels apart two lines can deviate spatially and still be merged.
104        /// This value is normalized to the diagonal length of the image.
105        ///
106        /// Returns: A valid object or nil, if failure.
107        #[unsafe(method(initWithDevice:gaussianSigma:minLineLength:maxLines:detailRatio:gradientThreshold:lineErrorThreshold:mergeLocalityThreshold:))]
108        #[unsafe(method_family = init)]
109        pub unsafe fn initWithDevice_gaussianSigma_minLineLength_maxLines_detailRatio_gradientThreshold_lineErrorThreshold_mergeLocalityThreshold(
110            this: Allocated<Self>,
111            device: &ProtocolObject<dyn MTLDevice>,
112            gaussian_sigma: c_float,
113            min_line_length: c_ushort,
114            max_lines: NSUInteger,
115            detail_ratio: c_ushort,
116            gradient_threshold: c_float,
117            line_error_threshold: c_float,
118            merge_locality_threshold: c_float,
119        ) -> Retained<Self>;
120
121        /// NSSecureCoding compatability
122        ///
123        /// While the standard NSSecureCoding/NSCoding method
124        /// -initWithCoder: should work, since the file can't
125        /// know which device your data is allocated on, we
126        /// have to guess and may guess incorrectly.  To avoid
127        /// that problem, use initWithCoder:device instead.
128        ///
129        /// Parameter `aDecoder`: The NSCoder subclass with your serialized MPSKernel
130        ///
131        /// Parameter `device`: The MTLDevice on which to make the MPSKernel
132        ///
133        /// Returns: A new MPSKernel object, or nil if failure.
134        ///
135        /// # Safety
136        ///
137        /// `a_decoder` possibly has further requirements.
138        #[unsafe(method(initWithCoder:device:))]
139        #[unsafe(method_family = init)]
140        pub unsafe fn initWithCoder_device(
141            this: Allocated<Self>,
142            a_decoder: &NSCoder,
143            device: &ProtocolObject<dyn MTLDevice>,
144        ) -> Option<Retained<Self>>;
145
146        /// Encode the filter to a command buffer using a MTLComputeCommandEncoder.
147        ///
148        /// The filter will not begin to execute until after the command
149        /// buffer has been enqueued and committed.
150        ///
151        ///
152        /// Parameter `commandBuffer`: A valid MTLCommandBuffer.
153        ///
154        /// Parameter `source`: A valid MTLTexture containing the source image for the filter
155        ///
156        /// Parameter `dest`: A valid MTLTexture containing the destination image for the filter. If not nil, the output will be the edges
157        /// found through the Edge Drawing algorithm.
158        ///
159        /// Parameter `endpointBuffer`: A valid MTLBuffer to receive the line segment count and endpoint results.
160        ///
161        /// Parameter `endpointOffset`: Byte offset into endpoint buffer at which to write the  line segment endpoint results. Must be a multiple of 32 bytes.
162        /// The total line segment count and the line segment endpoints are written to the endpoint buffer. The count
163        /// is written as a uint32_t at the start of the buffer. The line segments are written to the endpoint buffer as
164        /// start and end pixel coordinates of the segment. Coordinates are stored as unsigned short pairs, and a
165        /// single line segment will consist of two pairs, or four total unsigned shorts. The endpoint buffer size must
166        /// be >= 4 * maxLines * sizeof(unsigned short) + sizeof(uint32_t).
167        ///
168        /// # Safety
169        ///
170        /// - `source` may need to be synchronized.
171        /// - `source` may be unretained, you must ensure it is kept alive while in use.
172        /// - `dest` may need to be synchronized.
173        /// - `dest` may be unretained, you must ensure it is kept alive while in use.
174        /// - `endpoint_buffer` may need to be synchronized.
175        /// - `endpoint_buffer` may be unretained, you must ensure it is kept alive while in use.
176        /// - `endpoint_buffer` contents should be of the correct type.
177        #[unsafe(method(encodeToCommandBuffer:sourceTexture:destinationTexture:endpointBuffer:endpointOffset:))]
178        #[unsafe(method_family = none)]
179        pub unsafe fn encodeToCommandBuffer_sourceTexture_destinationTexture_endpointBuffer_endpointOffset(
180            &self,
181            command_buffer: &ProtocolObject<dyn MTLCommandBuffer>,
182            source: &ProtocolObject<dyn MTLTexture>,
183            dest: Option<&ProtocolObject<dyn MTLTexture>>,
184            endpoint_buffer: &ProtocolObject<dyn MTLBuffer>,
185            endpoint_offset: NSUInteger,
186        );
187
188        /// The source rectangle to use when reading data.
189        ///
190        /// A MTLRegion that indicates which part of the source to read. If the clipRectSource does not lie
191        /// completely within the source image, the intersection of the image bounds and clipRectSource will
192        /// be used. The clipRectSource replaces the MPSUnaryImageKernel offset parameter for this filter.
193        /// The latter is ignored.   Default: MPSRectNoClip, use the entire source texture.
194        #[unsafe(method(clipRectSource))]
195        #[unsafe(method_family = none)]
196        pub unsafe fn clipRectSource(&self) -> MTLRegion;
197
198        /// Setter for [`clipRectSource`][Self::clipRectSource].
199        #[unsafe(method(setClipRectSource:))]
200        #[unsafe(method_family = none)]
201        pub unsafe fn setClipRectSource(&self, clip_rect_source: MTLRegion);
202
203        /// Read-only sigma value used in performing Gaussian blur of the image.
204        /// Default is 2.0
205        #[unsafe(method(gaussianSigma))]
206        #[unsafe(method_family = none)]
207        pub unsafe fn gaussianSigma(&self) -> c_float;
208
209        /// Read-write value used to set the minimum length of a line segment.
210        /// Default is 32
211        #[unsafe(method(minLineLength))]
212        #[unsafe(method_family = none)]
213        pub unsafe fn minLineLength(&self) -> c_ushort;
214
215        /// Setter for [`minLineLength`][Self::minLineLength].
216        #[unsafe(method(setMinLineLength:))]
217        #[unsafe(method_family = none)]
218        pub unsafe fn setMinLineLength(&self, min_line_length: c_ushort);
219
220        /// Read-write value used to set the max number of line segments to be written out.
221        /// The endpointBuffer at encode must be >= maxLines * 4 * sizeof(unsigned short) + sizeof(uint32_t).
222        /// Default is 256
223        #[unsafe(method(maxLines))]
224        #[unsafe(method_family = none)]
225        pub unsafe fn maxLines(&self) -> NSUInteger;
226
227        /// Setter for [`maxLines`][Self::maxLines].
228        #[unsafe(method(setMaxLines:))]
229        #[unsafe(method_family = none)]
230        pub unsafe fn setMaxLines(&self, max_lines: NSUInteger);
231
232        /// Read-write value used to set the detailRatio to use in the EDLines algorithm
233        /// Default is 32
234        #[unsafe(method(detailRatio))]
235        #[unsafe(method_family = none)]
236        pub unsafe fn detailRatio(&self) -> c_ushort;
237
238        /// Setter for [`detailRatio`][Self::detailRatio].
239        #[unsafe(method(setDetailRatio:))]
240        #[unsafe(method_family = none)]
241        pub unsafe fn setDetailRatio(&self, detail_ratio: c_ushort);
242
243        /// Read-write value used to set the threshold for a pixel to be considered an edge
244        /// Default is 0.2
245        #[unsafe(method(gradientThreshold))]
246        #[unsafe(method_family = none)]
247        pub unsafe fn gradientThreshold(&self) -> c_float;
248
249        /// Setter for [`gradientThreshold`][Self::gradientThreshold].
250        #[unsafe(method(setGradientThreshold:))]
251        #[unsafe(method_family = none)]
252        pub unsafe fn setGradientThreshold(&self, gradient_threshold: c_float);
253
254        /// Read-write value used to set the limit on error for a line segment relative to the edge it fits
255        /// Default is 0.05
256        #[unsafe(method(lineErrorThreshold))]
257        #[unsafe(method_family = none)]
258        pub unsafe fn lineErrorThreshold(&self) -> c_float;
259
260        /// Setter for [`lineErrorThreshold`][Self::lineErrorThreshold].
261        #[unsafe(method(setLineErrorThreshold:))]
262        #[unsafe(method_family = none)]
263        pub unsafe fn setLineErrorThreshold(&self, line_error_threshold: c_float);
264
265        /// Read-write value used to set how many pixels apart two lines can deviate spatially and still be merged.
266        /// Default is 0.0025
267        #[unsafe(method(mergeLocalityThreshold))]
268        #[unsafe(method_family = none)]
269        pub unsafe fn mergeLocalityThreshold(&self) -> c_float;
270
271        /// Setter for [`mergeLocalityThreshold`][Self::mergeLocalityThreshold].
272        #[unsafe(method(setMergeLocalityThreshold:))]
273        #[unsafe(method_family = none)]
274        pub unsafe fn setMergeLocalityThreshold(&self, merge_locality_threshold: c_float);
275    );
276}
277
278/// Methods declared on superclass `MPSKernel`.
279#[cfg(all(feature = "MPSCore", feature = "MPSKernel"))]
280impl MPSImageEDLines {
281    extern_methods!(
282        /// Standard init with default properties per filter type
283        ///
284        /// Parameter `device`: The device that the filter will be used on. May not be NULL.
285        ///
286        /// Returns: a pointer to the newly initialized object. This will fail, returning
287        /// nil if the device is not supported. Devices must be
288        /// MTLFeatureSet_iOS_GPUFamily2_v1 or later.
289        #[unsafe(method(initWithDevice:))]
290        #[unsafe(method_family = init)]
291        pub unsafe fn initWithDevice(
292            this: Allocated<Self>,
293            device: &ProtocolObject<dyn MTLDevice>,
294        ) -> Retained<Self>;
295
296        /// Called by NSCoder to decode MPSKernels
297        ///
298        /// This isn't the right interface to decode a MPSKernel, but
299        /// it is the one that NSCoder uses. To enable your NSCoder
300        /// (e.g. NSKeyedUnarchiver) to set which device to use
301        /// extend the object to adopt the MPSDeviceProvider
302        /// protocol. Otherwise, the Metal system default device
303        /// will be used.
304        ///
305        /// # Safety
306        ///
307        /// `a_decoder` possibly has further requirements.
308        #[unsafe(method(initWithCoder:))]
309        #[unsafe(method_family = init)]
310        pub unsafe fn initWithCoder(
311            this: Allocated<Self>,
312            a_decoder: &NSCoder,
313        ) -> Option<Retained<Self>>;
314    );
315}
316
317/// Methods declared on superclass `NSObject`.
318#[cfg(all(feature = "MPSCore", feature = "MPSKernel"))]
319impl MPSImageEDLines {
320    extern_methods!(
321        #[unsafe(method(init))]
322        #[unsafe(method_family = init)]
323        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
324
325        #[unsafe(method(new))]
326        #[unsafe(method_family = new)]
327        pub unsafe fn new() -> Retained<Self>;
328    );
329}