Source for gnu.java.awt.peer.gtk.GtkVolatileImage

   1: /* GtkVolatileImage.java -- wraps an X pixmap
   2:    Copyright (C) 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.java.awt.peer.gtk;
  39: 
  40: import java.awt.Graphics;
  41: import java.awt.Graphics2D;
  42: import java.awt.GraphicsConfiguration;
  43: import java.awt.ImageCapabilities;
  44: import java.awt.Point;
  45: import java.awt.image.BufferedImage;
  46: import java.awt.image.ColorModel;
  47: import java.awt.image.DataBuffer;
  48: import java.awt.image.DirectColorModel;
  49: import java.awt.image.ImageObserver;
  50: import java.awt.image.Raster;
  51: import java.awt.image.SampleModel;
  52: import java.awt.image.SinglePixelPackedSampleModel;
  53: import java.awt.image.VolatileImage;
  54: import java.awt.image.WritableRaster;
  55: 
  56: public class GtkVolatileImage extends VolatileImage
  57: {
  58:   int width, height;
  59:   private ImageCapabilities caps;
  60: 
  61:   final GtkComponentPeer component;
  62: 
  63:   static ColorModel gdkColorModel = new DirectColorModel(32,
  64:                                                          0x000000FF,
  65:                                                          0x0000FF00,
  66:                                                          0x00FF0000,
  67:                                                          0xFF000000);
  68: 
  69:   /**
  70:    * Don't touch, accessed from native code.
  71:    */
  72:   long nativePointer;
  73: 
  74:   native long init(GtkComponentPeer component, int width, int height);
  75: 
  76:   native void destroy(long pointer);
  77: 
  78:   native int[] nativeGetPixels(long pointer);
  79: 
  80:   /**
  81:    * Gets the pixels in the current image from GDK.
  82:    *
  83:    * Note that pixels are in 32-bit RGBA, non-premultiplied, which is different
  84:    * from Cairo's premultiplied ARGB, which is different from Java's standard
  85:    * non-premultiplied ARGB.  Caution is advised when using this method, to
  86:    * ensure that the data format remains consistent with what you expect.
  87:    *
  88:    * @return the current pixels, as reported by GDK.
  89:    */
  90:   public int[] getPixels()
  91:   {
  92:     return nativeGetPixels(nativePointer);
  93:   }
  94: 
  95:   native void nativeCopyArea(long pointer, int x, int y, int w, int h, int dx,
  96:                              int dy );
  97:   public void copyArea(int x, int y, int w, int h, int dx, int dy)
  98:   {
  99:     nativeCopyArea(nativePointer, x, y, w, h, dx, dy);
 100:   }
 101: 
 102:   native void nativeDrawVolatile(long pointer, long srcPtr, int x, int y,
 103:                                  int w, int h );
 104:   public void drawVolatile(long srcPtr, int x, int y, int w, int h )
 105:   {
 106:     nativeDrawVolatile(nativePointer, srcPtr, x, y, w, h);
 107:   }
 108: 
 109:   public GtkVolatileImage(GtkComponentPeer component,
 110:                           int width, int height, ImageCapabilities caps)
 111:   {
 112:     this.width = width;
 113:     this.height = height;
 114:     this.caps = caps;
 115:     this.component = component;
 116:     nativePointer = init( component, width, height );
 117:   }
 118: 
 119:   public GtkVolatileImage(int width, int height, ImageCapabilities caps)
 120:   {
 121:     this(null, width, height, caps);
 122:   }
 123: 
 124:   public GtkVolatileImage(int width, int height)
 125:   {
 126:     this(null, width, height, null);
 127:   }
 128: 
 129:   public void finalize()
 130:   {
 131:     dispose();
 132:   }
 133: 
 134:   public void dispose()
 135:   {
 136:     destroy(nativePointer);
 137:   }
 138: 
 139:   public BufferedImage getSnapshot()
 140:   {
 141:     WritableRaster raster = Raster.createWritableRaster(createGdkSampleModel(width, height),
 142:                                                         new Point(0, 0));
 143:     raster.setDataElements(0, 0, getPixels());
 144:     return new BufferedImage(gdkColorModel, raster,
 145:                              gdkColorModel.isAlphaPremultiplied(), null);
 146:   }
 147: 
 148:   public Graphics getGraphics()
 149:   {
 150:     return createGraphics();
 151:   }
 152: 
 153:   public Graphics2D createGraphics()
 154:   {
 155:     return new VolatileImageGraphics( this );
 156:   }
 157: 
 158:   public int validate(GraphicsConfiguration gc)
 159:   {
 160:     return VolatileImage.IMAGE_OK;
 161:   }
 162: 
 163:   public boolean contentsLost()
 164:   {
 165:     return false;
 166:   }
 167: 
 168:   public ImageCapabilities getCapabilities()
 169:   {
 170:     return caps;
 171:   }
 172: 
 173:   public int getWidth()
 174:   {
 175:     return width;
 176:   }
 177: 
 178:   public int getHeight()
 179:   {
 180:     return height;
 181:   }
 182: 
 183:   public int getWidth(java.awt.image.ImageObserver observer)
 184:   {
 185:     return width;
 186:   }
 187: 
 188:   public int getHeight(java.awt.image.ImageObserver observer)
 189:   {
 190:     return height;
 191:   }
 192: 
 193:   public Object getProperty(String name, ImageObserver observer)
 194:   {
 195:     return null;
 196:   }
 197: 
 198:   /**
 199:    * Creates a SampleModel that matches GDK's native format
 200:    */
 201:   protected static SampleModel createGdkSampleModel(int w, int h)
 202:   {
 203:     return new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, w, h,
 204:                                             new int[]{0x000000FF, 0x0000FF00,
 205:                                                       0x00FF0000, 0xFF000000});
 206:   }
 207: }