[MediaVision] Applied coding rules
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / MediaVision / Image.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.Multimedia;
20 using static Interop.MediaVision;
21
22 namespace Tizen.Multimedia
23 {
24     /// <summary>
25     /// This class represents an interface for Image objects.
26     /// </summary>
27     public class Image : IDisposable
28     {
29         internal IntPtr _imageObjectHandle = IntPtr.Zero;
30         private bool _disposed = false;
31
32         /// <summary>
33         /// Constructor of Image object class
34         /// </summary>
35         public Image()
36         {
37             int ret = Interop.MediaVision.Image.Create(out _imageObjectHandle);
38             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to create image object");
39         }
40
41         /// <summary>
42         /// Constructor of image object class
43         /// </summary>
44         /// <param name="fileName">Name of path/file to load the image object</param>
45         public Image(string fileName)
46         {
47             int ret = Interop.MediaVision.Image.Load(fileName, out _imageObjectHandle);
48             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to load image object from file");
49         }
50
51         /// <summary>
52         /// Destructor of ImageObject
53         /// </summary>
54         ~Image()
55         {
56             Dispose(false);
57         }
58
59         /// <summary>
60         /// Sets and gets a label for the image object
61         /// </summary>
62         public int Label
63         {
64             get
65             {
66                 int label = 0;
67                 MediaVisionError ret = (MediaVisionError)Interop.MediaVision.Image.GetLabel(_imageObjectHandle, out label);
68                 if (ret != MediaVisionError.None)
69                 {
70                     Tizen.Log.Error(MediaVisionLog.Tag, "Failed to get label");
71                 }
72
73                 return label;
74             }
75
76             set
77             {
78                 int ret = Interop.MediaVision.Image.SetLabel(_imageObjectHandle, value);
79                 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to set label");
80             }
81         }
82
83         /// <summary>
84         /// Gets a value that determines how well an image object can be recognized.
85         /// </summary>
86         public double RecognitionRate
87         {
88             get
89             {
90                 double rate = 0;
91                 MediaVisionError ret = (MediaVisionError)Interop.MediaVision.Image.GetRecognitionRate(_imageObjectHandle, out rate);
92                 if (ret != MediaVisionError.None)
93                 {
94                     Tizen.Log.Error(MediaVisionLog.Tag, "Failed to get recognition rate, error : " + ret);
95                 }
96
97                 return rate;
98             }
99         }
100
101         /// <summary>
102         /// Fills the image object.\n
103         /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
104         /// </summary>
105         /// <param name="source">The source image where image object is depicted</param>
106         /// <param name="config">The configuration of engine which will be used for extract recognition data from source. If NULL, then default settings will be used.</param>
107         /// <param name="location">Location of the image object on the source image, or NULL if the object is shown in full</param>
108         public void Fill(MediaVisionSource source, ImageEngineConfiguration config = null, Rectangle location = null)
109         {
110             if (source == null)
111             {
112                 throw new ArgumentException("Inalid source");
113             }
114
115             IntPtr locationPtr = IntPtr.Zero;
116             if (location != null)
117             {
118                 Interop.MediaVision.Rectangle rectangle = new Interop.MediaVision.Rectangle()
119                 {
120                     width = location.Width,
121                     height = location.Height,
122                     x = location.Point.X,
123                     y = location.Point.Y
124                 };
125                 locationPtr = Marshal.AllocHGlobal(Marshal.SizeOf(rectangle));
126                 Marshal.StructureToPtr(rectangle, locationPtr, false);
127             }
128
129             int ret = Interop.MediaVision.Image.Fill(_imageObjectHandle,
130                                                     (config != null) ? config._engineHandle : IntPtr.Zero,
131                                                     source._sourceHandle, locationPtr);
132             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to fill the image object");
133         }
134
135         /// <summary>
136         /// Saves the image object.
137         /// </summary>
138         /// <param name="fileName">Name of the file to path/save the image object</param>
139         public void Save(string fileName)
140         {
141             if (string.IsNullOrEmpty(fileName))
142             {
143                 throw new ArgumentException("Inalid file name");
144             }
145
146             int ret = Interop.MediaVision.Image.Save(fileName, _imageObjectHandle);
147             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to save the image object");
148         }
149
150         public void Dispose()
151         {
152             Dispose(true);
153             GC.SuppressFinalize(this);
154         }
155
156         protected virtual void Dispose(bool disposing)
157         {
158             if (_disposed)
159             {
160                 return;
161             }
162
163             if (disposing)
164             {
165                 // Free managed objects
166             }
167
168             Interop.MediaVision.Image.Destroy(_imageObjectHandle);
169             _disposed = true;
170         }
171     }
172 }