dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] pnetlib/Xsharp ConvertImage.cs, NONE, 1.1 Colormap


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] pnetlib/Xsharp ConvertImage.cs, NONE, 1.1 Colormap.cs, 1.1, 1.2 Graphics.cs, 1.10, 1.11 Image.cs, 1.2, 1.3 Xlib.cs.in, 1.13, 1.14 Xsharp.build, 1.2, 1.3 XsharpSupport.c, 1.12, 1.13
Date: Wed, 01 Oct 2003 23:50:06 +0000

Update of /cvsroot/dotgnu-pnet/pnetlib/Xsharp
In directory subversions:/tmp/cvs-serv25701/Xsharp

Modified Files:
        Colormap.cs Graphics.cs Image.cs Xlib.cs.in Xsharp.build 
        XsharpSupport.c 
Added Files:
        ConvertImage.cs 
Log Message:


Hook DIB conversion into Xsharp.


Index: XsharpSupport.c
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/XsharpSupport.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** XsharpSupport.c     24 Jul 2003 00:54:19 -0000      1.12
--- XsharpSupport.c     1 Oct 2003 23:50:03 -0000       1.13
***************
*** 911,914 ****
--- 911,931 ----
  
  /*
+  * Get the size of an image.
+  */
+ void XSharpGetImageSize(XImage *image, int *width, int *height)
+ {
+       if(image)
+       {
+               *width = (int)(image->width);
+               *height = (int)(image->height);
+       }
+       else
+       {
+               *width = 1;
+               *height = 1;
+       }
+ }
+ 
+ /*
   * Internal structure of X11 region objects.  We have to define
   * this here because there is no public header that contains it.
***************
*** 1012,1015 ****
--- 1029,1038 ----
  {
        /* Nothing to do here */
+ }
+ 
+ void XSharpGetImageSize(void *image, int *width, int *height)
+ {
+       *width = 1;
+       *height = 1;
  }
  

