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

  1. /*
  2.  * @(#)PixelStore.java    1.19 95/10/06 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.Image;
  23. import java.awt.image.*;
  24. import java.util.Hashtable;
  25. import sun.misc.Ref;
  26.  
  27. /**
  28.  * A memory cache object for pixel data.  The actual pixels are stored
  29.  * in a Ref so that they can be freed when necessary.
  30.  *
  31.  * @version     1.19 10/06/95
  32.  * @author     Jim Graham
  33.  */
  34. public abstract class PixelStore extends Ref {
  35.     int width;
  36.     int height;
  37.     ColorModel colormodel;
  38.     Hashtable properties;
  39.  
  40.     boolean seen[];
  41.     int numlines;
  42.  
  43.     int availinfo;
  44.     int hints;
  45.  
  46.     public PixelStore() {
  47.     }
  48.  
  49.     public PixelStore(int w, int h) {
  50.     setDimensions(w, h);
  51.     }
  52.  
  53.     public PixelStore(int w, int h, ColorModel cm) {
  54.     setDimensions(w, h);
  55.     setColorModel(cm);
  56.     }
  57.  
  58.     public synchronized void setDimensions(int w, int h) {
  59.     width = w;
  60.     height = h;
  61.     availinfo |= ImageObserver.WIDTH | ImageObserver.HEIGHT;
  62.     }
  63.  
  64.     public synchronized void setProperties(Hashtable props) {
  65.     properties = props;
  66.     availinfo |= ImageObserver.PROPERTIES;
  67.     }
  68.  
  69.     public synchronized void setColorModel(ColorModel cm) {
  70.     colormodel = cm;
  71.     }
  72.  
  73.     public synchronized void setHints(int h) {
  74.     hints = h;
  75.     }
  76.  
  77.     protected void recordPixels(int x, int y, int w, int h) {
  78.     numlines = Math.max(numlines, y+h);
  79.     if ((hints & ImageConsumer.TOPDOWNLEFTRIGHT) == 0) {
  80.         if (seen == null) {
  81.         seen = new boolean[height];
  82.         }
  83.         for (int i = 0; i < h; i++) {
  84.         seen[y+i] = true;
  85.         }
  86.     }
  87.     }
  88.  
  89.     public synchronized boolean setPixels(int x, int y, int w, int h,
  90.                       byte pix[], int srcoff, int scansize)
  91.     {
  92.     recordPixels(x, y, w, h);
  93.     Object[] lines = (Object[]) get();
  94.     if (lines != null) {
  95.         int i = srcoff;
  96.         for (int Y = y; Y < y + h; Y++) {
  97.         byte ras[] = (byte[]) lines[Y];
  98.         int dstoff = offsets[Y] + x;
  99.         System.arraycopy(pix, i, ras, dstoff, w);
  100.         i += scansize;
  101.         }
  102.         availinfo |= ImageObserver.SOMEBITS;
  103.     }
  104.     return (lines != null);
  105.     }
  106.  
  107.     public synchronized boolean setPixels(int x, int y, int w, int h,
  108.                       int pix[], int srcoff, int scansize)
  109.     {
  110.     recordPixels(x, y, w, h);
  111.     Object[] lines = (Object[]) get();
  112.     if (lines != null) {
  113.         int i = srcoff;
  114.         for (int Y = y; Y < y + h; Y++) {
  115.         int ras[] = (int[]) lines[Y];
  116.         int dstoff = offsets[Y] + x;
  117.         System.arraycopy(pix, i, ras, dstoff, w);
  118.         i += scansize;
  119.         }
  120.         availinfo |= ImageObserver.SOMEBITS;
  121.     }
  122.     return (lines != null);
  123.     }
  124.  
  125.     public synchronized void imageComplete() {
  126.     if (get() != null && bit_state == BITS_ALLOCATED) {
  127.         numlines = height;
  128.         hints = (ImageConsumer.TOPDOWNLEFTRIGHT |
  129.              ImageConsumer.COMPLETESCANLINES |
  130.              ImageConsumer.SINGLEPASS |
  131.              ImageConsumer.SINGLEFRAME);
  132.         availinfo |= ImageObserver.ALLBITS;
  133.     }
  134.     }
  135.  
  136.     public synchronized int getWidth() {
  137.     return width;
  138.     }
  139.  
  140.     public synchronized int getHeight() {
  141.     return height;
  142.     }
  143.  
  144.     public synchronized ColorModel getColorModel() {
  145.     return colormodel;
  146.     }
  147.  
  148.     public synchronized int getBitState() {
  149.     if (bit_state == BITS_ALLOCATED && numlines > 0) {
  150.         // Trigger a get() to make sure the bits are still there.
  151.         Object lines = get();
  152.     }
  153.     return bit_state;
  154.     }
  155.  
  156.     abstract void replayLines(ImageConsumer ic, int i, int cnt, Object line);
  157.  
  158.     public synchronized boolean replay(ImageProducer ip, ImageConsumer ic) {
  159.     return replay(ip, ic, true);
  160.     }
  161.  
  162.     public synchronized boolean replay(ImageProducer ip, ImageConsumer ic,
  163.                        boolean full) {
  164.     if (full && (availinfo & ImageObserver.WIDTH) != 0
  165.         && (availinfo & ImageObserver.HEIGHT) != 0) {
  166.         ic.setDimensions(width, height);
  167.         if (!ip.isConsumer(ic)) {
  168.         return true;
  169.         }
  170.     }
  171.     if (full && (availinfo & ImageObserver.PROPERTIES) != 0) {
  172.         ic.setProperties(properties);
  173.     }
  174.     if (full && colormodel != null) {
  175.         ic.setColorModel(colormodel);
  176.         if (!ip.isConsumer(ic)) {
  177.         return true;
  178.         }
  179.     }
  180.     if (hints != 0) {
  181.         ic.setHints(hints & (ImageConsumer.TOPDOWNLEFTRIGHT |
  182.                  ImageConsumer.COMPLETESCANLINES |
  183.                  ImageConsumer.SINGLEPASS |
  184.                  ImageConsumer.SINGLEFRAME));
  185.         if (!ip.isConsumer(ic)) {
  186.         return true;
  187.         }
  188.     }
  189.     Object lines[] = null;
  190.     if (bit_state == BITS_ALLOCATED && numlines > 0) {
  191.         lines = (Object[]) get();
  192.     }
  193.     if (bit_state == BITS_LOST) {
  194.         return false;
  195.     }
  196.     Thread me = Thread.currentThread();
  197.     if (ImageFetcher.isFetcher(me) &&
  198.         me.getPriority() > ImageFetcher.LOW_PRIORITY)
  199.     {
  200.         me.setPriority(ImageFetcher.LOW_PRIORITY);
  201.         //me.yield();
  202.     }
  203.     if (lines != null) {
  204.         Object prevline = null;
  205.         int prevcount = 0;
  206.         for (int i = 0; i < numlines; i++) {
  207.         if (seen != null && !seen[i]) {
  208.             if (prevline != null) {
  209.             replayLines(ic, i - prevcount, prevcount, prevline);
  210.             if (!ip.isConsumer(ic)) {
  211.                 return true;
  212.             }
  213.             prevline = null;
  214.             prevcount = 0;
  215.             }
  216.             continue;
  217.         }
  218.         Object line = lines[i];
  219.         if (prevline != line && prevline != null) {
  220.             replayLines(ic, i - prevcount, prevcount, prevline);
  221.             if (!ip.isConsumer(ic)) {
  222.             return true;
  223.             }
  224.             prevcount = 0;
  225.         }
  226.         prevline = line;
  227.         prevcount++;
  228.         }
  229.         if (prevline != null) {
  230.         replayLines(ic, numlines - prevcount, prevcount, prevline);
  231.         if (!ip.isConsumer(ic)) {
  232.             return true;
  233.         }
  234.         }
  235.     }
  236.     if (full && bit_state == BITS_ALLOCATED &&
  237.         (availinfo & ImageObserver.ALLBITS) != 0)
  238.     {
  239.         ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
  240.     }
  241.     return true;
  242.     }
  243.  
  244.     static final int NO_BITS_YET = 0;
  245.     static final int BITS_ALLOCATED = 1;
  246.     static final int BITS_LOST = 2;
  247.  
  248.     int bit_state = NO_BITS_YET;
  249.     int offsets[];
  250.  
  251.     abstract Object allocateLines(int num);
  252.  
  253.     public Object reconstitute() {
  254.     Object[] lines = null;
  255.     if (bit_state == NO_BITS_YET) {
  256.         if (((availinfo & ImageObserver.HEIGHT) == 0)
  257.         || ((availinfo & ImageObserver.WIDTH) == 0)
  258.         || (colormodel == null)) {
  259.         return null;
  260.         }
  261.         bit_state = BITS_ALLOCATED;
  262.         lines = new Object[height];
  263.         offsets = new int[height];
  264.         int i = 0;
  265.         while (i < height) {
  266.         int trylines = height - i;
  267.         Object chunk = null;
  268.         while (chunk == null && trylines > 0) {
  269.             try {
  270.             chunk = allocateLines(trylines);
  271.             if (chunk == null) {
  272.                 break;
  273.             }
  274.             } catch (OutOfMemoryError e) {
  275.             chunk = null;
  276.             trylines /= 2;
  277.             }
  278.         }
  279.         if (chunk == null) {
  280.             bit_state = BITS_LOST;
  281.             return null;
  282.         }
  283.         int off = 0;
  284.         while (trylines > 0) {
  285.             lines[i] = chunk;
  286.             offsets[i] = off;
  287.             off += width;
  288.             i++;
  289.             trylines--;
  290.         }
  291.         }
  292.     } else if (bit_state == BITS_ALLOCATED) {
  293.         // We had some bits, but they're gone now.
  294.         bit_state = BITS_LOST;
  295.     }
  296.     return lines;
  297.     }
  298. }
  299.