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

  1. /*
  2.  * @(#)ImageRepresentation.java    1.40 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.awt.Color;
  23. import java.awt.Graphics;
  24. import java.awt.AWTException;
  25. import java.awt.Rectangle;
  26. import java.awt.image.ColorModel;
  27. import java.awt.image.ImageConsumer;
  28. import java.awt.image.ImageObserver;
  29. import sun.awt.image.ImageWatched;
  30. import java.util.Hashtable;
  31.  
  32. public class ImageRepresentation extends ImageWatched implements ImageConsumer {
  33.     int pData;
  34.  
  35.     InputStreamImageSource src;
  36.     Image image;
  37.     int tag;
  38.  
  39.     int srcW;
  40.     int srcH;
  41.     int width;
  42.     int height;
  43.     int hints;
  44.  
  45.     int availinfo;
  46.  
  47.     boolean offscreen;
  48.  
  49.     Rectangle newbits;
  50.  
  51.     /**
  52.      * Create an ImageRepresentation for the given Image scaled
  53.      * to the given width and height and dithered or converted to
  54.      * a ColorModel appropriate for the given image tag.
  55.      */
  56.     ImageRepresentation(Image im, int w, int h, int t) {
  57.     image = im;
  58.     tag = t;
  59.     if (w == 0 || h == 0) {
  60.         throw new IllegalArgumentException();
  61.     }
  62.     if (w > 0) {
  63.         width = w;
  64.         availinfo |= ImageObserver.WIDTH;
  65.     }
  66.     if (h > 0) {
  67.         height = h;
  68.         availinfo |= ImageObserver.HEIGHT;
  69.     }
  70.     if (image.getSource() instanceof InputStreamImageSource) {
  71.         src = (InputStreamImageSource) image.getSource();
  72.     }
  73.     }
  74.  
  75.     /**
  76.      * Initialize this ImageRepresentation object to act as the
  77.      * destination drawable for this OffScreen Image.
  78.      */
  79.     native void offscreenInit(Color bg);
  80.  
  81.     /* REMIND: Only used for Frame.setIcon - should use ImageWatcher instead */
  82.     public synchronized void reconstruct(int flags) {
  83.     if (src != null) {
  84.         src.checkSecurity(null, false);
  85.     }
  86.     int missinginfo = flags & ~availinfo;
  87.     if (missinginfo != 0) {
  88.         numWaiters++;
  89.         try {
  90.         startProduction();
  91.         missinginfo = flags & ~availinfo;
  92.         while (missinginfo != 0) {
  93.             try {
  94.             wait();
  95.             } catch (InterruptedException e) {
  96.             Thread.currentThread().interrupt();
  97.             return;
  98.             }
  99.             missinginfo = flags & ~availinfo;
  100.         }
  101.         } finally {
  102.         decrementWaiters();
  103.         }
  104.     }
  105.     }
  106.  
  107.     public synchronized void setDimensions(int w, int h) {
  108.     if (src != null) {
  109.         src.checkSecurity(null, false);
  110.     }
  111.     srcW = w;
  112.     srcH = h;
  113.     if ((availinfo & ImageObserver.WIDTH) == 0
  114.         && (availinfo & ImageObserver.HEIGHT) == 0)
  115.     {
  116.         width = w;
  117.         height = h;
  118.     } else if ((availinfo & ImageObserver.WIDTH) == 0) {
  119.         // Height is set, but width is defaulted.
  120.         width = w * height / h;
  121.     } else if ((availinfo & ImageObserver.HEIGHT) == 0) {
  122.         // Width is set, but height is defaulted.
  123.         height = h * width / w;
  124.     } else {
  125.         // Both were set, no need to do a notification.
  126.         return;
  127.     }
  128.     if (srcW <= 0 || srcH <= 0 || width <= 0 || height <= 0) {
  129.         imageComplete(ImageConsumer.IMAGEERROR);
  130.         return;
  131.     }
  132.     availinfo |= ImageObserver.WIDTH | ImageObserver.HEIGHT;
  133.     }
  134.  
  135.     public void setProperties(Hashtable props) {
  136.     if (src != null) {
  137.         src.checkSecurity(null, false);
  138.     }
  139.     }
  140.  
  141.     public void setColorModel(ColorModel model) {
  142.     if (src != null) {
  143.         src.checkSecurity(null, false);
  144.     }
  145.     }
  146.  
  147.     public void setHints(int h) {
  148.     if (src != null) {
  149.         src.checkSecurity(null, false);
  150.     }
  151.     hints = h;
  152.     }
  153.  
  154.     private native boolean setBytePixels(int x, int y, int w, int h,
  155.                      ColorModel model,
  156.                      byte pix[], int off, int scansize);
  157.  
  158.     public void setPixels(int x, int y, int w, int h,
  159.                        ColorModel model,
  160.                        byte pix[], int off, int scansize) {
  161.     if (src != null) {
  162.         src.checkSecurity(null, false);
  163.     }
  164.     boolean ok = false;
  165.     int cx, cy, cw, ch;
  166.     synchronized (this) {
  167.         if (newbits == null) {
  168.         newbits = new Rectangle(0, 0, width, height);
  169.         }
  170.         if (setBytePixels(x, y, w, h, model, pix, off, scansize)) {
  171.         availinfo |= ImageObserver.SOMEBITS;
  172.         ok = true;
  173.         }
  174.         cx = newbits.x;
  175.         cy = newbits.y;
  176.         cw = newbits.width;
  177.         ch = newbits.height;
  178.     }
  179.     if (ok && !offscreen) {
  180.         newInfo(image, ImageObserver.SOMEBITS, cx, cy, cw, ch);
  181.     }
  182.     }
  183.  
  184.     private native boolean setIntPixels(int x, int y, int w, int h,
  185.                     ColorModel model,
  186.                     int pix[], int off, int scansize);
  187.  
  188.     public void setPixels(int x, int y, int w, int h,
  189.                        ColorModel model,
  190.                        int pix[], int off, int scansize) {
  191.     if (src != null) {
  192.         src.checkSecurity(null, false);
  193.     }
  194.     boolean ok = false;
  195.     int cx, cy, cw, ch;
  196.     synchronized (this) {
  197.         if (newbits == null) {
  198.         newbits = new Rectangle(0, 0, width, height);
  199.         }
  200.         if (setIntPixels(x, y, w, h, model, pix, off, scansize)) {
  201.         availinfo |= ImageObserver.SOMEBITS;
  202.         ok = true;
  203.         }
  204.         cx = newbits.x;
  205.         cy = newbits.y;
  206.         cw = newbits.width;
  207.         ch = newbits.height;
  208.     }
  209.     if (ok && !offscreen) {
  210.         newInfo(image, ImageObserver.SOMEBITS, cx, cy, cw, ch);
  211.     }
  212.     }
  213.  
  214.     private boolean consuming = false;
  215.  
  216.     private native boolean finish(boolean force);
  217.  
  218.     public void imageComplete(int status) {
  219.     if (src != null) {
  220.         src.checkSecurity(null, false);
  221.     }
  222.     boolean done;
  223.     int info;
  224.     switch (status) {
  225.     default:
  226.     case ImageConsumer.IMAGEABORTED:
  227.         done = true;
  228.         info = ImageObserver.ABORT;
  229.         break;
  230.     case ImageConsumer.IMAGEERROR:
  231.         image.addInfo(ImageObserver.ERROR);
  232.         done = true;
  233.         info = ImageObserver.ERROR;
  234.         dispose();
  235.         break;
  236.     case ImageConsumer.STATICIMAGEDONE:
  237.         done = true;
  238.         info = ImageObserver.ALLBITS;
  239.         if (finish(false)) {
  240.         image.getSource().requestTopDownLeftRightResend(this);
  241.         finish(true);
  242.         }
  243.         break;
  244.     case ImageConsumer.SINGLEFRAMEDONE:
  245.         done = false;
  246.         info = ImageObserver.FRAMEBITS;
  247.         break;
  248.     }
  249.     synchronized (this) {
  250.         if (done) {
  251.         image.getSource().removeConsumer(this);
  252.         consuming = false;
  253.         newbits = null;
  254.         }
  255.         availinfo |= info;
  256.         notifyAll();
  257.     }
  258.     if (!offscreen) {
  259.         newInfo(image, info, 0, 0, width, height);
  260.     }
  261.     }
  262.  
  263.     /*synchronized*/ void startProduction() {
  264.     if (!consuming) {
  265.         consuming = true;
  266.         image.getSource().startProduction(this);
  267.     }
  268.     }
  269.  
  270.     private int numWaiters;
  271.  
  272.     private synchronized void checkConsumption() {
  273.     if (watchers == null && numWaiters == 0
  274.         && ((availinfo & ImageObserver.ALLBITS) == 0))
  275.     {
  276.         dispose();
  277.     }
  278.     }
  279.  
  280.     public synchronized void removeWatcher(ImageObserver iw) {
  281.     super.removeWatcher(iw);
  282.     checkConsumption();
  283.     }
  284.  
  285.     private synchronized void decrementWaiters() {
  286.     --numWaiters;
  287.     checkConsumption();
  288.     }
  289.  
  290.     public boolean prepare(ImageObserver iw) {
  291.     if (src != null) {
  292.         src.checkSecurity(null, false);
  293.     }
  294.     if ((availinfo & ImageObserver.ERROR) != 0) {
  295.         if (iw != null) {
  296.         iw.imageUpdate(image, ImageObserver.ERROR|ImageObserver.ABORT,
  297.                    -1, -1, -1, -1);
  298.         }
  299.         return false;
  300.     }
  301.     boolean done = ((availinfo & ImageObserver.ALLBITS) != 0);
  302.     if (!done) {
  303.         addWatcher(iw);
  304.         startProduction();
  305.     }
  306.     return done;
  307.     }
  308.  
  309.     public int check(ImageObserver iw) {
  310.     if (src != null) {
  311.         src.checkSecurity(null, false);
  312.     }
  313.     if ((availinfo & (ImageObserver.ERROR | ImageObserver.ALLBITS)) != 0) {
  314.         addWatcher(iw);
  315.     }
  316.     return availinfo;
  317.     }
  318.  
  319.     native synchronized void imageDraw(Graphics g, int x, int y, Color c);
  320.  
  321.     public boolean drawImage(Graphics g, int x, int y, Color c,
  322.                  ImageObserver iw) {
  323.     if (src != null) {
  324.         src.checkSecurity(null, false);
  325.     }
  326.     if ((availinfo & ImageObserver.ERROR) != 0) {
  327.         if (iw != null) {
  328.         iw.imageUpdate(image, ImageObserver.ERROR|ImageObserver.ABORT,
  329.                    -1, -1, -1, -1);
  330.         }
  331.         return false;
  332.     }
  333.     boolean done = ((availinfo & ImageObserver.ALLBITS) != 0);
  334.     if (!done) {
  335.         addWatcher(iw);
  336.         startProduction();
  337.     }
  338.     imageDraw(g, x, y, c);
  339.     return done;
  340.     }
  341.  
  342.     private native void disposeImage();
  343.  
  344.     synchronized void abort() {
  345.     image.getSource().removeConsumer(this);
  346.     consuming = false;
  347.     newbits = null;
  348.     disposeImage();
  349.     newInfo(image, ImageObserver.ABORT, -1, -1, -1, -1);
  350.     availinfo &= ~(ImageObserver.SOMEBITS
  351.                | ImageObserver.FRAMEBITS
  352.                | ImageObserver.ALLBITS
  353.                | ImageObserver.ERROR);
  354.     }
  355.  
  356.     synchronized void dispose() {
  357.     image.getSource().removeConsumer(this);
  358.     consuming = false;
  359.     newbits = null;
  360.     disposeImage();
  361.     availinfo &= ~(ImageObserver.SOMEBITS
  362.                | ImageObserver.FRAMEBITS
  363.                | ImageObserver.ALLBITS);
  364.     }
  365.  
  366.     public void finalize() {
  367.     disposeImage();
  368.     }
  369. }
  370.