Merge "C# binding for PixelBuffer::GetMetadata()"
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / PixelBuffer.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 namespace Tizen.NUI
18 {
19
20     /// <summary>
21     /// The PixelBuffer object holds a pixel buffer.
22     /// The PixelBuffer keeps ownership of it's initial buffer however, the
23     /// user is free to modify the pixel data, either directly, or via image operations.
24     ///
25     /// In order to upload the pixel data to texture memory, there are two
26     /// possibilities - either convert it back to a PixelData object, which
27     /// releases the PixelBuffer object, leaving the user with an empty handle
28     /// (ideal for one-time indirect image manipulation), or create a new
29     /// PixelData object from this object, leaving the buffer intact (ideal
30     /// for continuous manipulation)
31     /// </summary>
32     public class PixelBuffer : BaseHandle
33     {
34         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
35
36         internal PixelBuffer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.PixelBuffer_SWIGUpcast(cPtr), cMemoryOwn)
37         {
38             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
39         }
40
41         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(PixelBuffer obj)
42         {
43             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
44         }
45
46         /// <summary>
47         /// Dispose.
48         /// </summary>
49         /// <since_tizen> 5 </since_tizen>
50         protected override void Dispose(DisposeTypes type)
51         {
52             if (disposed)
53             {
54                 return;
55             }
56
57             if (type == DisposeTypes.Explicit)
58             {
59                 //Called by User
60                 //Release your own managed resources here.
61                 //You should release all of your own disposable objects here.
62
63             }
64
65             //Release your own unmanaged resources here.
66             //You should not access any managed member here except static instance.
67             //because the execution order of Finalizes is non-deterministic.
68             if (swigCPtr.Handle != global::System.IntPtr.Zero)
69             {
70                 if (swigCMemOwn)
71                 {
72                     swigCMemOwn = false;
73                     NDalicPINVOKE.delete_PixelBuffer(swigCPtr);
74                 }
75                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
76             }
77
78             base.Dispose(type);
79         }
80
81         /// <summary>
82         /// Create a PixelBuffer with it's own data buffer.
83         /// </summary>
84         /// <param name="width">The pixel buffer width</param>
85         /// <param name="height">The pixel buffer height</param>
86         /// <param name="pixelFormat">The pixel format</param>
87         public PixelBuffer(uint width, uint height, PixelFormat pixelFormat) : this(NDalicPINVOKE.PixelBuffer_New(width, height, (int)pixelFormat), true)
88         {
89             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
90         }
91
92         internal PixelBuffer(PixelBuffer handle) : this(NDalicPINVOKE.new_PixelBuffer__SWIG_1(PixelBuffer.getCPtr(handle)), true)
93         {
94             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
95         }
96
97         internal PixelBuffer Assign(PixelBuffer rhs)
98         {
99             PixelBuffer ret = new PixelBuffer(NDalicPINVOKE.PixelBuffer_Assign(swigCPtr, PixelBuffer.getCPtr(rhs)), false);
100             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
101             return ret;
102         }
103
104         /// <summary>
105         /// Convert to a pixel data and release the pixelBuffer's object.
106         /// This handle is left empty.
107         /// Any other handles that keep a reference to this object
108         /// will be left with no buffer, trying to access it will return NULL.
109         /// </summary>
110         /// <param name="pixelBuffer">A pixel buffer</param>
111         /// <returns>A new PixelData which takes ownership of the PixelBuffer's buffer.</returns>
112         public static PixelData Convert(PixelBuffer pixelBuffer)
113         {
114             PixelData ret = new PixelData(NDalicPINVOKE.PixelBuffer_Convert(PixelBuffer.getCPtr(pixelBuffer)), true);
115             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
116             return ret;
117         }
118
119         /// <summary>
120         /// Copy the data from this object into a new PixelData object, which could be
121         /// used for uploading to a texture.
122         /// </summary>
123         /// <returns>.</returns>
124         public PixelData CreatePixelData()
125         {
126             PixelData ret = new PixelData(NDalicPINVOKE.PixelBuffer_CreatePixelData(swigCPtr), true);
127             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
128             return ret;
129         }
130
131         internal SWIGTYPE_p_unsigned_char GetBuffer()
132         {
133             global::System.IntPtr cPtr = NDalicPINVOKE.PixelBuffer_GetBuffer(swigCPtr);
134             SWIGTYPE_p_unsigned_char ret = (cPtr == global::System.IntPtr.Zero) ? null : new SWIGTYPE_p_unsigned_char(cPtr, false);
135             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
136             return ret;
137         }
138
139         /// <summary>
140         /// Gets the width of the buffer in pixels.
141         /// </summary>
142         /// <returns>The width of the buffer in pixels.</returns>
143         public uint GetWidth()
144         {
145             uint ret = NDalicPINVOKE.PixelBuffer_GetWidth(swigCPtr);
146             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
147             return ret;
148         }
149
150         /// <summary>
151         /// Gets the height of the buffer in pixels.
152         /// </summary>
153         /// <returns>The height of the buffer in pixels.</returns>
154         public uint GetHeight()
155         {
156             uint ret = NDalicPINVOKE.PixelBuffer_GetHeight(swigCPtr);
157             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
158             return ret;
159         }
160
161         /// <summary>
162         /// Gets the pixel format.
163         /// </summary>
164         /// <returns>The pixel format.</returns>
165         public PixelFormat GetPixelFormat()
166         {
167             PixelFormat ret = (PixelFormat)NDalicPINVOKE.PixelBuffer_GetPixelFormat(swigCPtr);
168             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
169             return ret;
170         }
171
172         /// <summary>
173         /// Apply the mask to this pixel data, and return a new pixel data containing
174         /// the masked image. If this PixelBuffer doesn't have an alpha channel, then
175         /// the resultant PixelBuffer will be converted to a format that supports at
176         /// least the width of the color channels and the alpha channel from the mask.
177         ///
178         /// If cropToMask is set to true, then the contentScale is applied first to
179         /// this buffer, and the target buffer is cropped to the size of the mask. If
180         /// it's set to false, then the mask is scaled to match this buffer's size
181         /// before the mask is applied.
182         /// </summary>
183         /// <param name="mask">The mask to apply.</param>
184         /// <param name="contentScale">The scaling factor to apply to the content.</param>
185         /// <param name="cropToMask">Whether to crop the output to the mask size (true) or scale the mask to the content size (false).</param>
186         public void ApplyMask(PixelBuffer mask, float contentScale, bool cropToMask)
187         {
188             NDalicPINVOKE.PixelBuffer_ApplyMask__SWIG_0(swigCPtr, PixelBuffer.getCPtr(mask), contentScale, cropToMask);
189             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
190         }
191
192         /// <summary>
193         /// Apply the mask to this pixel data, and return a new pixel data containing
194         /// the masked image. If this PixelBuffer doesn't have an alpha channel, then
195         /// the resultant PixelBuffer will be converted to a format that supports at
196         /// least the width of the color channels and the alpha channel from the mask.
197         ///
198         /// If cropToMask is set to true, then the contentScale is applied first to
199         /// this buffer, and the target buffer is cropped to the size of the mask. If
200         /// it's set to false, then the mask is scaled to match this buffer's size
201         /// before the mask is applied.
202         /// </summary>
203         /// <param name="mask">The mask to apply.</param>
204         /// <param name="contentScale">The scaling factor to apply to the content.</param>
205         /// <returns>.</returns>
206         public void ApplyMask(PixelBuffer mask, float contentScale)
207         {
208             NDalicPINVOKE.PixelBuffer_ApplyMask__SWIG_1(swigCPtr, PixelBuffer.getCPtr(mask), contentScale);
209             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
210         }
211
212         /// <summary>
213         /// Apply the mask to this pixel data, and return a new pixel data containing
214         /// the masked image. If this PixelBuffer doesn't have an alpha channel, then
215         /// the resultant PixelBuffer will be converted to a format that supports at
216         /// least the width of the color channels and the alpha channel from the mask.
217         ///
218         /// If cropToMask is set to true, then the contentScale is applied first to
219         /// this buffer, and the target buffer is cropped to the size of the mask. If
220         /// it's set to false, then the mask is scaled to match this buffer's size
221         /// before the mask is applied.
222         /// </summary>
223         /// <param name="mask">The mask to apply.</param>
224         public void ApplyMask(PixelBuffer mask)
225         {
226             NDalicPINVOKE.PixelBuffer_ApplyMask__SWIG_2(swigCPtr, PixelBuffer.getCPtr(mask));
227             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
228         }
229
230         /// <summary>
231         /// Apply a Gaussian blur to this pixel data with the given radius.
232         /// A bigger radius will yield a blurrier image. Only works for pixel data in RGBA format.
233         /// </summary>
234         /// <param name="blurRadius">The radius for Gaussian blur. A value of 0 or negative value indicates no blur.</param>
235         public void ApplyGaussianBlur(float blurRadius)
236         {
237             NDalicPINVOKE.PixelBuffer_ApplyGaussianBlur(swigCPtr, blurRadius);
238             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
239         }
240
241         /// <summary>
242         /// Crops this buffer to the given crop rectangle.
243         /// </summary>
244         /// <param name="x">The top left corner's X.</param>
245         /// <param name="y">The top left corner's Y.</param>
246         /// <param name="width">The crop width.</param>
247         /// <param name="height">The crop height.</param>
248         public void Crop(ushort x, ushort y, ushort width, ushort height)
249         {
250             NDalicPINVOKE.PixelBuffer_Crop(swigCPtr, x, y, width, height);
251             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
252         }
253
254         /// <summary>
255         /// Resizes the buffer to the given dimensions.
256         /// </summary>
257         /// <param name="width">The new width.</param>
258         /// <param name="height">The new height.</param>
259         public void Resize(ushort width, ushort height)
260         {
261             NDalicPINVOKE.PixelBuffer_Resize(swigCPtr, width, height);
262             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
263         }
264
265         public PropertyMap GetMetadata()
266         {
267             var retval = new PropertyMap();
268             NDalicPINVOKE.PixelBuffer_GetMetadata(swigCPtr, PropertyMap.getCPtr(retval));
269             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
270             return retval;
271         }
272
273         internal PixelBuffer(SWIGTYPE_p_unsigned_char pointer) : this(NDalicPINVOKE.new_PixelBuffer__SWIG_2(SWIGTYPE_p_unsigned_char.getCPtr(pointer)), true)
274         {
275             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
276         }
277
278     }
279
280 }