Beispiel #1
0
        /// <summary>
        /// Convert the <c>CameraImage</c> to one of the supported formats (see <see cref="FormatSupported(TextureFormat)"/>)
        /// using the specified <paramref name="conversionParams"/>. The conversion is performed asynchronously. Use the returned
        /// <see cref="AsyncCameraImageConversion"/> to check for the status of the conversion, and retrieve the data when complete.
        /// It is safe to <c>Dispose</c> the <c>CameraImage</c> before the asynchronous operation has completed.
        /// </summary>
        /// <param name="conversionParams">The <see cref="CameraImageConversionParams"/> to use for the conversion.</param>
        /// <returns>A new <see cref="AsyncCameraImageConversion"/> which can be used to check the status of the conversion operation
        /// and get the resulting data.</returns>
        public AsyncCameraImageConversion ConvertAsync(CameraImageConversionParams conversionParams)
        {
            ValidateNativeHandleAndThrow();
            ValidateConversionParamsAndThrow(conversionParams);

            return(new AsyncCameraImageConversion(m_CameraImageApi, m_NativeHandle, conversionParams));
        }
Beispiel #2
0
        static unsafe void OnAsyncConversionComplete(
            AsyncCameraImageConversionStatus status, CameraImageConversionParams conversionParams, IntPtr dataPtr, int dataLength, IntPtr context)
        {
            var handle     = GCHandle.FromIntPtr(context);
            var onComplete = (Action <AsyncCameraImageConversionStatus, CameraImageConversionParams, NativeArray <byte> >)handle.Target;

            if (onComplete != null)
            {
                var data = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <byte>(
                    (void *)dataPtr, dataLength, Allocator.None);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
                var safetyHandle = AtomicSafetyHandle.Create();
                NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref data, safetyHandle);
#endif

                onComplete(status, conversionParams, data);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
                AtomicSafetyHandle.Release(safetyHandle);
#endif
            }

            handle.Free();
        }
 public void ConvertAsync(
     int nativeHandle,
     CameraImageConversionParams conversionParams,
     XRCameraExtensions.OnImageRequestCompleteDelegate callback,
     IntPtr context)
 {
     callback(AsyncCameraImageConversionStatus.Disposed, conversionParams, IntPtr.Zero, 0, context);
 }
        internal AsyncCameraImageConversion(ICameraImageApi api, int nativeHandle, CameraImageConversionParams conversionParams)
        {
            m_CameraImageApi      = api;
            m_RequestId           = m_CameraImageApi.ConvertAsync(nativeHandle, conversionParams);
            this.conversionParams = conversionParams;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            m_SafetyHandle = AtomicSafetyHandle.Create();
#endif
        }
Beispiel #5
0
        /// <summary>
        /// Convert the <c>CameraImage</c> to one of the supported formats (see <see cref="FormatSupported(TextureFormat)"/>)
        /// using the specified <paramref name="conversionParams"/>. The conversion is performed asynchronously, and <paramref name="onComplete"/>
        /// is invoked when the conversion is complete, whether successful or not.
        ///
        /// The <c>NativeArray</c> provided in the <paramref name="onComplete"/> delegate is only valid during the invocation and is disposed
        /// immediately upon return.
        /// </summary>
        /// <param name="conversionParams">The <see cref="CameraImageConversionParams"/> to use for the conversion.</param>
        /// <param name="onComplete">A delegate to invoke when the conversion operation completes. The delegate is always invoked.</param>
        public void ConvertAsync(
            CameraImageConversionParams conversionParams,
            Action <AsyncCameraImageConversionStatus, CameraImageConversionParams, NativeArray <byte> > onComplete)
        {
            ValidateNativeHandleAndThrow();
            ValidateConversionParamsAndThrow(conversionParams);

            var handle  = GCHandle.Alloc(onComplete);
            var context = GCHandle.ToIntPtr(handle);

            m_CameraImageApi.ConvertAsync(m_NativeHandle, conversionParams, s_OnAsyncConversionComplete, context);
        }
Beispiel #6
0
        /// <summary>
        /// Convert the <c>CameraImage</c> to one of the supported formats (see <see cref="FormatSupported(TextureFormat)"/>)
        /// using the specified <paramref name="conversionParams"/>.
        /// </summary>
        /// <param name="conversionParams">The <see cref="CameraImageConversionParams"/> to use for the conversion.</param>
        /// <param name="destinationBuffer">A pointer to memory to write the converted image to.</param>
        /// <param name="bufferLength">The number of bytes pointed to by <paramref name="destinationBuffer"/>. Must be greater than or equal to the
        /// value returned by <see cref="GetConvertedDataSize(CameraImageConversionParams)"/>.</param>
        public void Convert(CameraImageConversionParams conversionParams, IntPtr destinationBuffer, int bufferLength)
        {
            ValidateNativeHandleAndThrow();
            ValidateConversionParamsAndThrow(conversionParams);
            int requiredDataSize = GetConvertedDataSize(conversionParams);

            if (bufferLength < requiredDataSize)
            {
                throw new InvalidOperationException(string.Format(
                                                        "Conversion requires {0} bytes but only provided {1} bytes.", requiredDataSize, bufferLength));
            }

            if (!m_CameraImageApi.TryConvert(m_NativeHandle, conversionParams, destinationBuffer, bufferLength))
            {
                throw new InvalidOperationException("Conversion failed.");
            }
        }
Beispiel #7
0
        void ValidateConversionParamsAndThrow(CameraImageConversionParams conversionParams)
        {
            if ((conversionParams.inputRect.x + conversionParams.inputRect.width > width) ||
                (conversionParams.inputRect.y + conversionParams.inputRect.height > height))
            {
                throw new ArgumentOutOfRangeException(
                          "conversionParams.inputRect",
                          "Input rect must be completely within the original image.");
            }

            if ((conversionParams.outputDimensions.x > conversionParams.inputRect.width) ||
                (conversionParams.outputDimensions.y > conversionParams.inputRect.height))
            {
                throw new InvalidOperationException(string.Format(
                                                        "Output dimensions must be less than or equal to the inputRect's dimensions: ({0}x{1} > {2}x{3}).",
                                                        conversionParams.outputDimensions.x, conversionParams.outputDimensions.y,
                                                        conversionParams.inputRect.width, conversionParams.inputRect.height));
            }

            if (!FormatSupported(conversionParams.outputFormat))
            {
                throw new ArgumentException("TextureFormat not supported.", "conversionParams.format");
            }
        }
Beispiel #8
0
 public int ConvertAsync(int nativeHandle, CameraImageConversionParams conversionParams)
 {
     return(0);
 }
Beispiel #9
0
 public bool TryConvert(int nativeHandle, CameraImageConversionParams conversionParams, IntPtr destinationBuffer, int bufferLength)
 {
     return(false);
 }
Beispiel #10
0
 /// <summary>
 /// Get the number of bytes required to store a converted image with the given parameters.
 /// </summary>
 /// <param name="conversionParams">The <see cref="CameraImageConversionParams"/> to use for the conversion.</param>
 /// <returns>The number of bytes required to store the converted image.</returns>
 public int GetConvertedDataSize(CameraImageConversionParams conversionParams)
 {
     return(GetConvertedDataSize(
                conversionParams.outputDimensions,
                conversionParams.outputFormat));
 }