< 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             break;
 744 
 745         default:
 746             throw new IllegalArgumentException("Unsupported data type " +
 747                                                 dataType);
 748         }
 749         
 750         // Create the generic raster
 751         return new SunWritableRaster(bsm, dataBuffer, location);
 752     }
 753 
 754     /**
 755      * Creates a Raster based on a SinglePixelPackedSampleModel with
 756      * the specified DataBuffer, width, height, scanline stride, and
 757      * band masks.  The number of bands is inferred from bandMasks.length.
 758      * The upper left corner of the Raster is given by
 759      * the location argument.  If location is null, (0, 0) will be used.
 760      * @param dataBuffer the {@code DataBuffer} that contains the
 761      *        image data
 762      * @param w         the width in pixels of the image data
 763      * @param h         the height in pixels of the image data
 764      * @param scanlineStride the line stride of the image data
 765      * @param bandMasks an array containing an entry for each band
 766      * @param location  the upper-left corner of the {@code Raster}
 767      * @return a WritableRaster object with the specified
 768      *         {@code DataBuffer}, width, height, scanline stride,
 769      *         and band masks.
 770      * @throws RasterFormatException if {@code w} or {@code h}
 771      *         is less than or equal to zero, or computing either
 772      *         {@code location.x + w} or
 773      *         {@code location.y + h} results in integer
 774      *         overflow
 775      * @throws IllegalArgumentException if {@code dataType} is not
 776      *         one of the supported data types, which are
 777      *         {@code DataBuffer.TYPE_BYTE},
 778      *         {@code DataBuffer.TYPE_USHORT}
 779      *         or {@code DataBuffer.TYPE_INT}
 780      * @throws RasterFormatException if {@code dataBuffer} has more
 781      *         than one bank.
 782      * @throws NullPointerException if {@code dataBuffer} is null
 783      */
 784     public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
 785                                                     int w, int h,
 786                                                     int scanlineStride,
 787                                                     int bandMasks[],
 788                                                     Point location)
 789     {
 790         if (dataBuffer == null) {
 791             throw new NullPointerException("DataBuffer cannot be null");
 792         }
 793         if (location == null) {
 794            location = new Point(0,0);
 795         }
 796         int dataType = dataBuffer.getDataType();
 797 
 798         SinglePixelPackedSampleModel sppsm =
 799             new SinglePixelPackedSampleModel(dataType, w, h, scanlineStride,
 800                                              bandMasks);
 801 
 802         switch(dataType) {
 803         case DataBuffer.TYPE_BYTE:
 804             if (dataBuffer instanceof DataBufferByte) {
 805                 return new ByteInterleavedRaster(sppsm,
 806                         (DataBufferByte) dataBuffer, location);
 807             }
 808             break;
 809 
 810         case DataBuffer.TYPE_USHORT:
 811             if (dataBuffer instanceof DataBufferUShort) {
 812                 return new ShortInterleavedRaster(sppsm,
 813                         (DataBufferUShort) dataBuffer, location);
 814             }
 815             break;
 816 
 817         case DataBuffer.TYPE_INT:
 818             if (dataBuffer instanceof DataBufferInt) {
 819                 return new IntegerInterleavedRaster(sppsm,
 820                         (DataBufferInt) dataBuffer, location);
 821             }
 822             break;
 823 
 824         default:
 825             throw new IllegalArgumentException("Unsupported data type " +
 826                                                 dataType);
 827         }
 828         
 829         // Create the generic raster
 830         return new SunWritableRaster(sppsm, dataBuffer, location);
 831     }
 832 
 833     /**
 834      * Creates a Raster based on a MultiPixelPackedSampleModel with the
 835      * specified DataBuffer, width, height, and bits per pixel.  The upper
 836      * left corner of the Raster is given by the location argument.  If
 837      * location is null, (0, 0) will be used.
 838      * @param dataBuffer the {@code DataBuffer} that contains the
 839      *        image data
 840      * @param w         the width in pixels of the image data
 841      * @param h         the height in pixels of the image data
 842      * @param bitsPerPixel the number of bits for each pixel
 843      * @param location  the upper-left corner of the {@code Raster}
 844      * @return a WritableRaster object with the specified
 845      *         {@code DataBuffer}, width, height, and
 846      *         bits per pixel.
 847      * @throws RasterFormatException if {@code w} or {@code h}
 848      *         is less than or equal to zero, or computing either
 849      *         {@code location.x + w} or
 850      *         {@code location.y + h} results in integer
 851      *         overflow
 852      * @throws IllegalArgumentException if {@code dataType} is not
 853      *         one of the supported data types, which are
 854      *         {@code DataBuffer.TYPE_BYTE},
 855      *         {@code DataBuffer.TYPE_USHORT}
 856      *         or {@code DataBuffer.TYPE_INT}
 857      * @throws RasterFormatException if {@code dataBuffer} has more
 858      *         than one bank.
 859      * @throws NullPointerException if {@code dataBuffer} is null
 860      */
 861     public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
 862                                                     int w, int h,
 863                                                     int bitsPerPixel,
 864                                                     Point location)
 865     {
 866         if (dataBuffer == null) {
 867             throw new NullPointerException("DataBuffer cannot be null");
 868         }
 869         if (location == null) {
 870            location = new Point(0,0);
 871         }
 872         int dataType = dataBuffer.getDataType();
 873 
 874         if (dataType != DataBuffer.TYPE_BYTE &&
 875             dataType != DataBuffer.TYPE_USHORT &&
 876             dataType != DataBuffer.TYPE_INT) {
 877             throw new IllegalArgumentException("Unsupported data type " +
 878                                                dataType);
 879         }
 880 
 881         if (dataBuffer.getNumBanks() != 1) {
 882             throw new
 883                 RasterFormatException("DataBuffer for packed Rasters"+
 884                                       " must only have 1 bank.");
 885         }
 886 
 887         MultiPixelPackedSampleModel mppsm =
 888                 new MultiPixelPackedSampleModel(dataType, w, h, bitsPerPixel);
 889 
 890         if (dataBuffer instanceof DataBufferByte &&
 891             (bitsPerPixel == 1 || bitsPerPixel == 2 || bitsPerPixel == 4))
 892         {
 893             return new BytePackedRaster(mppsm, (DataBufferByte) dataBuffer, location);
 894         } else {
 895             return new SunWritableRaster(mppsm, dataBuffer, location);
 896         }
 897     }
 898 
 899 
 900     /**
 901      *  Creates a Raster with the specified SampleModel and DataBuffer.
 902      *  The upper left corner of the Raster is given by the location argument.
 903      *  If location is null, (0, 0) will be used.
 904      *  @param sm the specified {@code SampleModel}
 905      *  @param db the specified {@code DataBuffer}
 906      *  @param location the upper-left corner of the {@code Raster}
 907      *  @return a {@code Raster} with the specified
 908      *          {@code SampleModel}, {@code DataBuffer}, and
 909      *          location.
 910      * @throws RasterFormatException if computing either
 911      *         {@code location.x + sm.getWidth()} or
 912      *         {@code location.y + sm.getHeight()} results in integer
 913      *         overflow
 914      * @throws RasterFormatException if {@code db} has more
 915      *         than one bank and {@code sm} is a
 916      *         PixelInterleavedSampleModel, SinglePixelPackedSampleModel,
 917      *         or MultiPixelPackedSampleModel.
 918      *  @throws NullPointerException if either SampleModel or DataBuffer is
 919      *          null
 920      */
 921     public static Raster createRaster(SampleModel sm,
 922                                       DataBuffer db,
 923                                       Point location)
 924     {
 925         if ((sm == null) || (db == null)) {
 926             throw new NullPointerException("SampleModel and DataBuffer cannot be null");
 927         }
 928 
 929         if (location == null) {
 930            location = new Point(0,0);
 931         }
 932         int dataType = sm.getDataType();
 933 
 934         if (sm instanceof PixelInterleavedSampleModel) {
 935             switch(dataType) {
 936             case DataBuffer.TYPE_BYTE:
 937                 if (db instanceof DataBufferByte) {
 938                     return new ByteInterleavedRaster(sm, 
 939                             (DataBufferByte) db, location);
 940                 }
 941                 break;
 942 
 943             case DataBuffer.TYPE_USHORT:
 944                 if (db instanceof DataBufferUShort) {
 945                     return new ShortInterleavedRaster(sm, 
 946                             (DataBufferUShort) db, location);
 947                 }
 948                 break;
 949             }
 950         } else if (sm instanceof SinglePixelPackedSampleModel) {
 951             switch(dataType) {
 952             case DataBuffer.TYPE_BYTE:
 953                 if (db instanceof DataBufferByte) {
 954                     return new ByteInterleavedRaster(sm, 
 955                             (DataBufferByte) db, location);
 956                 }
 957                 break;
 958 
 959             case DataBuffer.TYPE_USHORT:
 960                 if (db instanceof DataBufferUShort) {
 961                     return new ShortInterleavedRaster(sm, 
 962                             (DataBufferUShort) db, location);
 963                 }
 964                 break;
 965 
 966             case DataBuffer.TYPE_INT:
 967                 if (db instanceof DataBufferInt) {
 968                     return new IntegerInterleavedRaster(sm, 
 969                             (DataBufferInt) db, location);
 970                 }
 971                 break;
 972             }
 973         } else if (sm instanceof MultiPixelPackedSampleModel &&
 974                    dataType == DataBuffer.TYPE_BYTE &&
 975                    db instanceof DataBufferByte &&
 976                    sm.getSampleSize(0) < 8)
 977         {
 978             return new BytePackedRaster(sm, (DataBufferByte) db, location);
 979         }
 980 
 981         // we couldn't do anything special - do the generic thing
 982         return new Raster(sm, db, location);

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


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

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


< prev index next >