Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / MediaFace.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
18
19 using System;
20 using System.Runtime.InteropServices;
21
22 namespace Tizen.Content.MediaContent
23 {
24     /// <summary>
25     /// The Media Face Information API provides functions to manage the face information in the image files.
26     /// </summary>
27     public class MediaFace : IDisposable
28     {
29         private IntPtr _faceHandle = IntPtr.Zero;
30         private bool _disposedValue = false;
31         internal IntPtr Handle
32         {
33             get
34             {
35                 if (_faceHandle == IntPtr.Zero)
36                 {
37                     throw new ObjectDisposedException(nameof(MediaFace));
38                 }
39
40                 return _faceHandle;
41             }
42         }
43
44
45         internal MediaFace(IntPtr handle)
46         {
47             _faceHandle = handle;
48         }
49
50         /// <summary>
51         /// Create Face for Given Image
52         /// </summary>
53         /// <since_tizen> 3 </since_tizen>
54         /// <param name="image">
55         ///image item through which FaceRect has to be tagged.
56         ///</param>
57         ///<param name="rect">Position about the detacted face in the media</param>
58         internal MediaFace(MediaInformation image, FaceRect rect)
59         {
60             MediaContentValidator.ThrowIfError(
61                 Interop.Face.Create(image.MediaId, out _faceHandle), "Failed to create MediaFace");
62
63             try
64             {
65                 MediaContentValidator.ThrowIfError(
66                     Interop.Face.SetFaceRect(Handle, rect.X, rect.Y, rect.Width, rect.Height), "Failed to set Rect to MediaFace");
67             }
68             catch (Exception)
69             {
70                 Interop.Face.Destroy(_faceHandle);
71                 throw;
72             }
73         }
74
75         ~MediaFace()
76         {
77             Dispose(false);
78         }
79
80         /// <summary>
81         /// The Media Face Information API provides functions to manage the face information in the image files.
82         /// </summary>
83         /// <since_tizen> 3 </since_tizen>
84         public FaceRect Rect
85         {
86             get
87             {
88                 int x;
89                 int y;
90                 int width;
91                 int height;
92                 MediaContentValidator.ThrowIfError(
93                     Interop.Face.GetFaceRect(Handle, out x, out y, out width, out height), "Failed to get Rect for the Face");
94
95                 return new FaceRect(x, y, width, height);
96             }
97
98             set
99             {
100                 FaceRect rect = (FaceRect)value;
101                 MediaContentValidator.ThrowIfError(
102                     Interop.Face.SetFaceRect(Handle, rect.X, rect.Y, rect.Width, rect.Height), "Failed to set Rect for the Face");
103             }
104         }
105
106         /// <summary>
107         /// Face id.
108         /// </summary>
109         /// <since_tizen> 3 </since_tizen>
110         public string Id
111         {
112             get
113             {
114                 IntPtr val = IntPtr.Zero;
115                 try
116                 {
117                     MediaContentValidator.ThrowIfError(
118                         Interop.Face.GetFaceId(Handle, out val), "Failed to get value");
119
120                     return Marshal.PtrToStringAnsi(val);
121                 }
122                 finally
123                 {
124                     Interop.Libc.Free(val);
125                 }
126             }
127         }
128
129         /// <summary>
130         /// Media uuid from the face
131         /// </summary>
132         /// <since_tizen> 3 </since_tizen>
133         public string MediaInformationId
134         {
135             get
136             {
137                 IntPtr val = IntPtr.Zero;
138                 try
139                 {
140                     MediaContentValidator.ThrowIfError(
141                         Interop.Face.GetMediaId(Handle, out val), "Failed to get value");
142
143                     return Marshal.PtrToStringAnsi(val);
144                 }
145                 finally
146                 {
147                     Interop.Libc.Free(val);
148                 }
149             }
150         }
151         /// <summary>
152         /// Tag name for the MediaFace.
153         /// </summary>
154         /// <since_tizen> 3 </since_tizen>
155         public string Tag
156         {
157             get
158             {
159                 IntPtr val = IntPtr.Zero;
160                 try
161                 {
162                     MediaContentValidator.ThrowIfError(
163                         Interop.Face.GetTag(Handle, out val), "Failed to get value");
164
165                     return Marshal.PtrToStringAnsi(val);
166                 }
167                 finally
168                 {
169                     Interop.Libc.Free(val);
170                 }
171             }
172
173             set
174             {
175                 MediaContentValidator.ThrowIfError(
176                     Interop.Face.SetTag(Handle, value), "Failed to set value");
177             }
178         }
179
180         /// <summary>
181         /// Orientation Value for the face
182         /// </summary>
183         /// <since_tizen> 3 </since_tizen>
184         public MediaContentOrientation Orientation
185         {
186             get
187             {
188                 int orientation;
189                 MediaContentValidator.ThrowIfError(
190                     Interop.Face.GetOrientation(Handle, out orientation), "Failed to value");
191
192                 return (MediaContentOrientation)orientation;
193             }
194
195             set
196             {
197                 MediaContentValidator.ThrowIfError(
198                     Interop.Face.SetOrientation(Handle, (int)value), "Failed to set value");
199             }
200         }
201
202         /// <summary>
203         /// Dispose API for closing the internal resources.
204         /// </summary>
205         /// <since_tizen> 3 </since_tizen>
206         public void Dispose()
207         {
208             Dispose(true);
209             GC.SuppressFinalize(this);
210         }
211
212         protected virtual void Dispose(bool disposing)
213         {
214             if (!_disposedValue)
215             {
216                 if (_faceHandle != IntPtr.Zero)
217                 {
218                     Interop.Face.Destroy(_faceHandle);
219                     _faceHandle = IntPtr.Zero;
220                 }
221
222                 _disposedValue = true;
223             }
224         }
225     }
226 }