From d28c2131493cebaa9e9fc27e1ae5d58e2a96d55f Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 5 Oct 2008 20:31:11 +0000 Subject: [PATCH] ThePimp: we can now open and display files. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2873 92316355-f0b4-4df1-b90c-862c8a59935f --- ThePimp/MainWindow.cs | 11 +++++------ ThePimp/PictureView.cs | 26 ++++++++++++++++--------- pipi-sharp/Picture.cs | 44 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 15 deletions(-) diff --git a/ThePimp/MainWindow.cs b/ThePimp/MainWindow.cs index 3d4ab07..bcdc007 100644 --- a/ThePimp/MainWindow.cs +++ b/ThePimp/MainWindow.cs @@ -23,9 +23,6 @@ public partial class MainWindow: Gtk.Window { Build (); Title += " v" + Libpipi.getVersion(); - - notebook1.Add(new PictureView(new Pipi.Picture("random:1024x1024"))); - //scrolledwindow1.Add(new PictureView(new Pipi.Picture("random:1024x1024"))); } protected void OnDeleteEvent (object sender, DeleteEventArgs a) @@ -41,9 +38,11 @@ public partial class MainWindow: Gtk.Window open.Destroy(); if(p != null) { - Title += " image " + p.Width + "x" + p.Height; - notebook1.RemovePage(0); - notebook1.AppendPage(new PictureView(p), null); + while(notebook1.NPages > 0) + notebook1.RemovePage(0); + int n = notebook1.AppendPage(new PictureView(p), + new Label(p.FileName)); + notebook1.Page = n; } } diff --git a/ThePimp/PictureView.cs b/ThePimp/PictureView.cs index 8ac95b7..c8a0d53 100644 --- a/ThePimp/PictureView.cs +++ b/ThePimp/PictureView.cs @@ -37,20 +37,28 @@ namespace ThePimp protected override bool OnExposeEvent(Gdk.EventExpose e) { bool ret = base.OnExposeEvent(e); - //Console.WriteLine("expose {0}x{1}+{2}+{3}", e.Area.Width, e.Area.Height, e.Area.X, e.Area.Y); - GdkWindow.DrawLayout(Style.TextGC(StateType.Normal), 40 - (int)_hadj.Value, 40 - (int)_vadj.Value, _l); - GdkWindow.DrawLayout(Style.TextGC(StateType.Normal), 560 - (int)_hadj.Value, 40 - (int)_vadj.Value, _l); - GdkWindow.DrawLayout(Style.TextGC(StateType.Normal), 40 - (int)_hadj.Value, 560 - (int)_vadj.Value, _l); - GdkWindow.DrawLayout(Style.TextGC(StateType.Normal), 560 - (int)_hadj.Value, 560 - (int)_vadj.Value, _l); + + using (Gdk.GC gc = new Gdk.GC(GdkWindow)) + { + int w = e.Area.Width; + int h = e.Area.Height; + int x = (int)_hadj.Value + e.Area.X; + int y = (int)_vadj.Value + e.Area.Y; + if(x + w > _p.Width) + w = _p.Width - x < 0 ? 0 : _p.Width - x; + if(y + h > _p.Height) + h = _p.Height - y < 0 ? 0 : _p.Height - y; + + byte[] a = _p.GetPixels(w, h, x, y); + GdkWindow.DrawRgb32Image(gc, e.Area.X, e.Area.Y, w, h, + 0 /* no dithering */, a, w * 4); + } + return ret; } protected override void OnRealized() { - _l = new Pango.Layout(PangoContext); - _l.Wrap = Pango.WrapMode.Word; - _l.FontDescription = Pango.FontDescription.FromString("Tahoma 64"); - _l.SetMarkup("WHAT THE\nFUCK IS\nTHIS SHIT\nLOL ♥ ♥"); base.OnRealized(); } diff --git a/pipi-sharp/Picture.cs b/pipi-sharp/Picture.cs index f8ccdf9..6ffb5a7 100644 --- a/pipi-sharp/Picture.cs +++ b/pipi-sharp/Picture.cs @@ -22,12 +22,15 @@ namespace Pipi { private IntPtr _picture; + public readonly string FileName; + [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern IntPtr pipi_load(string s); public Picture(string s) { _picture = pipi_load(s); + FileName = s; } [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl), @@ -61,5 +64,46 @@ namespace Pipi { get { return pipi_get_image_height(_picture); } } + + [StructLayout(LayoutKind.Sequential)] + public struct PixelsStruct + { + public IntPtr pixels; + public Int32 w, h, pitch, bpp; + public Int64 bytes; + } + + [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl), + SuppressUnmanagedCodeSecurity] + private static extern IntPtr pipi_getpixels(IntPtr img, int type); + public byte[] GetPixels(int w, int h, int x, int y) + { + byte[] array = new byte[w * h * 4]; + IntPtr pixels = pipi_getpixels(_picture, 0); + PixelsStruct p; + Int64 address; + + p = (PixelsStruct)Marshal.PtrToStructure(pixels, + typeof(PixelsStruct)); + address = p.pixels.ToInt64(); + + unsafe + { + for(int j = 0; j < h; j++) + { + Marshal.Copy((IntPtr)(address + ((j + y) * p.w + x) * 4), + array, j * w * 4, w * 4); + for(int i = 0; i < w; i++) + { + byte c = array[j * w * 4 + i * 4]; + array[j * w * 4 + i * 4] = array[j * w * 4 + i * 4 + 2]; + array[j * w * 4 + i * 4 + 2] = c; + } + } + } + + return array; + } } } +