[ImageUtil] Add sync thumbnail extract API (#928)
authorhsgwon <haesu.gwon@samsung.com>
Wed, 10 Jul 2019 08:25:41 +0000 (17:25 +0900)
committerGitHub <noreply@github.com>
Wed, 10 Jul 2019 08:25:41 +0000 (17:25 +0900)
* [ImageUtil] Add sync thumbnail extract API

src/Tizen.Multimedia.Util/Interop/Interop.ThumbnailExtractor.cs
src/Tizen.Multimedia.Util/ThumbnailExtractor/ThumbnailExtractionResult.cs [changed mode: 0755->0644]
src/Tizen.Multimedia.Util/ThumbnailExtractor/ThumbnailExtractor.cs

index c1a02de..49b298c 100644 (file)
@@ -46,6 +46,13 @@ internal static partial class Interop
 
         [DllImport(Libraries.ThumbnailExtractor, EntryPoint = "thumbnail_util_destroy")]
         internal static extern ThumbnailExtractorError Destroy(IntPtr handle);
+
+        [DllImport(Libraries.ThumbnailExtractor, EntryPoint = "thumbnail_util_extract_to_buffer")]
+        internal static extern ThumbnailExtractorError ExtractToBuffer(string path, uint width, uint height, out IntPtr thumbData,
+            out int dataSize, out uint thumbWidth, out uint thumbHeight);
+
+        [DllImport(Libraries.ThumbnailExtractor, EntryPoint = "thumbnail_util_extract_to_file")]
+        internal static extern ThumbnailExtractorError ExtractToFile(string path, uint width, uint height, string thumbPath);
     }
 
     internal class ThumbnailExtractorHandle : CriticalHandle
old mode 100755 (executable)
new mode 100644 (file)
index 0e6b4b2..3149d72
@@ -1,4 +1,4 @@
-/*
+/*
  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the License);
  * limitations under the License.
  */
 
+using System;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+
 namespace Tizen.Multimedia.Util
 {
     /// <summary>
@@ -22,9 +26,14 @@ namespace Tizen.Multimedia.Util
     /// <since_tizen> 4 </since_tizen>
     public class ThumbnailExtractionResult
     {
-        internal ThumbnailExtractionResult(byte[] thumbnailData, int width, int height)
+        internal ThumbnailExtractionResult(IntPtr nativeBuffer, int width, int height, int size)
         {
-            RawData = thumbnailData;
+            Debug.Assert(nativeBuffer != IntPtr.Zero);
+
+            byte[] buf = new byte[size];
+            Marshal.Copy(nativeBuffer, buf, 0, size);
+
+            RawData = buf;
             Size = new Size(width, height);
         }
 
index 45ccbe3..27e0343 100644 (file)
@@ -202,14 +202,11 @@ namespace Tizen.Multimedia.Util
                 {
                     try
                     {
-                        byte[] tmpBuf = new byte[dataSize];
-                        Marshal.Copy(thumbData, tmpBuf, 0, dataSize);
-
-                        tcs.TrySetResult(new ThumbnailExtractionResult(tmpBuf, thumbWidth, thumbHeight));
+                        tcs.TrySetResult(new ThumbnailExtractionResult(thumbData, thumbWidth, thumbHeight, dataSize));
                     }
                     catch (Exception e)
                     {
-                        tcs.TrySetException(new InvalidOperationException("[" + error + "] Failed to copy data.", e));
+                        tcs.TrySetException(new InvalidOperationException("[" + error + "] Failed to create ThumbnailExtractionResult instance.", e));
                     }
                     finally
                     {
@@ -242,5 +239,171 @@ namespace Tizen.Multimedia.Util
                 tcs.TrySetCanceled();
             });
         }
+
+        /// <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 320x240.<br/>
+        /// If you want to set the size of generated thumbnail, please use <see cref="Extract(string, Size)"/><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>
+        /// <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="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)
+        {
+            return Extract(path, new Size(320, 240));
+        }
+
+        /// <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>
+        /// 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);
+                }
+            }
+        }
+
+        /// <summary>
+        /// Extracts the thumbnail for the given media with the specified path and size.
+        /// The generated thumbnail will be saved in <paramref name="resultThumbnailPath"/>.
+        /// </summary>
+        /// <remarks>
+        /// The size of generated thumbnail will be 320x240.<br/>
+        /// If you want to set the size of generated thumbnail, please use <see cref="Extract(string, Size, string)"/><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="resultThumbnailPath">The path to save the generated 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="FileFormatException">The specified file is not supported.</exception>
+        /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
+        /// <since_tizen> 6 </since_tizen>
+        public static void Extract(string path, string resultThumbnailPath)
+        {
+            Extract(path, new Size(320, 240), resultThumbnailPath);
+        }
+
+        /// <summary>
+        /// Extracts the thumbnail for the given media with the specified path and size.
+        /// The generated thumbnail will be saved in <paramref name="resultThumbnailPath"/>.
+        /// </summary>
+        /// <remarks>
+        /// 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>
+        /// <param name="resultThumbnailPath">The path to save the generated 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>
+        /// <since_tizen> 6 </since_tizen>
+        public static void Extract(string path, Size size, string resultThumbnailPath)
+        {
+            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.ExtractToFile(path, (uint)size.Width, (uint)size.Height, resultThumbnailPath).
+                ThrowIfError("Failed to extract thumbnail to file.");
+        }
     }
 }