Source for java.awt.image.MemoryImageSource

   1: /* MemoryImageSource.java -- Java class for providing image data
   2:    Copyright (C) 1999, 2004  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 java.awt.image;
  40: 
  41: import java.util.Hashtable;
  42: import java.util.Vector;
  43: 
  44: public class MemoryImageSource implements ImageProducer
  45: {
  46:   private boolean animated = false;
  47:   private boolean fullbuffers = false;
  48:   private int[] pixeli;
  49:   private int width;
  50:   private int height;
  51:   private int offset;
  52:   private int scansize;
  53:   private byte[] pixelb;
  54:   private ColorModel cm;
  55:   private Hashtable props = new Hashtable();
  56:   private Vector consumers = new Vector();
  57: 
  58:   /**
  59:    * Construct an image producer that reads image data from a byte
  60:    * array.
  61:    *
  62:    * @param w width of image
  63:    * @param h height of image
  64:    * @param cm the color model used to represent pixel values
  65:    * @param pix a byte array of pixel values
  66:    * @param off the offset into the array at which the first pixel is stored
  67:    * @param scan the number of array elements that represents a single pixel row
  68:    */
  69:   public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off,
  70:                            int scan)
  71:   {
  72:     this(w, h, cm, pix, off, scan, null);
  73:   }
  74: 
  75:   /**
  76:    * Constructs an ImageProducer from memory
  77:    */
  78:   public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off,
  79:                            int scan, Hashtable props)
  80:   {
  81:     width = w;
  82:     height = h;
  83:     this.cm = cm;
  84:     offset = off;
  85:     scansize = scan;
  86:     this.props = props;
  87:     int max = ((scansize > width) ? scansize : width);
  88:     pixelb = pix;
  89:   }
  90: 
  91:   /**
  92:    * Construct an image producer that reads image data from an
  93:    * integer array.
  94:    *
  95:    * @param w width of image
  96:    * @param h height of image
  97:    * @param cm the color model used to represent pixel values
  98:    * @param pix an integer array of pixel values
  99:    * @param off the offset into the array at which the first pixel is stored
 100:    * @param scan the number of array elements that represents a single pixel row
 101:    */
 102:   public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off,
 103:                            int scan)
 104:   {
 105:     this(w, h, cm, pix, off, scan, null);
 106:   }
 107: 
 108:   /**
 109:      Constructs an ImageProducer from memory
 110:   */
 111:   public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off,
 112:                            int scan, Hashtable props)
 113:   {
 114:     width = w;
 115:     height = h;
 116:     this.cm = cm;
 117:     offset = off;
 118:     scansize = scan;
 119:     this.props = props;
 120:     int max = ((scansize > width) ? scansize : width);
 121:     pixeli = pix;
 122:   }
 123: 
 124:   /**
 125:    * Constructs an ImageProducer from memory using the default RGB ColorModel
 126:    */
 127:   public MemoryImageSource(int w, int h, int[] pix, int off, int scan,
 128:                            Hashtable props)
 129:   {
 130:     this(w, h, ColorModel.getRGBdefault(), pix, off, scan, props);
 131:   }
 132: 
 133:   /**
 134:    * Constructs an ImageProducer from memory using the default RGB ColorModel
 135:    */
 136:   public MemoryImageSource(int w, int h, int[] pix, int off, int scan)
 137:   {
 138:     this(w, h, ColorModel.getRGBdefault(), pix, off, scan, null);
 139:   }
 140: 
 141:   /**
 142:    * Used to register an <code>ImageConsumer</code> with this
 143:    * <code>ImageProducer</code>.
 144:    */
 145:   public synchronized void addConsumer(ImageConsumer ic)
 146:   {
 147:     if (consumers.contains(ic))
 148:       return;
 149: 
 150:     consumers.addElement(ic);
 151:   }
 152: 
 153:   /**
 154:    * Used to determine if the given <code>ImageConsumer</code> is
 155:    * already registered with this <code>ImageProducer</code>.
 156:    */
 157:   public synchronized boolean isConsumer(ImageConsumer ic)
 158:   {
 159:     if (consumers.contains(ic))
 160:       return true;
 161:     return false;
 162:   }
 163: 
 164:   /**
 165:    * Used to remove an <code>ImageConsumer</code> from the list of
 166:    * registered consumers for this <code>ImageProducer</code>.
 167:    */
 168:   public synchronized void removeConsumer(ImageConsumer ic)
 169:   {
 170:     consumers.removeElement(ic);
 171:   }
 172: 
 173:   /**
 174:    * Used to register an <code>ImageConsumer</code> with this
 175:    * <code>ImageProducer</code> and then immediately start
 176:    * reconstruction of the image data to be delivered to all
 177:    * registered consumers.
 178:    */
 179:   public void startProduction(ImageConsumer ic)
 180:   {
 181:     if (! (consumers.contains(ic)))
 182:       consumers.addElement(ic);
 183: 
 184:     Vector list = (Vector) consumers.clone();
 185:     for (int i = 0; i < list.size(); i++)
 186:       {
 187:     ic = (ImageConsumer) list.elementAt(i);
 188:     sendPicture(ic);
 189:     if (animated)
 190:       ic.imageComplete(ImageConsumer.SINGLEFRAMEDONE);
 191:     else
 192:       ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
 193:       }
 194:   }
 195: 
 196:   /**
 197:    * Used to register an <code>ImageConsumer</code> with this
 198:    * <code>ImageProducer</code> and then request that this producer
 199:    * resend the image data in the order top-down, left-right.
 200:    */
 201:   public void requestTopDownLeftRightResend(ImageConsumer ic)
 202:   {
 203:     startProduction(ic);
 204:   }
 205: 
 206:   /**
 207:    * Changes a flag to indicate whether this MemoryImageSource supports
 208:    * animations.
 209:    *
 210:    * @param animated A flag indicating whether this class supports animations
 211:    */
 212:   public synchronized void setAnimated(boolean animated)
 213:   {
 214:     this.animated = animated;
 215:   }
 216: 
 217:   /**
 218:    * A flag to indicate whether or not to send full buffer updates when
 219:    * sending animation. If this flag is set then full buffers are sent
 220:    * in the newPixels methods instead of just regions.
 221:    *
 222:    * @param fullbuffers - a flag indicating whether to send the full buffers
 223:    */
 224:   public synchronized void setFullBufferUpdates(boolean fullbuffers)
 225:   {
 226:     this.fullbuffers = fullbuffers;
 227:   }
 228: 
 229:   /**
 230:    * Send an animation frame to the image consumers.
 231:    */
 232:   public void newPixels()
 233:   {
 234:     if (animated == true)
 235:       {
 236:     ImageConsumer ic;
 237:     Vector list = (Vector) consumers.clone();
 238:     for (int i = 0; i < list.size(); i++)
 239:       {
 240:         ic = (ImageConsumer) list.elementAt(i);
 241:         sendPicture(ic);
 242:         ic.imageComplete(ImageConsumer.SINGLEFRAME);
 243:       }
 244:       }
 245:   }
 246: 
 247:   private void sendPicture(ImageConsumer ic)
 248:   {
 249:     ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT);
 250:     if (props != null)
 251:       ic.setProperties(props);
 252:     ic.setDimensions(width, height);
 253:     ic.setColorModel(cm);
 254:     if (pixeli != null)
 255:       ic.setPixels(0, 0, width, height, cm, pixeli, offset, scansize);
 256:     else
 257:       ic.setPixels(0, 0, width, height, cm, pixelb, offset, scansize);
 258:   }
 259: 
 260:   /**
 261:    * Send an animation frame to the image consumers containing the specified
 262:    * pixels unless setFullBufferUpdates is set.
 263:    */
 264:   public synchronized void newPixels(int x, int y, int w, int h)
 265:   {
 266:     if (animated == true)
 267:       {
 268:     if (fullbuffers)
 269:       newPixels();
 270:     else
 271:       {
 272:         ImageConsumer ic;
 273:         Vector list = (Vector) consumers.clone();
 274:         for (int i = 0; i < list.size(); i++)
 275:           {
 276:         ic = (ImageConsumer) list.elementAt(i);
 277:         ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT);
 278:         if (props != null)
 279:           ic.setProperties(props);
 280:         if (pixeli != null)
 281:           {
 282:             int[] pixelbuf = new int[w * h];
 283:             for (int row = y; row < y + h; row++)
 284:               System.arraycopy(pixeli, row * scansize + x + offset,
 285:                                pixelbuf, 0, w * h);
 286:             ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
 287:           }
 288:         else
 289:           {
 290:             byte[] pixelbuf = new byte[w * h];
 291:             for (int row = y; row < y + h; row++)
 292:               System.arraycopy(pixelb, row * scansize + x + offset,
 293:                                pixelbuf, 0, w * h);
 294: 
 295:             ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
 296:           }
 297:         ic.imageComplete(ImageConsumer.SINGLEFRAME);
 298:           }
 299:       }
 300:       }
 301:   }
 302: 
 303:   /**
 304:    * Send an animation frame to the image consumers containing the specified
 305:    * pixels unless setFullBufferUpdates is set.
 306:    *
 307:    * If framenotify is set then a notification is sent when the frame
 308:    * is sent otherwise no status is sent.
 309:    */
 310:   public synchronized void newPixels(int x, int y, int w, int h,
 311:                                      boolean framenotify)
 312:   {
 313:     if (animated == true)
 314:       {
 315:     if (fullbuffers)
 316:       newPixels();
 317:     else
 318:       {
 319:         ImageConsumer ic;
 320:         Vector list = (Vector) consumers.clone();
 321:         for (int i = 0; i < list.size(); i++)
 322:           {
 323:         ic = (ImageConsumer) list.elementAt(i);
 324:         ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT);
 325:         if (props != null)
 326:           ic.setProperties(props);
 327:         if (pixeli != null)
 328:           {
 329:             int[] pixelbuf = new int[w * h];
 330:             for (int row = y; row < y + h; row++)
 331:               System.arraycopy(pixeli, row * scansize + x + offset,
 332:                                pixelbuf, 0, w * h);
 333:             ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
 334:           }
 335:         else
 336:           {
 337:             byte[] pixelbuf = new byte[w * h];
 338:             for (int row = y; row < y + h; row++)
 339:               System.arraycopy(pixelb, row * scansize + x + offset,
 340:                                pixelbuf, 0, w * h);
 341:             ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
 342:           }
 343:         if (framenotify == true)
 344:           ic.imageComplete(ImageConsumer.SINGLEFRAME);
 345:           }
 346:       }
 347:       }
 348:   }
 349: 
 350:   public synchronized void newPixels(byte[] newpix, ColorModel newmodel,
 351:                                      int offset, int scansize)
 352:   {
 353:     pixeli = null;
 354:     pixelb = newpix;
 355:     cm = newmodel;
 356:     this.offset = offset;
 357:     this.scansize = scansize;
 358:     if (animated == true)
 359:       newPixels();
 360:   }
 361: 
 362:   public synchronized void newPixels(int[] newpix, ColorModel newmodel,
 363:                                      int offset, int scansize)
 364:   {
 365:     pixelb = null;
 366:     pixeli = newpix;
 367:     cm = newmodel;
 368:     this.offset = offset;
 369:     this.scansize = scansize;
 370:     if (animated == true)
 371:       newPixels();
 372:   }
 373: }