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