< 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


 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.


 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


 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


 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


 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);


 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,


   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


 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             if (dataBuffer instanceof DataBufferByte) {
 649                 return new ByteInterleavedRaster(csm, 
 650                         (DataBufferByte)dataBuffer, location);
 651             }
 652             break;
 653 
 654         case DataBuffer.TYPE_USHORT:
 655             if (dataBuffer instanceof DataBufferUShort) {
 656                 return new ShortInterleavedRaster(csm,
 657                         (DataBufferUShort)dataBuffer, location);
 658             }
 659             break;
 660 
 661         default:
 662             throw new IllegalArgumentException("Unsupported data type " +
 663                                                 dataType);
 664         }
 665         
 666         // Create the generic raster
 667         return new SunWritableRaster(csm, dataBuffer, location);
 668     }
 669 
 670     /**
 671      * Creates a Raster based on a BandedSampleModel with the
 672      * specified DataBuffer, width, height, scanline stride, bank
 673      * indices, and band offsets.  The number of bands is inferred
 674      * from bankIndices.length and bandOffsets.length, which must be
 675      * the same.  The upper left corner of the Raster is given by the
 676      * location argument.  If location is null, (0, 0) will be used.
 677      * @param dataBuffer the {@code DataBuffer} that contains the
 678      *        image data
 679      * @param w         the width in pixels of the image data
 680      * @param h         the height in pixels of the image data
 681      * @param scanlineStride the line stride of the image data
 682      * @param bankIndices the bank indices for each band
 683      * @param bandOffsets the offsets of all bands
 684      * @param location  the upper-left corner of the {@code Raster}
 685      * @return a WritableRaster object with the specified
 686      *         {@code DataBuffer}, width, height, scanline stride,
 687      *         bank indices and band offsets.


 707             throw new NullPointerException("DataBuffer cannot be null");
 708         }
 709         if (location == null) {
 710            location = new Point(0,0);
 711         }
 712         int dataType = dataBuffer.getDataType();
 713 
 714         int bands = bankIndices.length;
 715         if (bandOffsets.length != bands) {
 716             throw new IllegalArgumentException(
 717                                    "bankIndices.length != bandOffsets.length");
 718         }
 719 
 720         BandedSampleModel bsm =
 721             new BandedSampleModel(dataType, w, h,
 722                                   scanlineStride,
 723                                   bankIndices, bandOffsets);
 724 
 725         switch(dataType) {
 726         case DataBuffer.TYPE_BYTE:
 727             if (dataBuffer instanceof DataBufferByte) {
 728                 return new ByteBandedRaster(bsm, 
 729                         (DataBufferByte)dataBuffer, location);
 730             }
 731             break;
 732 
 733         case DataBuffer.TYPE_USHORT:
 734             if (dataBuffer instanceof DataBufferUShort) {
 735                 return new ShortBandedRaster(bsm,
 736                         (DataBufferUShort)dataBuffer, location);
 737             }
 738             break;
 739 
 740         case DataBuffer.TYPE_INT:
 741             if (dataBuffer instanceof DataBufferInt) {
 742                 return new SunWritableRaster(bsm, 
 743                         (DataBufferInt)dataBuffer, location);
 744             }
 745             break;
 746 
 747         default:
 748             throw new IllegalArgumentException("Unsupported data type " +
 749                                                 dataType);
 750         }
 751         
 752         // Create the generic raster
 753         return new SunWritableRaster(bsm, dataBuffer, location);
 754     }
 755 
 756     /**
 757      * Creates a Raster based on a SinglePixelPackedSampleModel with
 758      * the specified DataBuffer, width, height, scanline stride, and
 759      * band masks.  The number of bands is inferred from bandMasks.length.
 760      * The upper left corner of the Raster is given by
 761      * the location argument.  If location is null, (0, 0) will be used.
 762      * @param dataBuffer the {@code DataBuffer} that contains the
 763      *        image data
 764      * @param w         the width in pixels of the image data
 765      * @param h         the height in pixels of the image data
 766      * @param scanlineStride the line stride of the image data
 767      * @param bandMasks an array containing an entry for each band
 768      * @param location  the upper-left corner of the {@code Raster}
 769      * @return a WritableRaster object with the specified
 770      *         {@code DataBuffer}, width, height, scanline stride,
 771      *         and band masks.
 772      * @throws RasterFormatException if {@code w} or {@code h}
 773      *         is less than or equal to zero, or computing either


 785      */
 786     public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
 787                                                     int w, int h,
 788                                                     int scanlineStride,
 789                                                     int bandMasks[],
 790                                                     Point location) {
 791         if (dataBuffer == null) {
 792             throw new NullPointerException("DataBuffer cannot be null");
 793         }
 794         if (location == null) {
 795            location = new Point(0,0);
 796         }
 797         int dataType = dataBuffer.getDataType();
 798 
 799         SinglePixelPackedSampleModel sppsm =
 800             new SinglePixelPackedSampleModel(dataType, w, h, scanlineStride,
 801                                              bandMasks);
 802 
 803         switch(dataType) {
 804         case DataBuffer.TYPE_BYTE:
 805             if(dataBuffer instanceof DataBufferByte) {
 806                 return new ByteInterleavedRaster(sppsm,
 807                         (DataBufferByte)dataBuffer, location);
 808             }
 809             break;
 810 
 811         case DataBuffer.TYPE_USHORT:
 812             if(dataBuffer instanceof DataBufferUShort) {
 813                 return new ShortInterleavedRaster(sppsm,
 814                         (DataBufferUShort) dataBuffer, location);
 815             }
 816             break;
 817 
 818         case DataBuffer.TYPE_INT:
 819             if(dataBuffer instanceof DataBufferInt) {
 820                 return new IntegerInterleavedRaster(sppsm,
 821                         (DataBufferInt) dataBuffer, location);
 822             }
 823             break;
 824 
 825         default:
 826             throw new IllegalArgumentException("Unsupported data type " +
 827                                                 dataType);
 828         }
 829         
 830         // Create the generic raster
 831         return new SunWritableRaster(sppsm, dataBuffer, location);
 832     }
 833 
 834     /**
 835      * Creates a Raster based on a MultiPixelPackedSampleModel with the
 836      * specified DataBuffer, width, height, and bits per pixel.  The upper
 837      * left corner of the Raster is given by the location argument.  If
 838      * location is null, (0, 0) will be used.
 839      * @param dataBuffer the {@code DataBuffer} that contains the
 840      *        image data
 841      * @param w         the width in pixels of the image data
 842      * @param h         the height in pixels of the image data
 843      * @param bitsPerPixel the number of bits for each pixel
 844      * @param location  the upper-left corner of the {@code Raster}
 845      * @return a WritableRaster object with the specified
 846      *         {@code DataBuffer}, width, height, and
 847      *         bits per pixel.
 848      * @throws RasterFormatException if {@code w} or {@code h}
 849      *         is less than or equal to zero, or computing either
 850      *         {@code location.x + w} or
 851      *         {@code location.y + h} results in integer


 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 (dataType == DataBuffer.TYPE_BYTE &&
 891             dataBuffer instanceof DataBufferByte &&
 892             (bitsPerPixel == 1 || bitsPerPixel == 2 || bitsPerPixel == 4)) {
 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


 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         if ((sm == null) || (db == null)) {
 925             throw new NullPointerException("SampleModel and DataBuffer cannot be null");
 926         }
 927 
 928         if (location == null) {
 929            location = new Point(0,0);
 930         }
 931         int dataType = sm.getDataType();
 932 
 933         if (sm instanceof PixelInterleavedSampleModel) {
 934             switch(dataType) {
 935                 case DataBuffer.TYPE_BYTE:
 936                     if(db instanceof DataBufferByte) {
 937                         return new ByteInterleavedRaster(sm, 
 938                                 (DataBufferByte)db, location);
 939                     }
 940                     break;
 941 
 942                 case DataBuffer.TYPE_USHORT:
 943                     if(db instanceof DataBufferUShort) {
 944                         return new ShortInterleavedRaster(sm, 
 945                                 (DataBufferUShort)db, location);
 946                     }
 947                     break;
 948             }
 949         } else if (sm instanceof SinglePixelPackedSampleModel) {
 950             switch(dataType) {
 951                 case DataBuffer.TYPE_BYTE:
 952                     if(db instanceof DataBufferByte) {
 953                         return new ByteInterleavedRaster(sm, 
 954                                 (DataBufferByte)db, location);
 955                     }
 956                     break;
 957 
 958                 case DataBuffer.TYPE_USHORT:
 959                     if(db instanceof DataBufferUShort) {
 960                         return new ShortInterleavedRaster(sm, 
 961                                 (DataBufferUShort)db, location);
 962                     }
 963                     break;                    
 964 
 965                 case DataBuffer.TYPE_INT:
 966                     if(db instanceof DataBufferInt) {
 967                         return new IntegerInterleavedRaster(sm, 
 968                                 (DataBufferInt)db, location);
 969                     }
 970                     break;
 971             }
 972         } else if (sm instanceof MultiPixelPackedSampleModel &&
 973                    dataType == DataBuffer.TYPE_BYTE &&
 974                    db instanceof DataBufferByte &&
 975                    sm.getSampleSize(0) < 8) {
 976             return new BytePackedRaster(sm, (DataBufferByte)db, location);
 977         }
 978 
 979         // we couldn't do anything special - do the generic thing
 980         return new Raster(sm, db, location);

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


1022      *         than one bank and {@code sm} is a
1023      *         PixelInterleavedSampleModel, SinglePixelPackedSampleModel,
1024      *         or MultiPixelPackedSampleModel.
1025      * @throws NullPointerException if either SampleModel or DataBuffer is null
1026      */
1027     public static WritableRaster createWritableRaster(SampleModel sm,
1028                                                       DataBuffer db,
1029                                                       Point location) {
1030         if ((sm == null) || (db == null)) {
1031             throw new NullPointerException("SampleModel and DataBuffer cannot be null");
1032         }
1033         if (location == null) {
1034            location = new Point(0,0);
1035         }
1036 
1037         int dataType = sm.getDataType();
1038 
1039         if (sm instanceof PixelInterleavedSampleModel) {
1040             switch(dataType) {
1041                 case DataBuffer.TYPE_BYTE:
1042                     if (db instanceof DataBufferByte) {
1043                         return new ByteInterleavedRaster(sm,
1044                                 (DataBufferByte)db, location);
1045                     }
1046                     break;
1047 
1048                 case DataBuffer.TYPE_USHORT:
1049                     if (db instanceof DataBufferUShort) {
1050                         return new ShortInterleavedRaster(sm,
1051                                 (DataBufferUShort)db, location);
1052                     }
1053                     break;
1054             }
1055         } else if (sm instanceof SinglePixelPackedSampleModel) {
1056             switch(dataType) {
1057                 case DataBuffer.TYPE_BYTE:
1058                     if (db instanceof DataBufferByte) {
1059                         return new ByteInterleavedRaster(sm,
1060                                 (DataBufferByte)db, location);
1061                     }
1062                     break;
1063 
1064                 case DataBuffer.TYPE_USHORT:
1065                     if (db instanceof DataBufferUShort) {
1066                         return new ShortInterleavedRaster(sm, 
1067                                 (DataBufferUShort)db, location);
1068                     }
1069                     break;
1070 
1071                 case DataBuffer.TYPE_INT:
1072                     if (db instanceof DataBufferInt) {
1073                         return new IntegerInterleavedRaster(sm,
1074                                 (DataBufferInt)db, location);
1075                     }
1076                     break;
1077             }
1078         } else if (sm instanceof MultiPixelPackedSampleModel &&
1079                    dataType == DataBuffer.TYPE_BYTE &&
1080                    db instanceof DataBufferByte &&
1081                    sm.getSampleSize(0) < 8) {
1082             return new BytePackedRaster(sm, (DataBufferByte)db, location);
1083         }
1084 
1085         // we couldn't do anything special - do the generic thing

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


< prev index next >