Source for java.awt.image.renderable.RenderableImageProducer

   1: /*
   2:  * @(#)RenderableImageProducer.java    1.11 03/12/19
   3:  *
   4:  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
   5:  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
   6:  */
   7: 
   8: /* ********************************************************************
   9:  **********************************************************************
  10:  **********************************************************************
  11:  *** COPYRIGHT (c) Eastman Kodak Company, 1997                      ***
  12:  *** As  an unpublished  work pursuant to Title 17 of the United    ***
  13:  *** States Code.  All rights reserved.                             ***
  14:  **********************************************************************
  15:  **********************************************************************
  16:  **********************************************************************/
  17: 
  18: package java.awt.image.renderable;
  19: import java.awt.color.ColorSpace;
  20: import java.awt.image.ColorModel;
  21: import java.awt.image.DataBuffer;
  22: import java.awt.image.DirectColorModel;
  23: import java.awt.image.ImageConsumer;
  24: import java.awt.image.ImageProducer;
  25: import java.awt.image.Raster;
  26: import java.awt.image.RenderedImage;
  27: import java.awt.image.SampleModel;
  28: import java.util.Enumeration;
  29: import java.util.Vector;
  30: 
  31: /** 
  32:  * An adapter class that implements ImageProducer to allow the
  33:  * asynchronous production of a RenderableImage.  The size of the
  34:  * ImageConsumer is determined by the scale factor of the usr2dev
  35:  * transform in the RenderContext.  If the RenderContext is null, the
  36:  * default rendering of the RenderableImage is used.  This class
  37:  * implements an asynchronous production that produces the image in
  38:  * one thread at one resolution.  This class may be subclassed to
  39:  * implement versions that will render the image using several
  40:  * threads.  These threads could render either the same image at
  41:  * progressively better quality, or different sections of the image at
  42:  * a single resolution.
  43:  */
  44:	  public class RenderableImageProducer implements ImageProducer, Runnable {
  45:
  46:    /** The RenderableImage source for the producer. */
  47:    RenderableImage rdblImage;
  48:
  49:    /** The RenderContext to use for producing the image. */
  50:    RenderContext rc;
  51:
  52:    /** A Vector of image consumers. */
  53:    Vector ics = new Vector();
  54:
  55:    /**
  56:     * Constructs a new RenderableImageProducer from a RenderableImage
  57:     * and a RenderContext.
  58:     *
  59:     * @param rdblImage the RenderableImage to be rendered.
  60:     * @param rc the RenderContext to use for producing the pixels.
  61:     */
  62:    public RenderableImageProducer(RenderableImage rdblImage,
  63:	                                     RenderContext rc) {
  64:        this.rdblImage = rdblImage;
  65:        this.rc = rc;
  66:    }
  67:
  68:    /**
  69:     * Sets a new RenderContext to use for the next startProduction() call.
  70:     *
  71:     * @param rc the new RenderContext.
  72:     */
  73:	      public synchronized void setRenderContext(RenderContext rc) {
  74:        this.rc = rc;
  75:    }
  76:
  77:   /**
  78:     * Adds an ImageConsumer to the list of consumers interested in
  79:     * data for this image.
  80:     *
  81:     * @param ic an ImageConsumer to be added to the interest list.
  82:     */
  83:	      public synchronized void addConsumer(ImageConsumer ic) {
  84:	          if (!ics.contains(ic)) {
  85:            ics.addElement(ic);
  86:        }
  87:    }
  88:
  89:    /**
  90:     * Determine if an ImageConsumer is on the list of consumers
  91:     * currently interested in data for this image.
  92:     *
  93:     * @param ic the ImageConsumer to be checked.
  94:     * @return true if the ImageConsumer is on the list; false otherwise.
  95:     */
  96:	      public synchronized boolean isConsumer(ImageConsumer ic) {
  97:        return ics.contains(ic);
  98:    }
  99:
 100:    /**
 101:     * Remove an ImageConsumer from the list of consumers interested in
 102:     * data for this image.
 103:     *
 104:     * @param ic the ImageConsumer to be removed.
 105:     */
 106:	      public synchronized void removeConsumer(ImageConsumer ic) {
 107:        ics.removeElement(ic);
 108:    }
 109:
 110:    /**
 111:     * Adds an ImageConsumer to the list of consumers interested in
 112:     * data for this image, and immediately starts delivery of the
 113:     * image data through the ImageConsumer interface.
 114:     *
 115:     * @param ic the ImageConsumer to be added to the list of consumers.
 116:     */
 117:	      public synchronized void startProduction(ImageConsumer ic) {
 118:        addConsumer(ic);
 119:        // Need to build a runnable object for the Thread.
 120:        Thread thread = new Thread(this, "RenderableImageProducer Thread");
 121:        thread.start();
 122:    }
 123:
 124:    /**
 125:     * Requests that a given ImageConsumer have the image data delivered
 126:     * one more time in top-down, left-right order.
 127:     *
 128:     * @param ic the ImageConsumer requesting the resend.
 129:     */
 130:	      public void requestTopDownLeftRightResend(ImageConsumer ic) {
 131:        // So far, all pixels are already sent in TDLR order
 132:    }
 133:
 134:    /**
 135:     * The runnable method for this class. This will produce an image using
 136:     * the current RenderableImage and RenderContext and send it to all the
 137:     * ImageConsumer currently registered with this class.
 138:     */
 139:	      public void run() {
 140:        // First get the rendered image
 141:        RenderedImage rdrdImage;
 142:	          if (rc != null) {
 143:            rdrdImage = rdblImage.createRendering(rc);
 144:        } else {
 145:            rdrdImage = rdblImage.createDefaultRendering();
 146:        }
 147:        
 148:        // And its ColorModel
 149:        ColorModel colorModel = rdrdImage.getColorModel();
 150:        Raster raster = rdrdImage.getData();
 151:        SampleModel sampleModel = raster.getSampleModel();
 152:        DataBuffer dataBuffer = raster.getDataBuffer();
 153:        
 154:	          if (colorModel == null) {
 155:            colorModel = ColorModel.getRGBdefault();
 156:        }
 157:        int minX = raster.getMinX();
 158:        int minY = raster.getMinY();
 159:        int width = raster.getWidth();
 160:        int height = raster.getHeight();
 161:        
 162:        Enumeration icList;
 163:        ImageConsumer ic;
 164:        // Set up the ImageConsumers
 165:        icList = ics.elements();
 166:	          while (icList.hasMoreElements()) {
 167:            ic = (ImageConsumer)icList.nextElement();
 168:            ic.setDimensions(width,height);
 169:            ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT |
 170:                        ImageConsumer.COMPLETESCANLINES |
 171:                        ImageConsumer.SINGLEPASS |
 172:                        ImageConsumer.SINGLEFRAME);
 173:        }
 174:        
 175:        // Get RGB pixels from the raster scanline by scanline and
 176:        // send to consumers.
 177:        int pix[] = new int[width];
 178:        int i,j;
 179:        int numBands = sampleModel.getNumBands();
 180:        int tmpPixel[] = new int[numBands];
 181:	          for (j = 0; j < height; j++) {
 182:	              for(i = 0; i < width; i++) {
 183:                sampleModel.getPixel(i, j, tmpPixel, dataBuffer);
 184:                pix[i] = colorModel.getDataElement(tmpPixel, 0);
 185:            }
 186:            // Now send the scanline to the Consumers
 187:            icList = ics.elements();
 188:	              while (icList.hasMoreElements()) {
 189:                ic = (ImageConsumer)icList.nextElement();
 190:                ic.setPixels(0, j, width, 1, colorModel, pix, 0, width);
 191:            }
 192:        }
 193:
 194:        // Now tell the consumers we're done.
 195:        icList = ics.elements();
 196:	          while (icList.hasMoreElements()) {
 197:            ic = (ImageConsumer)icList.nextElement();
 198:            ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
 199:        }
 200:    }
 201:}