Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Util / Interop / Interop.ImageUtil.Decode.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 class Decode
25     {
26         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
27         internal delegate void DecodeCompletedCallback(ImageUtilError error, IntPtr userData, int width, int height, ulong size);
28
29         [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_create")]
30         public static extern ImageUtilError Create(out ImageDecoderHandle handle);
31
32         [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_destroy")]
33         internal static extern ImageUtilError Destroy(IntPtr handle);
34
35         [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_input_path")]
36         internal static extern ImageUtilError SetInputPath(ImageDecoderHandle handle, IntPtr path);
37
38         [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_input_buffer")]
39         internal static extern ImageUtilError SetInputBuffer(ImageDecoderHandle handle, byte[] srcBuffer, ulong srcSize);
40
41         [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_output_buffer")]
42         internal static extern ImageUtilError SetOutputBuffer(ImageDecoderHandle handle, out IntPtr dstBuffer);
43
44         [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_colorspace")]
45         internal static extern ImageUtilError SetColorspace(ImageDecoderHandle handle, ImageColorSpace colorspace);
46
47         [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_jpeg_downscale")]
48         internal static extern ImageUtilError SetJpegDownscale(ImageDecoderHandle handle, JpegDownscale downscale);
49
50         [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_run_async")]
51         internal static extern ImageUtilError DecodeRunAsync(ImageDecoderHandle handle, DecodeCompletedCallback callback,
52             IntPtr userData = default(IntPtr));
53
54         [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_run")]
55         internal static extern ImageUtilError DecodeRun(ImageDecoderHandle handle, out int width,
56             out int height, out ulong size);
57     }
58
59     internal class ImageDecoderHandle : SafeHandle
60     {
61         protected ImageDecoderHandle() : base(IntPtr.Zero, true)
62         {
63         }
64
65         public override bool IsInvalid => handle == IntPtr.Zero;
66
67         protected override bool ReleaseHandle()
68         {
69             var ret = Decode.Destroy(handle);
70             if (ret != ImageUtilError.None)
71             {
72                 Log.Debug(GetType().FullName, $"Failed to release native {GetType()}");
73                 return false;
74             }
75
76             return true;
77         }
78
79     }
80 }