From 670a2a2573deae440a109cc009bec9c022dfa505 Mon Sep 17 00:00:00 2001 From: Nelson Carrillo Date: Sun, 13 Sep 2015 15:44:34 -0400 Subject: [PATCH 1/2] Added support for loading Bitmaps from a Stream to the Gtk/Cairo platform --- src/Gtk/Perspex.Cairo/CairoPlatform.cs | 11 ++++++---- src/Gtk/Perspex.Cairo/Media/DrawingContext.cs | 2 +- .../Perspex.Cairo/Media/Imaging/BitmapImpl.cs | 12 ++++++----- .../Media/Imaging/RenderTargetBitmapImpl.cs | 21 ++++++++++++++++--- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/Gtk/Perspex.Cairo/CairoPlatform.cs b/src/Gtk/Perspex.Cairo/CairoPlatform.cs index 6105ef3046..5ddfe1eab0 100644 --- a/src/Gtk/Perspex.Cairo/CairoPlatform.cs +++ b/src/Gtk/Perspex.Cairo/CairoPlatform.cs @@ -25,7 +25,7 @@ namespace Perspex.Cairo public IBitmapImpl CreateBitmap(int width, int height) { - return new BitmapImpl(new ImageSurface(Format.Argb32, width, height)); + return new BitmapImpl(new Gdk.Pixbuf(Gdk.Colorspace.Rgb, true, 32, width, height)); } public IFormattedTextImpl CreateFormattedText( @@ -57,13 +57,16 @@ namespace Perspex.Cairo public IBitmapImpl LoadBitmap(string fileName) { - ImageSurface result = new ImageSurface(fileName); - return new BitmapImpl(result); + var pixbuf = new Gdk.Pixbuf(fileName); + + return new BitmapImpl(pixbuf); } public IBitmapImpl LoadBitmap(Stream stream) { - throw new NotImplementedException(); + var pixbuf = new Gdk.Pixbuf(stream); + + return new BitmapImpl(pixbuf); } private Pango.Context GetPangoContext(IPlatformHandle handle) diff --git a/src/Gtk/Perspex.Cairo/Media/DrawingContext.cs b/src/Gtk/Perspex.Cairo/Media/DrawingContext.cs index c01d1f1ac3..4c99590cb6 100644 --- a/src/Gtk/Perspex.Cairo/Media/DrawingContext.cs +++ b/src/Gtk/Perspex.Cairo/Media/DrawingContext.cs @@ -63,7 +63,7 @@ namespace Perspex.Cairo.Media _context.Save(); _context.Scale(scaleX, scaleY); - _context.SetSourceSurface(impl.Surface, (int)sourceRect.X, (int)sourceRect.Y); + Gdk.CairoHelper.SetSourcePixbuf(_context, impl.Surface, (int)sourceRect.X, (int)sourceRect.Y); _context.Rectangle(sourceRect.ToCairo()); _context.Fill(); _context.Restore(); diff --git a/src/Gtk/Perspex.Cairo/Media/Imaging/BitmapImpl.cs b/src/Gtk/Perspex.Cairo/Media/Imaging/BitmapImpl.cs index 5748790626..4abecba6c6 100644 --- a/src/Gtk/Perspex.Cairo/Media/Imaging/BitmapImpl.cs +++ b/src/Gtk/Perspex.Cairo/Media/Imaging/BitmapImpl.cs @@ -10,22 +10,24 @@ namespace Perspex.Cairo.Media.Imaging public class BitmapImpl : IBitmapImpl { - public BitmapImpl(Cairo.ImageSurface surface) + public BitmapImpl(Gdk.Pixbuf pixbuf) { - Surface = surface; + Surface = pixbuf; } public int PixelWidth => Surface.Width; public int PixelHeight => Surface.Height; - public Cairo.ImageSurface Surface + public Gdk.Pixbuf Surface { - get; } + get; + } public void Save(string fileName) { - Surface.WriteToPng(fileName); + // TODO: Test + Surface.Save(fileName, "png"); } } } diff --git a/src/Gtk/Perspex.Cairo/Media/Imaging/RenderTargetBitmapImpl.cs b/src/Gtk/Perspex.Cairo/Media/Imaging/RenderTargetBitmapImpl.cs index 51cdc645ef..d3e0a049f4 100644 --- a/src/Gtk/Perspex.Cairo/Media/Imaging/RenderTargetBitmapImpl.cs +++ b/src/Gtk/Perspex.Cairo/Media/Imaging/RenderTargetBitmapImpl.cs @@ -8,23 +8,38 @@ namespace Perspex.Cairo.Media.Imaging { using Cairo = global::Cairo; - public class RenderTargetBitmapImpl : BitmapImpl, IRenderTargetBitmapImpl + public class RenderTargetBitmapImpl : IRenderTargetBitmapImpl { public RenderTargetBitmapImpl( - Cairo.ImageSurface surface) - : base(surface) + Cairo.ImageSurface surface) { + } + public int PixelWidth => Surface.Width; + + public int PixelHeight => Surface.Height; + public void Dispose() { Surface.Dispose(); } + + public Cairo.ImageSurface Surface + { + get; + } + public void Render(IVisual visual) { Renderer renderer = new Renderer(Surface); renderer.Render(visual, new PlatformHandle(IntPtr.Zero, "RTB")); } + + public void Save(string fileName) + { + throw new NotImplementedException(); + } } } \ No newline at end of file From a1785ea43f1f5b21c5af5d29f74767c735fb895a Mon Sep 17 00:00:00 2001 From: Nelson Carrillo Date: Sun, 13 Sep 2015 15:46:07 -0400 Subject: [PATCH 2/2] Fixing broken RTB --- src/Gtk/Perspex.Cairo/Media/Imaging/RenderTargetBitmapImpl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Gtk/Perspex.Cairo/Media/Imaging/RenderTargetBitmapImpl.cs b/src/Gtk/Perspex.Cairo/Media/Imaging/RenderTargetBitmapImpl.cs index d3e0a049f4..8c017d9cfd 100644 --- a/src/Gtk/Perspex.Cairo/Media/Imaging/RenderTargetBitmapImpl.cs +++ b/src/Gtk/Perspex.Cairo/Media/Imaging/RenderTargetBitmapImpl.cs @@ -39,7 +39,7 @@ namespace Perspex.Cairo.Media.Imaging public void Save(string fileName) { - throw new NotImplementedException(); + Surface.WriteToPng(fileName); } } } \ No newline at end of file