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