< prev index next >

src/java.desktop/share/classes/java/awt/image/Raster.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 612      *         {@code DataBuffer}, width, height, scanline stride,
 613      *         pixel stride and band offsets.
 614      * @throws RasterFormatException if {@code w} or {@code h}
 615      *         is less than or equal to zero, or computing either
 616      *         {@code location.x + w} or
 617      *         {@code location.y + h} results in integer
 618      *         overflow
 619      * @throws IllegalArgumentException if {@code dataType} is not
 620      *         one of the supported data types, which are
 621      *         {@code DataBuffer.TYPE_BYTE},
 622      *         {@code DataBuffer.TYPE_USHORT}
 623      * @throws RasterFormatException if {@code dataBuffer} has more
 624      *         than one bank.
 625      * @throws NullPointerException if {@code dataBuffer} is null
 626      */
 627     public static WritableRaster createInterleavedRaster(DataBuffer dataBuffer,
 628                                                          int w, int h,
 629                                                          int scanlineStride,
 630                                                          int pixelStride,
 631                                                          int bandOffsets[],
 632                                                          Point location) {

 633         if (dataBuffer == null) {
 634             throw new NullPointerException("DataBuffer cannot be null");
 635         }
 636         if (location == null) {
 637             location = new Point(0, 0);
 638         }
 639         int dataType = dataBuffer.getDataType();
 640 
 641         PixelInterleavedSampleModel csm =
 642             new PixelInterleavedSampleModel(dataType, w, h,
 643                                             pixelStride,
 644                                             scanlineStride,
 645                                             bandOffsets);
 646         switch(dataType) {
 647         case DataBuffer.TYPE_BYTE:
 648             return new ByteInterleavedRaster(csm, dataBuffer, location);




 649 
 650         case DataBuffer.TYPE_USHORT:
 651             return new ShortInterleavedRaster(csm, dataBuffer, location);




 652 
 653         default:
 654             throw new IllegalArgumentException("Unsupported data type " +
 655                                                 dataType);
 656         }



 657     }
 658 
 659     /**
 660      * Creates a Raster based on a BandedSampleModel with the
 661      * specified DataBuffer, width, height, scanline stride, bank
 662      * indices, and band offsets.  The number of bands is inferred
 663      * from bankIndices.length and bandOffsets.length, which must be
 664      * the same.  The upper left corner of the Raster is given by the
 665      * location argument.  If location is null, (0, 0) will be used.
 666      * @param dataBuffer the {@code DataBuffer} that contains the
 667      *        image data
 668      * @param w         the width in pixels of the image data
 669      * @param h         the height in pixels of the image data
 670      * @param scanlineStride the line stride of the image data
 671      * @param bankIndices the bank indices for each band
 672      * @param bandOffsets the offsets of all bands
 673      * @param location  the upper-left corner of the {@code Raster}
 674      * @return a WritableRaster object with the specified
 675      *         {@code DataBuffer}, width, height, scanline stride,
 676      *         bank indices and band offsets.
 677      * @throws RasterFormatException if {@code w} or {@code h}
 678      *         is less than or equal to zero, or computing either
 679      *         {@code location.x + w} or
 680      *         {@code location.y + h} results in integer
 681      *         overflow
 682      * @throws IllegalArgumentException if {@code dataType} is not
 683      *         one of the supported data types, which are
 684      *         {@code DataBuffer.TYPE_BYTE},
 685      *         {@code DataBuffer.TYPE_USHORT}
 686      *         or {@code DataBuffer.TYPE_INT}
 687      * @throws NullPointerException if {@code dataBuffer} is null
 688      */
 689     public static WritableRaster createBandedRaster(DataBuffer dataBuffer,
 690                                                     int w, int h,
 691                                                     int scanlineStride,
 692                                                     int bankIndices[],
 693                                                     int bandOffsets[],
 694                                                     Point location) {

 695         if (dataBuffer == null) {
 696             throw new NullPointerException("DataBuffer cannot be null");
 697         }
 698         if (location == null) {
 699            location = new Point(0,0);
 700         }
 701         int dataType = dataBuffer.getDataType();
 702 
 703         int bands = bankIndices.length;
 704         if (bandOffsets.length != bands) {
 705             throw new IllegalArgumentException(
 706                                    "bankIndices.length != bandOffsets.length");
 707         }
 708 
 709         BandedSampleModel bsm =
 710             new BandedSampleModel(dataType, w, h,
 711                                   scanlineStride,
 712                                   bankIndices, bandOffsets);
 713 
 714         switch(dataType) {
 715         case DataBuffer.TYPE_BYTE:
 716             return new ByteBandedRaster(bsm, dataBuffer, location);




 717 
 718         case DataBuffer.TYPE_USHORT:
 719             return new ShortBandedRaster(bsm, dataBuffer, location);




 720 
 721         case DataBuffer.TYPE_INT:
 722             return new SunWritableRaster(bsm, dataBuffer, location);




 723 
 724         default:
 725             throw new IllegalArgumentException("Unsupported data type " +
 726                                                 dataType);
 727         }



 728     }
 729 
 730     /**
 731      * Creates a Raster based on a SinglePixelPackedSampleModel with
 732      * the specified DataBuffer, width, height, scanline stride, and
 733      * band masks.  The number of bands is inferred from bandMasks.length.
 734      * The upper left corner of the Raster is given by
 735      * the location argument.  If location is null, (0, 0) will be used.
 736      * @param dataBuffer the {@code DataBuffer} that contains the
 737      *        image data
 738      * @param w         the width in pixels of the image data
 739      * @param h         the height in pixels of the image data
 740      * @param scanlineStride the line stride of the image data
 741      * @param bandMasks an array containing an entry for each band
 742      * @param location  the upper-left corner of the {@code Raster}
 743      * @return a WritableRaster object with the specified
 744      *         {@code DataBuffer}, width, height, scanline stride,
 745      *         and band masks.
 746      * @throws RasterFormatException if {@code w} or {@code h}
 747      *         is less than or equal to zero, or computing either
 748      *         {@code location.x + w} or
 749      *         {@code location.y + h} results in integer
 750      *         overflow
 751      * @throws IllegalArgumentException if {@code dataType} is not
 752      *         one of the supported data types, which are
 753      *         {@code DataBuffer.TYPE_BYTE},
 754      *         {@code DataBuffer.TYPE_USHORT}
 755      *         or {@code DataBuffer.TYPE_INT}
 756      * @throws RasterFormatException if {@code dataBuffer} has more
 757      *         than one bank.
 758      * @throws NullPointerException if {@code dataBuffer} is null
 759      */
 760     public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
 761                                                     int w, int h,
 762                                                     int scanlineStride,
 763                                                     int bandMasks[],
 764                                                     Point location) {

 765         if (dataBuffer == null) {
 766             throw new NullPointerException("DataBuffer cannot be null");
 767         }
 768         if (location == null) {
 769            location = new Point(0,0);
 770         }
 771         int dataType = dataBuffer.getDataType();
 772 
 773         SinglePixelPackedSampleModel sppsm =
 774             new SinglePixelPackedSampleModel(dataType, w, h, scanlineStride,
 775                                              bandMasks);
 776 
 777         switch(dataType) {
 778         case DataBuffer.TYPE_BYTE:
 779             return new ByteInterleavedRaster(sppsm, dataBuffer, location);




 780 
 781         case DataBuffer.TYPE_USHORT:
 782             return new ShortInterleavedRaster(sppsm, dataBuffer, location);




 783 
 784         case DataBuffer.TYPE_INT:
 785             return new IntegerInterleavedRaster(sppsm, dataBuffer, location);




 786 
 787         default:
 788             throw new IllegalArgumentException("Unsupported data type " +
 789                                                 dataType);
 790         }



 791     }
 792 
 793     /**
 794      * Creates a Raster based on a MultiPixelPackedSampleModel with the
 795      * specified DataBuffer, width, height, and bits per pixel.  The upper
 796      * left corner of the Raster is given by the location argument.  If
 797      * location is null, (0, 0) will be used.
 798      * @param dataBuffer the {@code DataBuffer} that contains the
 799      *        image data
 800      * @param w         the width in pixels of the image data
 801      * @param h         the height in pixels of the image data
 802      * @param bitsPerPixel the number of bits for each pixel
 803      * @param location  the upper-left corner of the {@code Raster}
 804      * @return a WritableRaster object with the specified
 805      *         {@code DataBuffer}, width, height, and
 806      *         bits per pixel.
 807      * @throws RasterFormatException if {@code w} or {@code h}
 808      *         is less than or equal to zero, or computing either
 809      *         {@code location.x + w} or
 810      *         {@code location.y + h} results in integer
 811      *         overflow
 812      * @throws IllegalArgumentException if {@code dataType} is not
 813      *         one of the supported data types, which are
 814      *         {@code DataBuffer.TYPE_BYTE},
 815      *         {@code DataBuffer.TYPE_USHORT}
 816      *         or {@code DataBuffer.TYPE_INT}
 817      * @throws RasterFormatException if {@code dataBuffer} has more
 818      *         than one bank.
 819      * @throws NullPointerException if {@code dataBuffer} is null
 820      */
 821     public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
 822                                                     int w, int h,
 823                                                     int bitsPerPixel,
 824                                                     Point location) {

 825         if (dataBuffer == null) {
 826             throw new NullPointerException("DataBuffer cannot be null");
 827         }
 828         if (location == null) {
 829            location = new Point(0,0);
 830         }
 831         int dataType = dataBuffer.getDataType();
 832 
 833         if (dataType != DataBuffer.TYPE_BYTE &&
 834             dataType != DataBuffer.TYPE_USHORT &&
 835             dataType != DataBuffer.TYPE_INT) {
 836             throw new IllegalArgumentException("Unsupported data type " +
 837                                                dataType);
 838         }
 839 
 840         if (dataBuffer.getNumBanks() != 1) {
 841             throw new
 842                 RasterFormatException("DataBuffer for packed Rasters"+
 843                                       " must only have 1 bank.");
 844         }
 845 
 846         MultiPixelPackedSampleModel mppsm =
 847                 new MultiPixelPackedSampleModel(dataType, w, h, bitsPerPixel);
 848 
 849         if (dataType == DataBuffer.TYPE_BYTE &&
 850             (bitsPerPixel == 1 || bitsPerPixel == 2 || bitsPerPixel == 4)) {
 851             return new BytePackedRaster(mppsm, dataBuffer, location);

 852         } else {
 853             return new SunWritableRaster(mppsm, dataBuffer, location);
 854         }
 855     }
 856 
 857 
 858     /**
 859      *  Creates a Raster with the specified SampleModel and DataBuffer.
 860      *  The upper left corner of the Raster is given by the location argument.
 861      *  If location is null, (0, 0) will be used.
 862      *  @param sm the specified {@code SampleModel}
 863      *  @param db the specified {@code DataBuffer}
 864      *  @param location the upper-left corner of the {@code Raster}
 865      *  @return a {@code Raster} with the specified
 866      *          {@code SampleModel}, {@code DataBuffer}, and
 867      *          location.
 868      * @throws RasterFormatException if computing either
 869      *         {@code location.x + sm.getWidth()} or
 870      *         {@code location.y + sm.getHeight()} results in integer
 871      *         overflow
 872      * @throws RasterFormatException if {@code db} has more
 873      *         than one bank and {@code sm} is a
 874      *         PixelInterleavedSampleModel, SinglePixelPackedSampleModel,
 875      *         or MultiPixelPackedSampleModel.
 876      *  @throws NullPointerException if either SampleModel or DataBuffer is
 877      *          null
 878      */
 879     public static Raster createRaster(SampleModel sm,
 880                                       DataBuffer db,
 881                                       Point location) {

 882         if ((sm == null) || (db == null)) {
 883             throw new NullPointerException("SampleModel and DataBuffer cannot be null");
 884         }
 885 
 886         if (location == null) {
 887            location = new Point(0,0);
 888         }
 889         int dataType = sm.getDataType();
 890 
 891         if (sm instanceof PixelInterleavedSampleModel) {
 892             switch(dataType) {
 893                 case DataBuffer.TYPE_BYTE:
 894                     return new ByteInterleavedRaster(sm, db, location);




 895 
 896                 case DataBuffer.TYPE_USHORT:
 897                     return new ShortInterleavedRaster(sm, db, location);




 898             }
 899         } else if (sm instanceof SinglePixelPackedSampleModel) {
 900             switch(dataType) {
 901                 case DataBuffer.TYPE_BYTE:
 902                     return new ByteInterleavedRaster(sm, db, location);




 903 
 904                 case DataBuffer.TYPE_USHORT:
 905                     return new ShortInterleavedRaster(sm, db, location);




 906 
 907                 case DataBuffer.TYPE_INT:
 908                     return new IntegerInterleavedRaster(sm, db, location);




 909             }
 910         } else if (sm instanceof MultiPixelPackedSampleModel &&
 911                    dataType == DataBuffer.TYPE_BYTE &&
 912                    sm.getSampleSize(0) < 8) {
 913             return new BytePackedRaster(sm, db, location);


 914         }
 915 
 916         // we couldn't do anything special - do the generic thing
 917 
 918         return new Raster(sm,db,location);
 919     }
 920 
 921     /**
 922      *  Creates a WritableRaster with the specified SampleModel.
 923      *  The upper left corner of the Raster is given by the location argument.
 924      *  If location is null, (0, 0) will be used.
 925      *  @param sm the specified {@code SampleModel}
 926      *  @param location the upper-left corner of the
 927      *         {@code WritableRaster}
 928      *  @return a {@code WritableRaster} with the specified
 929      *          {@code SampleModel} and location.
 930      *  @throws RasterFormatException if computing either
 931      *          {@code location.x + sm.getWidth()} or
 932      *          {@code location.y + sm.getHeight()} results in integer
 933      *          overflow
 934      */
 935     public static WritableRaster createWritableRaster(SampleModel sm,
 936                                                       Point location) {
 937         if (location == null) {
 938            location = new Point(0,0);


 947      *  If location is null, (0, 0) will be used.
 948      *  @param sm the specified {@code SampleModel}
 949      *  @param db the specified {@code DataBuffer}
 950      *  @param location the upper-left corner of the
 951      *         {@code WritableRaster}
 952      *  @return a {@code WritableRaster} with the specified
 953      *          {@code SampleModel}, {@code DataBuffer}, and
 954      *          location.
 955      * @throws RasterFormatException if computing either
 956      *         {@code location.x + sm.getWidth()} or
 957      *         {@code location.y + sm.getHeight()} results in integer
 958      *         overflow
 959      * @throws RasterFormatException if {@code db} has more
 960      *         than one bank and {@code sm} is a
 961      *         PixelInterleavedSampleModel, SinglePixelPackedSampleModel,
 962      *         or MultiPixelPackedSampleModel.
 963      * @throws NullPointerException if either SampleModel or DataBuffer is null
 964      */
 965     public static WritableRaster createWritableRaster(SampleModel sm,
 966                                                       DataBuffer db,
 967                                                       Point location) {

 968         if ((sm == null) || (db == null)) {
 969             throw new NullPointerException("SampleModel and DataBuffer cannot be null");
 970         }
 971         if (location == null) {
 972            location = new Point(0,0);
 973         }
 974 
 975         int dataType = sm.getDataType();
 976 
 977         if (sm instanceof PixelInterleavedSampleModel) {
 978             switch(dataType) {
 979                 case DataBuffer.TYPE_BYTE:
 980                     return new ByteInterleavedRaster(sm, db, location);




 981 
 982                 case DataBuffer.TYPE_USHORT:
 983                     return new ShortInterleavedRaster(sm, db, location);




 984             }
 985         } else if (sm instanceof SinglePixelPackedSampleModel) {
 986             switch(dataType) {
 987                 case DataBuffer.TYPE_BYTE:
 988                     return new ByteInterleavedRaster(sm, db, location);




 989 
 990                 case DataBuffer.TYPE_USHORT:
 991                     return new ShortInterleavedRaster(sm, db, location);




 992 
 993                 case DataBuffer.TYPE_INT:
 994                     return new IntegerInterleavedRaster(sm, db, location);




 995             }
 996         } else if (sm instanceof MultiPixelPackedSampleModel &&
 997                    dataType == DataBuffer.TYPE_BYTE &&
 998                    sm.getSampleSize(0) < 8) {
 999             return new BytePackedRaster(sm, db, location);


1000         }
1001 
1002         // we couldn't do anything special - do the generic thing
1003 
1004         return new SunWritableRaster(sm,db,location);
1005     }
1006 
1007     /**
1008      *  Constructs a Raster with the given SampleModel.  The Raster's
1009      *  upper left corner is origin and it is the same size as the
1010      *  SampleModel.  A DataBuffer large enough to describe the
1011      *  Raster is automatically created.
1012      *  @param sampleModel     The SampleModel that specifies the layout
1013      *  @param origin          The Point that specified the origin
1014      *  @throws RasterFormatException if computing either
1015      *          {@code origin.x + sampleModel.getWidth()} or
1016      *          {@code origin.y + sampleModel.getHeight()} results in
1017      *          integer overflow
1018      *  @throws NullPointerException either {@code sampleModel} or
1019      *          {@code origin} is null
1020      */
1021     protected Raster(SampleModel sampleModel,
1022                      Point origin) {
1023         this(sampleModel,
1024              sampleModel.createDataBuffer(),


   1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 612      *         {@code DataBuffer}, width, height, scanline stride,
 613      *         pixel stride and band offsets.
 614      * @throws RasterFormatException if {@code w} or {@code h}
 615      *         is less than or equal to zero, or computing either
 616      *         {@code location.x + w} or
 617      *         {@code location.y + h} results in integer
 618      *         overflow
 619      * @throws IllegalArgumentException if {@code dataType} is not
 620      *         one of the supported data types, which are
 621      *         {@code DataBuffer.TYPE_BYTE},
 622      *         {@code DataBuffer.TYPE_USHORT}
 623      * @throws RasterFormatException if {@code dataBuffer} has more
 624      *         than one bank.
 625      * @throws NullPointerException if {@code dataBuffer} is null
 626      */
 627     public static WritableRaster createInterleavedRaster(DataBuffer dataBuffer,
 628                                                          int w, int h,
 629                                                          int scanlineStride,
 630                                                          int pixelStride,
 631                                                          int bandOffsets[],
 632                                                          Point location)
 633     {
 634         if (dataBuffer == null) {
 635             throw new NullPointerException("DataBuffer cannot be null");
 636         }
 637         if (location == null) {
 638             location = new Point(0, 0);
 639         }
 640         int dataType = dataBuffer.getDataType();
 641 
 642         PixelInterleavedSampleModel csm =
 643             new PixelInterleavedSampleModel(dataType, w, h,
 644                                             pixelStride,
 645                                             scanlineStride,
 646                                             bandOffsets);
 647         switch(dataType) {
 648         case DataBuffer.TYPE_BYTE:
 649             if (dataBuffer instanceof DataBufferByte) {
 650                 return new ByteInterleavedRaster(csm, 
 651                         (DataBufferByte) dataBuffer, location);
 652             }
 653             break;
 654 
 655         case DataBuffer.TYPE_USHORT:
 656             if (dataBuffer instanceof DataBufferUShort) {
 657                 return new ShortInterleavedRaster(csm,
 658                         (DataBufferUShort) dataBuffer, location);
 659             }
 660             break;
 661 
 662         default:
 663             throw new IllegalArgumentException("Unsupported data type " +
 664                                                 dataType);
 665         }
 666         
 667         // Create the generic raster
 668         return new SunWritableRaster(csm, dataBuffer, location);
 669     }
 670 
 671     /**
 672      * Creates a Raster based on a BandedSampleModel with the
 673      * specified DataBuffer, width, height, scanline stride, bank
 674      * indices, and band offsets.  The number of bands is inferred
 675      * from bankIndices.length and bandOffsets.length, which must be
 676      * the same.  The upper left corner of the Raster is given by the
 677      * location argument.  If location is null, (0, 0) will be used.
 678      * @param dataBuffer the {@code DataBuffer} that contains the
 679      *        image data
 680      * @param w         the width in pixels of the image data
 681      * @param h         the height in pixels of the image data
 682      * @param scanlineStride the line stride of the image data
 683      * @param bankIndices the bank indices for each band
 684      * @param bandOffsets the offsets of all bands
 685      * @param location  the upper-left corner of the {@code Raster}
 686      * @return a WritableRaster object with the specified
 687      *         {@code DataBuffer}, width, height, scanline stride,
 688      *         bank indices and band offsets.
 689      * @throws RasterFormatException if {@code w} or {@code h}
 690      *         is less than or equal to zero, or computing either
 691      *         {@code location.x + w} or
 692      *         {@code location.y + h} results in integer
 693      *         overflow
 694      * @throws IllegalArgumentException if {@code dataType} is not
 695      *         one of the supported data types, which are
 696      *         {@code DataBuffer.TYPE_BYTE},
 697      *         {@code DataBuffer.TYPE_USHORT}
 698      *         or {@code DataBuffer.TYPE_INT}
 699      * @throws NullPointerException if {@code dataBuffer} is null
 700      */
 701     public static WritableRaster createBandedRaster(DataBuffer dataBuffer,
 702                                                     int w, int h,
 703                                                     int scanlineStride,
 704                                                     int bankIndices[],
 705                                                     int bandOffsets[],
 706                                                     Point location)
 707     {
 708         if (dataBuffer == null) {
 709             throw new NullPointerException("DataBuffer cannot be null");
 710         }
 711         if (location == null) {
 712            location = new Point(0,0);
 713         }
 714         int dataType = dataBuffer.getDataType();
 715 
 716         int bands = bankIndices.length;
 717         if (bandOffsets.length != bands) {
 718             throw new IllegalArgumentException(
 719                                    "bankIndices.length != bandOffsets.length");
 720         }
 721 
 722         BandedSampleModel bsm =
 723             new BandedSampleModel(dataType, w, h,
 724                                   scanlineStride,
 725                                   bankIndices, bandOffsets);
 726 
 727         switch(dataType) {
 728         case DataBuffer.TYPE_BYTE:
 729             if (dataBuffer instanceof DataBufferByte) {
 730                 return new ByteBandedRaster(bsm, 
 731                         (DataBufferByte) dataBuffer, location);
 732             }
 733             break;
 734 
 735         case DataBuffer.TYPE_USHORT:
 736             if (dataBuffer instanceof DataBufferUShort) {
 737                 return new ShortBandedRaster(bsm,
 738                         (DataBufferUShort) dataBuffer, location);
 739             }
 740             break;
 741 
 742         case DataBuffer.TYPE_INT:
 743             if (dataBuffer instanceof DataBufferInt) {
 744                 return new SunWritableRaster(bsm, 
 745                         (DataBufferInt) dataBuffer, location);
 746             }
 747             break;
 748 
 749         default:
 750             throw new IllegalArgumentException("Unsupported data type " +
 751                                                 dataType);
 752         }
 753         
 754         // Create the generic raster
 755         return new SunWritableRaster(bsm, dataBuffer, location);
 756     }
 757 
 758     /**
 759      * Creates a Raster based on a SinglePixelPackedSampleModel with
 760      * the specified DataBuffer, width, height, scanline stride, and
 761      * band masks.  The number of bands is inferred from bandMasks.length.
 762      * The upper left corner of the Raster is given by
 763      * the location argument.  If location is null, (0, 0) will be used.
 764      * @param dataBuffer the {@code DataBuffer} that contains the
 765      *        image data
 766      * @param w         the width in pixels of the image data
 767      * @param h         the height in pixels of the image data
 768      * @param scanlineStride the line stride of the image data
 769      * @param bandMasks an array containing an entry for each band
 770      * @param location  the upper-left corner of the {@code Raster}
 771      * @return a WritableRaster object with the specified
 772      *         {@code DataBuffer}, width, height, scanline stride,
 773      *         and band masks.
 774      * @throws RasterFormatException if {@code w} or {@code h}
 775      *         is less than or equal to zero, or computing either
 776      *         {@code location.x + w} or
 777      *         {@code location.y + h} results in integer
 778      *         overflow
 779      * @throws IllegalArgumentException if {@code dataType} is not
 780      *         one of the supported data types, which are
 781      *         {@code DataBuffer.TYPE_BYTE},
 782      *         {@code DataBuffer.TYPE_USHORT}
 783      *         or {@code DataBuffer.TYPE_INT}
 784      * @throws RasterFormatException if {@code dataBuffer} has more
 785      *         than one bank.
 786      * @throws NullPointerException if {@code dataBuffer} is null
 787      */
 788     public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
 789                                                     int w, int h,
 790                                                     int scanlineStride,
 791                                                     int bandMasks[],
 792                                                     Point location)
 793     {
 794         if (dataBuffer == null) {
 795             throw new NullPointerException("DataBuffer cannot be null");
 796         }
 797         if (location == null) {
 798            location = new Point(0,0);
 799         }
 800         int dataType = dataBuffer.getDataType();
 801 
 802         SinglePixelPackedSampleModel sppsm =
 803             new SinglePixelPackedSampleModel(dataType, w, h, scanlineStride,
 804                                              bandMasks);
 805 
 806         switch(dataType) {
 807         case DataBuffer.TYPE_BYTE:
 808             if (dataBuffer instanceof DataBufferByte) {
 809                 return new ByteInterleavedRaster(sppsm,
 810                         (DataBufferByte) dataBuffer, location);
 811             }
 812             break;
 813 
 814         case DataBuffer.TYPE_USHORT:
 815             if (dataBuffer instanceof DataBufferUShort) {
 816                 return new ShortInterleavedRaster(sppsm,
 817                         (DataBufferUShort) dataBuffer, location);
 818             }
 819             break;
 820 
 821         case DataBuffer.TYPE_INT:
 822             if (dataBuffer instanceof DataBufferInt) {
 823                 return new IntegerInterleavedRaster(sppsm,
 824                         (DataBufferInt) dataBuffer, location);
 825             }
 826             break;
 827 
 828         default:
 829             throw new IllegalArgumentException("Unsupported data type " +
 830                                                 dataType);
 831         }
 832         
 833         // Create the generic raster
 834         return new SunWritableRaster(sppsm, dataBuffer, location);
 835     }
 836 
 837     /**
 838      * Creates a Raster based on a MultiPixelPackedSampleModel with the
 839      * specified DataBuffer, width, height, and bits per pixel.  The upper
 840      * left corner of the Raster is given by the location argument.  If
 841      * location is null, (0, 0) will be used.
 842      * @param dataBuffer the {@code DataBuffer} that contains the
 843      *        image data
 844      * @param w         the width in pixels of the image data
 845      * @param h         the height in pixels of the image data
 846      * @param bitsPerPixel the number of bits for each pixel
 847      * @param location  the upper-left corner of the {@code Raster}
 848      * @return a WritableRaster object with the specified
 849      *         {@code DataBuffer}, width, height, and
 850      *         bits per pixel.
 851      * @throws RasterFormatException if {@code w} or {@code h}
 852      *         is less than or equal to zero, or computing either
 853      *         {@code location.x + w} or
 854      *         {@code location.y + h} results in integer
 855      *         overflow
 856      * @throws IllegalArgumentException if {@code dataType} is not
 857      *         one of the supported data types, which are
 858      *         {@code DataBuffer.TYPE_BYTE},
 859      *         {@code DataBuffer.TYPE_USHORT}
 860      *         or {@code DataBuffer.TYPE_INT}
 861      * @throws RasterFormatException if {@code dataBuffer} has more
 862      *         than one bank.
 863      * @throws NullPointerException if {@code dataBuffer} is null
 864      */
 865     public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
 866                                                     int w, int h,
 867                                                     int bitsPerPixel,
 868                                                     Point location)
 869     {
 870         if (dataBuffer == null) {
 871             throw new NullPointerException("DataBuffer cannot be null");
 872         }
 873         if (location == null) {
 874            location = new Point(0,0);
 875         }
 876         int dataType = dataBuffer.getDataType();
 877 
 878         if (dataType != DataBuffer.TYPE_BYTE &&
 879             dataType != DataBuffer.TYPE_USHORT &&
 880             dataType != DataBuffer.TYPE_INT) {
 881             throw new IllegalArgumentException("Unsupported data type " +
 882                                                dataType);
 883         }
 884 
 885         if (dataBuffer.getNumBanks() != 1) {
 886             throw new
 887                 RasterFormatException("DataBuffer for packed Rasters"+
 888                                       " must only have 1 bank.");
 889         }
 890 
 891         MultiPixelPackedSampleModel mppsm =
 892                 new MultiPixelPackedSampleModel(dataType, w, h, bitsPerPixel);
 893 
 894         if (dataBuffer instanceof DataBufferByte &&
 895             (bitsPerPixel == 1 || bitsPerPixel == 2 || bitsPerPixel == 4))
 896         {
 897             return new BytePackedRaster(mppsm, (DataBufferByte) dataBuffer, location);
 898         } else {
 899             return new SunWritableRaster(mppsm, dataBuffer, location);
 900         }
 901     }
 902 
 903 
 904     /**
 905      *  Creates a Raster with the specified SampleModel and DataBuffer.
 906      *  The upper left corner of the Raster is given by the location argument.
 907      *  If location is null, (0, 0) will be used.
 908      *  @param sm the specified {@code SampleModel}
 909      *  @param db the specified {@code DataBuffer}
 910      *  @param location the upper-left corner of the {@code Raster}
 911      *  @return a {@code Raster} with the specified
 912      *          {@code SampleModel}, {@code DataBuffer}, and
 913      *          location.
 914      * @throws RasterFormatException if computing either
 915      *         {@code location.x + sm.getWidth()} or
 916      *         {@code location.y + sm.getHeight()} results in integer
 917      *         overflow
 918      * @throws RasterFormatException if {@code db} has more
 919      *         than one bank and {@code sm} is a
 920      *         PixelInterleavedSampleModel, SinglePixelPackedSampleModel,
 921      *         or MultiPixelPackedSampleModel.
 922      *  @throws NullPointerException if either SampleModel or DataBuffer is
 923      *          null
 924      */
 925     public static Raster createRaster(SampleModel sm,
 926                                       DataBuffer db,
 927                                       Point location)
 928     {
 929         if ((sm == null) || (db == null)) {
 930             throw new NullPointerException("SampleModel and DataBuffer cannot be null");
 931         }
 932 
 933         if (location == null) {
 934            location = new Point(0,0);
 935         }
 936         int dataType = sm.getDataType();
 937 
 938         if (sm instanceof PixelInterleavedSampleModel) {
 939             switch(dataType) {
 940             case DataBuffer.TYPE_BYTE:
 941                 if (db instanceof DataBufferByte) {
 942                     return new ByteInterleavedRaster(sm, 
 943                             (DataBufferByte) db, location);
 944                 }
 945                 break;
 946 
 947             case DataBuffer.TYPE_USHORT:
 948                 if (db instanceof DataBufferUShort) {
 949                     return new ShortInterleavedRaster(sm, 
 950                             (DataBufferUShort) db, location);
 951                 }
 952                 break;
 953             }
 954         } else if (sm instanceof SinglePixelPackedSampleModel) {
 955             switch(dataType) {
 956             case DataBuffer.TYPE_BYTE:
 957                 if (db instanceof DataBufferByte) {
 958                     return new ByteInterleavedRaster(sm, 
 959                             (DataBufferByte) db, location);
 960                 }
 961                 break;
 962 
 963             case DataBuffer.TYPE_USHORT:
 964                 if (db instanceof DataBufferUShort) {
 965                     return new ShortInterleavedRaster(sm, 
 966                             (DataBufferUShort) db, location);
 967                 }
 968                 break;
 969 
 970             case DataBuffer.TYPE_INT:
 971                 if (db instanceof DataBufferInt) {
 972                     return new IntegerInterleavedRaster(sm, 
 973                             (DataBufferInt) db, location);
 974                 }
 975                 break;
 976             }
 977         } else if (sm instanceof MultiPixelPackedSampleModel &&
 978                    dataType == DataBuffer.TYPE_BYTE &&
 979                    db instanceof DataBufferByte &&
 980                    sm.getSampleSize(0) < 8)
 981         {
 982             return new BytePackedRaster(sm, (DataBufferByte) db, location);
 983         }
 984 
 985         // we couldn't do anything special - do the generic thing
 986         return new Raster(sm, db, location);

 987     }
 988 
 989     /**
 990      *  Creates a WritableRaster with the specified SampleModel.
 991      *  The upper left corner of the Raster is given by the location argument.
 992      *  If location is null, (0, 0) will be used.
 993      *  @param sm the specified {@code SampleModel}
 994      *  @param location the upper-left corner of the
 995      *         {@code WritableRaster}
 996      *  @return a {@code WritableRaster} with the specified
 997      *          {@code SampleModel} and location.
 998      *  @throws RasterFormatException if computing either
 999      *          {@code location.x + sm.getWidth()} or
1000      *          {@code location.y + sm.getHeight()} results in integer
1001      *          overflow
1002      */
1003     public static WritableRaster createWritableRaster(SampleModel sm,
1004                                                       Point location) {
1005         if (location == null) {
1006            location = new Point(0,0);


1015      *  If location is null, (0, 0) will be used.
1016      *  @param sm the specified {@code SampleModel}
1017      *  @param db the specified {@code DataBuffer}
1018      *  @param location the upper-left corner of the
1019      *         {@code WritableRaster}
1020      *  @return a {@code WritableRaster} with the specified
1021      *          {@code SampleModel}, {@code DataBuffer}, and
1022      *          location.
1023      * @throws RasterFormatException if computing either
1024      *         {@code location.x + sm.getWidth()} or
1025      *         {@code location.y + sm.getHeight()} results in integer
1026      *         overflow
1027      * @throws RasterFormatException if {@code db} has more
1028      *         than one bank and {@code sm} is a
1029      *         PixelInterleavedSampleModel, SinglePixelPackedSampleModel,
1030      *         or MultiPixelPackedSampleModel.
1031      * @throws NullPointerException if either SampleModel or DataBuffer is null
1032      */
1033     public static WritableRaster createWritableRaster(SampleModel sm,
1034                                                       DataBuffer db,
1035                                                       Point location)
1036     {
1037         if ((sm == null) || (db == null)) {
1038             throw new NullPointerException("SampleModel and DataBuffer cannot be null");
1039         }
1040         if (location == null) {
1041            location = new Point(0,0);
1042         }
1043 
1044         int dataType = sm.getDataType();
1045 
1046         if (sm instanceof PixelInterleavedSampleModel) {
1047             switch(dataType) {
1048             case DataBuffer.TYPE_BYTE:
1049                 if (db instanceof DataBufferByte) {
1050                     return new ByteInterleavedRaster(sm,
1051                             (DataBufferByte) db, location);
1052                 }
1053                 break;
1054 
1055             case DataBuffer.TYPE_USHORT:
1056                 if (db instanceof DataBufferUShort) {
1057                     return new ShortInterleavedRaster(sm,
1058                             (DataBufferUShort) db, location);
1059                 }
1060                 break;
1061             }
1062         } else if (sm instanceof SinglePixelPackedSampleModel) {
1063             switch(dataType) {
1064             case DataBuffer.TYPE_BYTE:
1065                 if (db instanceof DataBufferByte) {
1066                     return new ByteInterleavedRaster(sm,
1067                             (DataBufferByte) db, location);
1068                 }
1069                 break;
1070 
1071             case DataBuffer.TYPE_USHORT:
1072                 if (db instanceof DataBufferUShort) {
1073                     return new ShortInterleavedRaster(sm, 
1074                             (DataBufferUShort) db, location);
1075                 }
1076                 break;
1077 
1078             case DataBuffer.TYPE_INT:
1079                 if (db instanceof DataBufferInt) {
1080                     return new IntegerInterleavedRaster(sm,
1081                             (DataBufferInt) db, location);
1082                 }
1083                 break;
1084             }
1085         } else if (sm instanceof MultiPixelPackedSampleModel &&
1086                    dataType == DataBuffer.TYPE_BYTE &&
1087                    db instanceof DataBufferByte &&
1088                    sm.getSampleSize(0) < 8)
1089         {
1090             return new BytePackedRaster(sm, (DataBufferByte) db, location);
1091         }
1092 
1093         // we couldn't do anything special - do the generic thing
1094         return new SunWritableRaster(sm, db, location);

1095     }
1096 
1097     /**
1098      *  Constructs a Raster with the given SampleModel.  The Raster's
1099      *  upper left corner is origin and it is the same size as the
1100      *  SampleModel.  A DataBuffer large enough to describe the
1101      *  Raster is automatically created.
1102      *  @param sampleModel     The SampleModel that specifies the layout
1103      *  @param origin          The Point that specified the origin
1104      *  @throws RasterFormatException if computing either
1105      *          {@code origin.x + sampleModel.getWidth()} or
1106      *          {@code origin.y + sampleModel.getHeight()} results in
1107      *          integer overflow
1108      *  @throws NullPointerException either {@code sampleModel} or
1109      *          {@code origin} is null
1110      */
1111     protected Raster(SampleModel sampleModel,
1112                      Point origin) {
1113         this(sampleModel,
1114              sampleModel.createDataBuffer(),


< prev index next >