[ImageUtil] Replace deprecated decode pinvoke func. (#929)
authorhsgwon <haesu.gwon@samsung.com>
Tue, 9 Jul 2019 07:49:43 +0000 (16:49 +0900)
committerGitHub <noreply@github.com>
Tue, 9 Jul 2019 07:49:43 +0000 (16:49 +0900)
* [ImageUtil] Replace deprecated pinvoke func.

14 files changed:
src/Tizen.Multimedia.Util/ImageUtil/ColorSpaceTransform.cs
src/Tizen.Multimedia.Util/ImageUtil/CropTransform.cs
src/Tizen.Multimedia.Util/ImageUtil/FlipTransform.cs
src/Tizen.Multimedia.Util/ImageUtil/ImageDecoder.cs
src/Tizen.Multimedia.Util/ImageUtil/ImageEncoder.cs
src/Tizen.Multimedia.Util/ImageUtil/ImageTransform.cs
src/Tizen.Multimedia.Util/ImageUtil/ImageTransformGroup.cs
src/Tizen.Multimedia.Util/ImageUtil/ImageUtil.cs
src/Tizen.Multimedia.Util/ImageUtil/ResizeTransform.cs
src/Tizen.Multimedia.Util/ImageUtil/RotateTransform.cs
src/Tizen.Multimedia.Util/Interop/Interop.ImageUtil.Decode.cs
src/Tizen.Multimedia.Util/Interop/Interop.ImageUtil.Encode.cs
src/Tizen.Multimedia.Util/Interop/Interop.ImageUtil.Transform.cs
src/Tizen.Multimedia.Util/Interop/Interop.ImageUtil.cs

index e4a468a..62a3f01 100644 (file)
@@ -16,8 +16,8 @@
 
 using System;
 using System.Collections.Generic;
-using static Interop.ImageUtil;
-using static Interop.ImageUtil.Transform;
+using static Interop;
+using NativeTransform = Interop.ImageUtil.Transform;
 
 namespace Tizen.Multimedia.Util
 {
@@ -66,7 +66,7 @@ namespace Tizen.Multimedia.Util
 
         internal override void Configure(TransformHandle handle)
         {
-            SetColorspace(handle, _imageColorSpace);
+            NativeTransform.SetColorspace(handle, _imageColorSpace);
         }
 
 
index 489a0ad..cf6c34d 100644 (file)
@@ -15,8 +15,8 @@
  */
 
 using System;
-using static Interop.ImageUtil;
-using static Interop.ImageUtil.Transform;
+using static Interop;
+using NativeTransform = Interop.ImageUtil.Transform;
 
 namespace Tizen.Multimedia.Util
 {
@@ -96,7 +96,7 @@ namespace Tizen.Multimedia.Util
 
         internal override void Configure(TransformHandle handle)
         {
-            SetCropArea(handle, Region.Left, Region.Top, Region.Right, Region.Bottom);
+            NativeTransform.SetCropArea(handle, Region.Left, Region.Top, Region.Right, Region.Bottom);
         }
 
         internal override void ValidateFormat(VideoMediaFormat format)
index 71da15e..4483fc7 100644 (file)
@@ -16,8 +16,8 @@
 
 using System;
 using System.Threading.Tasks;
-using static Interop.ImageUtil;
-using static Interop.ImageUtil.Transform;
+using static Interop;
+using NativeTransform = Interop.ImageUtil.Transform;
 
 namespace Tizen.Multimedia.Util
 {
@@ -75,7 +75,7 @@ namespace Tizen.Multimedia.Util
         private async Task<MediaPacket> ApplyAsync(TransformHandle handle, MediaPacket source,
             ImageRotation rotation)
         {
-            SetRotation(handle, rotation);
+            NativeTransform.SetRotation(handle, rotation);
             return await RunAsync(handle, source);
         }
 
index 81824ec..71236b5 100644 (file)
@@ -23,7 +23,8 @@ using System.Runtime.InteropServices;
 using System.Threading;
 using System.Threading.Tasks;
 using static Interop;
-using static Interop.Decode;
+using NativeUtil = Interop.ImageUtil;
+using NativeDecoder = Interop.ImageUtil.Decode;
 
 namespace Tizen.Multimedia.Util
 {
@@ -39,7 +40,7 @@ namespace Tizen.Multimedia.Util
 
         internal ImageDecoder(ImageFormat format)
         {
-            Create(out _handle).ThrowIfFailed("Failed to create ImageDecoder");
+            NativeDecoder.Create(out _handle).ThrowIfFailed("Failed to create ImageDecoder");
 
             Debug.Assert(_handle != null);
 
@@ -126,7 +127,8 @@ namespace Tizen.Multimedia.Util
             var pathPtr = Marshal.StringToHGlobalAnsi(inputFilePath);
             try
             {
-                SetInputPath(Handle, pathPtr).ThrowIfFailed("Failed to set input file path for decoding");
+                NativeDecoder.SetInputPath(Handle, pathPtr).ThrowIfFailed("Failed to set input file path for decoding");
+
                 return await DecodeAsync();
             }
             finally
@@ -163,7 +165,7 @@ namespace Tizen.Multimedia.Util
                 throw new FileFormatException("buffer has an invalid header.");
             }
 
-            SetInputBuffer(Handle, inputBuffer, (ulong)inputBuffer.Length).
+            NativeDecoder.SetInputBuffer(Handle, inputBuffer, (ulong)inputBuffer.Length).
                 ThrowIfFailed("Failed to set input buffer for decoding");
 
             return DecodeAsync();
@@ -203,26 +205,29 @@ namespace Tizen.Multimedia.Util
 
         private IEnumerable<BitmapFrame> RunDecoding()
         {
-            IntPtr outBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
-            Marshal.WriteIntPtr(outBuffer, IntPtr.Zero);
+            IntPtr imageHandle = IntPtr.Zero;
+            IntPtr outBuffer = IntPtr.Zero;
 
             try
             {
-                SetOutputBuffer(Handle, outBuffer).ThrowIfFailed("Failed to decode given image");
+                NativeDecoder.DecodeRun(Handle, out imageHandle).ThrowIfFailed("Failed to decode");
 
-                DecodeRun(Handle, out var width, out var height, out var size).
-                    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(Marshal.ReadIntPtr(outBuffer), width, height, (int)size);
+                yield return new BitmapFrame(outBuffer, width, height, size);
             }
             finally
             {
-                if (Marshal.ReadIntPtr(outBuffer) != IntPtr.Zero)
+                if (outBuffer != IntPtr.Zero)
                 {
-                    LibcSupport.Free(Marshal.ReadIntPtr(outBuffer));
+                    LibcSupport.Free(outBuffer);
                 }
 
-                Marshal.FreeHGlobal(outBuffer);
+                if (imageHandle != IntPtr.Zero)
+                {
+                    NativeUtil.Destroy(imageHandle).ThrowIfFailed("Failed to destroy handle");
+                }
             }
         }
 
@@ -239,7 +244,7 @@ namespace Tizen.Multimedia.Util
         {
             if (_colorSpace.HasValue)
             {
-                SetColorspace(Handle, _colorSpace.Value.ToImageColorSpace()).ThrowIfFailed("Failed to set color space");
+                NativeDecoder.SetColorspace(Handle, _colorSpace.Value.ToImageColorSpace()).ThrowIfFailed("Failed to set color space");
             }
         }
 
@@ -364,7 +369,7 @@ namespace Tizen.Multimedia.Util
         {
             base.Initialize(handle);
 
-            SetJpegDownscale(handle, Downscale).ThrowIfFailed("Failed to set downscale for decoding");
+            NativeDecoder.SetJpegDownscale(handle, Downscale).ThrowIfFailed("Failed to set downscale for decoding");
         }
 
         internal override byte[] Header => _header;
index e98f678..c20c710 100644 (file)
@@ -22,8 +22,8 @@ using System.Linq;
 using System.Runtime.InteropServices;
 using System.Threading;
 using System.Threading.Tasks;
-using static Interop.ImageUtil;
-using Unmanaged = Interop.ImageUtil.Encode;
+using static Interop;
+using NativeEncoder = Interop.ImageUtil.Encode;
 
 namespace Tizen.Multimedia.Util
 {
@@ -39,7 +39,7 @@ namespace Tizen.Multimedia.Util
 
         internal ImageEncoder(ImageFormat format)
         {
-            Unmanaged.Create(format, out _handle).ThrowIfFailed("Failed to create ImageEncoder");
+            NativeEncoder.Create(format, out _handle).ThrowIfFailed("Failed to create ImageEncoder");
 
             Debug.Assert(_handle != null);
 
@@ -88,7 +88,7 @@ namespace Tizen.Multimedia.Util
                     "The height of resolution can't be less than or equal to zero.");
             }
 
-            Unmanaged.SetResolution(Handle, (uint)resolution.Width, (uint)resolution.Height).
+            NativeEncoder.SetResolution(Handle, (uint)resolution.Width, (uint)resolution.Height).
                 ThrowIfFailed("Failed to set the resolution");
 
             _hasResolution = true;
@@ -111,7 +111,7 @@ namespace Tizen.Multimedia.Util
                 throw new NotSupportedException($"{colorSpace.ToString()} is not supported for {OutputFormat}.");
             }
 
-            Unmanaged.SetColorspace(Handle, colorSpace.ToImageColorSpace()).
+            NativeEncoder.SetColorspace(Handle, colorSpace.ToImageColorSpace()).
                 ThrowIfFailed("Failed to set the color space");
         }
 
@@ -121,9 +121,9 @@ namespace Tizen.Multimedia.Util
 
             try
             {
-                Unmanaged.SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to initialize encoder");
+                NativeEncoder.SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to initialize encoder");
 
-                Unmanaged.Run(Handle, out var size).ThrowIfFailed("Failed to encode given image");
+                NativeEncoder.Run(Handle, out var size).ThrowIfFailed("Failed to encode given image");
 
                 byte[] buf = new byte[size];
                 Marshal.Copy(outBuffer, buf, 0, (int)size);
@@ -197,7 +197,7 @@ namespace Tizen.Multimedia.Util
 
             return EncodeAsync(handle =>
             {
-                Unmanaged.SetInputBuffer(handle, inputBuffer).
+                NativeEncoder.SetInputBuffer(handle, inputBuffer).
                         ThrowIfFailed("Failed to configure encoder; InputBuffer");
             }, outStream);
         }
@@ -323,7 +323,7 @@ namespace Tizen.Multimedia.Util
         {
             if (_compression.HasValue)
             {
-                Unmanaged.SetPngCompression(handle, _compression.Value).
+                NativeEncoder.SetPngCompression(handle, _compression.Value).
                     ThrowIfFailed("Failed to configure encoder; PngCompression");
             }
         }
@@ -401,7 +401,7 @@ namespace Tizen.Multimedia.Util
         {
             if (_quality.HasValue)
             {
-                Unmanaged.SetQuality(handle, _quality.Value).
+                NativeEncoder.SetQuality(handle, _quality.Value).
                     ThrowIfFailed("Failed to configure encoder; Quality");
             }
         }
@@ -466,10 +466,10 @@ namespace Tizen.Multimedia.Util
                     {
                         throw new ArgumentNullException(nameof(frames));
                     }
-                    Unmanaged.SetInputBuffer(handle, frame.Buffer).
+                    NativeEncoder.SetInputBuffer(handle, frame.Buffer).
                         ThrowIfFailed("Failed to configure encoder; Buffer");
 
-                    Unmanaged.SetGifFrameDelayTime(handle, (ulong)frame.Delay).
+                    NativeEncoder.SetGifFrameDelayTime(handle, (ulong)frame.Delay).
                         ThrowIfFailed("Failed to configure encoder; Delay");
                 }
             }, outStream);
