[Vision] Fixed documentation issue
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Vision / MediaVision / ImageObject.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.IO;
19 using InteropImage = Interop.MediaVision.Image;
20
21 namespace Tizen.Multimedia
22 {
23     /// <summary>
24     /// Represents an image object.
25     /// </summary>
26     public class ImageObject : IDisposable
27     {
28         private IntPtr _handle = IntPtr.Zero;
29         private bool _disposed = false;
30
31         /// <summary>
32         /// Initializes a new instance of the <see cref="ImageObject"/> class.
33         /// </summary>
34         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
35         public ImageObject()
36         {
37             InteropImage.Create(out _handle).Validate("Failed to create image object");
38         }
39
40         /// <summary>
41         /// Initializes a new instance of the <see cref="ImageObject"/> class from the specified file.
42         /// </summary>
43         /// <remarks>
44         /// ImageObject has been saved by <see cref="Save()"/> can be loaded.
45         /// </remarks>
46         /// <param name="path">Path to the image object to load.</param>
47         /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
48         /// <exception cref="FileNotFoundException"><paramref name="path"/> is invalid.</exception>
49         /// <exception cref="NotSupportedException">
50         ///     The feature is not supported.\n
51         ///     - or -\n
52         ///     <paramref name="path"/> is not supported file.
53         /// </exception>
54         /// <exception cref="UnauthorizedAccessException">No permission to access the specified file.</exception>
55         /// <seealso cref="Save(string)"/>
56         public ImageObject(string path)
57         {
58             if (path == null)
59             {
60                 throw new ArgumentNullException(nameof(path));
61             }
62             InteropImage.Load(path, out _handle).Validate("Failed to load image object from file");
63         }
64
65         ~ImageObject()
66         {
67             Dispose(false);
68         }
69
70         /// <summary>
71         /// Gets a value that determines how well an image object can be recognized.
72         /// </summary>
73         /// <remarks>
74         /// If recognition rate is too low, try to use another image or change some configuration parameters
75         /// and fill the image object again.
76         /// </remarks>
77         /// <value>
78         /// Recognition rate determines how well an image object can be recognized. This value can be from 0 to 1.
79         /// If the recognition rate is 0 object can not be recognized and the bigger it is the more likely to recognize the object.
80         /// </value>
81         /// <exception cref="ObjectDisposedException">The <see cref="ImageObject"/> has already been disposed of.</exception>
82         /// <seealso cref="ImageFillConfiguration"/>
83         /// <seealso cref="Fill(MediaVisionSource, ImageFillConfiguration, Rectangle?)"/>
84         public double RecognitionRate
85         {
86             get
87             {
88                 double rate = 0;
89                 InteropImage.GetRecognitionRate(Handle, out rate).Validate("Failed to get recognition rate");
90                 return rate;
91             }
92         }
93
94         #region Methods
95         /// <summary>
96         /// Gets the label for the image object.
97         /// </summary>
98         /// <returns>
99         /// The label value; or null if the <see cref="ImageObject"/> has no label.
100         /// </returns>
101         /// <exception cref="ObjectDisposedException">The <see cref="ImageObject"/> has already been disposed of.</exception>
102         /// <seealso cref="SetLabel(int)"/>
103         public int? GetLabel()
104         {
105             int label = 0;
106             var ret = InteropImage.GetLabel(Handle, out label);
107
108             if (ret == MediaVisionError.NoData)
109             {
110                 return null;
111             }
112
113             ret.Validate("Failed to get label");
114             return label;
115         }
116
117         /// <summary>
118         /// Sets the label for the <see cref="ImageObject"/>.
119         /// </summary>
120         /// <seealso cref="GetLabel"/>
121         public void SetLabel(int label)
122         {
123             InteropImage.SetLabel(Handle, label).Validate("Failed to set label");
124         }
125
126         /// <summary>
127         /// Fills the image object.\n
128         /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
129         /// </summary>
130         /// <param name="source">The source image where image object is depicted.</param>
131         /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
132         /// <exception cref="ObjectDisposedException">
133         ///     The <see cref="ImageObject"/> has already been disposed of.\n
134         ///     - or -\n
135         ///     <paramref name="source"/> has already been disposed of.
136         /// </exception>
137         public void Fill(MediaVisionSource source)
138         {
139             InvokeFill(source, null, null);
140         }
141
142         /// <summary>
143         /// Fills the image object.\n
144         /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
145         /// </summary>
146         /// <param name="source">The source image where image object is depicted.</param>
147         /// <param name="config">The configuration used for extract recognition data from source. This value can be null.</param>
148         /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
149         /// <exception cref="ObjectDisposedException">
150         ///     The <see cref="ImageObject"/> has already been disposed of.\n
151         ///     - or -\n
152         ///     <paramref name="source"/> has already been disposed of.\n
153         ///     - or -\n
154         ///     <paramref name="config"/> has already been disposed of.
155         /// </exception>
156         public void Fill(MediaVisionSource source, ImageFillConfiguration config)
157         {
158             InvokeFill(source, config, null);
159         }
160
161         /// <summary>
162         /// Fills the image object.\n
163         /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
164         /// </summary>
165         /// <param name="source">The source image where image object is depicted.</param>
166         /// <param name="rect">Rectangular bound of the image object on the source image.</param>
167         /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
168         /// <exception cref="ObjectDisposedException">
169         ///     The <see cref="ImageObject"/> has already been disposed of.\n
170         ///     - or -\n
171         ///     <paramref name="source"/> has already been disposed of.\n
172         /// </exception>
173         public void Fill(MediaVisionSource source, Rectangle rect)
174         {
175             InvokeFill(source, null, rect);
176         }
177
178         /// <summary>
179         /// Fills the image object.\n
180         /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
181         /// </summary>
182         /// <param name="source">The source image where image object is depicted.</param>
183         /// <param name="config">The configuration used for extract recognition data from source. This value can be null.</param>
184         /// <param name="rect">Rectangular bound of the image object on the source image.</param>
185         /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
186         /// <exception cref="ObjectDisposedException">
187         ///     The <see cref="ImageObject"/> has already been disposed of.\n
188         ///     - or -\n
189         ///     <paramref name="source"/> has already been disposed of.\n
190         ///     - or -\n
191         ///     <paramref name="config"/> has already been disposed of.
192         /// </exception>
193         public void Fill(MediaVisionSource source, ImageFillConfiguration config, Rectangle rect)
194         {
195             InvokeFill(source, config, rect);
196         }
197
198         private MediaVisionError InvokeFill(IntPtr source, IntPtr config, Rectangle? area)
199         {
200             if (area == null)
201             {
202                 return InteropImage.Fill(Handle, config, source, IntPtr.Zero);
203             }
204
205             var rect = area.Value.ToMarshalable();
206
207             return InteropImage.Fill(Handle, config, source, ref rect);
208         }
209
210         private void InvokeFill(MediaVisionSource source, ImageFillConfiguration config, Rectangle? area)
211         {
212             if (source == null)
213             {
214                 throw new ArgumentNullException(nameof(source));
215             }
216
217             InvokeFill(source.Handle, EngineConfiguration.GetHandle(config), area).
218                 Validate("Failed to fill the image object");
219         }
220
221         /// <summary>
222         /// Saves the image object.
223         /// </summary>
224         /// <param name="path">Path to the file to save the model.</param>
225         /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
226         /// <exception cref="UnauthorizedAccessException">No permission to write to the specified path.</exception>
227         /// <exception cref="ObjectDisposedException">The <see cref="FaceRecognitionModel"/> has already been disposed of.</exception>
228         /// <exception cref="DirectoryNotFoundException">The directory for <paramref name="path"/> does not exist.</exception>
229         public void Save(string path)
230         {
231             if (path == null)
232             {
233                 throw new ArgumentNullException(nameof(path));
234             }
235
236             var ret = InteropImage.Save(path, Handle);
237
238             if (ret == MediaVisionError.InvalidPath)
239             {
240                 throw new DirectoryNotFoundException($"The directory for the path({path}) does not exist.");
241             }
242
243             ret.Validate("Failed to save the image object");
244         }
245         #endregion
246
247         #region IDisposable-support
248         public void Dispose()
249         {
250             Dispose(true);
251             GC.SuppressFinalize(this);
252         }
253
254         protected virtual void Dispose(bool disposing)
255         {
256             if (_disposed)
257             {
258                 return;
259             }
260
261             InteropImage.Destroy(_handle);
262             _disposed = true;
263         }
264
265         internal IntPtr Handle
266         {
267             get
268             {
269                 if (_disposed)
270                 {
271                     throw new ObjectDisposedException(nameof(ImageObject));
272                 }
273                 return _handle;
274             }
275         }
276         #endregion
277     }
278 }