home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / image / memoryim.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  6.7 KB  |  225 lines

  1. /*
  2.  * @(#)MemoryImageSource.java    1.13 95/09/08 Jim Graham
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.awt.image;
  21.  
  22. import java.awt.image.ImageConsumer;
  23. import java.awt.image.ImageProducer;
  24. import java.awt.image.ColorModel;
  25. import java.util.Hashtable;
  26.  
  27. /**
  28.  * This class is an implementation of the ImageProducer interface which
  29.  * uses an array to produce pixel values for an Image.  Here is an example
  30.  * which calculates a 100x100 image representing a fade from black to blue
  31.  * along the X axis and a fade from black to red along the Y axis:
  32.  * <pre>
  33.  * 
  34.  *    int w = 100;
  35.  *    int h = 100;
  36.  *    int pix[] = new int[w * h];
  37.  *    int index = 0;
  38.  *    for (int y = 0; y < h; y++) {
  39.  *        int red = (y * 255) / (h - 1);
  40.  *        for (int x = 0; x < w; x++) {
  41.  *        int blue = (x * 255) / (w - 1);
  42.  *        pix[index++] = (255 << 24) | (red << 16) | blue;
  43.  *        }
  44.  *    }
  45.  *    Image img = createImage(new MemoryImageSource(w, h, pix, 0, w));
  46.  * 
  47.  * </pre>
  48.  *
  49.  * @see ImageProducer
  50.  *
  51.  * @version    1.13 09/08/95
  52.  * @author     Jim Graham
  53.  */
  54. public class MemoryImageSource implements ImageProducer {
  55.     int width;
  56.     int height;
  57.     ColorModel model;
  58.     Object pixels;
  59.     int pixeloffset;
  60.     int pixelscan;
  61.     Hashtable properties;
  62.  
  63.     /**
  64.      * Construct an ImageProducer object which uses an array of bytes
  65.      * to produce data for an Image object.
  66.      * @see java.awt.Component#createImage
  67.      */
  68.     public MemoryImageSource(int w, int h, ColorModel cm,
  69.                  byte[] pix, int off, int scan) {
  70.     initialize(w, h, cm, (Object) pix, off, scan, null);
  71.     }
  72.  
  73.     /**
  74.      * Construct an ImageProducer object which uses an array of bytes
  75.      * to produce data for an Image object.
  76.      * @see java.awt.Component#createImage
  77.      */
  78.     public MemoryImageSource(int w, int h, ColorModel cm,
  79.                  byte[] pix, int off, int scan, Hashtable props) {
  80.     initialize(w, h, cm, (Object) pix, off, scan, props);
  81.     }
  82.  
  83.     /**
  84.      * Construct an ImageProducer object which uses an array of integers
  85.      * to produce data for an Image object.
  86.      * @see java.awt.Component#createImage
  87.      */
  88.     public MemoryImageSource(int w, int h, ColorModel cm,
  89.                  int[] pix, int off, int scan) {
  90.     initialize(w, h, cm, (Object) pix, off, scan, null);
  91.     }
  92.  
  93.     /**
  94.      * Construct an ImageProducer object which uses an array of integers
  95.      * to produce data for an Image object.
  96.      * @see java.awt.Component#createImage
  97.      */
  98.     public MemoryImageSource(int w, int h, ColorModel cm,
  99.                  int[] pix, int off, int scan, Hashtable props) {
  100.     initialize(w, h, cm, (Object) pix, off, scan, props);
  101.     }
  102.  
  103.     private void initialize(int w, int h, ColorModel cm,
  104.                 Object pix, int off, int scan, Hashtable props) {
  105.     width = w;
  106.     height = h;
  107.     model = cm;
  108.     pixels = pix;
  109.     pixeloffset = off;
  110.     pixelscan = scan;
  111.     if (props == null) {
  112.         props = new Hashtable();
  113.     }
  114.     properties = props;
  115.     }
  116.  
  117.     /**
  118.      * Construct an ImageProducer object which uses an array of integers
  119.      * in the default RGB ColorModel to produce data for an Image object.
  120.      * @see java.awt.Component#createImage
  121.      * @see ColorModel#getRGBdefault
  122.      */
  123.     public MemoryImageSource(int w, int h, int pix[], int off, int scan) {
  124.     initialize(w, h, ColorModel.getRGBdefault(),
  125.            (Object) pix, off, scan, null);
  126.     }
  127.  
  128.     /**
  129.      * Construct an ImageProducer object which uses an array of integers
  130.      * in the default RGB ColorModel to produce data for an Image object.
  131.      * @see java.awt.Component#createImage
  132.      * @see ColorModel#getRGBdefault
  133.      */
  134.     public MemoryImageSource(int w, int h, int pix[], int off, int scan,
  135.                  Hashtable props) {
  136.     initialize(w, h, ColorModel.getRGBdefault(),
  137.            (Object) pix, off, scan, props);
  138.     }
  139.  
  140.     // We can only have one consumer since we immediately return the data...
  141.     private ImageConsumer theConsumer;
  142.  
  143.     /**
  144.      * Add an ImageConsumer to the list of consumers interested in
  145.      * data for this image.
  146.      * @see ImageConsumer
  147.      */
  148.     public synchronized void addConsumer(ImageConsumer ic) {
  149.     theConsumer = ic;
  150.     produce();
  151.     }
  152.  
  153.     /**
  154.      * Determine if an ImageConsumer is on the list of consumers currently
  155.      * interested in data for this image.
  156.      * @return true if the ImageConsumer is on the list; false otherwise
  157.      * @see ImageConsumer
  158.      */
  159.     public synchronized boolean isConsumer(ImageConsumer ic) {
  160.     return (ic == theConsumer);
  161.     }
  162.  
  163.     /**
  164.      * Remove an ImageConsumer from the list of consumers interested in
  165.      * data for this image.
  166.      * @see ImageConsumer
  167.      */
  168.     public synchronized void removeConsumer(ImageConsumer ic) {
  169.     if (theConsumer == ic) {
  170.         theConsumer = null;
  171.     }
  172.     }
  173.  
  174.     /**
  175.      * Add an ImageConsumer to the list of consumers interested in
  176.      * data for this image, and immediately start delivery of the
  177.      * image data through the ImageConsumer interface.
  178.      * @see ImageConsumer
  179.      */
  180.     public void startProduction(ImageConsumer ic) {
  181.     addConsumer(ic);
  182.     }
  183.  
  184.     /**
  185.      * Request that a given ImageConsumer have the image data delivered
  186.      * one more time in top-down, left-right order.
  187.      * @see ImageConsumer
  188.      */
  189.     public void requestTopDownLeftRightResend(ImageConsumer ic) {
  190.     // Not needed.  The data is always in TDLR format.
  191.     }
  192.  
  193.     private void produce() {
  194.     if (theConsumer != null) {
  195.         theConsumer.setDimensions(width, height);
  196.     }
  197.     if (theConsumer != null) {
  198.         theConsumer.setProperties(properties);
  199.     }
  200.     if (theConsumer != null) {
  201.         theConsumer.setColorModel(model);
  202.     }
  203.     if (theConsumer != null) {
  204.         theConsumer.setHints(ImageConsumer.TOPDOWNLEFTRIGHT |
  205.                  ImageConsumer.COMPLETESCANLINES |
  206.                  ImageConsumer.SINGLEPASS |
  207.                  ImageConsumer.SINGLEFRAME);
  208.     }
  209.     if (theConsumer != null) {
  210.         if (pixels instanceof byte[]) {
  211.         theConsumer.setPixels(0, 0, width, height, model,
  212.                       ((byte[]) pixels), pixeloffset,
  213.                       pixelscan);
  214.         } else {
  215.         theConsumer.setPixels(0, 0, width, height, model,
  216.                       ((int[]) pixels), pixeloffset,
  217.                       pixelscan);
  218.         }
  219.     }
  220.     if (theConsumer != null) {
  221.         theConsumer.imageComplete(ImageConsumer.STATICIMAGEDONE);
  222.     }
  223.     }
  224. }
  225.