Пример #1
0
 private static Native.ThumbnailExtractCallback GetCallback(TaskCompletionSource <ThumbnailExtractionResult> tcs)
 {
     return((error, requestId, thumbWidth, thumbHeight, thumbData, dataSize, _) =>
     {
         if (error == ThumbnailExtractorError.None)
         {
             try
             {
                 tcs.TrySetResult(new ThumbnailExtractionResult(thumbData, thumbWidth, thumbHeight, dataSize));
             }
             catch (Exception e)
             {
                 tcs.TrySetException(new InvalidOperationException("[" + error + "] Failed to create ThumbnailExtractionResult instance.", e));
             }
             finally
             {
                 LibcSupport.Free(thumbData);
             }
         }
         else
         {
             tcs.TrySetException(error.ToException("Failed to extract."));
         }
     });
 }
Пример #2
0
        private static async Task <ThumbnailExtractionResult> ExtractAsyncCore(string path, Size?size,
                                                                               CancellationToken cancellationToken)
        {
            using (var handle = CreateHandle())
            {
                Native.SetPath(handle, path).ThrowIfError("Failed to extract; failed to set the path.");

                if (size.HasValue)
                {
                    Native.SetSize(handle, size.Value.Width, size.Value.Height).
                    ThrowIfError("Failed to extract; failed to set the size");
                }

                var tcs = new TaskCompletionSource <ThumbnailExtractionResult>();

                IntPtr id = IntPtr.Zero;

                try
                {
                    Native.Extract(handle, GetCallback(tcs), IntPtr.Zero, out id)
                    .ThrowIfError("Failed to extract.");

                    using (RegisterCancellationToken(tcs, cancellationToken, handle, Marshal.PtrToStringAnsi(id)))
                    {
                        return(await tcs.Task);
                    }
                }
                finally
                {
                    LibcSupport.Free(id);
                }
            }
        }
Пример #3
0
        private IEnumerable <BitmapFrame> RunDecoding()
        {
            IntPtr imageHandle = IntPtr.Zero;
            IntPtr outBuffer   = IntPtr.Zero;

            try
            {
                NativeDecoder.DecodeRun(Handle, out imageHandle).ThrowIfFailed("Failed to decode");

                NativeUtil.GetImage(imageHandle, out int width, out int height, out ImageColorSpace colorspace,
                                    out outBuffer, out int size).ThrowIfFailed("Failed to get decoded image.");

                yield return(new BitmapFrame(outBuffer, width, height, size));
            }
            finally
            {
                if (outBuffer != IntPtr.Zero)
                {
                    LibcSupport.Free(outBuffer);
                }

                if (imageHandle != IntPtr.Zero)
                {
                    NativeUtil.Destroy(imageHandle).ThrowIfFailed("Failed to destroy handle");
                }
            }
        }
Пример #4
0
        internal string[] GetStringArray(string key)
        {
            IntPtr values = IntPtr.Zero;
            int    size   = 0;

            try
            {
                EngineConfig.GetStringArray(Handle, key, out values, out size).
                Validate("Failed to get the value");

                var current = values;
                var result  = new string[size];

                for (int i = 0; i < size; i++)
                {
                    result[i] = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(current));
                    current   = (IntPtr)((long)current + Marshal.SizeOf(typeof(IntPtr)));
                }

                return(result);
            }
            finally
            {
                var current = values;
                for (int i = 0; i < size; i++)
                {
                    LibcSupport.Free(Marshal.ReadIntPtr(current));
                    current = (IntPtr)((long)current + Marshal.SizeOf(typeof(IntPtr)));
                }
            }
        }
Пример #5
0
        internal string GetString(string key)
        {
            IntPtr ptr = IntPtr.Zero;

            try
            {
                EngineConfig.GetString(Handle, key, out ptr).Validate("Failed to get the value");
                return(Marshal.PtrToStringAnsi(ptr));
            }
            finally
            {
                LibcSupport.Free(ptr);
            }
        }
Пример #6
0
        private IEnumerable <BitmapFrame> RunDecoding()
        {
            IntPtr outBuffer = IntPtr.Zero;

            try
            {
                SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to decode given image");

                DecodeRun(Handle, out var width, out var height, out var size).
                ThrowIfFailed("Failed to decode");

                yield return(new BitmapFrame(outBuffer, width, height, (int)size));
            }
            finally
            {
                LibcSupport.Free(outBuffer);
            }
        }
Пример #7
0
        /// <summary>
        /// Extracts the thumbnail for the given media with the specified path and size.
        /// The generated thumbnail will be returned in <see cref="ThumbnailExtractionResult"/>.
        /// </summary>
        /// <remarks>
        /// The size of generated thumbnail will be <paramref name="size"/>.<br/>
        /// But, if the size of <paramref name="path"/> has different ratio with <paramref name="size"/>,<br/>
        /// thumbnail will be generated in a way to keep the ratio of <paramref name="path"/>, which based on short axis of <paramref name="path"/>.<br/>
        /// For example, if the size of <paramref name="path"/> is 900x500 (1.8:1)) and <paramref name="size"/> is 320x240,<br/>
        /// the size of generated thumbnail is 432x240(1.8:1).<br/>
        /// <br/>
        /// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage. <br/>
        /// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage.
        /// </remarks>
        /// <privilege>http://tizen.org/privilege/mediastorage</privilege>
        /// <privilege>http://tizen.org/privilege/externalstorage</privilege>
        /// <param name="path">The path of the media file to extract the thumbnail.</param>
        /// <param name="size">The size of the thumbnail.</param>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exist.</exception>
        /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     The width or the height of <paramref name="size"/> is less than or equal to zero.
        /// </exception>
        /// <exception cref="FileFormatException">The specified file is not supported.</exception>
        /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
        /// <returns>The result of extracting operation.</returns>
        /// <since_tizen> 6 </since_tizen>
        public static ThumbnailExtractionResult Extract(string path, Size size)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (File.Exists(path) == false)
            {
                throw new FileNotFoundException("File does not exists.", path);
            }

            if (size.Width <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(size), size.Width,
                                                      "The width must be greater than zero.");
            }

            if (size.Height <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(size), size.Height,
                                                      "The height must be greater than zero.");
            }

            Native.ExtractToBuffer(path, (uint)size.Width, (uint)size.Height, out IntPtr thumbData,
                                   out int dataSize, out uint thumbWidth, out uint thumbHeight).
            ThrowIfError("Failed to extract thumbnail to buffer");

            try
            {
                return(new ThumbnailExtractionResult(thumbData, (int)thumbWidth, (int)thumbHeight,
                                                     dataSize));
            }
            finally
            {
                if (thumbData != IntPtr.Zero)
                {
                    LibcSupport.Free(thumbData);
                }
            }
        }