home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / image / WritableRaster.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  19.9 KB  |  473 lines

  1. /*
  2.  * @(#)WritableRaster.java    1.20 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. /* ****************************************************************
  16.  ******************************************************************
  17.  ******************************************************************
  18.  *** COPYRIGHT (c) Eastman Kodak Company, 1997
  19.  *** As  an unpublished  work pursuant to Title 17 of the United
  20.  *** States Code.  All rights reserved.
  21.  ******************************************************************
  22.  ******************************************************************
  23.  ******************************************************************/
  24.  
  25. package java.awt.image;
  26. import java.awt.Rectangle;
  27. import java.awt.Point;
  28.  
  29. /** 
  30.   * This class provides methods for storing image data and inherits
  31.   * methods for retrieving image data from it's parent class Raster.
  32.   * It contains a DataBuffer object that holds a buffer of image
  33.   * data in some format, a SampleModel which describes th formate and is 
  34.   * capabable of storing and retrieving Samples from the DataBuffer 
  35.   * and a Rect that defines the WritableRaster's coordinate space. (Upper
  36.   * left corner, width and height.)
  37.   */
  38.  
  39. public class WritableRaster extends Raster {
  40.  
  41.     /**
  42.      *  Constructs a WritableRaster with the given SampleModel.  The 
  43.      *  WritableRaster's upper left corner is origin and it is the 
  44.      *  same size as the  SampleModel.  A DataBuffer large enough to 
  45.      *  describe the WriteableRaster is automatically created.
  46.      *  @param sampleModel     The SampleModel that specifies the layout.
  47.      *  @param origin          The Point that specified the origin.
  48.      */
  49.     protected WritableRaster(SampleModel sampleModel,
  50.                              Point origin) {
  51.         this(sampleModel,
  52.              sampleModel.createCompatibleDataBuffer(),
  53.              new Rectangle(origin.x,
  54.                            origin.y,
  55.                            sampleModel.getWidth(),
  56.                            sampleModel.getHeight()),
  57.              origin,
  58.              null);
  59.     }
  60.  
  61.     /**
  62.      *  Constructs a WritableRaster with the given SampleModel and DataBuffer.
  63.      *  The WritableRaster's upper left corner is origin and it is the same 
  64.      *  size as the SampleModel.  The DataBuffer is not initialized and must 
  65.      *  be compatible with SampleModel.
  66.      *  @param sampleModel     The SampleModel that specifies the layout.
  67.      *  @param dataBuffer      The DataBufferShort that contains the image data.
  68.      *  @param origin          The Point that specifies the origin.
  69.      */
  70.     protected WritableRaster(SampleModel sampleModel,
  71.                              DataBuffer dataBuffer,
  72.                              Point origin) {
  73.         this(sampleModel, 
  74.              dataBuffer, 
  75.              new Rectangle(origin.x,
  76.                            origin.y,
  77.                            sampleModel.getWidth(),
  78.                            sampleModel.getHeight()),
  79.              origin, 
  80.              null);
  81.     }
  82.  
  83.     /**
  84.      * Constructs a WriteableRaster with the given SampleModel, DataBuffer,
  85.      * and parent.  When translated into the base Raster's coordinate
  86.      * system, aRegion must be contained by the base Raster.  Origin
  87.      * is the coodinate in the new Raster's coordinate system of the
  88.      * origin of the base Raster.  (The base Raster is the Raster's
  89.      * ancestor which has no parent.) The parameter parent must be 
  90.      * a WritableRaster or an exception will be thrown.
  91.      *
  92.      * Note that this constructor should generally be called by other 
  93.      * constructors or create methods, it should not be used directly.  
  94.      * @param sampleModel     The SampleModel that specifies the layout.
  95.      * @param dataBuffer      The DataBufferShort that contains the image data.
  96.      * @param aRegion         The Rectangle that specifies the image area.     
  97.      * @param origin          The Point that specifies the origin.
  98.      * @param parent          The parent (if any) of this raster.
  99.      */
  100.     protected WritableRaster(SampleModel sampleModel, 
  101.                              DataBuffer dataBuffer, 
  102.                              Rectangle aRegion,
  103.                              Point origin,
  104.                              WritableRaster parent){
  105.         super(sampleModel,dataBuffer,aRegion,origin,parent);
  106.         if (parent != null) {
  107.             if (!(parent instanceof WritableRaster)) {
  108.                 throw new RasterFormatException("Attempted to construct a " +
  109.                                "WritableRaster with a non-writable Parent");
  110.             }
  111.         }
  112.     }
  113.  
  114.  
  115.     /** Returns the parent WritableRaster (if any) of this Raster. */
  116.     public WritableRaster getWritableParent() {
  117.         return (WritableRaster)parent;
  118.     }
  119.  
  120.     /** 
  121.      *  Create a WritableRaster with the same size, SampleModel and DataBuffer
  122.      *  as this one, but with a different location.
  123.      *  @param location The upper left corner of the new returned Raster.
  124.      */
  125.     public WritableRaster createWritableTranslatedRaster(Point location) {
  126.         return createWritableSubRaster(minX,minY,width,height,
  127.                                        location.x,location.y,null);
  128.     }
  129.  
  130.     /**
  131.      * Returns a WritableRaster which references this WritableRaster's 
  132.      * DataBuffer.  x,y, width and height form a rectangle in this
  133.      * WritableRaster's coordinate space. An error is thrown if this
  134.      * rectangle is not fully contained by this Raster
  135.      * @param x      The X coordinate of the upper left hand corner.
  136.      * @param y      The Y coordinate of the upper left hand corner.
  137.      * @param width  Width of the region starting at (x,y).
  138.      * @param height Height of the region starting at (x,y).
  139.      */
  140.     public WritableRaster createWritableSubRaster(int x, int y, 
  141.                                                   int w, int h) {
  142.     return createWritableSubRaster(x, y, w, h, x, y, null);
  143.     }
  144.  
  145.     /**
  146.      * Creates a subraster given a region of the raster. Returns a
  147.      * WritableRaster which references this WritableRaster's 
  148.      * DataBuffer.
  149.      * @param rect is a rectangle in this WritableRaster's coordinate 
  150.      * space.  An error is thrown if rect is not fully contained by this 
  151.      * WriteableRaster.
  152.      * @param rect  The rectangle specifying the dimensions of the subraster.
  153.      */
  154.     public WritableRaster createWritableSubRaster(Rectangle r) {
  155.         return createWritableSubRaster(r.x, r.y, 
  156.                                        r.width, r.height,
  157.                        r.x, r.y,
  158.                                        null);
  159.     }
  160.  
  161.     /**
  162.      * Returns a translated WritableRaster which references this 
  163.      * WriteableRaster's DataBuffer.  The parameters x,y, width and height 
  164.      * form a rectangle.  The new WritableRaster's coordinate system has 
  165.      * an upper left corner of x0, y0.  An error is thrown if rectangle is 
  166.      * not fully contained by this WritableRaster.
  167.      * @param x        X of the upper left corner in this Raster's coordinates.
  168.      * @param y        Y of the upper left corner in this Raster's coordinates.
  169.      * @param width    Width of the region starting at (x,y).
  170.      * @param height   Height of the region starting at (x,y).
  171.      * @param x0       X of the upper left corner of returned Raster.
  172.      * @param y0       Y of the upper left corner of returned Raster.
  173.      * @param bandList Array of band indices.
  174.      */
  175.     public WritableRaster createWritableSubRaster(int x, int y,
  176.                                                   int w, int h,
  177.                                                   int x0, int y0,
  178.                                                   int bandList[]) {
  179.         if (x < this.minX) {
  180.             throw new RasterFormatException("x lies outside raster");
  181.         }
  182.         if (y < this.minY) {
  183.             throw new RasterFormatException("y lies outside raster");
  184.         }
  185.         if (x+width > this.width + this.minX) {
  186.             throw new RasterFormatException("(x + width) is outside raster");
  187.         }
  188.         if (y+height > this.height + this.minY) {
  189.             throw new RasterFormatException("(y + height) is outside raster");
  190.         }
  191.  
  192.         SampleModel sm;
  193.  
  194.         if (bandList != null)
  195.             sm = sampleModel.createSubsetSampleModel(sampleModel.width,
  196.                                                       sampleModel.height,
  197.                                                       bandList);
  198.         else
  199.             sm = sampleModel;
  200.  
  201.         int deltaX = x0 - x;
  202.         int deltaY = y0 - y;
  203.  
  204.         return new WritableRaster(sm,
  205.                                    dataBuffer,
  206.                                    new Rectangle(x0,y0,width,height),
  207.                                    new Point(baseRasterOriginX+deltaX,
  208.                                              baseRasterOriginY+deltaY),
  209.                                    this);
  210.     }
  211.  
  212.     /**
  213.      * Stores the data elements for all channels at the specified location.
  214.      * There will be no explicit bounds checking on the parameters.  
  215.      * An ArrayOutOfBounds exception will be thrown at runtime
  216.      * if data outside of the array is accessed.
  217.      * A ClassCastException will be thrown if the input object is non null
  218.      * and references anything other than an array of transferType.
  219.      * @param x        The X coordinate of the pixel location.
  220.      * @param y        The Y coordinate of the pixel location.
  221.      * @param inData   An object reference to an array of type defined by
  222.      *                 getTransferType() and length getNumDataElements()
  223.      *                 containing the pixel data to place at x,y.
  224.      */
  225.     public void setPixelData(int x, int y, Object obj) {
  226.         sampleModel.setPixelData(x-baseRasterOriginX, y-baseRasterOriginY, 
  227.                                  obj, dataBuffer);
  228.     } 
  229.  
  230.     /**
  231.      * Stores the Raster data at the specified location.
  232.      * There will be no explicit bounds checking on the parameters.  
  233.      * An ArrayOutOfBounds exception will be thrown at runtime
  234.      * if data outside of the array is accessed.
  235.      * @param x        The X coordinate of the pixel location.
  236.      * @param y        The Y coordinate of the pixel location.
  237.      * @param inRaster   Raster of data to place at x,y location.
  238.      */
  239.     public void setPixelData(int x, int y, Raster inRaster) {
  240.         int width  = inRaster.getWidth();
  241.         int height = inRaster.getHeight();
  242.         int srcOffX = inRaster.getMinX();
  243.         int srcOffY = inRaster.getMinY();
  244.         int dstOffX = x+inRaster.getMinX();
  245.         int dstOffY = y+inRaster.getMinY();
  246.         Object tdata = null;
  247.  
  248.         for (int startY=0; startY < height; startY++) {
  249.             tdata = inRaster.getPixelData(srcOffX, srcOffY+startY, 
  250.                                           width, 1, tdata);
  251.             setPixelData(dstOffX, dstOffY+startY, 
  252.                          width, 1, tdata);
  253.         }
  254.     }
  255.  
  256.     /**
  257.      * Stores an array of data elements into the specified rectangular
  258.      * region.
  259.      * There will be no explicit bounds checking on the parameters.
  260.      * An ArrayOutOfBounds exception will be thrown at runtime
  261.      * if data outside of the array is accessed.
  262.      * A ClassCastException will be thrown if the input object is non null
  263.      * and references anything other than an array of transferType.
  264.      * @param x        The X coordinate of the upper left pixel location.
  265.      * @param y        The Y coordinate of the upper left pixel location.
  266.      * @param w        Width of the pixel rectangle.
  267.      * @param h        Height of the pixel rectangle.
  268.      * @param inData   An object reference to an array of type defined by
  269.      *                 getTransferType() and length w*h*getNumDataElements()
  270.      *                 containing the pixel data to place between x,y and
  271.      *                 x+h, y+h.
  272.      */
  273.     public void setPixelData(int x, int y, int w, int h, Object obj) {
  274.         sampleModel.setPixelData(x-baseRasterOriginX,y-baseRasterOriginY,
  275.                                  w,h,obj,dataBuffer);
  276.     }
  277.  
  278.     /**
  279.      * Copies pixels from Raster srcRaster to this WritableRaster.  Each pixel
  280.      * in srcRaster is copied to the same x,y address in this raster, unless 
  281.      * the address falls outside the bounds of this raster.
  282.      * @param srcRaster  The  Raster from which to copy pixels.
  283.      */
  284.     public void setRect(Raster srcRaster) {
  285.         setRect(0,0,srcRaster);
  286.     }
  287.  
  288.     /**
  289.      * Copies pixels from Raster srcRaster to this WritableRaster.  For 
  290.      * each a,b address in srcRaster, the corresponding pixel is copied 
  291.      * to address a+x, b+y in this raster, unless a+x,b+y falls outside
  292.      * the bounds of this raster.
  293.      * @param x          The X coordinate of the pixel location.
  294.      * @param y          The Y coordinate of the pixel location.
  295.      * @param srcRaster  The  Raster from which to copy pixels.
  296.      */
  297.     public void setRect(int x, int y, Raster srcRaster) {
  298.         int width  = srcRaster.getWidth();
  299.         int height = srcRaster.getHeight();
  300.         int[] tdata = null;
  301.         int srcOffX = srcRaster.getMinX();
  302.         int srcOffY = srcRaster.getMinY();
  303.         int dstOffX = x+srcRaster.getMinX();
  304.         int dstOffY = y+srcRaster.getMinY();
  305.  
  306.  
  307.         for (int startY=0; startY < height; startY++) {
  308.             // Grab one scanline at a time
  309.             tdata = srcRaster.getPixel(srcOffX, srcOffY+startY, 
  310.                                        width, 1, tdata);
  311.             setPixel(dstOffX, dstOffY+startY, 
  312.                      width, 1, tdata);
  313.         }
  314.     }
  315.  
  316.     /** 
  317.      * Sets a pixel in the DataBuffer using an int array for input.
  318.      * @param x      The X coordinate of the pixel location.
  319.      * @param y      The Y coordinate of the pixel location.
  320.      * @param iarray The input pixels in a int array.
  321.      */
  322.     public void setPixel(int x, int y, int iarray[]) {
  323.         sampleModel.setPixel(x-baseRasterOriginX,y-baseRasterOriginY,
  324.                              iarray,dataBuffer);
  325.     }
  326.  
  327.     /**
  328.      * Sets a pixel in the DataBuffer using a float array for input
  329.      * specified pixel.
  330.      * @param x      The X coordinate of the pixel location.
  331.      * @param y      The Y coordinate of the pixel location.
  332.      * @param farray The input pixels in a float array.
  333.      */
  334.     public void setPixel(int x, int y, float farray[]) {
  335.         sampleModel.setPixel(x-baseRasterOriginX,y-baseRasterOriginY,
  336.                  farray,dataBuffer);
  337.     }
  338.  
  339.     /**
  340.      * Sets a pixel in the DataBuffer using a double array for input
  341.      * specified pixel.
  342.      * @param x      The X coordinate of the pixel location.
  343.      * @param y      The Y coordinate of the pixel location.
  344.      * @param darray The input pixels in a double array.
  345.      */
  346.     public void setPixel(int x, int y, double darray[]) {
  347.         sampleModel.setPixel(x-baseRasterOriginX,y-baseRasterOriginY,
  348.                  darray,dataBuffer);
  349.     }
  350.  
  351.     /** 
  352.      * Sets a region of pixels using in input pixel array.
  353.      * @param x        The X coordinate of the upper left pixel location.
  354.      * @param y        The Y coordinate of the upper left pixel location.
  355.      * @param w        Width of the pixel rectangle.
  356.      * @param h        Height of the pixel rectangle.
  357.      * @param iarray   The input int pixel array.
  358.      */
  359.     public void setPixel(int x, int y, int w, int h, int iarray[]) {
  360.         sampleModel.setPixel(x-baseRasterOriginX,y-baseRasterOriginY,
  361.                  w,h,iarray,dataBuffer);
  362.     }
  363.  
  364.     /** 
  365.      * Sets region of pixels using in input pixel array.
  366.      * @param x        The X coordinate of the upper left pixel location.
  367.      * @param y        The Y coordinate of the upper left pixel location.
  368.      * @param w        Width of the pixel rectangle.
  369.      * @param h        Height of the pixel rectangle.
  370.      * @param farray   The input float pixel array.
  371.      */
  372.     public void setPixel(int x, int y, int w, int h, float farray[]) {
  373.         sampleModel.setPixel(x-baseRasterOriginX,y-baseRasterOriginY,
  374.                  w,h,farray,dataBuffer);
  375.     }
  376.  
  377.     /** 
  378.      * Sets region of pixels using in input pixel array.
  379.      * @param x        The X coordinate of the upper left pixel location.
  380.      * @param y        The Y coordinate of the upper left pixel location.
  381.      * @param w        Width of the pixel rectangle.
  382.      * @param h        Height of the pixel rectangle.
  383.      * @param darray   The input double pixel array.
  384.      */
  385.     public void setPixel(int x, int y, int w, int h, double darray[])
  386.     {
  387.         sampleModel.setPixel(x-baseRasterOriginX,y-baseRasterOriginY,
  388.                  w,h,darray,dataBuffer);
  389.     }
  390.  
  391.     /** 
  392.      * Sets a sample in the DataBuffer using a int for input.
  393.      * @param x        The X coordinate of the pixel location.
  394.      * @param y        The Y coordinate of the pixel location.
  395.      * @param b        The band to set.
  396.      * @param s        The input sample.
  397.      */
  398.     public void setSample(int x, int y, int b, int s) {
  399.         sampleModel.setSample(x-baseRasterOriginX, y-baseRasterOriginY, b, s, 
  400.                               dataBuffer);
  401.     }
  402.  
  403.     /** 
  404.      * Sets a sample in the DataBuffer using a float array for input.
  405.      * @param x        The X coordinate of the pixel location.
  406.      * @param y        The Y coordinate of the pixel location.
  407.      * @param b        The band to set.
  408.      * @param s        The input sample as a float.
  409.      */
  410.     public void setSample(int x, int y, int b, float s){
  411.         sampleModel.setSample(x-baseRasterOriginX,y-baseRasterOriginY,
  412.                   b,s,dataBuffer);
  413.     }
  414.  
  415.     /**
  416.      * Sets a sample in the DataBuffer using a double array for input.
  417.      * @param x        The X coordinate of the pixel location.
  418.      * @param y        The Y coordinate of the pixel location.
  419.      * @param b        The band to set.
  420.      * @param s        The input sample as a double.
  421.      */
  422.     public void setSample(int x, int y, int b, double s){
  423.         sampleModel.setSample(x-baseRasterOriginX,y-baseRasterOriginY,
  424.                                     b,s,dataBuffer);
  425.     }
  426.  
  427.     /** 
  428.      * Sets a region of samples using an input integer buffer as input.
  429.      * @param x        The X coordinate of the upper left pixel location.
  430.      * @param y        The Y coordinate of the upper left pixel location.
  431.      * @param w        Width of the pixel rectangle.
  432.      * @param h        Height of the pixel rectangle.
  433.      * @param b        The band to set.
  434.      * @param iarray   The input int sample array.
  435.      */
  436.     public void setSample(int x, int y, int w, int h, int b,
  437.                                int iarray[]) {
  438.         sampleModel.setSample(x-baseRasterOriginX,y-baseRasterOriginY,
  439.                   w,h,b,iarray,dataBuffer);
  440.     }
  441.  
  442.     /**
  443.      * Sets a region of samples using an input float buffer as input.
  444.      * @param x        The X coordinate of the upper left pixel location.
  445.      * @param y        The Y coordinate of the upper left pixel location.
  446.      * @param w        Width of the pixel rectangle.
  447.      * @param h        Height of the pixel rectangle.
  448.      * @param b        The band to set.
  449.      * @param farray   The input float sample array.
  450.      */
  451.     public void setSample(int x, int y, int w, int h, int b,
  452.                                     float farray[]) {
  453.         sampleModel.setSample(x-baseRasterOriginX,y-baseRasterOriginY,
  454.                   w,h,b,farray,dataBuffer);
  455.     }
  456.  
  457.     /**
  458.      * Sets a region of samples using an input double buffer as input.
  459.      * @param x        The X coordinate of the upper left pixel location.
  460.      * @param y        The Y coordinate of the upper left pixel location.
  461.      * @param w        Width of the pixel rectangle.
  462.      * @param h        Height of the pixel rectangle.
  463.      * @param b        The band to set.
  464.      * @param darray   The input double sample array.
  465.      */
  466.     public void setSample(int x, int y, int w, int h, int b,
  467.               double darray[]) {
  468.         sampleModel.setSample(x-baseRasterOriginX,y-baseRasterOriginY,
  469.                   w,h,b,darray,dataBuffer);
  470.     }
  471.  
  472. }
  473.