protected override void OnDrawFrame() { if (Size.Width <= 0 || Size.Height <= 0) { return; } int width = (int)Size.Width; int height = (int)Size.Height; int stride = 4 * (int)Size.Width; using var pixelBuffer = new PixelBuffer((uint)width, (uint)height, PixelFormat.BGRA8888); var buffer = pixelBuffer.GetBuffer(); var info = new SKImageInfo(width, height, SKColorType.Bgra8888); using (var surface = SKSurface.Create(info, buffer, stride)) { if (surface == null) { Invalidate(); return; } // draw using SkiaSharp SendPaintSurface(new SKPaintSurfaceEventArgs(surface, info)); surface.Canvas.Flush(); } using var pixelData = PixelBuffer.Convert(pixelBuffer); using var url = pixelData.GenerateUrl(); SetImage(url.ToString()); }
private Texture LoadStageFillingTexture(string filepath) { Size2D dimensions = new Size2D(Window.Instance.WindowSize.Width, Window.Instance.WindowSize.Height); PixelBuffer pb = ImageLoading.LoadImageFromFile(filepath, dimensions, FittingModeType.ScaleToFill); PixelData pd = PixelBuffer.Convert(pb); Texture texture = new Texture(TextureType.TEXTURE_2D, pd.GetPixelFormat(), pd.GetWidth(), pd.GetHeight()); texture.Upload(pd); return(texture); }
public void PixelBufferConvert() { tlog.Debug(tag, $"PixelBufferConvert START"); using (PixelBuffer pixelBuffer = new PixelBuffer(100, 50, PixelFormat.BGR8888)) { var testingTarget = PixelBuffer.Convert(pixelBuffer); Assert.IsNotNull(testingTarget, "Can't create success object PixelData"); Assert.IsInstanceOf <PixelData>(testingTarget, "Should be an instance of PixelData type."); testingTarget.Dispose(); } tlog.Debug(tag, $"PixelBufferConvert END (OK)"); }
protected override void OnCreate() { base.OnCreate(); Window window = NUIApplication.GetDefaultWindow(); window.BackgroundColor = Color.Black; View view = new View() { Size = new Size(window.WindowSize) }; window.Add(view); PropertyMap vertexFormat = new PropertyMap(); vertexFormat.Add("aPosition", new PropertyValue((int)PropertyType.Vector2)); VertexBuffer vertexBuffer = new VertexBuffer(vertexFormat); vertexBuffer.SetData(Quad()); Geometry geometry = new Geometry(); geometry.AddVertexBuffer(vertexBuffer); geometry.SetType(Geometry.Type.TRIANGLE_STRIP); Shader shader = new Shader(VERTEX_SHADER, FRAGMENT_SHADER); PixelData pixelData = PixelBuffer.Convert(ImageLoading.LoadImageFromFile( "./res/background_image.jpg", new Size2D(), FittingModeType.ScaleToFill )); Texture texture = new Texture( TextureType.TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() ); texture.Upload(pixelData); TextureSet textureSet = new TextureSet(); textureSet.SetTexture(0u, texture); Renderer renderer = new Renderer(geometry, shader); renderer.SetTextures(textureSet); view.AddRenderer(renderer); }
private void onCaptureFinished(object sender, CaptureFinishedEventArgs e) { log.Debug(tag, $"onCaptureFinished() statue={e.Success} \n"); if (sender is Capture) { log.Debug(tag, $"sender is Capture \n"); PixelBuffer pixelBuffer = capture.GetCapturedBuffer(); PixelData pixelData = PixelBuffer.Convert(pixelBuffer); var url = pixelData.Url;//capture.GetNativeImageSource().Url; capturedImage = new ImageView(url); log.Debug(tag, $"url={url} \n"); capturedImage.Size = new Size(510, 510); capturedImage.Position = new Position(10, 10); root.Add(capturedImage); done = false; } }
public override View Run() { var view = new View { Layout = new LinearLayout { LinearAlignment = LinearLayout.Alignment.Center, LinearOrientation = LinearLayout.Orientation.Vertical, } }; view.UpdateBackgroundColor(Color.FromHex("#618833")); var test1 = new Button { Text = "PixelBuffer Create" }; view.Add(test1); var test2 = new Button { Text = "NativeImageQueue Create" }; view.Add(test2); var label = new Label(); view.Add(label); test1.Clicked += (s, e) => { Stopwatch stopwatch = Stopwatch.StartNew(); for (int i = 0; i < 1000; i++) { using var buffer = new PixelBuffer(500, 500, PixelFormat.BGRA8888); buffer.GetBuffer(); using var pixelData = PixelBuffer.Convert(buffer); using var url = pixelData.GenerateUrl(); } stopwatch.Stop(); label.Text = $"PixelBuffer create time : {stopwatch.ElapsedMilliseconds} ms"; }; test2.Clicked += (s, e) => { Stopwatch stopwatch = Stopwatch.StartNew(); for (int i = 0; i < 1000; i++) { using var queue = new NativeImageQueue(500, 500, NativeImageQueue.ColorFormat.RGBA8888); int width = 0; int height = 0; int stride = 0; var buffer = queue.DequeueBuffer(ref width, ref height, ref stride); queue.EnqueueBuffer(buffer); } stopwatch.Stop(); label.Text = $"NativeImageQueue create time : {stopwatch.ElapsedMilliseconds} ms"; }; return(view); }
TextureSet CreateTextureSet(RendererParameters textParameters, List <string> embeddedItems) { EmbeddedItemInfo[] embeddedItemLayout = new EmbeddedItemInfo[0]; PixelBuffer pixelBuffer = TextUtils.Render(textParameters, ref embeddedItemLayout); uint dstWidth = pixelBuffer.GetWidth(); uint dstHeight = pixelBuffer.GetHeight(); int index = 0; int length = embeddedItemLayout.Length; for (int i = 0; i < length; i++) { EmbeddedItemInfo itemLayout = embeddedItemLayout[i]; int width = (int)itemLayout.Size.Width; int height = (int)itemLayout.Size.Height; int x = (int)itemLayout.Position.X; int y = (int)itemLayout.Position.Y; PixelBuffer itemPixelBuffer = ImageLoading.LoadImageFromFile(embeddedItems[index++]); if (itemPixelBuffer == null) { continue; } itemPixelBuffer.Resize((ushort)width, (ushort)height); itemPixelBuffer.Rotate(itemLayout.Angle); width = (int)itemPixelBuffer.GetWidth(); height = (int)itemPixelBuffer.GetHeight(); PixelFormat itemPixelFormat = itemPixelBuffer.GetPixelFormat(); // Check if the item is out of the buffer. if ((x + width < 0) || (x > dstWidth) || (y < 0) || (y - height > dstHeight)) { // The embedded item is completely out of the buffer. continue; } // Crop if it exceeds the boundaries of the destination buffer. int layoutX = 0; int layoutY = 0; int cropX = 0; int cropY = 0; int newWidth = width; int newHeight = height; bool crop = false; if (0 > x) { newWidth += x; cropX = Math.Abs(x); crop = true; } else { layoutX = x; } if (cropX + newWidth > dstWidth) { crop = true; newWidth -= (int)((cropX + newWidth) - dstWidth); } layoutY = y; if (0 > layoutY) { newHeight += layoutY; cropY = Math.Abs(layoutY); crop = true; } if (cropY + newHeight > dstHeight) { crop = true; newHeight -= (int)((cropY + newHeight) - dstHeight); } int uiCropX = cropX; int uiCropY = cropY; int uiNewWidth = newWidth; int uiNewHeight = newHeight; if (crop) { itemPixelBuffer.Crop((ushort)uiCropX, (ushort)uiCropY, (ushort)uiNewWidth, (ushort)uiNewHeight); } // Blend the item pixel buffer with the text's color according its blending mode. if (ColorBlendingMode.Multiply == itemLayout.ColorBlendingMode) { PixelBuffer buffer = new PixelBuffer((uint)uiNewWidth, (uint)uiNewHeight, itemPixelFormat); IntPtr bufferIntPtr = buffer.GetBuffer(); IntPtr itemBufferIntPtr = itemPixelBuffer.GetBuffer(); uint bytesPerPixel = GetBytesPerPixel(itemPixelFormat); uint size = (uint)(uiNewWidth * uiNewHeight * bytesPerPixel); unsafe { byte *bufferPtr = (byte *)bufferIntPtr.ToPointer(); byte *itemBufferPtr = (byte *)itemBufferIntPtr.ToPointer(); for (uint j = 0; j < size; j += bytesPerPixel) { *(bufferPtr + 0) = (byte)((*(itemBufferPtr + 0u)) * textParameters.TextColor.R); *(bufferPtr + 1) = (byte)((*(itemBufferPtr + 1u)) * textParameters.TextColor.G); *(bufferPtr + 2) = (byte)((*(itemBufferPtr + 2u)) * textParameters.TextColor.B); *(bufferPtr + 3) = (byte)((*(itemBufferPtr + 3u)) * textParameters.TextColor.A); itemBufferPtr += bytesPerPixel; bufferPtr += bytesPerPixel; } } itemPixelBuffer = buffer; } TextUtils.UpdateBuffer(itemPixelBuffer, pixelBuffer, (uint)layoutX, (uint)layoutY, true); } PixelData pixelData = PixelBuffer.Convert(pixelBuffer); Texture texture = new Texture(TextureType.TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight()); texture.Upload(pixelData); TextureSet textureSet = new TextureSet(); textureSet.SetTexture(0u, texture); return(textureSet); }
private void AddManyViews() { Random rand = new Random(); for (int i = 0; i < AutoDisposedObjectCount; i++) { int viewSize = 150; var view = new Custom3DView() { Size = new Size(viewSize, viewSize, viewSize), Position = new Position( rand.Next(10, win.WindowSize.Width - 10), rand.Next(10, win.WindowSize.Height - 10), rand.Next(-3 * viewSize, 3 * viewSize) ), }; root.Add(view); PixelData pixelData = PixelBuffer.Convert(ImageLoading.LoadImageFromFile( resource + "/images/PopupTest/circle.jpg", new Size2D(), FittingModeType.ScaleToFill )); Texture texture = new Texture( TextureType.TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() ); texture.Upload(pixelData); TextureSet textureSet = new TextureSet(); textureSet.SetTexture(0u, texture); Renderer renderer = new Renderer(GenerateGeometry(), new Shader(VERTEX_SHADER, FRAGMENT_SHADER)); renderer.SetTextures(textureSet); view.AddRenderer(renderer); rotateAnimation.AnimateBy(view, "Orientation", new Rotation(new Radian(new Degree(360.0f)), Vector3.YAxis)); } for (int i = 0; i < ManualDisposedObjectCount; i++) { int viewSize = 150; var view = new Custom3DView() { Size = new Size(viewSize, viewSize, viewSize), Position = new Position( rand.Next(10, win.WindowSize.Width - 10), rand.Next(10, win.WindowSize.Height - 10), rand.Next(-3 * viewSize, 3 * viewSize) ), }; root.Add(view); views.Add(view); PixelData pixelData = PixelBuffer.Convert(ImageLoading.LoadImageFromFile( resource + "/images/PaletteTest/red2.jpg", new Size2D(), FittingModeType.ScaleToFill )); Texture texture = new Texture( TextureType.TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() ); texture.Upload(pixelData); TextureSet textureSet = new TextureSet(); textureSet.SetTexture(0u, texture); Renderer renderer = new Renderer(GenerateGeometry(), new Shader(VERTEX_SHADER, FRAGMENT_SHADER)); renderer.SetTextures(textureSet); view.AddRenderer(renderer); rotateAnimation.AnimateBy(view, "Orientation", new Rotation(new Radian(new Degree(-360.0f)), Vector3.YAxis)); } rotateAnimation.Looping = true; rotateAnimation.Play(); }
/// <summary> /// View which is clipping image and applying mask /// </summary> /// <param name="resourceImageUrl">Image which will be cripped</param> /// <param name="maskImageUrl">Image for masking</param> public ClippingMaskView(string resourceImageUrl, string maskImageUrl) { // Load mask image file and make PixelData PixelData pixelData = PixelBuffer.Convert( ImageLoading.LoadImageFromFile( maskImageUrl, new Size2D(), FittingModeType.ScaleToFill ) ); // Make mask image texture and upload. Texture maskTexture = new Texture( TextureType.TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() ); maskTexture.Upload(pixelData); Size maskImageSize = new Size(maskTexture.GetWidth(), maskTexture.GetHeight()); // Background Image will be clipped BackgroundImage = new ImageView() { PositionUsesPivotPoint = true, PivotPoint = Tizen.NUI.PivotPoint.Center, ParentOrigin = Tizen.NUI.ParentOrigin.Center, Size = maskImageSize, ResourceUrl = resourceImageUrl, }; Add(BackgroundImage); // Set properties for render task Camera camera = new Camera(new Vector2(maskImageSize.Width, maskImageSize.Height)) { PositionUsesPivotPoint = true, PivotPoint = Tizen.NUI.PivotPoint.Center, ParentOrigin = Tizen.NUI.ParentOrigin.Center, }; camera.SetInvertYAxis(true); Add(camera); RenderTask task = NUIApplication.GetDefaultWindow().GetRenderTaskList().CreateTask(); task.SetRefreshRate((uint)RenderTask.RefreshRate.REFRESH_ALWAYS); task.SetSourceView(BackgroundImage); task.SetExclusive(true); task.SetInputEnabled(false); task.SetClearColor(new Vector4(1.0f, 1.0f, 1.0f, 1.0f)); task.SetClearEnabled(true); task.SetCamera(camera); // Clipped Texture Texture clippedTexture = new Texture( TextureType.TEXTURE_2D, PixelFormat.RGBA8888, (uint)maskImageSize.Width, (uint)maskImageSize.Height ); FrameBuffer frameBuffer = new FrameBuffer( (uint)maskImageSize.Width, (uint)maskImageSize.Height, (uint)FrameBuffer.Attachment.Mask.NONE ); frameBuffer.AttachColorTexture(clippedTexture); task.SetFrameBuffer(frameBuffer); /* Create Renderer to apply mask */ PropertyMap vertexFormat = new PropertyMap(); vertexFormat.Add("aPosition", new PropertyValue((int)PropertyType.Vector2)); PropertyBuffer vertexBuffer = new PropertyBuffer(vertexFormat); vertexBuffer.SetData(RectangleDataPtr(), 4); /* Create geometry */ Geometry geometry = new Geometry(); geometry.AddVertexBuffer(vertexBuffer); geometry.SetType(Geometry.Type.TRIANGLE_STRIP); /* Create Shader */ Shader shader = new Shader(VERTEX_SHADER, FRAGMENT_SHADER); TextureSet textureSet = new TextureSet(); textureSet.SetTexture(0u, clippedTexture); textureSet.SetTexture(1u, maskTexture); Renderer renderer = new Renderer(geometry, shader); renderer.SetTextures(textureSet); AddRenderer(renderer); }
private void AddManyViews() { Random rand = new Random(); for (int i = 0; i < MaxObject; i++) { int viewSize = 150; var view = new View() { Size = new Size(viewSize, viewSize, viewSize), Position = new Position(rand.Next(10, 600), rand.Next(10, 600)), }; root.Add(view); PixelData pixelData = PixelBuffer.Convert(ImageLoading.LoadImageFromFile( resource + "/images/PopupTest/circle.jpg", new Size2D(), FittingModeType.ScaleToFill )); Texture texture = new Texture( TextureType.TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() ); texture.Upload(pixelData); TextureSet textureSet = new TextureSet(); textureSet.SetTexture(0u, texture); Renderer renderer = new Renderer(GenerateGeometry(), new Shader(VERTEX_SHADER, FRAGMENT_SHADER)); renderer.SetTextures(textureSet); view.AddRenderer(renderer); } for (int i = 0; i < MaxObject; i++) { int viewSize = 150; var view = new View() { Size = new Size(viewSize, viewSize, viewSize), Position = new Position(rand.Next(10, 600), rand.Next(10, 600)), }; root.Add(view); views.Add(view); PixelData pixelData = PixelBuffer.Convert(ImageLoading.LoadImageFromFile( resource + "/images/PaletteTest/red2.jpg", new Size2D(), FittingModeType.ScaleToFill )); Texture texture = new Texture( TextureType.TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() ); texture.Upload(pixelData); TextureSet textureSet = new TextureSet(); textureSet.SetTexture(0u, texture); Renderer renderer = new Renderer(GenerateGeometry(), new Shader(VERTEX_SHADER, FRAGMENT_SHADER)); renderer.SetTextures(textureSet); view.AddRenderer(renderer); } }