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
41 /// The list of type of encoded image buffer.
42 /// It will be used when we want to specify the buffer data type.
44 /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
45 [EditorBrowsable(EditorBrowsableState.Never)]
46 [System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1717:Only FlagsAttribute enums should have plural names")]
47 public enum ImageTypes
52 /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
53 [EditorBrowsable(EditorBrowsableState.Never)]
57 /// Vector rasterize images.
59 /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
60 [EditorBrowsable(EditorBrowsableState.Never)]
64 /// Animated vector rasterize images.
66 /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
67 [EditorBrowsable(EditorBrowsableState.Never)]
74 /// <param name="stream">The Stream of the image file.</param>
75 /// <exception cref="ArgumentNullException"> Thrown when stream is null. </exception>
76 /// <exception cref="InvalidOperationException"> Thrown when stream don't have any data. </exception>
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 EncodedImageBuffer(System.IO.Stream stream) : this(GetRawBuffrFromStreamHelper(stream), ImageTypes.RegularImage)
81 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
85 /// Constructor with image type.
87 /// <param name="stream">The Stream of the image file.</param>
88 /// <param name="imageType">The type of the image stream.</param>
89 /// <exception cref="ArgumentNullException"> Thrown when stream is null. </exception>
90 /// <exception cref="InvalidOperationException"> Thrown when stream don't have any data. </exception>
91 /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
92 [EditorBrowsable(EditorBrowsableState.Never)]
93 public EncodedImageBuffer(System.IO.Stream stream, ImageTypes imageType) : this(GetRawBuffrFromStreamHelper(stream), imageType)
95 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
98 internal EncodedImageBuffer(VectorUnsignedChar buffer, ImageTypes imageType) : this(Interop.EncodedImageBuffer.New(VectorUnsignedChar.getCPtr(buffer), (int)imageType), true)
100 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
101 mCachedBuffer = buffer;
104 internal EncodedImageBuffer(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, false)
106 // Note : EncodedImageBuffer don't need to be register in Registry default. So we can create this class from worker thread.
109 internal EncodedImageBuffer(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
114 /// The type of image for this EncodedImageBuffer.
116 /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
117 [EditorBrowsable(EditorBrowsableState.Never)]
118 public ImageTypes ImageType
122 Interop.EncodedImageBuffer.SetImageType(SwigCPtr, (int)value);
123 NDalicPINVOKE.ThrowExceptionIfExists();
127 ImageTypes ret = (ImageTypes)Interop.EncodedImageBuffer.GetImageType(SwigCPtr);
128 NDalicPINVOKE.ThrowExceptionIfExists();
134 /// Generate URI from current buffer.
135 /// We can now use this url for ImageView.ResourceUrl
136 /// Note : the url lifecycle is same as ImageUrl and it's internal usage.
137 /// Store only ImageUrl.ToString() result and re-use that url is Undefined Behavior.
140 /// This API should not be called at worker thread.
142 /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
143 [EditorBrowsable(EditorBrowsableState.Never)]
144 public ImageUrl GenerateUrl()
146 return new ImageUrl(Interop.EncodedImageBuffer.GenerateUrl(this.SwigCPtr.Handle), true);
150 /// Get current raw buffer. (read-only)
151 /// Note : the raw buffer doesn't have memory ownership.
152 /// Access to write something to raw buffer is Undefined Behavior.
154 /// This will not be public opened.
155 internal VectorUnsignedChar GetRawBuffer()
157 mCachedBuffer ??= new VectorUnsignedChar(Interop.EncodedImageBuffer.GetRawBuffer(this.SwigCPtr.Handle), false);
158 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
159 return mCachedBuffer;
165 /// <param name="type"></param>
166 [EditorBrowsable(EditorBrowsableState.Never)]
167 protected override void Dispose(DisposeTypes type)
174 if (type == DisposeTypes.Explicit)
177 //Release your own managed resources here.
178 //You should release all of your own disposable objects here.
179 mCachedBuffer?.Dispose();
185 /// This will not be public opened.
186 [EditorBrowsable(EditorBrowsableState.Never)]
187 protected override void ReleaseSwigCPtr(HandleRef swigCPtr)
189 Interop.EncodedImageBuffer.DeleteEncodedImageBuffer(swigCPtr);
190 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
194 /// Get VectorUnsignedChar from System.IO.Stream.
195 /// This funcion exist only for Constructor.
197 /// This will not be public opened.
198 private static VectorUnsignedChar GetRawBuffrFromStreamHelper(System.IO.Stream stream)
202 throw new ArgumentNullException(nameof(stream));
206 throw new InvalidOperationException("stream don't support to read");
209 // Copy stream to memoryStream
210 System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
211 stream.CopyTo(memoryStream);
212 memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
214 long streamLength = memoryStream.Length;
215 if(streamLength <= 0)
217 throw new InvalidOperationException("stream length is <= 0");
220 // Allocate buffer that internal DALi engine can read
221 VectorUnsignedChar buffer = new VectorUnsignedChar();
223 buffer.Resize((uint)streamLength);
224 var bufferBegin = buffer.Begin();
225 global::System.Runtime.InteropServices.HandleRef bufferRef = SWIGTYPE_p_unsigned_char.getCPtr(bufferBegin);
227 // Copy data from memoryStream to buffer
228 System.Runtime.InteropServices.Marshal.Copy(memoryStream.GetBuffer(), 0, bufferRef.Handle, (int)streamLength);
230 memoryStream.Dispose();