home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / awt / image / Image.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  11.0 KB  |  421 lines

  1. /*
  2.  * @(#)Image.java    1.41 96/03/30 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 sun.awt.image;
  21.  
  22. import java.util.Hashtable;
  23. import java.util.Enumeration;
  24.  
  25. import java.awt.Component;
  26. import java.awt.Color;
  27. import java.awt.Graphics;
  28. import java.awt.image.ColorModel;
  29. import java.awt.image.ImageProducer;
  30. import java.awt.image.ImageConsumer;
  31. import java.awt.image.ImageObserver;
  32. import sun.awt.image.OffScreenImageSource;
  33. import sun.awt.image.ImageWatched;
  34. import sun.awt.image.ImageRepresentation;
  35. import sun.awt.image.FileImageSource;
  36.  
  37. public abstract class Image extends java.awt.Image {
  38.     /**
  39.      * The object which is used to reconstruct the original image data
  40.      * as needed.
  41.      */
  42.     ImageProducer source;
  43.  
  44.     InputStreamImageSource src;
  45.  
  46.     /**
  47.      * The object which intercepts the image dimension information
  48.      * and redirects it into the image.
  49.      */
  50.     ImageInfoGrabber info;
  51.  
  52.     protected Image() {
  53.     }
  54.  
  55.     /**
  56.      * Construct an image for offscreen rendering to be used with a
  57.      * given Component.
  58.      */
  59.     protected Image(Component c, int w, int h) {
  60.     OffScreenImageSource osis = new OffScreenImageSource(c, w, h);
  61.     source = osis;
  62.     width = w;
  63.     height = h;
  64.     availinfo |= (ImageObserver.WIDTH | ImageObserver.HEIGHT);
  65.     ImageRepresentation ir = getImageRep(-1, -1);
  66.     representations.put(w + "x" + h, ir);
  67.     representations.put("-1x-1", ir);
  68.     baseIR = ir;
  69.     ir.setDimensions(w, h);
  70.     ir.offscreenInit(c.getBackground());
  71.     osis.setImageRep(ir);
  72.     }
  73.  
  74.     /**
  75.      * Construct an image from an ImageProducer object.
  76.      */
  77.     protected Image(ImageProducer is) {
  78.     source = is;
  79.     if (is instanceof InputStreamImageSource) {
  80.         src = (InputStreamImageSource) is;
  81.     }
  82.     info = new ImageInfoGrabber(this);
  83.     info.setupConsumer();
  84.     }
  85.  
  86.     public ImageProducer getSource() {
  87.     if (src != null) {
  88.         src.checkSecurity(null, false);
  89.     }
  90.     return source;
  91.     }
  92.  
  93.     protected void initGraphics(Graphics g) {
  94.     OffScreenImageSource osis = (OffScreenImageSource) source;
  95.     g.setColor(osis.target.getForeground());
  96.     g.setFont(osis.target.getFont());
  97.     }
  98.  
  99.     public Color getBackground() {
  100.     OffScreenImageSource osis = (OffScreenImageSource) source;
  101.     return osis.target.getBackground();
  102.     }
  103.  
  104.     private int width = -1;
  105.     private int height = -1;
  106.     private Hashtable properties;
  107.  
  108.     private int availinfo;
  109.  
  110.     /**
  111.      * Return the width of the original image source.
  112.      * If the width isn't known, then the image is reconstructed.
  113.      */
  114.     public int getWidth() {
  115.     if (src != null) {
  116.         src.checkSecurity(null, false);
  117.     }
  118.     if ((availinfo & ImageObserver.WIDTH) == 0) {
  119.         reconstruct(ImageObserver.WIDTH);
  120.     }
  121.     return width;
  122.     }
  123.  
  124.     /**
  125.      * Return the width of the original image source.
  126.      * If the width isn't known, then the ImageObserver object will be
  127.      * notified when the data is available.
  128.      */
  129.     public synchronized int getWidth(ImageObserver iw) {
  130.     if (src != null) {
  131.         src.checkSecurity(null, false);
  132.     }
  133.     if ((availinfo & ImageObserver.WIDTH) == 0) {
  134.         addWatcher(iw, true);
  135.         return -1;
  136.     }
  137.     return width;
  138.     }
  139.  
  140.     /**
  141.      * Return the height of the original image source.
  142.      * If the height isn't known, then the image is reconstructed.
  143.      */
  144.     public int getHeight() {
  145.     if (src != null) {
  146.         src.checkSecurity(null, false);
  147.     }
  148.     if ((availinfo & ImageObserver.HEIGHT) == 0) {
  149.         reconstruct(ImageObserver.HEIGHT);
  150.     }
  151.     return height;
  152.     }
  153.  
  154.     /**
  155.      * Return the height of the original image source.
  156.      * If the height isn't known, then the ImageObserver object will be
  157.      * notified when the data is available.
  158.      */
  159.     public synchronized int getHeight(ImageObserver iw) {
  160.     if (src != null) {
  161.         src.checkSecurity(null, false);
  162.     }
  163.     if ((availinfo & ImageObserver.HEIGHT) == 0) {
  164.         addWatcher(iw, true);
  165.         return -1;
  166.     }
  167.     return height;
  168.     }
  169.  
  170.     /**
  171.      * Return a property of the image by name.  Individual property names
  172.      * are defined by the various image formats.  If a property is not
  173.      * defined for a particular image, then this method will return the
  174.      * UndefinedProperty object.  If the properties for this image are
  175.      * not yet known, then this method will return null and the ImageObserver
  176.      * object will be notified later.  The property name "comment" should
  177.      * be used to store an optional comment which can be presented to
  178.      * the user as a description of the image, its source, or its author.
  179.      */
  180.     public Object getProperty(String name, ImageObserver observer) {
  181.     if (src != null) {
  182.         src.checkSecurity(null, false);
  183.     }
  184.     if (properties == null) {
  185.         addWatcher(observer, true);
  186.         return null;
  187.     }
  188.     Object o = properties.get(name);
  189.     if (o == null) {
  190.         o = Image.UndefinedProperty;
  191.     }
  192.     return o;
  193.     }
  194.  
  195.     public boolean hasError() {
  196.     if (src != null) {
  197.         src.checkSecurity(null, false);
  198.     }
  199.     return (availinfo & ImageObserver.ERROR) != 0;
  200.     }
  201.  
  202.     public int check(ImageObserver iw) {
  203.     if (src != null) {
  204.         src.checkSecurity(null, false);
  205.     }
  206.     if ((availinfo & ImageObserver.ERROR) == 0 &&
  207.         ((~availinfo) & (ImageObserver.WIDTH |
  208.                  ImageObserver.HEIGHT |
  209.                  ImageObserver.PROPERTIES)) != 0) {
  210.         addWatcher(iw, false);
  211.     }
  212.     return availinfo;
  213.     }
  214.  
  215.     public void preload(ImageObserver iw) {
  216.     if (src != null) {
  217.         src.checkSecurity(null, false);
  218.     }
  219.     if ((availinfo & ImageObserver.ALLBITS) == 0) {
  220.         addWatcher(iw, true);
  221.     }
  222.     }
  223.  
  224.     private synchronized void addWatcher(ImageObserver iw, boolean load) {
  225.     if ((availinfo & ImageObserver.ERROR) != 0) {
  226.         if (iw != null) {
  227.         iw.imageUpdate(this, ImageObserver.ERROR|ImageObserver.ABORT,
  228.                    -1, -1, -1, -1);
  229.         }
  230.         return;
  231.     }
  232.     if (info == null) {
  233.         info = new ImageInfoGrabber(this);
  234.         info.setupConsumer();
  235.     }
  236.     info.addWatcher(iw);
  237.     if (load) {
  238.         info.getInfo();
  239.     }
  240.     }
  241.  
  242.     private synchronized void reconstruct(int flags) {
  243.     if ((flags & ~availinfo) != 0) {
  244.         if ((availinfo & ImageObserver.ERROR) != 0) {
  245.         return;
  246.         }
  247.         if (info == null) {
  248.         info = new ImageInfoGrabber(this);
  249.         }
  250.         info.getInfo();
  251.         while ((flags & ~availinfo) != 0) {
  252.         try {
  253.             wait();
  254.         } catch (InterruptedException e) {
  255.             Thread.currentThread().interrupt();
  256.             return;
  257.         }
  258.         if ((availinfo & ImageObserver.ERROR) != 0) {
  259.             return;
  260.         }
  261.         }
  262.     }
  263.     }
  264.  
  265.     synchronized void addInfo(int newinfo) {
  266.     availinfo |= newinfo;
  267.     notifyAll();
  268.     if (((~availinfo) & (ImageObserver.WIDTH |
  269.                  ImageObserver.HEIGHT |
  270.                  ImageObserver.PROPERTIES)) == 0) {
  271.         info.stopInfo();
  272.     }
  273.     }
  274.  
  275.     void setDimensions(int w, int h) {
  276.     width = w;
  277.     height = h;
  278.     addInfo(ImageObserver.WIDTH | ImageObserver.HEIGHT);
  279.     }
  280.  
  281.     void setProperties(Hashtable props) {
  282.     if (props == null) {
  283.         props = new Hashtable();
  284.     }
  285.     properties = props;
  286.     addInfo(ImageObserver.PROPERTIES);
  287.     }
  288.  
  289.     synchronized void infoDone(int status) {
  290.     if (status == ImageConsumer.IMAGEERROR ||
  291.         ((~availinfo) & (ImageObserver.WIDTH |
  292.                  ImageObserver.HEIGHT)) != 0) {
  293.         addInfo(ImageObserver.ERROR);
  294.     } else if ((availinfo & ImageObserver.PROPERTIES) == 0) {
  295.         setProperties(null);
  296.     }
  297.     }
  298.  
  299.     public void flush() {
  300.     if (src != null) {
  301.         src.checkSecurity(null, false);
  302.     }
  303.     if (!(source instanceof OffScreenImageSource)) {
  304.         Enumeration enum;
  305.         synchronized (this) {
  306.         availinfo &= ~ImageObserver.ERROR;
  307.         enum = representations.elements();
  308.         representations = new Hashtable();
  309.         baseIR = null;
  310.         if (source instanceof InputStreamImageSource) {
  311.             ((InputStreamImageSource) source).flush();
  312.         }
  313.         }
  314.         while (enum.hasMoreElements()) {
  315.         ImageRepresentation ir =
  316.             (ImageRepresentation) enum.nextElement();
  317.         ir.abort();
  318.         }
  319.     }
  320.     }
  321.  
  322.     Hashtable representations = new Hashtable();
  323.     ImageRepresentation baseIR;
  324.  
  325.     protected ImageRepresentation getImageRep(int w, int h) {
  326.     if (src != null) {
  327.         src.checkSecurity(null, false);
  328.     }
  329.     String key;
  330.     if (w == -1 && h == -1) {
  331.         if (baseIR != null) {
  332.         return baseIR;
  333.         }
  334.         key = "-1x-1";
  335.     } else {
  336.         key = w + "x" + h;
  337.     }
  338.     ImageRepresentation ir = (ImageRepresentation)representations.get(key);
  339.     if (ir == null) {
  340.         // Now try it synchronized and add it if it isn't there
  341.         synchronized (this) {
  342.         ir = (ImageRepresentation)representations.get(key);
  343.         if (ir == null) {
  344.             ir = new ImageRepresentation(this, w, h, 0);
  345.             if (source instanceof OffScreenImageSource) {
  346.             ir.offscreen = true;
  347.             } else {
  348.             representations.put(key, ir);
  349.             }
  350.             if (w == -1 && h == -1) {
  351.             baseIR = ir;
  352.             }
  353.         }
  354.         }
  355.     }
  356.     return ir;
  357.     }
  358. }
  359.  
  360. class ImageInfoGrabber extends ImageWatched implements ImageConsumer {
  361.     Image image;
  362.  
  363.     public ImageInfoGrabber(Image img) {
  364.     image = img;
  365.     }
  366.  
  367.     public void setupConsumer() {
  368.     image.getSource().addConsumer(this);
  369.     }
  370.  
  371.     public void getInfo() {
  372.     image.getSource().startProduction(this);
  373.     }
  374.  
  375.     public void stopInfo() {
  376.     image.getSource().removeConsumer(this);
  377.     watchers = null;
  378.     }
  379.  
  380.     public void setDimensions(int w, int h) {
  381.     image.setDimensions(w, h);
  382.     newInfo(image, (ImageObserver.WIDTH | ImageObserver.HEIGHT),
  383.         0, 0, w, h);
  384.     }
  385.  
  386.     public void setProperties(Hashtable props) {
  387.     image.setProperties(props);
  388.     newInfo(image, ImageObserver.PROPERTIES, 0, 0, 0, 0);
  389.     }
  390.  
  391.     public void setColorModel(ColorModel model) {
  392.     return;
  393.     }
  394.  
  395.     public void setHints(int hints) {
  396.     return;
  397.     }
  398.  
  399.     public void setPixels(int srcX, int srcY, int srcW, int srcH,
  400.               ColorModel model,
  401.               byte pixels[], int srcOff, int srcScan) {
  402.     return;
  403.     }
  404.  
  405.     public void setPixels(int srcX, int srcY, int srcW, int srcH,
  406.               ColorModel model,
  407.               int pixels[], int srcOff, int srcScan) {
  408.     return;
  409.     }
  410.  
  411.     public void imageComplete(int status) {
  412.     image.getSource().removeConsumer(this);
  413.     if (status == ImageConsumer.IMAGEERROR) {
  414.         newInfo(image, ImageObserver.ERROR, -1, -1, -1, -1);
  415.     } else if (status == ImageConsumer.IMAGEABORTED) {
  416.         newInfo(image, ImageObserver.ABORT, -1, -1, -1, -1);
  417.     }
  418.     image.infoDone(status);
  419.     }
  420. }
  421.