ada1c392ef27a4d2c488b7797a041c173f9d2a88
[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_resolution")]
42             internal static extern ImageUtilError SetResolution(ImageEncoderHandle handle, uint width, uint height);
43
44             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_set_colorspace")]
45             internal static extern ImageUtilError SetColorspace(ImageEncoderHandle handle, ImageColorSpace colorspace);
46
47             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_set_quality")]
48             internal static extern ImageUtilError SetQuality(ImageEncoderHandle handle, int quality);
49
50             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_set_png_compression")]
51             internal static extern ImageUtilError SetPngCompression(ImageEncoderHandle handle, PngCompression compression);
52
53             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_set_gif_frame_delay_time")]
54             internal static extern ImageUtilError SetGifFrameDelayTime(ImageEncoderHandle handle, ulong delayTime);
55
56             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_set_output_path")]
57             internal static extern ImageUtilError SetOutputPath(ImageEncoderHandle handle, string path);
58
59             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_set_input_buffer")]
60             internal static extern ImageUtilError SetInputBuffer(ImageEncoderHandle handle, byte[] srcBuffer);
61
62             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_set_output_buffer")]
63             internal static extern ImageUtilError SetOutputBuffer(ImageEncoderHandle handle, out IntPtr dstBuffer);
64
65             [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_encode_run")]
66             internal static extern ImageUtilError Run(ImageEncoderHandle handle, out ulong size);
67         }
68
69         internal class ImageEncoderHandle : SafeHandle
70         {
71             protected ImageEncoderHandle() : base(IntPtr.Zero, true)
72             {
73             }
74
75             public override bool IsInvalid => handle == IntPtr.Zero;
76
77
78             protected override bool ReleaseHandle()
79             {
80                 var ret = Encode.Destroy(handle);
81                 if (ret != ImageUtilError.None)
82                 {
83                     Log.Debug(GetType().FullName, $"Failed to release native {GetType().Name}");
84                     return false;
85                 }
86
87                 return true;
88             }
89         }
90     }
91 }