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 / Raster.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  35.8 KB  |  961 lines

  1. /*
  2.  * @(#)Raster.java    1.28 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.  
  26. package java.awt.image;
  27. import java.awt.Rectangle;
  28. import java.awt.Point;
  29.  
  30. import sun.awt.image.BytePackedRaster;
  31. import sun.awt.image.ShortComponentRaster;
  32. import sun.awt.image.ByteComponentRaster;
  33. import sun.awt.image.IntegerComponentRaster;
  34.  
  35.  
  36.  
  37. /** 
  38.   * This class represents a rectanglular array of pixels and provides 
  39.   * methods for retrieving image data.  It contains a DataBuffer object 
  40.   * that holds a buffer of image data in some format, a SampleModel 
  41.   * which describes the format is capable of storing and retrieving 
  42.   * Samples from the DataBuffer, and a Rect that defines the coordinate 
  43.   * space of the raster (upper left corner, width and height).  
  44.   *
  45.   * The Raster's coordinate space is determined by a point and a rectangle.
  46.   * The Point is the origin of the base Raster (this raster's ancestor
  47.   * that has no parent.)  The rectangle called the bounds determines the
  48.   * span over which this Raster can process pixel and sample requests.
  49.   * A point and a rectangle are needed so that sub rasters can use the 
  50.   * same DataBuffer and SampleModel as their parent rasters.  The point
  51.   * marks the origin of the SampleModel in the sub raster's coordinate
  52.   * space (possibly translated from the parent's coordinate space.)  The
  53.   * rectangle defines the span of the sub raster.  For base Rasters, the
  54.   * upper left corner of the bounds rectangle is equal to the origin.
  55.   *
  56.   * All constructors are protected.  The correct way to create a Raster
  57.   * is to use one of the static create methods defined in this class.
  58.   */
  59. public class Raster {
  60.  
  61.     /** SampleModel that describes how pixels are stored in the DataBuffer. */
  62.     protected SampleModel sampleModel; 
  63.  
  64.     /** DataBuffer that stores image data. */
  65.     protected DataBuffer dataBuffer;  
  66.  
  67.     /** The width of the Raster. */
  68.     protected int width; 
  69.  
  70.     /** The height of the Raster. */
  71.     protected int height;
  72.  
  73.     /** The X offset from the global origin. */
  74.     protected int minX;
  75.  
  76.     /** The Y offset from the global origin. */
  77.     protected int minY;
  78.  
  79.     /** The X offset of the base Raster from the global origin. */
  80.     protected int baseRasterOriginX;
  81.  
  82.     /** The Y offset of the base Raster from the global origin. */
  83.     protected int baseRasterOriginY;
  84.    
  85.     /** The number of bands in the raster. */ 
  86.     protected int numBands;
  87.  
  88.     /** The number of data elements per pixel. */
  89.     protected int numDataElements;
  90.  
  91.     /** If non-null, the parent of this Raster. */
  92.     protected Raster parent;
  93.     
  94.     static private native void initIDs();
  95.     static {
  96.         ColorModel.loadLibraries();
  97.         initIDs();
  98.     }
  99.  
  100.  
  101.     /**
  102.      * Creates a Raster based on a ComponentSampleModel with the specified
  103.      * data type, width, height, and number of bands.
  104.      */
  105.     public static WritableRaster createComponentRaster(int dataType, 
  106.                                                        int w, int h, 
  107.                                                        int n, 
  108.                                                        Point location) {
  109.         int[] bOffs = new int[n];
  110.         for (int i = 0; i < n; i++) {
  111.             bOffs[i] = i;
  112.         }
  113.         return createComponentRaster(dataType, w, h, w*n, n, bOffs, location);
  114.     }
  115.  
  116.     /**
  117.      * Creates a Raster based on a ComponentSampleModel with the specified
  118.      * data type, width, height, scanline stride, pixel stride, and band
  119.        offsets.
  120.      */
  121.     public static WritableRaster createComponentRaster(int dataType, 
  122.                                                        int w, int h,
  123.                                                        int scanlineStride, 
  124.                                                        int pixelStride,
  125.                                                        int bandOffsets[],
  126.                                                        Point location){
  127.     DataBuffer d;
  128.         int n = bandOffsets.length;
  129.  
  130.         switch(dataType) {
  131.         case DataBuffer.BYTE_DATA:
  132.             d = new DataBufferByte(w*h*n);
  133.             break;
  134.  
  135.         case DataBuffer.SHORT_DATA:
  136.             d = new DataBufferShort(w*h*n);
  137.             break;
  138.  
  139.         case DataBuffer.INT_DATA:
  140.             d = new DataBufferInt(w*h*n);
  141.             break;
  142.  
  143.         default:
  144.             throw new IllegalArgumentException("Unknown data type " +
  145.                                                 dataType);
  146.         }
  147.         return createComponentRaster(dataType, w, h, scanlineStride, 
  148.                                      pixelStride, bandOffsets, d, location);
  149.     }
  150.  
  151.     /**
  152.      * Creates a Raster based on a SinglePixelPackedSampleModel with the
  153.      * specified data type, width, height, and band masks.
  154.      */
  155.     public static WritableRaster createPackedRaster(int dataType, 
  156.                                                     int w, int h,
  157.                                                     int bandMasks[],
  158.                                                     Point location) {
  159.         DataBuffer d;
  160.  
  161.         switch(dataType) {
  162.         case DataBuffer.BYTE_DATA:
  163.             d = new DataBufferByte(w*h);
  164.             break;
  165.  
  166.         case DataBuffer.SHORT_DATA:
  167.             d = new DataBufferShort(w*h);
  168.             break;
  169.  
  170.         case DataBuffer.INT_DATA:
  171.             d = new DataBufferInt(w*h);
  172.             break;
  173.  
  174.         default:
  175.             throw new IllegalArgumentException("Unknown data type " +
  176.                                                 dataType);
  177.         }
  178.         return createPackedRaster(dataType, w, h, w, bandMasks, d, location);
  179.     }
  180.  
  181.     /**
  182.      * Creates a Raster based on a packed SampleModel with the specified
  183.      * data type, width, height, number of bands, and bits per band.
  184.      * If the number of bands is one, the SampleModel will be a
  185.      * MultiPixelPackedSampleModel.  If the number of bands is more
  186.      * than one, the SampleModel will be a SinglePixelPackedSampleModel,
  187.      * with each band having bitsPerBand bits.  In either case, the
  188.      * requirements on dataType and bitsPerBand imposed by the corresponding
  189.      * SampleModel must be met.
  190.      */
  191.     public static WritableRaster createPackedRaster(int dataType, 
  192.                                                     int w, int h, 
  193.                                                     int n, 
  194.                                                     int bitsPerBand,
  195.                                                     Point location) {
  196.         DataBuffer d;
  197.  
  198.         if (n != 1) {
  199.             int[] masks = new int[n];
  200.             int mask = (1 << bitsPerBand) - 1;
  201.             int shift;
  202.  
  203.             switch(dataType) {
  204.             case DataBuffer.BYTE_DATA:
  205.                 shift = 8 - n;
  206.                 break;
  207.             case DataBuffer.SHORT_DATA:
  208.                 shift = 16 - n;
  209.                 break;
  210.             case DataBuffer.INT_DATA:
  211.                 shift = 32 - n;
  212.                 break;
  213.             default:
  214.                 throw new IllegalArgumentException("Unknown data type " +
  215.                                                     dataType);
  216.             }
  217.  
  218.             for (int i = 0; i < n; i++) {
  219.                 masks[i] = mask << shift;
  220.                 shift = shift - n;
  221.             }
  222.  
  223.             return createPackedRaster(dataType, w, h, masks, location);
  224.         }
  225.  
  226.         double fw = w;
  227.         switch(dataType) {
  228.         case DataBuffer.BYTE_DATA:
  229.             d = new DataBufferByte((int)(Math.ceil(fw/(8/bitsPerBand)))*h);
  230.             break;
  231.  
  232.         case DataBuffer.SHORT_DATA:
  233.             d = new DataBufferShort((int)(Math.ceil(fw/(16/bitsPerBand)))*h);
  234.             break;
  235.  
  236.         case DataBuffer.INT_DATA:
  237.             d = new DataBufferInt((int)(Math.ceil(fw/(32/bitsPerBand)))*h);
  238.             break;
  239.  
  240.         default:
  241.             throw new IllegalArgumentException("Unknown data type " +
  242.                                                 dataType);
  243.         }
  244.         return createPackedRaster(dataType, w, h, bitsPerBand, d, location);
  245.     }
  246.  
  247.     /**
  248.      * Creates a Raster based on a ComponentSampleModel with the specified
  249.      * data type, width, height, scanline stride, pixel stride, band offsets,
  250.      * and data buffer.
  251.      */
  252.     public static WritableRaster createComponentRaster(int dataType, 
  253.                                                        int w, int h,
  254.                                                        int scanlineStride, 
  255.                                                        int pixelStride,
  256.                                                        int bandOffsets[],
  257.                                                        DataBuffer dataBuffer,
  258.                                                        Point location) {
  259.         ComponentSampleModel csm = new ComponentSampleModel(dataType, w, h,
  260.                                                             pixelStride,
  261.                                                             scanlineStride,
  262.                                                             bandOffsets);
  263.         if (location == null) {
  264.            location = new Point(0,0);
  265.         }
  266.  
  267.         switch(dataType) {
  268.         case DataBuffer.BYTE_DATA:
  269.             return new ByteComponentRaster(csm, dataBuffer, location);
  270.  
  271.         case DataBuffer.SHORT_DATA:
  272.             return new ShortComponentRaster(csm, dataBuffer, location);
  273.  
  274.         case DataBuffer.INT_DATA:
  275.             return new IntegerComponentRaster(csm, dataBuffer, location);
  276.  
  277.         default:
  278.             throw new IllegalArgumentException("Unknown data type " +
  279.                                                 dataType);
  280.         }
  281.     }
  282.  
  283.     /**
  284.      * Creates a Raster based on a SinglePixelPackedSampleModel with the
  285.      * specified data type, width, height, scanline stride, band masks,
  286.      * and data buffer.
  287.      */
  288.     public static WritableRaster createPackedRaster(int dataType, 
  289.                                                     int w, int h,
  290.                                                     int scanlineStride, 
  291.                                                     int bandMasks[],
  292.                                                     DataBuffer dataBuffer,
  293.                                                     Point location) {
  294.         SinglePixelPackedSampleModel sppsm =
  295.                 new SinglePixelPackedSampleModel(dataType, w, h, scanlineStride,
  296.                                                bandMasks);
  297.         if (location == null) {
  298.            location = new Point(0,0);
  299.         }
  300.  
  301.         switch(dataType) {
  302.         case DataBuffer.BYTE_DATA:
  303.             return new ByteComponentRaster(sppsm, dataBuffer, location);
  304.  
  305.         case DataBuffer.SHORT_DATA:
  306.             return new ShortComponentRaster(sppsm, dataBuffer, location);
  307.  
  308.         case DataBuffer.INT_DATA:
  309.             return new IntegerComponentRaster(sppsm, dataBuffer, location);
  310.  
  311.         default:
  312.             throw new IllegalArgumentException("Unknown data type " +
  313.                                                 dataType);
  314.         }
  315.     }
  316.    
  317.     /**
  318.      * Creates a Raster based on a MultiPixelPackedSampleModel with the
  319.      * specified data type, width, height, bits per pixel, and data buffer.
  320.      */
  321.     public static WritableRaster createPackedRaster(int dataType, 
  322.                                                     int w, int h,
  323.                                                     int bitsPerPixel, 
  324.                                                     DataBuffer dataBuffer,
  325.                                                     Point location) {
  326.         MultiPixelPackedSampleModel sbpsm =
  327.                 new MultiPixelPackedSampleModel(dataType, w, h, bitsPerPixel);
  328.  
  329.         if (location == null) {
  330.            location = new Point(0,0);
  331.         }
  332.  
  333.         switch(dataType) {
  334.         case DataBuffer.BYTE_DATA:
  335.             return new BytePackedRaster(sbpsm, dataBuffer, location);
  336.  
  337.         case DataBuffer.SHORT_DATA:
  338.             return new WritableRaster(sbpsm, dataBuffer, location);
  339.  
  340.         case DataBuffer.INT_DATA:
  341.             return new WritableRaster(sbpsm, dataBuffer, location);
  342.  
  343.         default:
  344.             throw new IllegalArgumentException("Unknown data type " +
  345.                                                 dataType);
  346.         }
  347.     }
  348.  
  349.  
  350.     /**
  351.      *  Creates a Raster with the specified SampleModel
  352.      */
  353.     public static Raster createRaster(SampleModel sm,
  354.                                       DataBuffer db,
  355.                                       Point location) {
  356.         if (location == null) {
  357.            location = new Point(0,0);
  358.         }
  359.         int dataType   = sm.getDataType();
  360.  
  361.         if (sm instanceof ComponentSampleModel) {
  362.             switch(dataType) {
  363.                 case DataBuffer.BYTE_DATA:
  364.                     return new ByteComponentRaster(sm, db, location);
  365.  
  366.                 case DataBuffer.SHORT_DATA:
  367.                     return new ShortComponentRaster(sm, db, location);
  368.             }
  369.         } else if (sm instanceof SinglePixelPackedSampleModel) {
  370.             switch(dataType) {
  371.                 case DataBuffer.BYTE_DATA:
  372.                     return new ByteComponentRaster(sm, db, location);
  373.  
  374.                 case DataBuffer.SHORT_DATA:
  375.                     return new ShortComponentRaster(sm, db, location);
  376.  
  377.                 case DataBuffer.INT_DATA:
  378.                     return new IntegerComponentRaster(sm, db, location);
  379.             }
  380.         } else if (sm instanceof MultiPixelPackedSampleModel &&
  381.                    dataType == DataBuffer.BYTE_DATA) {
  382.             return new BytePackedRaster(sm, db, location);
  383.         }
  384.  
  385.         // we couldn't do anything special - do the generic thing
  386.  
  387.         return new Raster(sm,db,location);
  388.     }
  389.  
  390.     /**
  391.      *  Creates a WritableRaster with the specified SampleModel
  392.      */
  393.     public static WritableRaster createWritableRaster(SampleModel sm,
  394.                                                       Point location) {
  395.         if (location == null) {
  396.            location = new Point(0,0);
  397.         }
  398.  
  399.         return createWritableRaster(sm,
  400.                                     sm.createCompatibleDataBuffer(),
  401.                                     location);
  402.     }
  403.  
  404.     /**
  405.      *  Creates a WritableRaster with the specified SampleModel
  406.      *  and DataBuffer
  407.      */
  408.     public static WritableRaster createWritableRaster(SampleModel sm,
  409.                                                       DataBuffer db,
  410.                                                       Point location) { 
  411.         if (location == null) {
  412.            location = new Point(0,0);
  413.         }
  414.  
  415.         int dataType   = sm.getDataType();
  416.  
  417.         if (sm instanceof ComponentSampleModel) {
  418.             switch(dataType) {
  419.                 case DataBuffer.BYTE_DATA:
  420.                     return new ByteComponentRaster(sm, db, location);
  421.  
  422.                 case DataBuffer.SHORT_DATA:
  423.                     return new ShortComponentRaster(sm, db, location);
  424.             }
  425.         } else if (sm instanceof SinglePixelPackedSampleModel) {
  426.             switch(dataType) {
  427.                 case DataBuffer.BYTE_DATA:
  428.                     return new ByteComponentRaster(sm, db, location);
  429.  
  430.                 case DataBuffer.SHORT_DATA:
  431.                     return new ShortComponentRaster(sm, db, location);
  432.  
  433.                 case DataBuffer.INT_DATA:
  434.                     return new IntegerComponentRaster(sm, db, location);
  435.             }
  436.         } else if (sm instanceof MultiPixelPackedSampleModel &&
  437.                    dataType == DataBuffer.BYTE_DATA) {
  438.             return new BytePackedRaster(sm, db, location);
  439.         }
  440.  
  441.         // we couldn't do anything special - do the generic thing
  442.  
  443.         return new WritableRaster(sm,db,location);
  444.     }
  445.  
  446.     /** 
  447.      *  Constructs a Raster with the given SampleModel.  The Raster's
  448.      *  upper left corner is origin and it is the same size as the
  449.      *  SampleModel.  A DataBuffer large enough to describe the 
  450.      *  Raster is automatically created.
  451.      *  @param sampleModel     The SampleModel that specifies the layout.
  452.      *  @param origin          The Point that specified the origin.
  453.      */
  454.     protected Raster(SampleModel sampleModel,
  455.                      Point origin) {
  456.     this(sampleModel,
  457.          sampleModel.createCompatibleDataBuffer(),
  458.          new Rectangle(origin.x,
  459.                            origin.y,
  460.                            sampleModel.getWidth(),
  461.                            sampleModel.getHeight()),
  462.              origin, 
  463.              null);
  464.     }
  465.    
  466.     /** 
  467.      *  Constructs a Raster with the given SampleModel and DataBuffer.
  468.      *  The Raster's upper left corner is origin and it is the same size 
  469.      *  as the SampleModel.  The DataBuffer is not initialized and must 
  470.      *  be compatible with SampleModel.
  471.      *  @param sampleModel     The SampleModel that specifies the layout.
  472.      *  @param dataBuffer      The DataBufferShort that contains the image data.
  473.      *  @param origin          The Point that specifies the origin.
  474.      */
  475.     protected Raster(SampleModel sampleModel,
  476.                      DataBuffer dataBuffer,
  477.                      Point origin) {
  478.     this(sampleModel, 
  479.              dataBuffer, 
  480.              new Rectangle(origin.x,
  481.                            origin.y,
  482.                            sampleModel.getWidth(),
  483.                            sampleModel.getHeight()),
  484.              origin, 
  485.              null);
  486.     }
  487.  
  488.     /**
  489.      * Constructs a Raster with the given SampleModel, DataBuffer, and
  490.      * parent.  When translated into the base Raster's coordinate
  491.      * system, aRegion must be contained by the base Raster.  Origin
  492.      * is the coodinate in the new Raster's coordinate system of the
  493.      * origin of the base Raster.  (The base Raster is the Raster's
  494.      * ancestor which has no parent.)
  495.      *
  496.      * Note that this constructor should generally be called by other 
  497.      * constructors or create methods, it should not be used directly.
  498.      * @param sampleModel     The SampleModel that specifies the layout.
  499.      * @param dataBuffer      The DataBufferShort that contains the image data.
  500.      * @param aRegion         The Rectangle that specifies the image area.     
  501.      * @param origin          The Point that specifies the origin.
  502.      * @param parent          The parent (if any) of this raster.
  503.      */
  504.     protected Raster(SampleModel sampleModel, 
  505.                      DataBuffer dataBuffer, 
  506.                      Rectangle aRegion,
  507.                      Point baseOrigin,
  508.                  Raster parent) {
  509.        this.sampleModel = sampleModel;
  510.        this.dataBuffer = dataBuffer;
  511.        minX = aRegion.x;
  512.        minY = aRegion.y;
  513.        width = aRegion.width;
  514.        height = aRegion.height;
  515.  
  516.        baseRasterOriginX = baseOrigin.x;
  517.        baseRasterOriginY = baseOrigin.y;
  518.  
  519.        numBands = sampleModel.getNumBands();
  520.        numDataElements = sampleModel.getNumDataElements();
  521.        this.parent = parent;
  522.     }
  523.  
  524.     
  525.     /** Returns the parent Raster (if any) of this Raster */
  526.     public Raster getParent() {
  527.         return parent;
  528.     }
  529.  
  530.     /**
  531.      * Returns the X coord of the origin of the base Raster in this Raster's
  532.      * coordinate system.
  533.      */
  534.     final public int getBaseRasterOriginX() {
  535.     return baseRasterOriginX;
  536.     }
  537.  
  538.     /**
  539.      * Returns the Y coord of the origin of the base Raster in this Raster's
  540.      * coordinate system.
  541.      */
  542.     final public int getBaseRasterOriginY() {
  543.     return baseRasterOriginY;
  544.     }
  545.  
  546.     /*
  547.      * Returns the X offset in pixels from the base Raster's upper left corner
  548.      * to this Raster's upper left corner.
  549.      */
  550.     final public int getBaseSubRasterOffsetX() {
  551.     return (minX-baseRasterOriginX);
  552.     }
  553.  
  554.     /*
  555.      * Returns the Y offset in pixels from the base Raster's upper left corner
  556.      * to this Raster's upper left corner.
  557.      */
  558.     final public int getBaseSubRasterOffsetY() {
  559.     return (minY-baseRasterOriginY);
  560.     }
  561.  
  562.     /**
  563.      * Create a compatible WritableRaster the same size as this Raster with
  564.      * the same SampleModel and a new initialized DataBuffer.
  565.      */
  566.     public WritableRaster createCompatibleWritableRaster() {
  567.         return createCompatibleWritableRaster(width,height);
  568.     }
  569.  
  570.     /**
  571.      * Create a compatible WritableRaster with the specified size, a new
  572.      * SampleModel, and a new initialized DataBuffer.
  573.      */
  574.     public WritableRaster createCompatibleWritableRaster(int w, 
  575.                                                          int h) {
  576.         if (w <= 0 || h <=0) {
  577.             throw new RasterFormatException("negative "+
  578.                                           ((w <= 0) ? "width" : "height"));
  579.         }
  580.  
  581.         SampleModel sm = sampleModel.createCompatibleSampleModel(w,h);
  582.  
  583.         return new WritableRaster(sm, new Point(0,0));
  584.     }
  585.  
  586.     /**
  587.      * Create a compatible WritableRaster with the specified size, a
  588.      * new SampleModel, and a new initialized DataBuffer. At the
  589.      * specified location.
  590.      */
  591.     public WritableRaster createCompatibleWritableRaster(Rectangle rect) {
  592.         return createCompatibleWritableRaster(rect.x, rect.y,
  593.                                               rect.width, rect.height);
  594.     }
  595.  
  596.     /**
  597.      * Create a compatible WritableRaster with the specified size, a
  598.      * new SampleModel, and a new initialized DataBuffer. At the
  599.      * specified location.
  600.      */
  601.     public WritableRaster createCompatibleWritableRaster(int x, int y,
  602.                                                          int w, int h) {
  603.         WritableRaster ret = createCompatibleWritableRaster(w, h);
  604.         return ret.createWritableSubRaster(0,0,w,h,x,y,null);
  605.     }
  606.  
  607.     /**
  608.      * Create a Raster with the same size, SampleModel and DataBuffer
  609.      * as this one, but with a different location.  
  610.      * @param location The upper left corner of the new returned Raster.
  611.      */
  612.     public Raster createTranslatedRaster(Point location) {
  613.         return createSubRaster(minX,minY,width,height,
  614.                                location.x,location.y,null);
  615.     }
  616.  
  617.  
  618.     /**
  619.      * Returns a Raster which references this Raster's DataBuffer 
  620.      * @param rect is a rectangle in this Raster's coordinate space.  An 
  621.      * error is thrown if rect is not fully contained by this Raster.
  622.      */
  623.     public Raster createSubRaster(Rectangle rect) {
  624.         return createSubRaster(rect.x, rect.y, 
  625.                                rect.width, rect.height,
  626.                                rect.x, rect.y,
  627.                                null);
  628.     }
  629.     
  630.     /**
  631.      * Returns a Raster which references this Raster's DataBuffer.
  632.      * x,y, width and height form a Rectangle in this Raster's
  633.      * coordinate space. An error is thrown if this rectangle
  634.      * is not fully contained by this Raster.
  635.      * @param x      The X coordinate of the upper left hand corner.
  636.      * @param y      The Y coordinate of the upper left hand corner.
  637.      * @param width  Width of the region starting at (x,y).
  638.      * @param height Height of the region starting at (x,y).
  639.      */
  640.     public Raster createSubRaster(int x, int y, int width, int height) {
  641.         return createSubRaster(x, y, width, height, x, y, null);
  642.     }
  643.  
  644.     /**
  645.      * Returns a translated SubRaster which references this Raster's 
  646.      * DataBuffer.  x,y, width and height form a Rectangle in this Raster's
  647.      * coordinate space.  The new Raster's coordinate system has an upper
  648.      * left corner of x0, y0. An error is thrown if rectangle is not 
  649.      * fully contained by this Raster.
  650.      * @param x          X of the upper left corner in this Raster's coordinates.
  651.      * @param y          Y of the upper left corner in this Raster's coordinates.
  652.      * @param width      Width of the region starting at (x,y).
  653.      * @param height     Height of the region starting at (x,y).
  654.      * @param x0         X of the upper left corner of returned Raster.
  655.      * @param y0         Y of the upper left corner of returned Raster.
  656.      * @param bandList   Array of band indices.
  657.      */
  658.     public Raster createSubRaster(int x, int y, int width,
  659.                                   int height,
  660.                   int x0, int y0,
  661.                                   int bandList[]) {
  662.         if (x < this.minX) {
  663.             throw new RasterFormatException("x lies outside raster");
  664.         }
  665.         if (y < this.minY) {
  666.             throw new RasterFormatException("y lies outside raster");
  667.         }
  668.         if (x+width > this.width + this.minX) {
  669.             throw new RasterFormatException("(x + width) is outside raster");
  670.         }
  671.         if (y+height > this.height + this.minY) {
  672.             throw new RasterFormatException("(y + height) is outside raster");
  673.         }
  674.  
  675.         SampleModel subSampleModel;
  676.         if (bandList == null) {
  677.             subSampleModel = sampleModel;
  678.         } else {
  679.             subSampleModel = 
  680.                 sampleModel.createSubsetSampleModel(width, height, bandList);
  681.         }
  682.  
  683.         int deltaX = x0 - x;
  684.         int deltaY = y0 - y;
  685.                         
  686.     return new Raster(subSampleModel, dataBuffer,
  687.               new Rectangle(x0, y0, width, height),
  688.               new Point(baseRasterOriginX+deltaX,
  689.                     baseRasterOriginY+deltaY), this);
  690.     }
  691.  
  692.     /** 
  693.      * Returns the bounds Rectangle of this Raster. This function returns 
  694.      * the same information as getMinX/MinY/Width/Height.
  695.      */
  696.     public Rectangle  getBounds(){
  697.         return new Rectangle(minX, minY, width, height);
  698.     }
  699.   
  700.     /** Returns the width in pixels of the Raster.  */
  701.     final public int  getWidth(){
  702.         return width;
  703.     }
  704.     
  705.     /** Returns the height in pixels of the Raster.  */
  706.     final public int getHeight(){
  707.         return height;
  708.     }
  709.     
  710.     /** Returns the minimum X coordinate (in the Raster's coordinate system). */
  711.     final public int getMinX() {
  712.         return minX;
  713.     }
  714.   
  715.     /** Returns the minimum Y coordinate (in the Raster's coordinate system). */
  716.     final public int getMinY() {
  717.         return minY;
  718.     }
  719.  
  720.     /** Returns the number of bands in this raster. */
  721.     final public int getNumBands() {
  722.         return numBands;
  723.     }
  724.  
  725.     /** Returns the number of data elements needed to store one pixel. */
  726.     final public int getNumDataElements() {
  727.         return sampleModel.getNumDataElements();
  728.     }
  729.  
  730.     final public int getTransferType() {
  731.         return sampleModel.getTransferType();
  732.     }
  733.  
  734.     /**
  735.      * Returns the data elements containing the pixel at x,y in packed 
  736.      * format.   
  737.      * There will be no explicit bounds checking on the parameters.
  738.      * An ArrayOutOfBounds exception will be thrown at runtime
  739.      * if data outside of the array is accessed.
  740.      * A ClassCastException will be thrown if the input object is non null
  741.      * and references anything other than an array of transferType.
  742.      * @param x        The X coordinate of the pixel location.
  743.      * @param y        The Y coordinate of the pixel location.
  744.      * @param outData  An object reference to an array of type defined by
  745.      *                 getTransferType() and length getNumDataElements().  
  746.      *                 If null an array of appropriate type and size will be
  747.      *                 allocated.
  748.      * @return         An object reference to an array of type defined by
  749.      *                 getTransferType() with the request pixel data.
  750.      */
  751.     public Object getPixelData(int x, int y, Object obj) {
  752.         return sampleModel.getPixelData(x-baseRasterOriginX, 
  753.                                         y-baseRasterOriginY, obj, dataBuffer);
  754.     }
  755.  
  756.     /**
  757.      * Returns the data elements containing the pixel data for the pixels
  758.      * in the specified region.
  759.      * There will be no explicit bounds checking on the parameters.
  760.      * An ArrayOutOfBounds exception will be thrown at runtime
  761.      * if data outside of the array is accessed.
  762.      * A ClassCastException will be thrown if the input object is non null
  763.      * and references anything other than an array of transferType.
  764.      * @param x        The X coordinate of the upper left pixel location.
  765.      * @param y        The Y coordinate of the upper left pixel location.
  766.      * @param width    Width of the pixel rectangle.
  767.      * @param height   Height of the pixel rectangle.
  768.      * @param outData  An object reference to an array of type defined by
  769.      *                 getTransferType() and length w*h*getNumDataElements().  
  770.      *                 If null an array of appropriate type and size will be
  771.      *                 allocated.
  772.      * @return         An object reference to an array of type defined by
  773.      *                 getTransferType() with the request pixel data.
  774.      */
  775.     public Object getPixelData(int x, int y, int w, int h, Object obj) {
  776.         return sampleModel.getPixelData(x-baseRasterOriginX, 
  777.                                         y-baseRasterOriginY, 
  778.                                         w, h, obj, dataBuffer);
  779.     }
  780.  
  781.     /** 
  782.      * Returns the samples in an array of int for the specified pixel.
  783.      * @param x    The X coordinate of the pixel location. 
  784.      * @param y    The Y coordinate of the pixel location.
  785.      * @param iarray An optionally preallocated int array. 
  786.      */
  787.     public int [] getPixel(int x, int y, int iarray[]) {
  788.         return sampleModel.getPixel(x-baseRasterOriginX,
  789.                     y-baseRasterOriginY, iarray,
  790.                     dataBuffer);
  791.     }
  792.   
  793.     /**
  794.      * Returns the samples in an array of Float for the 
  795.      * specified pixel.
  796.      * @param x    The X coordinate of the pixel location.
  797.      * @param y    The Y coordinate of the pixel location.
  798.      * @param farray An optionally preallocated float array.
  799.      */
  800.     public float [] getPixel(int x, int y, float farray[]) {
  801.         return sampleModel.getPixel(x-baseRasterOriginX,
  802.                     y-baseRasterOriginY,
  803.                     farray, dataBuffer);
  804.     }
  805.   
  806.     /** 
  807.      * Returns the samples in an array of double for the specified pixel
  808.      * @param x      The X coordinate of the pixel location.
  809.      * @param y      The Y coordinate of the pixel location.
  810.      * @param darray An optionally preallocated double array.
  811.      */
  812.     public double [] getPixel(int x, int y, double darray[]) {
  813.         return sampleModel.getPixel(x-baseRasterOriginX,
  814.                     y-baseRasterOriginY,
  815.                     darray, dataBuffer);
  816.     }
  817.   
  818.     /** 
  819.      * Returns a integer array of pixels representing a region of pixels
  820.      * @param x      The X coordinate of the upper left pixel location.
  821.      * @param y      The Y coordinate of the upper left pixel location.
  822.      * @param w      Width of the pixel rectangle.
  823.      * @param h      Height of the pixel rectangle.
  824.      * @param iarray An optionally pre-allocated int array.
  825.      */
  826.     public int [] getPixel(int x, int y, int w, int h, int iarray[]) {
  827.         return sampleModel.getPixel(x-baseRasterOriginX,
  828.                     y-baseRasterOriginY, w, h,
  829.                     iarray, dataBuffer);
  830.     }
  831.   
  832.     /**
  833.      * Return a float array of pixels representing a region of pixels
  834.      * @param x        The X coordinate of the upper left pixel location.
  835.      * @param y        The Y coordinate of the upper left pixel location.
  836.      * @param w        Width of the pixel rectangle.
  837.      * @param h        Height of the pixel rectangle.
  838.      * @param farray   An optionally pre-allocated float array.
  839.      */
  840.     public float [] getPixel(int x, int y, int w, int h, 
  841.                  float farray[]){
  842.         return sampleModel.getPixel(x-baseRasterOriginX,
  843.                     y-baseRasterOriginY, w, h,
  844.                     farray, dataBuffer);
  845.     }
  846.   
  847.     /**
  848.      * Return a double array of pixels representing a region of pixels
  849.      * @param x        The X coordinate of the upper left pixel location.
  850.      * @param y        The Y coordinate of the upper left pixel location.
  851.      * @param w        Width of the pixel rectangle.
  852.      * @param h        Height of the pixel rectangle.
  853.      * @param darray   An optionally pre-allocated double array. 
  854.      */
  855.     public double [] getPixel(int x, int y, int w, int h, 
  856.                                          double darray[]){
  857.         return sampleModel.getPixel(x-baseRasterOriginX,
  858.                     y-baseRasterOriginY,
  859.                     w, h, darray, dataBuffer);
  860.     }
  861.   
  862.   
  863.     /** 
  864.      * Returns the requested sample in a specified band for a pixel.
  865.      * @param x        The X coordinate of the pixel location.
  866.      * @param y        The Y coordinate of the pixel location.
  867.      * @param b        The band to return.
  868.      */
  869.     public int getSample(int x, int y, int b) {
  870.         return sampleModel.getSample(x-baseRasterOriginX,
  871.                      y-baseRasterOriginY, b,
  872.                      dataBuffer);
  873.     }
  874.     
  875.     /** 
  876.      * Returns the requested sample in a specified band for a pixel.
  877.      * @param x        The X coordinate of the pixel location.
  878.      * @param y        The Y coordinate of the pixel location.
  879.      * @param b        The band to return.
  880.      */
  881.     public float getSampleFloat(int x, int y, int b) {
  882.         return sampleModel.getSampleFloat(x-baseRasterOriginX,
  883.                       y-baseRasterOriginY, b,
  884.                       dataBuffer);
  885.     }
  886.   
  887.     /** 
  888.      * Returns the requested sample in a specified band for a pixel.
  889.      * @param x        The X coordinate of the pixel location.
  890.      * @param y        The Y coordinate of the pixel location.
  891.      * @param b        The band to return.
  892.      */
  893.     public double getSampleDouble(int x, int y, int b) {
  894.         return sampleModel.getSampleDouble(x-baseRasterOriginX,
  895.                        y-baseRasterOriginY,
  896.                        b, dataBuffer);
  897.     }
  898.   
  899.     /** 
  900.      * Returns an array of ints representing a region of samples.
  901.      * @param x        The X coordinate of the upper left pixel location.
  902.      * @param y        The Y coordinate of the upper left pixel location.
  903.      * @param w        Width of the pixel rectangle.
  904.      * @param h        Height of the pixel rectangle.
  905.      * @param b        The band to return.
  906.      * @param iarray   An optionally pre-allocated int array.
  907.      */
  908.     public int [] getSample(int x, int y, int w, int h, int b,
  909.                 int iarray[]) {
  910.         return sampleModel.getSample(x-baseRasterOriginX,
  911.                      y-baseRasterOriginY,
  912.                      w, h, b, iarray,
  913.                      dataBuffer);
  914.     }
  915.   
  916.     /**
  917.      * Returns an array of floats representing a region of samples.
  918.      * @param x        The X coordinate of the upper left pixel location.
  919.      * @param y        The Y coordinate of the upper left pixel location.
  920.      * @param w        Width of the pixel rectangle.
  921.      * @param h        Height of the pixel rectangle.
  922.      * @param b        The band to return.
  923.      * @param farray   An optionally pre-allocated float array.
  924.      */
  925.     public float [] getSample(int x, int y, int w, int h, int b,
  926.                   float farray[]) {
  927.         return sampleModel.getSample(x-baseRasterOriginX,
  928.                      y-baseRasterOriginY,
  929.                      w, h, b, farray, dataBuffer);
  930.     }
  931.   
  932.     /**
  933.      * Return an array of ints representing a region of samples
  934.      * @param x        The X coordinate of the upper left pixel location.
  935.      * @param y        The Y coordinate of the upper left pixel location.
  936.      * @param w        Width of the pixel rectangle.
  937.      * @param h        Height of the pixel rectangle.
  938.      * @param b        The band to return.
  939.      * @param darray   An optionally pre-allocated double array.
  940.      */
  941.     public double [] getSample(int x, int y, int w, int h, int b,
  942.                    double darray[]) {
  943.          return sampleModel.getSample(x-baseRasterOriginX,
  944.                       y-baseRasterOriginY,
  945.                       w, h, b, darray, dataBuffer);
  946.     }
  947.   
  948.     /** Returns the DataBuffer associated with this Raster. */
  949.     public DataBuffer getDataBuffer(){
  950.         return dataBuffer;
  951.     }
  952.   
  953.     /** Returns the SampleModel that describes that layout of the image data. */
  954.     public SampleModel getSampleModel(){
  955.         return sampleModel;
  956.     }
  957. }
  958.  
  959.  
  960.  
  961.