2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.ComponentModel;
24 using global::System.ComponentModel;
25 using global::System.Runtime.InteropServices;
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.
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
38 private VectorUnsignedChar mCachedBuffer = null; // cached encoded raw buffer
43 /// <param name="stream">The Stream of the image file.</param>
44 /// <exception cref="ArgumentNullException"> Thrown when stream is null. </exception>
45 /// <exception cref="InvalidOperationException"> Thrown when stream don't have any data. </exception>
46 /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
47 [EditorBrowsable(EditorBrowsableState.Never)]
48 public EncodedImageBuffer(System.IO.Stream stream) : this(GetRawBuffrFromStreamHelper(stream))
50 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
53 internal EncodedImageBuffer(VectorUnsignedChar buffer) : this(Interop.EncodedImageBuffer.New(VectorUnsignedChar.getCPtr(buffer)), true)
55 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
56 mCachedBuffer = buffer;
59 internal EncodedImageBuffer(EncodedImageBuffer handle) : this(Interop.EncodedImageBuffer.NewEncodedImageBuffer(EncodedImageBuffer.getCPtr(handle)), true)
61 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
64 internal EncodedImageBuffer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
69 /// Generate URI from current buffer.
70 /// We can now use this url for ImageView.ResourceUrl
71 /// Note : the url lifecycle is same as ImageUrl and it's internal usage.
72 /// Store only ImageUrl.ToString() result and re-use that url is Undefined Behavior.
75 /// This API should not be called at worker thread.
77 /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
78 [EditorBrowsable(EditorBrowsableState.Never)]
79 public ImageUrl GenerateUrl()
81 return new ImageUrl(Interop.EncodedImageBuffer.GenerateUrl(this.SwigCPtr.Handle), true);
85 /// Get current raw buffer. (read-only)
86 /// Note : the raw buffer doesn't have memory ownership.
87 /// Access to write something to raw buffer is Undefined Behavior.
89 /// This will not be public opened.
90 internal VectorUnsignedChar GetRawBuffer()
92 mCachedBuffer ??= new VectorUnsignedChar(Interop.EncodedImageBuffer.GetRawBuffer(this.SwigCPtr.Handle), false);
93 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
100 /// <param name="type"></param>
101 [EditorBrowsable(EditorBrowsableState.Never)]
102 protected override void Dispose(DisposeTypes type)
109 if (type == DisposeTypes.Explicit)
112 //Release your own managed resources here.
113 //You should release all of your own disposable objects here.
114 mCachedBuffer?.Dispose();
120 /// This will not be public opened.
121 [EditorBrowsable(EditorBrowsableState.Never)]
122 protected override void ReleaseSwigCPtr(HandleRef swigCPtr)
124 Interop.EncodedImageBuffer.DeleteEncodedImageBuffer(swigCPtr);
125 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
129 /// Get VectorUnsignedChar from System.IO.Stream.
130 /// This funcion exist only for Constructor.
132 /// This will not be public opened.
133 private static VectorUnsignedChar GetRawBuffrFromStreamHelper(System.IO.Stream stream)
137 throw new ArgumentNullException(nameof(stream));
141 throw new InvalidOperationException("stream don't support to read");
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);
149 long streamLength = memoryStream.Length;
150 if(streamLength <= 0)
152 throw new InvalidOperationException("stream length is <= 0");
155 // Allocate buffer that internal DALi engine can read
156 VectorUnsignedChar buffer = new VectorUnsignedChar();
158 buffer.Resize((uint)streamLength);
159 var bufferBegin = buffer.Begin();
160 global::System.Runtime.InteropServices.HandleRef bufferRef = SWIGTYPE_p_unsigned_char.getCPtr(bufferBegin);
162 // Copy data from memoryStream to buffer
163 System.Runtime.InteropServices.Marshal.Copy(memoryStream.GetBuffer(), 0, bufferRef.Handle, (int)streamLength);
165 memoryStream.Dispose();