Source for gnu.java.awt.peer.x.XImage

   1: /* XImage.java -- Image impl for X Pixmaps
   2:    Copyright (C) 2006 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: 
  39: package gnu.java.awt.peer.x;
  40: 
  41: import gnu.x11.Pixmap;
  42: import gnu.x11.image.ZPixmap;
  43: 
  44: import java.awt.Graphics;
  45: import java.awt.GraphicsEnvironment;
  46: import java.awt.Image;
  47: 
  48: import java.awt.image.ColorModel;
  49: import java.awt.image.ImageConsumer;
  50: import java.awt.image.ImageObserver;
  51: import java.awt.image.ImageProducer;
  52: 
  53: import java.util.Hashtable;
  54: import java.util.Vector;
  55: 
  56: public class XImage
  57:   extends Image
  58: {
  59: 
  60:   Pixmap pixmap;
  61: 
  62:   private Hashtable properties;
  63: 
  64:   XImage(int w, int h)
  65:   {
  66:     GraphicsEnvironment env =
  67:       GraphicsEnvironment.getLocalGraphicsEnvironment();
  68:     XGraphicsDevice dev = (XGraphicsDevice) env.getDefaultScreenDevice();
  69:     pixmap = new Pixmap(dev.getDisplay(), w, h);
  70:   }
  71: 
  72:   public int getWidth(ImageObserver observer)
  73:   {
  74:     return pixmap.width;
  75:   }
  76: 
  77:   public int getHeight(ImageObserver observer)
  78:   {
  79:     return pixmap.height;
  80:   }
  81: 
  82:   public ImageProducer getSource()
  83:   {
  84:     return new XImageProducer();
  85:   }
  86: 
  87:   /**
  88:    * Creates an XGraphics for drawing on this XImage.
  89:    *
  90:    * @return an XGraphics for drawing on this XImage
  91:    */
  92:   public Graphics getGraphics()
  93:   {
  94:     XGraphics2D g = new XGraphics2D(pixmap);
  95:     return g;
  96:   }
  97: 
  98:   public Object getProperty(String name, ImageObserver observer)
  99:   {
 100:     Object val = null;
 101:     if (properties != null)
 102:       val = properties.get(val);
 103:     return val;
 104:   }
 105: 
 106:   public void flush()
 107:   {
 108:     // TODO: Implement this.
 109:     throw new UnsupportedOperationException("Not yet implemented.");
 110:   }
 111: 
 112:   protected void finalize()
 113:   {
 114:     pixmap.free();
 115:   }
 116: 
 117:   protected class XImageProducer implements ImageProducer
 118:   {
 119:     private Vector<ImageConsumer> consumers = new Vector<ImageConsumer>();
 120: 
 121:     public void addConsumer(ImageConsumer ic)
 122:     {
 123:       if (ic != null && !isConsumer(ic))
 124:         this.consumers.add(ic);
 125:     }
 126: 
 127:     public boolean isConsumer(ImageConsumer ic)
 128:     {
 129:       return this.consumers.contains(ic);
 130:     }
 131: 
 132:     public void removeConsumer(ImageConsumer ic)
 133:     {
 134:       if (ic != null)
 135:         this.consumers.remove(ic);
 136:     }
 137: 
 138:     public void requestTopDownLeftRightResend(ImageConsumer ic)
 139:     {
 140:       /* just ignore the call */
 141:     }
 142: 
 143:     public void startProduction(ImageConsumer ic)
 144:     {
 145:       this.addConsumer(ic);
 146: 
 147:       for (ImageConsumer consumer : this.consumers)
 148:         {
 149:           int width = XImage.this.getWidth(null);
 150:           int height = XImage.this.getHeight(null);
 151: 
 152:           XGraphics2D graphics = (XGraphics2D) getGraphics();
 153:           ColorModel model = graphics.getColorModel();
 154:           graphics.dispose();
 155: 
 156:           ZPixmap zpixmap = (ZPixmap)
 157:             XImage.this.pixmap.image(0, 0, width, height,
 158:                                      0xffffffff,
 159:                                      gnu.x11.image.Image.Format.ZPIXMAP);
 160: 
 161:           int size = zpixmap.get_data_length();
 162:           System.out.println("size: " + size + ", w = " + width + ", h = " + height);
 163: 
 164:           int [] pixel = new int[size];
 165:           for (int i = 0; i < size; i++)
 166:             pixel[i] = zpixmap.get_data_element(i);
 167: 
 168:           consumer.setHints(ImageConsumer.SINGLEPASS);
 169: 
 170:           consumer.setDimensions(width, height);
 171:           consumer.setPixels(0, 0, width, height, model, pixel, 0, width);
 172:           consumer.imageComplete(ImageConsumer.STATICIMAGEDONE);
 173:         }
 174: 
 175:       System.out.println("done!");
 176:     }
 177:   }
 178: }