52f415e0c8a380a419950bcf52dd70d87367215e
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Images / EncodedImageBuffer.cs
1 /*
2  * Copyright(c) 2021 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
18 using System;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI
22 {
23     using global::System;
24     using global::System.ComponentModel;
25     using global::System.Runtime.InteropServices;
26
27     /// <summary>
28     /// Class for Encoded Image Buffer.
29     /// Buffer comes from System.IO.Stream.
30     /// This data will decode internally when you use GeneratedUrl as View's ResourceUrl.
31     /// Note: This class doesn't allow to fix/change anything.
32     /// Only constructor allow to setup data.
33     /// </summary>
34     /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
35     [EditorBrowsable(EditorBrowsableState.Never)]
36     public class EncodedImageBuffer : BaseHandle
37     {
38         private ImageUrl mCachedImageUrl = null; // cached Generated Url.
39         private VectorUnsignedChar mCachedBuffer = null; // cached encoded raw buffer
40
41         /// <summary>
42         /// Constructor.
43         /// </summary>
44         /// <param name="stream">The Stream of the image file.</param>
45         /// <exception cref="ArgumentNullException"> Thrown when stream is null. </exception>
46         /// <exception cref="InvalidOperationException"> Thrown when stream don't have any data. </exception>
47         /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
48         [EditorBrowsable(EditorBrowsableState.Never)]
49         public EncodedImageBuffer(System.IO.Stream stream) : this(GetRawBuffrFromStreamHelper(stream))
50         {
51             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
52         }
53
54         internal EncodedImageBuffer(VectorUnsignedChar buffer) : this(Interop.EncodedImageBuffer.New(VectorUnsignedChar.getCPtr(buffer)), true)
55         {
56             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
57             mCachedBuffer = buffer;
58         }
59
60         internal EncodedImageBuffer(EncodedImageBuffer handle) : this(Interop.EncodedImageBuffer.NewEncodedImageBuffer(EncodedImageBuffer.getCPtr(handle)), true)
61         {
62             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
63         }
64
65         internal EncodedImageBuffer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
66         {
67         }
68
69         /// <summary>
70         /// Generate URI from current buffer.
71         /// We can now use this url for ImageView.ResourceUrl
72         /// Note : the url lifecycle is same as ImageUrl and it's internal usage.
73         /// Store only ImageUrl.ToString() result and re-use that url is Undefined Behavior.
74         /// </summary>
75         /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
76         [EditorBrowsable(EditorBrowsableState.Never)]
77         public ImageUrl GenerateUrl()
78         {
79             mCachedImageUrl ??= new ImageUrl(Interop.EncodedImageBuffer.GenerateUrl(this.SwigCPtr.Handle), true);
80             return mCachedImageUrl;
81         }
82
83         /// <summary>
84         /// Get current raw buffer. (read-only)
85         /// Note : the raw buffer doesn't have memory ownership.
86         /// Access to write something to raw buffer is Undefined Behavior.
87         /// </summary>
88         /// This will not be public opened.
89         internal VectorUnsignedChar GetRawBuffer()
90         {
91             mCachedBuffer ??= new VectorUnsignedChar(Interop.EncodedImageBuffer.GetRawBuffer(this.SwigCPtr.Handle), false);
92             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
93             return mCachedBuffer;
94         }
95
96         /// <summary>
97         /// Dispose
98         /// </summary>
99         /// <param name="type"></param>
100         [EditorBrowsable(EditorBrowsableState.Never)]
101         protected override void Dispose(DisposeTypes type)
102         {
103             if (disposed)
104             {
105                 return;
106             }
107
108             if (type == DisposeTypes.Explicit)
109             {
110                 //Called by User
111                 //Release your own managed resources here.
112                 //You should release all of your own disposable objects here.
113                 mCachedImageUrl?.Dispose();
114                 mCachedBuffer?.Dispose();
115             }
116
117             base.Dispose(type);
118         }
119
120         /// This will not be public opened.
121         [EditorBrowsable(EditorBrowsableState.Never)]
122         protected override void ReleaseSwigCPtr(HandleRef swigCPtr)
123         {
124             Interop.EncodedImageBuffer.DeleteEncodedImageBuffer(swigCPtr);
125             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
126         }
127
128         /// <summary>
129         /// Get VectorUnsignedChar from System.IO.Stream.
130         /// This funcion exist only for Constructor.
131         /// </summary>
132         /// This will not be public opened.
133         private static VectorUnsignedChar GetRawBuffrFromStreamHelper(System.IO.Stream stream)
134         {
135             if(stream == null)
136             {
137                 throw new ArgumentNullException(nameof(stream));
138             }
139             if(!stream.CanRead)
140             {
141                 throw new InvalidOperationException("stream don't support to read");
142             }
143
144             // Copy stream to memoryStream
145             System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
146             stream.CopyTo(memoryStream);
147             memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
148
149             long streamLength = memoryStream.Length;
150             if(streamLength <= 0)
151             {
152                 throw new InvalidOperationException("stream length is <= 0");
153             }
154
155             // Allocate buffer that internal DALi engine can read
156             VectorUnsignedChar buffer = new VectorUnsignedChar();
157
158             buffer.Resize((uint)streamLength);
159             var bufferBegin = buffer.Begin();
160             global::System.Runtime.InteropServices.HandleRef bufferRef = SWIGTYPE_p_unsigned_char.getCPtr(bufferBegin);
161
162             // Copy data from memoryStream to buffer
163             System.Runtime.InteropServices.Marshal.Copy(memoryStream.GetBuffer(), 0, bufferRef.Handle, (int)streamLength);
164
165             memoryStream.Dispose();
166
167             return buffer;
168         }
169     }
170 }