RasterFactory.java
上传用户:btjssb159
上传日期:2018-01-04
资源大小:241k
文件大小:54k
源码类别:

DNA

开发平台:

Java

  1. /*
  2.  * Copyright (c) 2001 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without 
  5.  * modification, are permitted provided that the following conditions are met:
  6.  * 
  7.  * -Redistributions of source code must retain the above copyright notice, this 
  8.  * list of conditions and the following disclaimer.
  9.  *
  10.  * -Redistribution in binary form must reproduct the above copyright notice,
  11.  * this list of conditions and the following disclaimer in the documentation
  12.  * and/or other materials provided with the distribution.
  13.  * 
  14.  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
  15.  * be used to endorse or promote products derived from this software without
  16.  * specific prior written permission.
  17.  * 
  18.  * This software is provided "AS IS," without a warranty of any kind. ALL
  19.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  20.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  21.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  22.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  23.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  24.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  25.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  26.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  27.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  28.  * POSSIBILITY OF SUCH DAMAGES.
  29.  * 
  30.  * You acknowledge that Software is not designed,licensed or intended for use in 
  31.  * the design, construction, operation or maintenance of any nuclear facility.
  32.  */
  33. import java.awt.Point;
  34. import java.awt.Rectangle;
  35. import java.awt.Transparency;
  36. import java.awt.color.ColorSpace;
  37. import java.awt.image.ComponentColorModel;
  38. import java.awt.image.ComponentSampleModel;
  39. import java.awt.image.DataBuffer;
  40. import java.awt.image.DataBufferByte;
  41. import java.awt.image.DataBufferShort;
  42. import java.awt.image.DataBufferInt;
  43. import java.awt.image.DataBufferUShort;
  44. import java.awt.image.BandedSampleModel;
  45. import java.awt.image.PixelInterleavedSampleModel;
  46. import java.awt.image.Raster;
  47. import java.awt.image.RasterFormatException;
  48. import java.awt.image.SampleModel;
  49. import java.awt.image.WritableRaster;
  50. class WritableRasterJAI extends WritableRaster {
  51.     protected WritableRasterJAI(SampleModel sampleModel,
  52.                                 DataBuffer dataBuffer,
  53.                                 Rectangle aRegion,
  54.                                 Point sampleModelTranslate,
  55.                                 WritableRaster parent){
  56.         super(sampleModel, dataBuffer, aRegion,
  57.               sampleModelTranslate, parent);
  58.     }
  59. }
  60. /**
  61.  * A convenience class for the construction of various types of
  62.  * <code>WritableRaster</code> and <code>SampleModel</code> objects.
  63.  *
  64.  * <p> This class provides the capability of creating
  65.  * <code>Raster</code>s with the enumerated data types in the
  66.  * java.awt.image.DataBuffer.
  67.  *
  68.  * <p> In some cases, instances of
  69.  * <code>ComponentSampleModelJAI</code>, a subclass of
  70.  * <code>java.awt.image.ComponentSampleModel</code> are instantiated
  71.  * instead of <code>java.awt.image.BandedSampleModel</code> in order
  72.  * to work around bugs in the current release of the Java 2 SDK.
  73.  */
  74. public class RasterFactory {
  75.     /**
  76.      * Creates a <code>WritableRaster</code> based on a
  77.      * <code>PixelInterleavedSampleModel</code> with the specified
  78.      * data type, width, height, and number of bands.
  79.      *
  80.      * <p> The upper left corner of the <code>WritableRaster</code> is
  81.      * given by the <code>location</code> argument.  If
  82.      * <code>location</code> is <code>null</code>, (0, 0) will be
  83.      * used.  The <code>dataType</code> parameter should be one of the
  84.      * enumerated values defined in the <code>DataBuffer</code> class.
  85.      *
  86.      * @param dataType The data type of the <code>SampleModel</code>,
  87.      *        one of <code>DataBuffer.TYPE_BYTE</code>,
  88.      *        <code>TYPE_USHORT</code>,
  89.      *        <code>TYPE_SHORT</code>,
  90.      *        <code>TYPE_INT</code>,
  91.      *        <code>TYPE_FLOAT</code>, or
  92.      *        <code>TYPE_DOUBLE</code>.
  93.      * @param width The desired width of the <code>WritableRaster</code>.
  94.      * @param height The desired height of the <code>WritableRaster</code>.
  95.      * @param numBands The desired number of bands.
  96.      * @param location A <code>Point</code> indicating the starting
  97.      *        coordinates of the <code>WritableRaster</code>.
  98.      *
  99.      * @throws IllegalArgumentException if <code>numbands</code> is
  100.      *         <code><1</code>.
  101.      */
  102.     public static WritableRaster createInterleavedRaster(int dataType,
  103.                                                          int width, int height,
  104.                                                          int numBands,
  105.                                                          Point location) {
  106.         if (numBands < 1) {
  107.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory0"));
  108.         }
  109.         int[] bandOffsets = new int[numBands];
  110.         for (int i = 0; i < numBands; i++) {
  111.             bandOffsets[i] = numBands - 1 - i;
  112.         }
  113.         return createInterleavedRaster(dataType, width, height,
  114.                                        width*numBands, numBands,
  115.                                        bandOffsets, location);
  116.     }
  117.     /**
  118.      * Creates a <code>WritableRaster</code> based on a
  119.      * <code>PixelInterleavedSampleModel</code> with the specified
  120.      * data type, width, height, scanline stride, pixel stride, and
  121.      * band offsets.  The number of bands is inferred from
  122.      * bandOffsets.length.
  123.      *
  124.      * <p> The upper left corner of the <code>WritableRaster</code> is
  125.      * given by the <code>location</code> argument.  If
  126.      * <code>location</code> is <code>null</code>, (0, 0) will be
  127.      * used.  The <code>dataType</code> parameter should be one of the
  128.      * enumerated values defined in the <code>DataBuffer</code> class.
  129.      *
  130.      * @param dataType The data type of the <code>WritableRaster</code>,
  131.      *        one of the enumerated dataType values in 
  132.      *        java.awt.image.DataBuffer.
  133.      * @param width The desired width of the <code>WritableRaster</code>.
  134.      * @param height The desired height of the <code>WritableRaster</code>.
  135.      * @param scanlineStride The desired scanline stride.
  136.      * @param pixelStride The desired pixel stride.
  137.      * @param bandOffsets An array of <code>int</code>s indicating the
  138.      *        relative offsets of the bands within a pixel.
  139.      * @param location A <code>Point</code> indicating the starting
  140.      *        coordinates of the <code>WritableRaster</code>.
  141.      *
  142.      * @throws IllegalArgumentException if <code>bandOffsets</code> is
  143.      *         <code>null</code>, <code>dataType</code> is not one of
  144.      *         the enumerated dataType value of java.awt.image.DataBuffer.
  145.      *
  146.      * @throws IllegalArgumentException if the number of array elements
  147.      *         required by the returned <code>WritableRaster</code>
  148.      *         would exceed <code>Integer.MAX_VALUE</code>.
  149.      */
  150.     public static WritableRaster createInterleavedRaster(int dataType,
  151.                                                          int width, int height,
  152.                                                          int scanlineStride,
  153.                                                          int pixelStride,
  154.                                                          int bandOffsets[],
  155.                                                          Point location) {
  156.         if (bandOffsets == null) {
  157.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory4"));
  158.         }
  159. DataBuffer d;
  160.         int bands = bandOffsets.length;
  161.         int maxBandOff = bandOffsets[0];
  162.         for (int i=1; i < bands; i++) {
  163.             if (bandOffsets[i] > maxBandOff) {
  164.                 maxBandOff = bandOffsets[i];
  165.             }
  166.         }
  167.         long lsize = (long)maxBandOff +
  168.             (long)scanlineStride*(height - 1) + (long)pixelStride*(width - 1) +
  169.             1L;
  170.         if (lsize > (long)Integer.MAX_VALUE) {
  171.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory16"));
  172.         }
  173.         int size = (int)lsize;
  174.         switch(dataType) {
  175.         case DataBuffer.TYPE_BYTE:
  176.             d = new DataBufferByte(size);
  177.             break;
  178.         case DataBuffer.TYPE_USHORT:
  179.             d = new DataBufferUShort(size);
  180.             break;
  181.         case DataBuffer.TYPE_SHORT:
  182.             d = new DataBufferShort(size);
  183.             break;
  184.         case DataBuffer.TYPE_INT:
  185.             d = new DataBufferInt(size);
  186.             break;
  187.         case DataBuffer.TYPE_FLOAT:
  188.             d = new DataBufferFloat(size);
  189.             break;
  190.         case DataBuffer.TYPE_DOUBLE:
  191.             d = new DataBufferDouble(size);
  192.             break;
  193.         default:
  194.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory3"));
  195.         }
  196.         return createInterleavedRaster(d, width, height, scanlineStride,
  197.                                        pixelStride, bandOffsets, location);
  198.     }
  199.     /**
  200.      * Creates a <code>WritableRaster</code> based on a
  201.      * <code>ComponentSampleModel</code> with the specified data type,
  202.      * width, height, and number of bands.
  203.      *
  204.      * <p> Note that the <code>Raster</code>'s
  205.      * <code>SampleModel</code> will be of type
  206.      * <code>ComponentSampleModel</code>, not
  207.      * <code>BandedSampleModel</code> as might be expected.
  208.      *
  209.      * <p> The upper left corner of the <code>WritableRaster</code> is
  210.      * given by the <code>location</code> argument.  If
  211.      * <code>location</code> is <code>null</code>, (0, 0) will be
  212.      * used.  The <code>dataType</code> parameter should be one of the
  213.      * enumerated values defined in the <code>DataBuffer</code> class.
  214.      *
  215.      * @param dataType The data type of the <code>WritableRaster</code>,
  216.      *        one of the enumerated dataType values in 
  217.      *        java.awt.image.DataBuffer.
  218.      * @param width The desired width of the <code>WritableRaster</code>.
  219.      * @param height The desired height of the <code>WritableRaster</code>.
  220.      * @param bands The desired number of bands.
  221.      * @param location A <code>Point</code> indicating the starting
  222.      *        coordinates of the <code>WritableRaster</code>.
  223.      *
  224.      * @throws IllegalArgumentException if <code>bands</code> is
  225.      *         <code><1</code>.
  226.      */
  227.     public static WritableRaster createBandedRaster(int dataType,
  228.                                                     int width, int height,
  229.                                                     int bands,
  230.                                                     Point location) {
  231.         if (bands < 1) {
  232.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory0"));
  233.         }
  234.         int[] bankIndices = new int[bands];
  235.         int[] bandOffsets = new int[bands];
  236.         for (int i = 0; i < bands; i++) {
  237.             bankIndices[i] = i;
  238.             bandOffsets[i] = 0;
  239.         }
  240.         return createBandedRaster(dataType, width, height, width,
  241.                                   bankIndices, bandOffsets,
  242.                                   location);
  243.     }
  244.     /**
  245.      * Creates a <code>WritableRaster</code> based on a
  246.      * <code>ComponentSampleModel</code> with the specified data type,
  247.      * width, height, scanline stride, bank indices and band offsets.
  248.      * The number of bands is inferred from
  249.      * <code>bankIndices.length</code> and
  250.      * <code>bandOffsets.length</code>, which must be the same.
  251.      *
  252.      * <p> Note that the <code>Raster</code>'s
  253.      * <code>SampleModel</code> will be of type
  254.      * <code>ComponentSampleModel</code>, not
  255.      * <code>BandedSampleModel</code> as might be expected.
  256.      *
  257.      * <p> The upper left corner of the <code>WritableRaster</code> is
  258.      * given by the <code>location</code> argument.  The
  259.      * <code>dataType</code> parameter should be one of the enumerated
  260.      * values defined in the <code>DataBuffer</code> class.
  261.      *
  262.      * @param dataType The data type of the <code>WritableRaster</code>,
  263.      *        one of the enumerated dataType values in 
  264.      *        java.awt.image.DataBuffer.
  265.      * @param width The desired width of the <code>WritableRaster</code>.
  266.      * @param height The desired height of the <code>WritableRaster</code>.
  267.      * @param scanlineStride The desired scanline stride.
  268.      * @param bankIndices An array of <code>int</code>s indicating the
  269.      *        bank index for each band.
  270.      * @param bandOffsets An array of <code>int</code>s indicating the
  271.      *        relative offsets of the bands within a pixel.
  272.      * @param location A <code>Point</code> indicating the starting
  273.      *        coordinates of the <code>WritableRaster</code>.
  274.      *
  275.      * @throws IllegalArgumentException if <code>bankIndices</code> is
  276.      *         <code>null</code>, <code>bandOffsets</code> is
  277.      *         <code>null</code>, if <code>bandOffsets.length</code>
  278.      *         is <code>!=</code> <code>bankIndices.length</code>,
  279.      *         if <code>dataType</code> is not one of the enumerated
  280.      *         datatypes of java.awt.image.DataBuffer.
  281.      */
  282.     public static WritableRaster createBandedRaster(int dataType,
  283.                                                     int width, int height,
  284.                                                     int scanlineStride,
  285.                                                     int bankIndices[],
  286.                                                     int bandOffsets[],
  287.                                                     Point location) {
  288. DataBuffer d;
  289.         int bands = bandOffsets.length;
  290.         if (bankIndices == null) {
  291.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory1"));
  292.         }
  293.         if (bandOffsets == null) {
  294.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory4"));
  295.         }
  296.         if (bandOffsets.length != bankIndices.length) {
  297.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory2"));
  298.         }
  299.         // Figure out the #banks and the largest band offset
  300.         int maxBank = bankIndices[0];
  301.         int maxBandOff = bandOffsets[0];
  302.         for (int i = 1; i < bands; i++) {
  303.             if (bankIndices[i] > maxBank) {
  304.                 maxBank = bankIndices[i];
  305.             }
  306.             if (bandOffsets[i] > maxBandOff) {
  307.                 maxBandOff = bandOffsets[i];
  308.             }
  309.         }
  310.         int banks = maxBank + 1;
  311.         long lsize = (long)maxBandOff + (long)scanlineStride*(height - 1) +
  312.             (long)(width - 1) + 1L;
  313.         if (lsize > (long)Integer.MAX_VALUE) {
  314.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory16"));
  315.         }
  316.         int size = (int)lsize;
  317.         switch(dataType) {
  318.         case DataBuffer.TYPE_BYTE:
  319.             d = new DataBufferByte(size, banks);
  320.             break;
  321.         case DataBuffer.TYPE_USHORT:
  322.             d = new DataBufferUShort(size, banks);
  323.             break;
  324.         case DataBuffer.TYPE_SHORT:
  325.             d = new DataBufferShort(size, banks);
  326.             break;
  327.         case DataBuffer.TYPE_INT:
  328.             d = new DataBufferInt(size, banks);
  329.             break;
  330.         case DataBuffer.TYPE_FLOAT:
  331.             d = new DataBufferFloat(size, banks);
  332.             break;
  333.         case DataBuffer.TYPE_DOUBLE:
  334.             d = new DataBufferDouble(size, banks);
  335.             break;
  336.         default:
  337.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory3"));
  338.         }
  339.         return createBandedRaster(d, width, height, scanlineStride,
  340.                                   bankIndices, bandOffsets, location);
  341.     }
  342.     /**
  343.      * Creates a <code>WritableRaster</code> based on a
  344.      * <code>SinglePixelPackedSampleModel</code> with the specified
  345.      * data type, width, height, and band masks.  The number of bands
  346.      * is inferred from <code>bandMasks.length</code>.
  347.      * 
  348.      * <p> The upper left corner of the <code>WritableRaster</code> is
  349.      * given by the <code>location</code> argument.  If
  350.      * <code>location</code> is <code>null</code>, (0, 0) will be
  351.      * used.  The <code>dataType</code> parameter should be one of the
  352.      * enumerated values defined in the <code>DataBuffer</code> class.
  353.      *
  354.      * @param dataType The data type of the <code>WritableRaster</code>,
  355.      *        one of <code>DataBuffer.TYPE_BYTE</code>,
  356.      *        <code>TYPE_USHORT</code> or <code>TYPE_INT</code>.
  357.      * @param width The desired width of the <code>WritableRaster</code>.
  358.      * @param height The desired height of the <code>WritableRaster</code>.
  359.      * @param location A <code>Point</code> indicating the starting
  360.      *        coordinates of the <code>WritableRaster</code>.
  361.      *
  362.      * @throws IllegalArgumentException is thrown if
  363.      *         the <code>dataType</code> is not of either TYPE_BYTE
  364.      *         or TYPE_USHORT or TYPE_INT.
  365.      */
  366.     public static WritableRaster createPackedRaster(int dataType,
  367.                                                     int width, int height,
  368.                                                     int bandMasks[],
  369.                                                     Point location) {
  370.         return Raster.createPackedRaster(dataType,
  371.                                          width, height, bandMasks, location);
  372.     }
  373.     /**
  374.      * Creates a <code>WritableRaster</code> based on a packed
  375.      * <code>SampleModel</code> with the specified data type, width,
  376.      * height, number of bands, and bits per band.  If the number of
  377.      * bands is one, the <code>SampleModel</code> will be a
  378.      * <code>MultiPixelPackedSampleModel</code>.
  379.      *
  380.      * <p> If the number of bands is more than one, the
  381.      * <code>SampleModel</code> will be a
  382.      * <code>SinglePixelPackedSampleModel</code>, with each band
  383.      * having <code>bitsPerBand</code> bits.  In either case, the
  384.      * requirements on <code>dataType</code> and
  385.      * <code>bitsPerBand</code> imposed by the corresponding
  386.      * <code>SampleModel</code> must be met.
  387.      *
  388.      * <p> The upper left corner of the <code>WritableRaster</code> is
  389.      * given by the <code>location</code> argument.  If
  390.      * <code>location</code> is <code>null</code>, (0, 0) will be
  391.      * used.  The <code>dataType</code> parameter should be one of the
  392.      * enumerated values defined in the <code>DataBuffer</code> class.
  393.      *
  394.      * @param dataType The data type of the <code>WritableRaster</code>,
  395.      *        one of <code>DataBuffer.TYPE_BYTE</code>,
  396.      *        <code>TYPE_USHORT</code> or <code>TYPE_INT</code>.
  397.      * @param width The desired width of the <code>WritableRaster</code>.
  398.      * @param height The desired height of the <code>WritableRaster</code>.
  399.      * @param numBands The desired number of bands.
  400.      * @param bitsPerBand The number of bits per band.
  401.      * @param location A <code>Point</code> indicating the starting
  402.      *        coordinates of the <code>WritableRaster</code>.
  403.      *
  404.      * @throws IllegalArgumentException is thrown if
  405.      *         the <code>dataType</code> is not of either TYPE_BYTE
  406.      *         or TYPE_USHORT or TYPE_INT.
  407.      * @throws IllegalArgumentException is thrown if bitsPerBand
  408.      *         is negative or zero.
  409.      */
  410.     public static WritableRaster createPackedRaster(int dataType,
  411.                                                     int width, int height,
  412.                                                     int numBands,
  413.                                                     int bitsPerBand,
  414.                                                     Point location) {
  415.         if (bitsPerBand <= 0) {
  416.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory15"));
  417.         }
  418.         return Raster.createPackedRaster(dataType, width, height, numBands, 
  419.                                          bitsPerBand, location);
  420.     }
  421.     /**
  422.      * Creates a <code>WritableRaster</code> based on a
  423.      * <code>PixelInterleavedSampleModel</code> with the specified
  424.      * <code>DataBuffer</code>, width, height, scanline stride, pixel
  425.      * stride, and band offsets.  The number of bands is inferred from
  426.      * <code>bandOffsets.length</code>.  The upper left corner of the
  427.      * <code>WritableRaster</code> is given by the
  428.      * <code>location</code> argument.  If <code>location</code> is
  429.      * <code>null</code>, (0, 0) will be used.
  430.      *
  431.      * @param dataBuffer The <code>DataBuffer</code> to be used.
  432.      * @param width The desired width of the <code>WritableRaster</code>.
  433.      * @param height The desired height of the <code>WritableRaster</code>.
  434.      * @param scanlineStride The desired scanline stride.
  435.      * @param pixelStride The desired pixel stride.
  436.      * @param bandOffsets An array of <code>int</code>s indicating the
  437.      *        relative offsets of the bands within a pixel.
  438.      * @param location A <code>Point</code> indicating the starting
  439.      *        coordinates of the <code>WritableRaster</code>.
  440.      *
  441.      * @throws IllegalArgumentException if <code>bandOffsets</code> is
  442.      *         <code>null</code>, if <code>pixelStride*width</code> is
  443.      *         <code>></code> <code>scanlineStride</code>, 
  444.      *         if <code>dataType</code>of the DataBuffer is not one
  445.      *         the enumerated dataType value of java.awt.image.DataBuffer.
  446.      */
  447.     public static WritableRaster createInterleavedRaster(DataBuffer dataBuffer,
  448.                                                          int width, int height,
  449.                                                          int scanlineStride,
  450.                                                          int pixelStride,
  451.                                                          int bandOffsets[],
  452.                                                          Point location) {
  453.         if (bandOffsets == null) {
  454.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory4"));
  455.         }
  456.         if (location == null) {
  457.             location = new Point(0, 0);
  458.         }
  459.         int dataType = dataBuffer.getDataType();
  460.         switch(dataType) {
  461.         case DataBuffer.TYPE_BYTE: 
  462.         case DataBuffer.TYPE_USHORT:
  463.             PixelInterleavedSampleModel csm =
  464.                 new PixelInterleavedSampleModel(dataType, width, height,
  465.                                                 pixelStride,
  466.                                                 scanlineStride,
  467.                                                 bandOffsets);
  468.             return Raster.createWritableRaster(csm,dataBuffer,location);
  469.         case DataBuffer.TYPE_INT:
  470.         case DataBuffer.TYPE_SHORT:
  471.         case DataBuffer.TYPE_FLOAT:
  472.         case DataBuffer.TYPE_DOUBLE:
  473.             int minBandOff=bandOffsets[0];
  474.             int maxBandOff=bandOffsets[0];
  475.             for (int i=1; i<bandOffsets.length; i++) {
  476.                 minBandOff = Math.min(minBandOff,bandOffsets[i]);
  477.                 maxBandOff = Math.max(maxBandOff,bandOffsets[i]);
  478.             }
  479.             maxBandOff -= minBandOff;
  480.             if (maxBandOff > scanlineStride) {
  481.                 throw new IllegalArgumentException(
  482.                                           JaiI18N.getString("RasterFactory5"));
  483.             }
  484.             if (pixelStride*width > scanlineStride) {
  485.                 throw new IllegalArgumentException(
  486.                                           JaiI18N.getString("RasterFactory6"));
  487.             }
  488.             if (pixelStride < maxBandOff) {
  489.                 throw new IllegalArgumentException(
  490.                                           JaiI18N.getString("RasterFactory7"));
  491.             }
  492.             SampleModel sm = 
  493.                  new ComponentSampleModelJAI(dataType,width,height,
  494.                                              pixelStride,
  495.                                              scanlineStride,
  496.                                              bandOffsets);
  497.             return Raster.createWritableRaster(sm, dataBuffer, location);
  498.         default:
  499.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory3"));
  500.         }
  501.     }
  502.     /**
  503.      * Creates a <code>WritableRaster</code> based on a
  504.      * <code>ComponentSampleModel</code> with the specified
  505.      * <code>DataBuffer</code>, width, height, scanline stride, bank
  506.      * indices, and band offsets.  The number of bands is inferred
  507.      * from <code>bankIndices.length</code> and
  508.      * <code>bandOffsets.length</code>, which must be the same.  The
  509.      * upper left corner of the <code>WritableRaster</code> is given
  510.      * by the <code>location</code> argument.  If
  511.      * <code>location</code> is <code>null</code>, (0, 0) will be
  512.      * used.
  513.      *
  514.      * <p> Note that the <code>Raster</code>'s
  515.      * <code>SampleModel</code> will be of type
  516.      * <code>ComponentSampleModel</code>, not
  517.      * <code>BandedSampleModel</code> as might be expected.
  518.      *
  519.      * @param dataBuffer The <code>DataBuffer</code> to be used.
  520.      * @param width The desired width of the <code>WritableRaster</code>.
  521.      * @param height The desired height of the <code>WritableRaster</code>.
  522.      * @param scanlineStride The desired scanline stride.
  523.      * @param bankIndices An array of <code>int</code>s indicating the
  524.      *        bank index for each band.
  525.      * @param bandOffsets An array of <code>int</code>s indicating the
  526.      *        relative offsets of the bands within a pixel.
  527.      * @param location A <code>Point</code> indicating the starting
  528.      *        coordinates of the <code>WritableRaster</code>.
  529.      *
  530.      * @throws IllegalArgumentException if <code>bankIndices</code> is
  531.      *         <code>null</code>, if <code>bandOffsets</code> is
  532.      *         <code>null</code>, if <code>bandOffsets.length</code>
  533.      *         is <code>!=</code> <code>bankIndices.length</code>,
  534.      *         if <code>dataType</code> is not one of the enumerated
  535.      *         datatypes of java.awt.image.DataBuffer.
  536.      */
  537.     public static WritableRaster createBandedRaster(DataBuffer dataBuffer,
  538.                                                     int width, int height,
  539.                                                     int scanlineStride,
  540.                                                     int bankIndices[],
  541.                                                     int bandOffsets[],
  542.                                                     Point location) {
  543.         if (location == null) {
  544.            location = new Point(0,0);
  545.         }
  546.         int dataType = dataBuffer.getDataType();
  547.         if (bankIndices == null) {
  548.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory1"));
  549.         }
  550.         if (bandOffsets == null) {
  551.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory4"));
  552.         }
  553.         int bands = bankIndices.length;
  554.         if (bandOffsets.length != bands) {
  555.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory2"));
  556.         }
  557.         SampleModel bsm =
  558.             new ComponentSampleModelJAI(dataType, width, height,
  559.                                         1, scanlineStride,
  560.                                         bankIndices, bandOffsets);
  561.         switch(dataType) {
  562.         case DataBuffer.TYPE_BYTE:
  563.         case DataBuffer.TYPE_USHORT:
  564.         case DataBuffer.TYPE_INT:
  565.         case DataBuffer.TYPE_SHORT:
  566.         case DataBuffer.TYPE_FLOAT:
  567.         case DataBuffer.TYPE_DOUBLE:
  568.            return Raster.createWritableRaster(bsm, dataBuffer, location);
  569.         default:
  570.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory3"));
  571.         }
  572.     }
  573.     /**
  574.      * Creates a <code>WritableRaster</code> based on a
  575.      * <code>SinglePixelPackedSampleModel</code> with the specified
  576.      * <code>DataBuffer</code>, width, height, scanline stride, and
  577.      * band masks.  The number of bands is inferred from
  578.      * <code>bandMasks.length</code>.  The upper left corner of the
  579.      * <code>WritableRaster</code> is given by the
  580.      * <code>location</code> argument.  If <code>location</code> is
  581.      * <code>null</code>, (0, 0) will be used.
  582.      *
  583.      * @param dataBuffer The <code>DataBuffer</code> to be used.
  584.      * @param width The desired width of the <code>WritableRaster</code>.
  585.      * @param height The desired height of the <code>WritableRaster</code>.
  586.      * @param scanlineStride The desired scanline stride.
  587.      * @param bandMasks An array of <code>int</code>s indicating the
  588.      *        bitmasks for each band within a pixel.
  589.      * @param location A <code>Point</code> indicating the starting
  590.      *        coordinates of the <code>WritableRaster</code>.
  591.      *
  592.      * @throws IllegalArgumentException is thrown if
  593.      *         the <code>dataType</code> is not of either TYPE_BYTE
  594.      *         or TYPE_USHORT or TYPE_INT.
  595.      */
  596.     public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
  597.                                                     int width,
  598.                                                     int height,
  599.                                                     int scanlineStride,
  600.                                                     int bandMasks[],
  601.                                                     Point location) {
  602.         return Raster.createPackedRaster(dataBuffer, width, height,
  603.                                          scanlineStride,
  604.                                          bandMasks,
  605.                                          location);
  606.     }
  607.     /**
  608.      * Creates a <code>WritableRaster</code> based on a
  609.      * <code>MultiPixelPackedSampleModel</code> with the specified
  610.      * <code>DataBuffer</code>, width, height, and bits per pixel.
  611.      * The upper left corner of the <code>WritableRaster</code> is
  612.      * given by the <code>location</code> argument.  If
  613.      * <code>location</code> is <code>null</code>, (0, 0) will be
  614.      * used.
  615.      *
  616.      * @param dataBuffer The <code>DataBuffer</code> to be used.
  617.      * @param width The desired width of the <code>WritableRaster</code>.
  618.      * @param height The desired height of the <code>WritableRaster</code>.
  619.      * @param bitsPerPixel The desired pixel depth.
  620.      * @param location A <code>Point</code> indicating the starting
  621.      *        coordinates of the <code>WritableRaster</code>.
  622.      *
  623.      * @throws IllegalArgumentException is thrown if
  624.      *         the <code>dataType</code> of the <code>dataBuffer</code>
  625.      *         is not of either TYPE_BYTE or TYPE_USHORT or TYPE_INT.
  626.      */
  627.     public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
  628.                                                     int width,
  629.                                                     int height,
  630.                                                     int bitsPerPixel,
  631.                                                     Point location) {
  632.         return Raster.createPackedRaster(dataBuffer, width,  height,
  633.                                          bitsPerPixel, location);
  634.     }
  635.     /**
  636.      *  Creates a <code>WritableRaster</code> with the specified
  637.      *  <code>SampleModel</code> and <code>DataBuffer</code>.  The
  638.      *  upper left corner of the <code>WritableRaster</code> is given
  639.      *  by the <code>location</code> argument.  If
  640.      *  <code>location</code> is <code>null</code>, (0, 0) will be
  641.      *  used.
  642.      *
  643.      * @param sampleModel The <code>SampleModel</code> to be used.
  644.      * @param dataBuffer The <code>DataBuffer</code> to be used.
  645.      * @param location A <code>Point</code> indicating the starting
  646.      *        coordinates of the <code>WritableRaster</code>.
  647.      */
  648.     public static Raster createRaster(SampleModel sampleModel,
  649.                                       DataBuffer dataBuffer,
  650.                                       Point location) {
  651.         return Raster.createRaster(sampleModel, dataBuffer, location);
  652.     }
  653.     /**
  654.      *  Creates a <code>WritableRaster</code> with the specified
  655.      *  <code>SampleModel</code>.  The upper left corner of the
  656.      *  <code>WritableRaster</code> is given by the
  657.      *  <code>location</code> argument.  If <code>location</code> is
  658.      *  <code>null</code>, (0, 0) will be used.
  659.      *
  660.      * @param sampleModel The <code>SampleModel</code> to use.
  661.      * @param location A <code>Point</code> indicating the starting
  662.      *        coordinates of the <code>WritableRaster</code>.
  663.      */
  664.     public static WritableRaster createWritableRaster(SampleModel sampleModel,
  665.                                                       Point location) {
  666.         if (location == null) {
  667.            location = new Point(0,0);
  668.         }
  669.         return createWritableRaster(sampleModel,
  670.                                     sampleModel.createDataBuffer(),
  671.                                     location);
  672.     }
  673.     /**
  674.      *  Creates a <code>WritableRaster</code> with the specified
  675.      *  <code>SampleModel</code> and <code>DataBuffer</code>.  The
  676.      *  upper left corner of the <code>WritableRaster</code> is given
  677.      *  by the <code>location</code> argument.  If
  678.      *  <code>location</code> is <code>null</code>, (0, 0) will be
  679.      *  used.
  680.      *
  681.      * @param sampleModel The <code>SampleModel</code> to be used.
  682.      * @param dataBuffer The <code>DataBuffer</code> to be used.
  683.      * @param location A <code>Point</code> indicating the starting
  684.      *        coordinates of the <code>WritableRaster</code>.
  685.      */
  686.     public static WritableRaster createWritableRaster(SampleModel sampleModel,
  687.                                                       DataBuffer dataBuffer,
  688.                                                       Point location) {
  689.         return Raster.createWritableRaster(sampleModel, dataBuffer, location);
  690.     }
  691.     /**
  692.      * Returns a new WritableRaster which shares all or part of the
  693.      * supplied WritableRaster's DataBuffer.  The new WritableRaster will
  694.      * possess a reference to the supplied WritableRaster, accessible
  695.      * through its getParent() and getWritableParent() methods.
  696.      *
  697.      * <p> This method provides a workaround for a bug in the
  698.      * implementation of WritableRaster.createWritableChild in
  699.      * the initial relase of the Java2 platform.
  700.      *
  701.      * <p> The <code>parentX</code>, <code>parentY</code>,
  702.      * <code>width</code> and <code>height</code> parameters form a
  703.      * Rectangle in this WritableRaster's coordinate space, indicating
  704.      * the area of pixels to be shared.  An error will be thrown if
  705.      * this Rectangle is not contained with the bounds of the supplied
  706.      * WritableRaster.
  707.      *
  708.      * <p> The new WritableRaster may additionally be translated to a
  709.      * different coordinate system for the plane than that used by the supplied
  710.      * WritableRaster.  The childMinX and childMinY parameters give
  711.      * the new (x, y) coordinate of the upper-left pixel of the
  712.      * returned WritableRaster; the coordinate (childMinX, childMinY)
  713.      * in the new WritableRaster will map to the same pixel as the
  714.      * coordinate (parentX, parentY) in the supplied WritableRaster.
  715.      *
  716.      * <p> The new WritableRaster may be defined to contain only a
  717.      * subset of the bands of the supplied WritableRaster, possibly
  718.      * reordered, by means of the bandList parameter.  If bandList is
  719.      * null, it is taken to include all of the bands of the supplied
  720.      * WritableRaster in their current order.
  721.      *
  722.      * <p> To create a new WritableRaster that contains a subregion of
  723.      * the supplied WritableRaster, but shares its coordinate system
  724.      * and bands, this method should be called with childMinX equal to
  725.      * parentX, childMinY equal to parentY, and bandList equal to
  726.      * null.
  727.      *
  728.      * @param raster     The parent WritableRaster.
  729.      * @param parentX    X coordinate of the upper left corner of the shared
  730.      *        rectangle in this WritableRaster's coordinates.
  731.      * @param parentY    Y coordinate of the upper left corner of the shared
  732.      *        rectangle in this WritableRaster's coordinates.
  733.      * @param width      Width of the shared rectangle starting at
  734.      *        (<code>parentX</code>, <code>parentY</code>).
  735.      * @param height     Height of the shared rectangle starting at
  736.      *        (<code>parentX</code>, <code>parentY</code>).
  737.      * @param childMinX  X coordinate of the upper left corner of
  738.      *        the returned WritableRaster.
  739.      * @param childMinY  Y coordinate of the upper left corner of
  740.      *        the returned WritableRaster.
  741.      * @param bandList   Array of band indices, or null to use all bands.
  742.      *
  743.      * @throws RasterFormatException if the subregion is outside of the
  744.      *         raster bounds. 
  745.      */
  746.     public static WritableRaster createWritableChild(WritableRaster raster,
  747.                                                      int parentX,
  748.                                                      int parentY,
  749.                                                      int width,
  750.                                                      int height,
  751.                                                      int childMinX,
  752.                                                      int childMinY,
  753.                                                      int bandList[]) {
  754.         if (parentX < raster.getMinX()) {
  755.             throw new RasterFormatException(JaiI18N.getString("RasterFactory9"));
  756.         }
  757.         if (parentY < raster.getMinY()) {
  758.             throw new 
  759. RasterFormatException(JaiI18N.getString("RasterFactory10"));
  760.         }
  761.         if (parentX + width > raster.getWidth() + raster.getMinX()) {
  762.             throw new 
  763. RasterFormatException(JaiI18N.getString("RasterFactory11"));
  764.         }
  765.         if (parentY + height > raster.getHeight() + raster.getMinY()) {
  766.             throw new 
  767. RasterFormatException(JaiI18N.getString("RasterFactory12"));
  768.         }
  769.         SampleModel sampleModel = raster.getSampleModel();
  770.         DataBuffer dataBuffer = raster.getDataBuffer();
  771.         int sampleModelTranslateX = raster.getSampleModelTranslateX();
  772.         int sampleModelTranslateY = raster.getSampleModelTranslateY();
  773.         SampleModel sm;
  774.         if (bandList != null) {
  775.             sm = sampleModel.createCompatibleSampleModel(
  776.                                                       sampleModel.getWidth(),
  777.                                                       sampleModel.getHeight());
  778.             sm = sm.createSubsetSampleModel(bandList);
  779.         }
  780.         else {
  781.             sm = sampleModel;
  782.         }
  783.         int deltaX = childMinX - parentX;
  784.         int deltaY = childMinY - parentY;
  785.         return new WritableRasterJAI(sm,
  786.                                      dataBuffer,
  787.                                      new Rectangle(childMinX, childMinY,
  788.                                                    width, height),
  789.                                      new Point(sampleModelTranslateX + deltaX,
  790.                                                sampleModelTranslateY + deltaY),
  791.                                      raster);
  792.     }
  793.     /**
  794.      * Creates a banded <code>SampleModel</code> with a given data
  795.      * type, width, height, number of bands, bank indices, and band
  796.      * offsets.
  797.      *
  798.      * <p> Note that the returned <code>SampleModel</code> will be of type
  799.      * <code>ComponentSampleModel</code>, not
  800.      * <code>BandedSampleModel</code> as might be expected.  Its
  801.      * behavior will be equivalent to that of a
  802.      * <code>BandedSampleModel</code>, and in particular its pixel
  803.      * stride will always be 1.
  804.      *
  805.      * @param dataType The data type of the <code>SampleModel</code>,
  806.      *        one of <code>DataBuffer.TYPE_BYTE</code>,
  807.      *        <code>TYPE_USHORT</code>,
  808.      *        <code>TYPE_SHORT</code>,
  809.      *        <code>TYPE_INT</code>,
  810.      *        <code>TYPE_FLOAT</code>, or
  811.      *        <code>TYPE_DOUBLE</code>.
  812.      * @param width The desired width of the <code>SampleModel</code>.
  813.      * @param height The desired height of the <code>SampleModel</code>.
  814.      * @param numBands The desired number of bands.
  815.      * @param bankIndices An array of <code>int</code>s indicating the
  816.      *        bank index for each band.
  817.      * @param bandOffsets An array of <code>int</code>s indicating the
  818.      *        relative offsets of the bands within a pixel.
  819.      *
  820.      * @throws IllegalArgumentException if <code>numBands</code> is
  821.      *         <code><1</code>, if <code>bandOffsets.length</code> is
  822.      *         <code>!=</code> <code>bankIndices.length</code>.
  823.      */
  824.     public static SampleModel createBandedSampleModel(int dataType, 
  825.                                                       int width, 
  826.                                                       int height,
  827.                                                       int numBands,
  828.                                                       int bankIndices[],
  829.                                                       int bandOffsets[]) {
  830.         if (numBands < 1) {
  831.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory0"));
  832.         }
  833.         if (bankIndices == null) {
  834.             bankIndices = new int[numBands];
  835.             for (int i=0; i < numBands; i++) {
  836.                 bankIndices[i] = i;
  837.             }
  838.         }
  839.         if (bandOffsets == null) {
  840.             bandOffsets = new int[numBands];
  841.             for (int i=0; i < numBands; i++) {
  842.                 bandOffsets[i] = 0;
  843.             }
  844.         }
  845.         if (bandOffsets.length != bankIndices.length) {
  846.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory2"));
  847.         }
  848.         return new ComponentSampleModelJAI(dataType,
  849.                                            width, height, 1, width,
  850.                                            bankIndices,
  851.                                            bandOffsets);
  852.     }
  853.     /**
  854.      * Creates a banded <code>SampleModel</code> with a given data
  855.      * type, width, height, and number of bands.  The bank indices and
  856.      * band offsets are set to default values.
  857.      *
  858.      * <p> Note that the returned <code>SampleModel</code> will be of type
  859.      * <code>ComponentSampleModel</code>, not
  860.      * <code>BandedSampleModel</code> as might be expected.  Its
  861.      * behavior will be equivalent to that of a
  862.      * <code>BandedSampleModel</code>, and in particular its pixel
  863.      * stride will always be 1.
  864.      *
  865.      * @param dataType The data type of the <code>SampleModel</code>,
  866.      *        one of <code>DataBuffer.TYPE_BYTE</code>,
  867.      *        <code>TYPE_USHORT</code>,
  868.      *        <code>TYPE_SHORT</code>,
  869.      *        <code>TYPE_INT</code>,
  870.      *        <code>TYPE_FLOAT</code>, or
  871.      *        <code>TYPE_DOUBLE</code>.
  872.      * @param width The desired width of the <code>SampleModel</code>.
  873.      * @param height The desired height of the <code>SampleModel</code>.
  874.      * @param numBands The desired number of bands.
  875.      */
  876.     public static SampleModel createBandedSampleModel(int dataType,
  877.                                                       int width,
  878.                                                       int height,
  879.                                                       int numBands) {
  880.         return createBandedSampleModel(dataType,
  881.                                        width, height, numBands, null, null);
  882.     }
  883.     /**
  884.      * Creates a pixel interleaved <code>SampleModel</code> with a
  885.      * given data type, width, height, pixel and scanline strides, and
  886.      * band offsets.
  887.      *
  888.      * @param dataType The data type of the <code>SampleModel</code>,
  889.      *        one of <code>DataBuffer.TYPE_BYTE</code>,
  890.      *        <code>TYPE_USHORT</code>,
  891.      *        <code>TYPE_SHORT</code>,
  892.      *        <code>TYPE_INT</code>,
  893.      *        <code>TYPE_FLOAT</code>, or
  894.      *        <code>TYPE_DOUBLE</code>.
  895.      * @param width The desired width of the <code>SampleModel</code>.
  896.      * @param height The desired height of the <code>SampleModel</code>.
  897.      * @param pixelStride The desired pixel stride.
  898.      * @param scanlineStride The desired scanline stride.
  899.      * @param bandOffsets An array of <code>int</code>s indicating the
  900.      *        relative offsets of the bands within a pixel.
  901.      *
  902.      * @throws IllegalArgumentException if <code>bandOffsets</code> is
  903.      *         <code>null</code>, if the <code>pixelStride*width</code>
  904.      *         is <code>></code> than <code>scanlineStride</code>,
  905.      *         if the <code>dataType</code> is not one of the above
  906.      *         mentioned datatypes.
  907.      */
  908.     public static SampleModel 
  909.     createPixelInterleavedSampleModel(int dataType,
  910.                                       int width, int height,
  911.                                       int pixelStride,
  912.                                       int scanlineStride,
  913.                                       int bandOffsets[]) {
  914.         if (bandOffsets == null) {
  915.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory4"));
  916.         }
  917.         int minBandOff=bandOffsets[0];
  918.         int maxBandOff=bandOffsets[0];
  919.         for (int i=1; i<bandOffsets.length; i++) {
  920.             minBandOff = Math.min(minBandOff,bandOffsets[i]);
  921.             maxBandOff = Math.max(maxBandOff,bandOffsets[i]);
  922.         }
  923.         maxBandOff -= minBandOff;
  924.         if (maxBandOff > scanlineStride) {
  925.             throw new IllegalArgumentException(
  926.                                       JaiI18N.getString("RasterFactory5"));
  927.         }
  928.         if (pixelStride*width > scanlineStride) {
  929.             throw new IllegalArgumentException(
  930.                                       JaiI18N.getString("RasterFactory6"));
  931.         }
  932.         if (pixelStride < maxBandOff) {
  933.             throw new IllegalArgumentException(
  934.                                       JaiI18N.getString("RasterFactory7"));
  935.         }
  936.         switch (dataType) {
  937.         case DataBuffer.TYPE_BYTE:
  938.         case DataBuffer.TYPE_USHORT:
  939.             return new PixelInterleavedSampleModel(dataType,
  940.                                                    width, height,
  941.                                                    pixelStride,
  942.                                                    scanlineStride,
  943.                                                    bandOffsets);
  944.         case DataBuffer.TYPE_INT:
  945.         case DataBuffer.TYPE_SHORT:
  946.         case DataBuffer.TYPE_FLOAT:
  947.         case DataBuffer.TYPE_DOUBLE:
  948.             return new ComponentSampleModelJAI(dataType,
  949.                                                width, height,
  950.                                                pixelStride,
  951.                                                scanlineStride,
  952.                                                bandOffsets);
  953.         default:
  954.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory3"));
  955.         }
  956.     }
  957.     /**
  958.      * Creates a pixel interleaved <code>SampleModel</code> with a
  959.      * given data type, width, height, and number of bands.  The pixel
  960.      * stride, scanline stride, and band offsets are set to default
  961.      * values.
  962.      *
  963.      * @param dataType The data type of the <code>SampleModel</code>,
  964.      *        one of <code>DataBuffer.TYPE_BYTE</code>,
  965.      *        <code>TYPE_USHORT</code>,
  966.      *        <code>TYPE_SHORT</code>,
  967.      *        <code>TYPE_INT</code>,
  968.      *        <code>TYPE_FLOAT</code>, or
  969.      *        <code>TYPE_DOUBLE</code>.
  970.      * @param width The desired width of the <code>SampleModel</code>.
  971.      * @param height The desired height of the <code>SampleModel</code>.
  972.      * @param numBands The desired number of bands.
  973.      *
  974.      * @throws IllegalArgumentException if <code>numBands</code> is
  975.      *         <code><1</code>.
  976.      */
  977.     public static SampleModel
  978.     createPixelInterleavedSampleModel(int dataType,
  979.                                       int width,
  980.                                       int height,
  981.                                       int numBands) {
  982.         if (numBands < 1) {
  983.             throw new IllegalArgumentException(JaiI18N.getString("RasterFactory0"));
  984.         }
  985.         int[] bandOffsets = new int[numBands];
  986.         for (int i = 0; i < numBands; i++) {
  987.             bandOffsets[i] = numBands - 1 - i;
  988.         }
  989.         return createPixelInterleavedSampleModel(dataType, width, height,
  990.                    numBands, numBands * width, bandOffsets);
  991.     }
  992.     /**
  993.      * Creates a component <code>SampleModel</code> with a given data
  994.      * type, width, height, and number of bands that is "compatible"
  995.      * with a given SampleModel.
  996.      *
  997.      * @param sm The <code>SampleModel</code> to be compatible with. 
  998.      * @param dataType The data type of the <code>SampleModel</code>,
  999.      *        one of <code>DataBuffer.TYPE_BYTE</code>,
  1000.      *        <code>TYPE_USHORT</code>,
  1001.      *        <code>TYPE_SHORT</code>,
  1002.      *        <code>TYPE_INT</code>,
  1003.      *        <code>TYPE_FLOAT</code>, or
  1004.      *        <code>TYPE_DOUBLE</code>.
  1005.      * @param width The desired width of the <code>SampleModel</code>.
  1006.      * @param height The desired height of the <code>SampleModel</code>.
  1007.      * @param numBands The desired number of bands.
  1008.      */
  1009.     public static SampleModel createComponentSampleModel(SampleModel sm,
  1010.                                                          int dataType,
  1011.                                                          int width,
  1012.                                                          int height,
  1013.                                                          int numBands) {
  1014.         if (sm instanceof BandedSampleModel) {
  1015.             return createBandedSampleModel(dataType, width, height, numBands);
  1016.         } else { // default SampleModel
  1017.             return createPixelInterleavedSampleModel(
  1018.                    dataType, width, height, numBands);
  1019.         }
  1020.     }
  1021.     /**
  1022.      * Creates a component-based <code>ColorModel</code> with a given 
  1023.      * data type, color space, and transparency type.  Currently this
  1024.      * method does not support data type <code>DataBuffer.TYPE_SHORT</code>.
  1025.      * If useAlpha is false, both premultiplied and transparency input
  1026.      * are ignored and they are set to be <code> false</code> and 
  1027.      * <code> Transparency.OPQAUE </code>, respectively.
  1028.      *
  1029.      * @param dataType The data type of the <code>ColorModel</code>,
  1030.      *        one of <code>DataBuffer.TYPE_BYTE</code>,
  1031.      *        <code>TYPE_USHORT</code>,
  1032.      *        <code>TYPE_INT</code>,
  1033.      *        <code>TYPE_FLOAT</code>, or
  1034.      *        <code>TYPE_DOUBLE</code>.
  1035.      * @param colorSpace An instance of <code>ColorSpace</code>.
  1036.      * @param useAlpha <code>true</code> if alpha is to be used.
  1037.      * @param premultiplied <code>true</code> if alpha values are
  1038.      *        premultiplied.  If <code>useAlpha</code> is
  1039.      *        <code>false</code>, the value of
  1040.      *        <code>premultiplied</code> is ignored.
  1041.      * @param transparency One of <code>Transparency.OPAQUE</code>,
  1042.      * <code>Transparency.BITMASK</code>, or
  1043.      * <code>Transparency.TRANSLUCENT</code>.  If
  1044.      * <code>useAlpha</code> is <code>false</code>, the value of
  1045.      * <code>transparency</code> is ignored.  If <code>useAlpha</code>
  1046.      * is <code>true</code>, <code>transparency</code> must not equal
  1047.      * <code>Transparency.OPQAUE</code>.
  1048.      *
  1049.      * @throws IllegalArgumentExceptionException if <code>colorSpace</code> is
  1050.      *         <code>null</code>.
  1051.      * @throws IllegalArgumentException if <code>transparency</code>
  1052.      *         has an unknown value, if <code>useAlpha == true</code> but
  1053.      *         <code>transparency == Transparency.OPAQUE</code>, or if
  1054.      *         <code>dataType</code> is not one of the standard types listed
  1055.      *         above.
  1056.      */
  1057.     public static ComponentColorModel createComponentColorModel(int dataType,
  1058.                                                        ColorSpace colorSpace,
  1059.                                                        boolean useAlpha,
  1060.                                                        boolean premultiplied,
  1061.                                                        int transparency) {
  1062.         if ( colorSpace == null ) {
  1063.             throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
  1064.         }
  1065.         if ((transparency != Transparency.OPAQUE) &&
  1066.             (transparency != Transparency.BITMASK) &&
  1067.             (transparency != Transparency.TRANSLUCENT)) {
  1068.             // Illegal value for transparency
  1069.             throw new 
  1070. IllegalArgumentException(JaiI18N.getString("RasterFactory13"));
  1071.         }
  1072.         if (useAlpha && (transparency == Transparency.OPAQUE)) {
  1073.             throw new 
  1074. IllegalArgumentException(JaiI18N.getString("RasterFactory14"));
  1075.         }
  1076.         
  1077.         if (!useAlpha) {
  1078.             premultiplied = false;
  1079.             transparency = Transparency.OPAQUE;
  1080.         }
  1081.         int bands = colorSpace.getNumComponents();
  1082.         if (useAlpha) {
  1083.             ++bands;
  1084.         }
  1085.         int dataTypeSize = DataBuffer.getDataTypeSize(dataType);
  1086.         int[] bits = new int[bands];
  1087.         for (int i = 0; i < bands; i++) {
  1088.             bits[i] = dataTypeSize;
  1089.         }
  1090.         switch (dataType) {
  1091.         case DataBuffer.TYPE_BYTE:
  1092.             return new ComponentColorModel(colorSpace,
  1093.                                            bits,
  1094.                                            useAlpha,
  1095.                                            premultiplied,
  1096.                                            transparency,
  1097.                                            dataType);
  1098.         case DataBuffer.TYPE_USHORT:
  1099.             return new ComponentColorModel(colorSpace,
  1100.                                            bits,
  1101.                                            useAlpha,
  1102.                                            premultiplied,
  1103.                                            transparency,
  1104.                                            dataType);
  1105.         /// case DataBuffer.TYPE_SHORT:
  1106.             /// return new ShortComponentColorModel(colorSpace,
  1107.                                                 /// bits,
  1108.                                                 /// useAlpha,
  1109.                                                 /// premultiplied,
  1110.                                                 /// transparency);
  1111.         case DataBuffer.TYPE_INT:
  1112.             return new ComponentColorModel(colorSpace,
  1113.                                            bits,
  1114.                                            useAlpha,
  1115.                                            premultiplied,
  1116.                                            transparency,
  1117.                                            dataType);
  1118.         case DataBuffer.TYPE_FLOAT:
  1119.             return new FloatDoubleColorModel(colorSpace,
  1120.                                              useAlpha,
  1121.                                              premultiplied,
  1122.                                              transparency,
  1123.                                              dataType);
  1124.         case DataBuffer.TYPE_DOUBLE:
  1125.             return new FloatDoubleColorModel(colorSpace,
  1126.                                              useAlpha,
  1127.                                              premultiplied,
  1128.                                              transparency,
  1129.                                              dataType);
  1130.         default:
  1131.             throw new IllegalArgumentException(
  1132.                 JaiI18N.getString("RasterFactory8"));
  1133.         }
  1134.     }
  1135. }