Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Vision / MediaVision / ImageTrackingModel.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 InteropModel = Interop.MediaVision.ImageTrackingModel;
20
21 namespace Tizen.Multimedia
22 {
23     /// <summary>
24     /// Represents the image tracking model interface.
25     /// </summary>
26     /// <since_tizen> 3</since_tizen>
27     public class ImageTrackingModel : 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="ImageTrackingModel"/> class.
34         /// </summary>
35         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
36         /// <since_tizen> 3</since_tizen>
37         public ImageTrackingModel()
38         {
39             InteropModel.Create(out _handle).Validate("Failed to create FaceTrackingModel");
40         }
41
42         /// <summary>
43         /// Initializes a new instance of the <see cref="ImageTrackingModel"/> class with the specified path.
44         /// </summary>
45         /// <remarks>
46         /// Model have been saved by <see cref="Save()"/> can be loaded.
47         /// </remarks>
48         /// <param name="modelPath">Path to the model to load.</param>
49         /// <exception cref="ArgumentNullException"><paramref name="modelPath"/> is null.</exception>
50         /// <exception cref="FileNotFoundException"><paramref name="modelPath"/> is invalid.</exception>
51         /// <exception cref="NotSupportedException">
52         ///     The feature is not supported.\n
53         ///     -or-\n
54         ///     <paramref name="modelPath"/> is not supported format.
55         /// </exception>
56         /// <exception cref="UnauthorizedAccessException">No permission to access the specified file.</exception>
57         /// <seealso cref="Save()"/>
58         /// <since_tizen> 3</since_tizen>
59         public ImageTrackingModel(string modelPath)
60         {
61             if (modelPath == null)
62             {
63                 throw new ArgumentNullException(nameof(modelPath));
64             }
65             InteropModel.Load(modelPath, out _handle).Validate("Failed to load ImageTrackingModel from file");
66         }
67
68         ~ImageTrackingModel()
69         {
70             Dispose(false);
71         }
72
73         /// <summary>
74         /// Sets target of image tracking model.\n
75         /// Sets image object which will be tracked by using tracking functionality with this tracking model.
76         /// </summary>
77         /// <param name="imageObject">Image object which will be set as the target for tracking.</param>
78         /// <exception cref="ArgumentNullException"><paramref name="imageObject"/> is null.</exception>
79         /// <exception cref="ObjectDisposedException">
80         ///     The <see cref="ImageTrackingModel"/> has already been disposed of.\n
81         ///     -or-\n
82         ///     <paramref name="imageObject"/> has already been disposed of.
83         /// </exception>
84         /// <since_tizen> 3</since_tizen>
85         public void SetTarget(ImageObject imageObject)
86         {
87             if (imageObject == null)
88             {
89                 throw new ArgumentNullException(nameof(imageObject));
90             }
91
92             InteropModel.SetTarget(imageObject.Handle, Handle).
93                 Validate("Failed to set target of image tracking model");
94         }
95
96         /// <summary>
97         /// Refreshes the state of image tracking model.\n
98         /// Clears moving history and change state to undetected. It is usually called each time before tracking is started
99         /// for the new sequence of sources which is not the direct continuation of the sequence for which tracking has been performed before.
100         /// Tracking algorithm will try to find image by itself.
101         /// </summary>
102         /// <exception cref="ObjectDisposedException">The <see cref="ImageTrackingModel"/> has already been disposed of.</exception>
103         /// <since_tizen> 3</since_tizen>
104         public void Refresh()
105         {
106             InteropModel.Refresh(Handle, IntPtr.Zero).Validate("Failed to refresh state");
107         }
108
109         /// <summary>
110         /// Saves tracking model to the file.
111         /// </summary>
112         /// <param name="path">Path to the file to save the model.</param>
113         /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
114         /// <exception cref="UnauthorizedAccessException">No permission to write to the specified path.</exception>
115         /// <exception cref="ObjectDisposedException">The <see cref="ImageTrackingModel"/> has already been disposed of.</exception>
116         /// <exception cref="DirectoryNotFoundException">The directory for <paramref name="path"/> does not exist.</exception>
117         /// <since_tizen> 3</since_tizen>
118         public void Save(string path)
119         {
120             if (path == null)
121             {
122                 throw new ArgumentNullException(path);
123             }
124
125             var ret = InteropModel.Save(path, Handle);
126
127             if (ret == MediaVisionError.InvalidPath)
128             {
129                 throw new DirectoryNotFoundException($"The directory for the path({path}) does not exist.");
130             }
131
132             ret.Validate("Failed to save tracking model to file");
133         }
134
135         /// <summary>
136         /// Releases all resources used by the <see cref="ImageTrackingModel"/> object.
137         /// </summary>
138         public void Dispose()
139         {
140             Dispose(true);
141             GC.SuppressFinalize(this);
142         }
143
144         /// <summary>
145         /// Releases the resources used by the <see cref="ImageTrackingModel"/> object.
146         /// </summary>
147         /// <param name="disposing">
148         /// true to release both managed and unmanaged resources; false to release only unmanaged resources.
149         /// </param>
150         protected virtual void Dispose(bool disposing)
151         {
152             if (_disposed)
153             {
154                 return;
155             }
156
157             InteropModel.Destroy(_handle);
158             _disposed = true;
159         }
160
161         internal IntPtr Handle
162         {
163             get
164             {
165                 if (_disposed)
166                 {
167                     throw new ObjectDisposedException(nameof(ImageTrackingModel));
168                 }
169                 return _handle;
170             }
171         }
172     }
173 }