index 5923882..c52e269 100644 (file)
@@ -18,8 +18,8 @@ using System;
 using System.Diagnostics;
 using System.Runtime.InteropServices;
 using System.Threading.Tasks;
-using static Interop.ImageUtil;
-using static Interop.ImageUtil.Transform;
+using static Interop;
+using NativeTransform = Interop.ImageUtil.Transform;
 
 namespace Tizen.Multimedia.Util
 {
@@ -42,7 +42,7 @@ namespace Tizen.Multimedia.Util
 
             using (var cbKeeper = ObjectKeeper.Get(GetCallback(tcs, source)))
             {
-                var result = Run(handle, source.GetHandle(), cbKeeper.Target);
+                var result = NativeTransform.Run(handle, source.GetHandle(), cbKeeper.Target);
 
                 if (result == ImageUtilError.NotSupportedFormat)
                 {
@@ -55,7 +55,7 @@ namespace Tizen.Multimedia.Util
             }
         }
 
-        private TransformCompletedCallback GetCallback(TaskCompletionSource<MediaPacket> tcs,
+        private NativeTransform.TransformCompletedCallback GetCallback(TaskCompletionSource<MediaPacket> tcs,
             MediaPacket source)
         {
             return (nativehandle, errorCode, _) =>
@@ -85,7 +85,7 @@ namespace Tizen.Multimedia.Util
 
         internal static TransformHandle CreateHandle()
         {
-            Create(out var handle).ThrowIfFailed("Failed to run ImageTransformer");
+            NativeTransform.Create(out var handle).ThrowIfFailed("Failed to run ImageTransformer");
             Debug.Assert(handle != null);
             return handle;
         }
index e445d09..8829341 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 using System.Threading.Tasks;
-using static Interop.ImageUtil;
+using static Interop;
 
 namespace Tizen.Multimedia.Util
 {
index 4e62803..f1483b2 100644 (file)
@@ -17,7 +17,7 @@
 using System;
 using System.Collections.Generic;
 using Tizen.Common;
-using static Interop.ImageUtil;
+using NativeUtil = Interop.ImageUtil;
 
 namespace Tizen.Multimedia.Util
 {
@@ -40,7 +40,7 @@ namespace Tizen.Multimedia.Util
 
             var colorspaces = new List<ColorSpace>();
 
-            ForeachSupportedColorspace(format,
+            NativeUtil.ForeachSupportedColorspace(format,
                 (colorspace, _) => { colorspaces.Add(colorspace.ToCommonColorSpace()); return true; }).
                 ThrowIfFailed("Failed to get supported color-space list from native handle");
 
@@ -123,7 +123,7 @@ namespace Tizen.Multimedia.Util
                     "height can't be less than or equal to zero.");
             }
 
-            ExtractColorFromMemory(buffer, size.Width, size.Height, out var r, out var g, out var b)
+            NativeUtil.ExtractColorFromMemory(buffer, size.Width, size.Height, out var r, out var g, out var b)
                 .ThrowIfFailed("Failed to extract color from buffer");
 
             return Color.FromRgb(r, g, b);
index 8489134..a4e1f0e 100644 (file)
@@ -15,8 +15,8 @@
  */
 
 using System;
-using static Interop.ImageUtil;
-using static Interop.ImageUtil.Transform;
+using static Interop;
+using NativeTransform = Interop.ImageUtil.Transform;
 
 namespace Tizen.Multimedia.Util
 {
@@ -78,7 +78,7 @@ namespace Tizen.Multimedia.Util
 
         internal override void Configure(TransformHandle handle)
         {
-            SetResolution(handle, (uint)Size.Width, (uint)Size.Height);
+            NativeTransform.SetResolution(handle, (uint)Size.Width, (uint)Size.Height);
         }
     }
 }
index 0470be0..4bfea73 100644 (file)
@@ -16,8 +16,8 @@
 
 using System;
 using System.Diagnostics;
-using static Interop.ImageUtil;
-using static Interop.ImageUtil.Transform;
+using static Interop;
+using NativeTransform = Interop.ImageUtil.Transform;
 
 namespace Tizen.Multimedia.Util
 {
@@ -69,7 +69,7 @@ namespace Tizen.Multimedia.Util
 
         internal override void Configure(TransformHandle handle)
         {
-            SetRotation(handle, GetImageRotation());
+            NativeTransform.SetRotation(handle, GetImageRotation());
         }
 
         private ImageRotation GetImageRotation()
index 938db12..3bdd41b 100644 (file)
@@ -21,39 +21,34 @@ using Tizen.Multimedia.Util;
 
 internal static partial class Interop
 {
-    internal static class Decode
+    internal static partial class ImageUtil
     {
-        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-        internal delegate void DecodeCompletedCallback(ImageUtilError error, IntPtr userData, int width, int height, ulong size);
-
-        [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_create")]
-        public static extern ImageUtilError Create(out ImageDecoderHandle handle);
-
-        [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_destroy")]
-        internal static extern ImageUtilError Destroy(IntPtr handle);
+        internal static partial class Decode
+        {
+            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+            internal delegate void DecodeCompletedCallback(ImageUtilError error, IntPtr userData, int width, int height, ulong size);
 
-        [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_input_path")]
-        internal static extern ImageUtilError SetInputPath(ImageDecoderHandle handle, IntPtr path);
+            [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_create")]
+            public static extern ImageUtilError Create(out ImageDecoderHandle handle);
 
-        [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_input_buffer")]
-        internal static extern ImageUtilError SetInputBuffer(ImageDecoderHandle handle, byte[] srcBuffer, ulong srcSize);
+            [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_destroy")]
+            internal static extern ImageUtilError Destroy(IntPtr handle);
 
-        [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_output_buffer")]
-        internal static extern ImageUtilError SetOutputBuffer(ImageDecoderHandle handle, IntPtr dstBuffer);
+            [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_input_path")]
+            internal static extern ImageUtilError SetInputPath(ImageDecoderHandle handle, IntPtr path);
 
-        [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_colorspace")]
-        internal static extern ImageUtilError SetColorspace(ImageDecoderHandle handle, ImageColorSpace colorspace);
+            [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_input_buffer")]
+            internal static extern ImageUtilError SetInputBuffer(ImageDecoderHandle handle, byte[] srcBuffer, ulong srcSize);
 
-        [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_jpeg_downscale")]
-        internal static extern ImageUtilError SetJpegDownscale(ImageDecoderHandle handle, JpegDownscale downscale);
+            [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_colorspace")]
+            internal static extern ImageUtilError SetColorspace(ImageDecoderHandle handle, ImageColorSpace colorspace);
 
-        [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_run_async")]
-        internal static extern ImageUtilError DecodeRunAsync(ImageDecoderHandle handle, DecodeCompletedCallback callback,
-            IntPtr userData = default(IntPtr));
+            [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_jpeg_downscale")]
+            internal static extern ImageUtilError SetJpegDownscale(ImageDecoderHandle handle, JpegDownscale downscale);
 
-        [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_run")]
-        internal static extern ImageUtilError DecodeRun(ImageDecoderHandle handle, out int width,
-            out int height, out ulong size);
+            [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_run2")]
+            internal static extern ImageUtilError DecodeRun(ImageDecoderHandle handle, out IntPtr imageHandle);
+        }
     }
 
     internal class ImageDecoderHandle : SafeHandle
@@ -66,7 +61,7 @@ internal static partial class Interop
 
         protected override bool ReleaseHandle()
         {
-            var ret = Decode.Destroy(handle);
+            var ret = ImageUtil.Decode.Destroy(handle);
             if (ret != ImageUtilError.None)
             {
                 Log.Debug(GetType().FullName, $"Failed to release native {GetType()}");
@@ -75,6 +70,5 @@ internal static partial class Interop
 
             return true;
         }
-
     }
 }
index ada1c39..f2df4b3 100644 (file)
@@ -65,27 +65,27 @@ internal static partial class Interop
             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_run")]
             internal static extern ImageUtilError Run(ImageEncoderHandle handle, out ulong size);
         }
+    }
 
-        internal class ImageEncoderHandle : SafeHandle
+    internal class ImageEncoderHandle : SafeHandle
+    {
+        protected ImageEncoderHandle() : base(IntPtr.Zero, true)
         {
-            protected ImageEncoderHandle() : base(IntPtr.Zero, true)
-            {
-            }
+        }
 
-            public override bool IsInvalid => handle == IntPtr.Zero;
+        public override bool IsInvalid => handle == IntPtr.Zero;
 
 
-            protected override bool ReleaseHandle()
+        protected override bool ReleaseHandle()
+        {
+            var ret = ImageUtil.Encode.Destroy(handle);
+            if (ret != ImageUtilError.None)
             {
-                var ret = Encode.Destroy(handle);
-                if (ret != ImageUtilError.None)
-                {
-                    Log.Debug(GetType().FullName, $"Failed to release native {GetType().Name}");
-                    return false;
-                }
-
-                return true;
+                Log.Debug(GetType().FullName, $"Failed to release native {GetType().Name}");
+                return false;
             }
+
+            return true;
         }
     }
 }
index 056266d..cc26f74 100644 (file)
@@ -63,27 +63,26 @@ internal static partial class Interop
             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_transform_set_resolution")]
             internal static extern ImageUtilError SetResolution(TransformHandle handle, uint width, uint height);
         }
+    }
 
-        internal class TransformHandle : SafeHandle
+    internal class TransformHandle : SafeHandle
+    {
+        protected TransformHandle() : base(IntPtr.Zero, true)
         {
-            protected TransformHandle() : base(IntPtr.Zero, true)
-            {
-            }
+        }
 
-            public override bool IsInvalid => handle == IntPtr.Zero;
+        public override bool IsInvalid => handle == IntPtr.Zero;
 
-            protected override bool ReleaseHandle()
+        protected override bool ReleaseHandle()
+        {
+            var ret = ImageUtil.Transform.Destroy(handle);
+            if (ret != ImageUtilError.None)
             {
-                var ret = Transform.Destroy(handle);
-                if (ret != ImageUtilError.None)
-                {
-                    Log.Debug(GetType().FullName, $"Failed to release native {GetType().Name}");
-                    return false;
-                }
-
-                return true;
+                Log.Debug(GetType().FullName, $"Failed to release native {GetType().Name}");
+                return false;
             }
+
+            return true;
         }
     }
-
 }
index 728f4c0..fd593a0 100644 (file)
@@ -16,6 +16,7 @@
 
 using System;
 using System.Runtime.InteropServices;
+using Tizen;
 using Tizen.Multimedia.Util;
 
 internal static partial class Interop
@@ -34,5 +35,12 @@ internal static partial class Interop
 
         [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_extract_color_from_memory")]
         internal static extern ImageUtilError ExtractColorFromMemory(byte[] buffer, int width, int height, out byte rgbR, out byte rgbG, out byte rgbB);
+
+        [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_get_image")]
+        internal static extern ImageUtilError GetImage(IntPtr handle, out int width, out int height,
+            out ImageColorSpace colorspace, out IntPtr data, out int size);
+
+        [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_destroy_image")]
+        internal static extern ImageUtilError Destroy(IntPtr handle);
     }
 }