--- NEW FILE: ConvertImage.cs ---
/*
 * ConvertImage.cs - Convert DIB images into XImage structures.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace Xsharp
{

using System;
using DotGNU.Images;

internal sealed class ConvertImage
{
        // Convert an image frame into an XImage.
        public static IntPtr FrameToXImage(Screen screen, Frame frame)
                        {
                                int[] fpalette;
                                Xlib.Pixel[] palette;
                                int index, color;
                                Colormap colormap = screen.DefaultColormap;

                                // Create a palette to use to render the image.
                                fpalette = frame.Palette;
                                if(fpalette != null)
                                {
                                        // Convert the palette within the image 
frame itself.
                                        palette = new Xlib.Pixel [256];
                                        for(index = 0; index < 256 && index < 
fpalette.Length;
                                                ++index)
                                        {
                                                color = fpalette[index];
                                                palette[index] = 
colormap.RGBToPixel
                                                        (new Color((color >> 
16) & 0xFF,
                                                                           
(color >> 8) & 0xFF, color & 0xFF));
                                        }
                                }
                                else
                                {
                                        // We have an RGB image: use a standard 
palette.
                                        palette = colormap.GetStandardPalette();
                                }

                                // Convert the frame into an XImage and return 
it.
                                return Xlib.XSharpCreateImageFromDIB
                                                (screen.screen, frame.Width, 
frame.Height,
                                                 frame.Stride, 
(int)(frame.PixelFormat),
                                                 frame.Data, 0, palette);
                        }

        // Convert an image frame's mask into an XImage.  IntPtr.Zero if no 
mask.
        public static IntPtr MaskToXImage(Screen screen, Frame frame)
                        {
                                byte[] mask = frame.Mask;
                                if(mask == null)
                                {
                                        return IntPtr.Zero;
                                }
                                return Xlib.XSharpCreateImageFromDIB
                                        (screen.screen, frame.Width, 
frame.Height,
                                         frame.MaskStride, 
(int)(PixelFormat.Format1bppIndexed),
                                         mask, 1, null);
                        }

        // Convert an XImage into a Pixmap object.
        public static Pixmap XImageToPixmap(Screen screen, IntPtr ximage)
                        {
                                int width, height;
                                Xlib.XSharpGetImageSize(ximage, out width, out 
height);
                                Pixmap pixmap = new Pixmap(screen, width, 
height);
                                Graphics graphics = new Graphics(pixmap);
                                graphics.PutXImage(ximage, 0, 0, 0, 0, width, 
height);
                                graphics.Dispose();
                                return pixmap;
                        }

        // Convert an XImage mask into a Bitmap object.
        public static Bitmap XImageMaskToBitmap(Screen screen, IntPtr ximage)
                        {
                                int width, height;
                                Xlib.XSharpGetImageSize(ximage, out width, out 
height);
                                Bitmap bitmap = new Bitmap(screen, width, 
height);
                                Graphics graphics = new Graphics(bitmap);
                                graphics.PutXImage(ximage, 0, 0, 0, 0, width, 
height);
                                graphics.Dispose();
                                return bitmap;
                        }

} // class ConvertImage

} // namespace Xsharp

Index: Xlib.cs.in
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/Xlib.cs.in,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** Xlib.cs.in  24 Jul 2003 00:54:19 -0000      1.13
--- Xlib.cs.in  1 Oct 2003 23:50:03 -0000       1.14
***************
*** 438,441 ****
--- 438,448 ----
                         @X_int@ angle1, @X_int@ angle2);
  
+       [DllImport("X11")]
+       extern public static @X_int@ XPutImage
+                       (IntPtr display, Drawable drawable, IntPtr gc,
+                        IntPtr image, @X_int@ src_x, @X_int@ src_y,
+                        @X_int@ dest_x, @X_int@ dest_y,
+                        @X_int@ width, @X_int@ height);
+ 
        // Declare window manager related external functions.
  
***************
*** 554,557 ****
--- 561,577 ----
        extern public static void XSharpGetRegionRect
                        (IntPtr region, @X_int@ index, out XRectangle rect);
+ 
+       [DllImport("XsharpSupport")]
+       extern public static IntPtr XSharpCreateImageFromDIB
+                       (IntPtr screen, @X_int@ width, @X_int@ height,
+                        @X_int@ stride, @X_int@ pixelFormat, byte[] data,
+                        @X_int@ isMask, Xlib.Pixel[] palette);
+ 
+       [DllImport("XsharpSupport")]
+       extern public static void XSharpDestroyImage(IntPtr image);
+ 
+       [DllImport("XsharpSupport")]
+       extern public static void XSharpGetImageSize
+                       (IntPtr image, out @X_int@ width, out @X_int@ height);
  
        // Helper functions from "libImlib.so" for loading images.

Index: Xsharp.build
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/Xsharp.build,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** Xsharp.build        28 May 2003 04:41:06 -0000      1.2
--- Xsharp.build        1 Oct 2003 23:50:03 -0000       1.3
***************
*** 20,23 ****
--- 20,24 ----
  
                        <references>
+                               <file 
name="../DotGNU.Images/DotGNU.Images.dll"/>
                                <file name="../runtime/mscorlib.dll"/>
                        </references>

Index: Graphics.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/Graphics.cs,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** Graphics.cs 28 Aug 2003 13:30:06 -0000      1.10
--- Graphics.cs 1 Oct 2003 23:50:03 -0000       1.11
***************
*** 2114,2122 ****
                                }
  
!                               // Set the context to "tiling" and fill the 
region.
!                               SetFillTiled(image.Pixmap, x, y);
!                               if (image.Mask != null)
!                                       SetClipMask(image.Mask, x, y);
!                               FillRectangle(x, y, image.Pixmap.Width, 
image.Pixmap.Height);
  
                                // Revert the context to a sane fill mode.
--- 2114,2139 ----
                                }
  
!                               // Can we take a short-cut using the XImage?
!                               if(image.ShouldUseXImage)
!                               {
!                                       // Use "PutXImage" to draw through the 
clip mask,
!                                       // to avoid having to create a Pixmap 
in the server.
!                                       int width, height;
!                                       SetFillSolid();
!                                       if (image.Mask != null)
!                                               SetClipMask(image.Mask, x, y);
!                                       Xlib.XSharpGetImageSize
!                                               (image.XImage, out width, out 
height);
!                                       PutXImage(image.XImage, 0, 0, x, y, 
width, height);
!                               }
!                               else
!                               {
!                                       // Set the context to "tiling" and fill 
the region.
!                                       SetFillTiled(image.Pixmap, x, y);
!                                       if (image.Mask != null)
!                                               SetClipMask(image.Mask, x, y);
!                                       FillRectangle(x, y, image.Pixmap.Width,
!                                                                 
image.Pixmap.Height);
!                               }
  
                                // Revert the context to a sane fill mode.
***************
*** 2179,2186 ****
                                }
  
!                               // Set the context to "tiling" and fill the 
region.
!                               SetFillTiled(image.Pixmap, x - srcX, y - srcY);
!                               SetClipMask(image.Mask, x - srcX, y - srcY);
!                               FillRectangle(x, y, srcWidth, srcHeight);
  
                                // Revert the context to a sane fill mode.
--- 2196,2230 ----
                                }
  
!                               // Bail out if the source co-ordinates are out 
of range.
!                               int width, height;
!                               width = image.Width;
!                               height = image.Height;
!                               if(srcX < 0 || srcX >= width ||
!                                  srcY < 0 || srcY >= height ||
!                                  srcWidth <= 0 || srcHeight <= 0 ||
!                                  srcWidth > width || srcHeight > height ||
!                                  srcX > (width - srcWidth) ||
!                                  srcY > (height - srcHeight))
!                               {
!                                       throw new 
XException(S._("X_RectCoordRange"));
!                               }
! 
!                               // Can we take a short-cut using the XImage?
!                               if(image.ShouldUseXImage)
!                               {
!                                       // Use "PutXImage" to draw through the 
clip mask,
!                                       // to avoid having to create a Pixmap 
in the server.
!                                       SetFillSolid();
!                                       SetClipMask(image.Mask, x - srcX, y - 
srcY);
!                                       PutXImage(image.XImage, srcX, srcY, x, 
y,
!                                                         srcWidth, srcHeight);
!                               }
!                               else
!                               {
!                                       // Set the context to "tiling" and fill 
the region.
!                                       SetFillTiled(image.Pixmap, x - srcX, y 
- srcY);
!                                       SetClipMask(image.Mask, x - srcX, y - 
srcY);
!                                       FillRectangle(x, y, srcWidth, 
srcHeight);
!                               }
  
                                // Revert the context to a sane fill mode.
***************
*** 2778,2781 ****
--- 2822,2842 ----
                                        dpy.Unlock();
                                }
+                       }
+ 
+       // Put an XImage into this graphics context.
+       internal void PutXImage(IntPtr ximage, int srcX, int srcY,
+                                                       int destX, int destY, 
int width, int height)
+                       {
+                               try
+                               {
+                                       IntPtr display = Lock();
+                                       Xlib.XPutImage(display, drawableHandle, 
gc, ximage,
+                                                                  srcX, srcY, 
destX, destY, width, height);
+                               }
+                               finally
+                               {
+                                       dpy.Unlock();
+                               }
+ 
                        }
  

Index: Image.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/Image.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** Image.cs    28 Aug 2003 13:30:06 -0000      1.2
--- Image.cs    1 Oct 2003 23:50:03 -0000       1.3
***************
*** 23,26 ****
--- 23,27 ----
  
  using System;
+ using DotGNU.Images;
  
  /// <summary>
***************
*** 32,37 ****
--- 33,41 ----
  {
        // Internal state.
+       private Screen screen;
        private Xsharp.Pixmap pixmap;
        private Bitmap mask;
+       private IntPtr pixmapXImage;
+       private IntPtr maskXImage;
  
        /// <summary>
***************
*** 60,63 ****
--- 64,68 ----
                        {
                                pixmap = new Pixmap(width, height);
+                               screen = pixmap.screen;
                                if(hasMask)
                                {
***************
*** 99,102 ****
--- 104,108 ----
                        {
                                pixmap = new Xsharp.Pixmap(screen, width, 
height);
+                               screen = pixmap.screen;
                                if(hasMask)
                                {
***************
*** 150,153 ****
--- 156,160 ----
                                        screen = dpy.DefaultScreenOfDisplay;
                                }
+                               this.screen = screen;
                                if(width < 1 || width > 32767 ||
                                        height < 1 || height > 32767)
***************
*** 241,244 ****
--- 248,252 ----
                                        screen = dpy.DefaultScreenOfDisplay;
                                }
+                               this.screen = screen;
                                try
                                {
***************
*** 287,290 ****
--- 295,369 ----
  
        /// <summary>
+       /// <para>Constructs a new <see cref="T:Xsharp.Image"/> instance
+       /// from a <see cref="T:DotGNU.Images.Frame"/> instance.
+       /// </summary>
+       ///
+       /// <param name="frame">
+       /// <para>The frame to load the image from.</para>
+       /// </param>
+       ///
+       /// <exception cref="T:System.ArgumentNullException">
+       /// <para>The <paramref name="frame"/> parameter is
+       /// <see langword="null"/>.</para>
+       /// </exception>
+       ///
+       /// <exception cref="T:Xsharp.XInvalidOperationException">
+       /// <para>Raised if <paramref name="filename"/> could not be
+       /// loaded for some reason.</para>
+       /// </exception>
+       public Image(Frame frame) : this(null, frame) {}
+ 
+       /// <summary>
+       /// <para>Constructs a new <see cref="T:Xsharp.Image"/> instance
+       /// from a <see cref="T:DotGNU.Images.Frame"/> instance.
+       /// </summary>
+       ///
+       /// <param name="screen">
+       /// <para>The screen upon which to create the new image.</para>
+       /// </param>
+       ///
+       /// <param name="frame">
+       /// <para>The frame to load the image from.</para>
+       /// </param>
+       ///
+       /// <exception cref="T:System.ArgumentNullException">
+       /// <para>The <paramref name="frame"/> parameter is
+       /// <see langword="null"/>.</para>
+       /// </exception>
+       ///
+       /// <exception cref="T:Xsharp.XInvalidOperationException">
+       /// <para>Raised if <paramref name="filename"/> could not be
+       /// loaded for some reason.</para>
+       /// </exception>
+       public Image(Screen screen, Frame frame)
+                       {
+                               Display dpy;
+                               if(frame == null)
+                               {
+                                       throw new 
ArgumentNullException("frame");
+                               }
+                               if(screen != null)
+                               {
+                                       dpy = screen.DisplayOfScreen;
+                               }
+                               else
+                               {
+                                       dpy = Application.Primary.Display;
+                                       screen = dpy.DefaultScreenOfDisplay;
+                               }
+                               this.screen = screen;
+                               try
+                               {
+                                       dpy.Lock();
+                                       pixmapXImage = 
ConvertImage.FrameToXImage(screen, frame);
+                                       maskXImage = 
ConvertImage.MaskToXImage(screen, frame);
+                               }
+                               finally
+                               {
+                                       dpy.Unlock();
+                               }
+                       }
+ 
+       /// <summary>
        /// <para>Retrieve the pixmap that is associated with this image.</para>
        /// </summary>
***************
*** 298,301 ****
--- 377,385 ----
                                get
                                {
+                                       if(pixmap == null && pixmapXImage != 
IntPtr.Zero)
+                                       {
+                                               pixmap = 
ConvertImage.XImageToPixmap
+                                                       (screen, pixmapXImage);
+                                       }
                                        return pixmap;
                                }
***************
*** 314,317 ****
--- 398,406 ----
                                get
                                {
+                                       if(mask == null && maskXImage != 
IntPtr.Zero)
+                                       {
+                                               mask = 
ConvertImage.XImageMaskToBitmap
+                                                       (screen, maskXImage);
+                                       }
                                        return mask;
                                }
***************
*** 330,334 ****
                                get
                                {
!                                       return pixmap.Width;
                                }
                        }
--- 419,437 ----
                                get
                                {
!                                       if(pixmap != null)
!                                       {
!                                               return pixmap.Width;
!                                       }
!                                       else if(pixmapXImage != IntPtr.Zero)
!                                       {
!                                               int width, height;
!                                               Xlib.XSharpGetImageSize
!                                                       (pixmapXImage, out 
width, out height);
!                                               return width;
!                                       }
!                                       else
!                                       {
!                                               return 0;
!                                       }
                                }
                        }
***************
*** 346,350 ****
                                get
                                {
!                                       return pixmap.Height;
                                }
                        }
--- 449,467 ----
                                get
                                {
!                                       if(pixmap != null)
!                                       {
!                                               return pixmap.Height;
!                                       }
!                                       else if(pixmapXImage != IntPtr.Zero)
!                                       {
!                                               int width, height;
!                                               Xlib.XSharpGetImageSize
!                                                       (pixmapXImage, out 
width, out height);
!                                               return height;
!                                       }
!                                       else
!                                       {
!                                               return 0;
!                                       }
                                }
                        }
***************
*** 365,368 ****
--- 482,495 ----
                                        mask = null;
                                }
+                               if(pixmapXImage != IntPtr.Zero)
+                               {
+                                       Xlib.XSharpDestroyImage(pixmapXImage);
+                                       pixmapXImage = IntPtr.Zero;
+                               }
+                               if(maskXImage != IntPtr.Zero)
+                               {
+                                       Xlib.XSharpDestroyImage(maskXImage);
+                                       maskXImage = IntPtr.Zero;
+                               }
                        }
  
***************
*** 378,381 ****
--- 505,546 ----
                        {
                                Destroy();
+                       }
+ 
+       // Get the image as an XImage, if possible.
+       internal IntPtr XImage
+                       {
+                               get
+                               {
+                                       return pixmapXImage;
+                               }
+                       }
+ 
+       // Determine if we should use the XImage.  If the image is small,
+       // we want to convert it into a "Pixmap", as it will probably be
+       // used over and over again (e.g. toolbar icons).
+       internal bool ShouldUseXImage
+                       {
+                               get
+                               {
+                                       if(pixmap != null)
+                                       {
+                                               return false;
+                                       }
+                                       else if(pixmapXImage != IntPtr.Zero)
+                                       {
+                                               int width, height;
+                                               Xlib.XSharpGetImageSize
+                                                       (pixmapXImage, out 
width, out height);
+                                               if(width > 32 || height > 32)
+                                               {
+                                                       return true;
+                                               }
+                                               return false;
+                                       }
+                                       else
+                                       {
+                                               return false;
+                                       }
+                               }
                        }
  

Index: Colormap.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/Colormap.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** Colormap.cs 28 May 2003 04:17:53 -0000      1.1
--- Colormap.cs 1 Oct 2003 23:50:03 -0000       1.2
***************
*** 37,40 ****
--- 37,41 ----
        private Xlib.Colormap colormap;
        private Hashtable cachedPixels;
+       private Xlib.Pixel[] standardPalette;
  
        // Constructor that is called from the "Screen" class.
***************
*** 100,103 ****
--- 101,129 ----
                                        dpy.Unlock();
                                }
+                       }
+ 
+       // Get a standard 216-entry palette for mapping RGB images
+       // to indexed display hardware.
+       internal Xlib.Pixel[] GetStandardPalette()
+                       {
+                               if(standardPalette != null)
+                               {
+                                       return standardPalette;
+                               }
+                               standardPalette = new Xlib.Pixel [216];
+                               int red, green, blue, index;
+                               index = 0;
+                               for(red = 0x00; red <= 0xFF; red += 0x33)
+                               {
+                                       for(green = 0x00; green <= 0xFF; green 
+= 0x33)
+                                       {
+                                               for(blue = 0x00; blue <= 0xFF; 
blue += 0x33)
+                                               {
+                                                       
standardPalette[index++] =
+                                                               RGBToPixel(new 
Color(red, green, blue));
+                                               }
+                                       }
+                               }
+                               return standardPalette;
                        }
  





reply via email to

[Prev in Thread] Current Thread [Next in Thread]