[ImageUtil] Remove deprecated API and change native pinvoke APIs (#5950)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Util / Interop / Interop.ImageUtil.Encode.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using Tizen;
20 using Tizen.Multimedia.Util;
21
22 internal static partial class Interop
23 {
24     internal static partial class ImageUtil
25     {
26         internal static class Encode
27         {
28             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
29             internal delegate void EncodeCompletedCallback(ImageUtilError ImageUtilError, IntPtr userData, ulong size);
30
31             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_run_async")]
32             internal static extern ImageUtilError EncodeRunAsync(ImageEncoderHandle handle,
33                 EncodeCompletedCallback callback, IntPtr userData = default(IntPtr));
34
35             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_create")]
36             internal static extern ImageUtilError Create(ImageFormat type, out ImageEncoderHandle handle);
37
38             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_destroy")]
39             internal static extern ImageUtilError Destroy(IntPtr handle);
40
41             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_set_quality")]
42             internal static extern ImageUtilError SetQuality(ImageEncoderHandle handle, int quality);
43
44             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_set_png_compression")]
45             internal static extern ImageUtilError SetPngCompression(ImageEncoderHandle handle, PngCompression compression);
46
47             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_run_to_buffer")]
48             internal static extern ImageUtilError RunToBuffer(ImageEncoderHandle handle, IntPtr imageUtilHandle, out IntPtr buffer, out int size);
49
50             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_set_lossless")]
51             internal static extern ImageUtilError SetLossless(ImageEncoderHandle handle, bool lossless);
52
53             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_anim_encode_create")]
54             internal static extern ImageUtilError AnimationCreate(AnimationType type, out IntPtr animHandle);
55
56             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_anim_encode_set_loop_count")]
57             internal static extern ImageUtilError AnimationSetLoopCount(IntPtr animHandle, uint count);
58
59             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_anim_encode_set_background_color")]
60             internal static extern ImageUtilError AnimationSetBackgroundColor(IntPtr animHandle, byte r, byte g, byte b, byte a);
61
62             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_anim_encode_set_lossless")]
63             internal static extern ImageUtilError AnimationSetLossless(IntPtr animHandle, bool isLossless);
64
65             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_anim_encode_add_frame")]
66             internal static extern ImageUtilError AnimationAddFrame(IntPtr animHandle, IntPtr utilHandle, uint delayTime);
67
68             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_anim_encode_save_to_file")]
69             internal static extern ImageUtilError AnimationSaveToFile(IntPtr animHandle, string path);
70
71             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_anim_encode_save_to_buffer")]
72             internal static extern ImageUtilError AnimationSaveToBuffer(IntPtr animHandle, out IntPtr dstBuffer, out ulong size);
73
74             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_anim_encode_destroy")]
75             internal static extern ImageUtilError AnimationDestroy(IntPtr animHandle);
76         }
77     }
78
79     internal class ImageEncoderHandle : SafeHandle
80     {
81         protected ImageEncoderHandle() : base(IntPtr.Zero, true)
82         {
83         }
84
85         public override bool IsInvalid => handle == IntPtr.Zero;
86
87
88         protected override bool ReleaseHandle()
89         {
90             var ret = ImageUtil.Encode.Destroy(handle);
91             if (ret != ImageUtilError.None)
92             {
93                 Log.Debug(GetType().FullName, $"Failed to release native {GetType().Name}");
94                 return false;
95             }
96
97             return true;
98         }
99     }
100
101     internal class AgifImageEncoderHandle : SafeHandle
102     {
103         protected AgifImageEncoderHandle() : base(IntPtr.Zero, true)
104         {
105         }
106
107         public override bool IsInvalid => handle == IntPtr.Zero;
108
109
110         protected override bool ReleaseHandle()
111         {
112             var ret = ImageUtil.Encode.Destroy(handle);
113             if (ret != ImageUtilError.None)
114             {
115                 Log.Debug(GetType().FullName, $"Failed to release native {GetType().Name}");
116                 return false;
117             }
118
119             return true;
120         }
121     }
122 }