< prev index next >

src/java.desktop/share/classes/sun/awt/image/ShortInterleavedRaster.java

Print this page


   1 /*
   2  * Copyright (c) 1998, 2014, 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


  54  */
  55 public class ShortInterleavedRaster extends ShortComponentRaster {
  56 
  57     /** A cached copy of minX + width for use in bounds checks. */
  58     private int maxX;
  59 
  60     /** A cached copy of minY + height for use in bounds checks. */
  61     private int maxY;
  62 
  63     /**
  64      *  Constructs a ShortInterleavedRaster with the given SampleModel.
  65      *  The Raster's upper left corner is origin and it is the same
  66      *  size as the SampleModel.  A DataBuffer large enough to describe the
  67      *  Raster is automatically created.  SampleModel must be of type
  68      *  PixelInterleavedSampleModel or SinglePixelPackedSampleModel.
  69      *  @param sampleModel     The SampleModel that specifies the layout.
  70      *  @param origin          The Point that specified the origin.
  71      */
  72     public ShortInterleavedRaster(SampleModel sampleModel, Point origin) {
  73         this(sampleModel,
  74              sampleModel.createDataBuffer(),
  75              new Rectangle(origin.x,
  76                            origin.y,
  77                            sampleModel.getWidth(),
  78                            sampleModel.getHeight()),
  79              origin,
  80              null);
  81     }
  82 
  83     /**
  84      * Constructs a ShortInterleavedRaster with the given SampleModel
  85      * and DataBuffer.  The Raster's upper left corner is origin and
  86      * it is the same sizes the SampleModel.  The DataBuffer is not
  87      * initialized and must be a DataBufferUShort compatible with SampleModel.
  88      * SampleModel must be of type PixelInterleavedSampleModel or
  89      * SinglePixelPackedSampleModel.
  90      * @param sampleModel     The SampleModel that specifies the layout.
  91      * @param dataBuffer      The DataBufferUShort that contains the image data.
  92      * @param origin          The Point that specifies the origin.
  93      */
  94     public ShortInterleavedRaster(SampleModel sampleModel,
  95                                    DataBuffer dataBuffer,
  96                                    Point origin) {
  97         this(sampleModel,
  98              dataBuffer,
  99              new Rectangle(origin.x,
 100                            origin.y,
 101                            sampleModel.getWidth(),
 102                            sampleModel.getHeight()),
 103              origin,
 104              null);
 105     }
 106 
 107     /**
 108      * Constructs a ShortInterleavedRaster with the given SampleModel,
 109      * DataBuffer, and parent.  DataBuffer must be a DataBufferUShort and
 110      * SampleModel must be of type PixelInterleavedSampleModel or
 111      * SinglePixelPackedSampleModel.  When translated into the base Raster's
 112      * coordinate system, aRegion must be contained by the base Raster.
 113      * Origin is the coodinate in the new Raster's coordinate system of
 114      * the origin of the base Raster.  (The base Raster is the Raster's
 115      * ancestor which has no parent.)
 116      *
 117      * Note that this constructor should generally be called by other
 118      * constructors or create methods, it should not be used directly.
 119      * @param sampleModel     The SampleModel that specifies the layout.
 120      * @param dataBuffer      The DataBufferUShort that contains the image data.
 121      * @param aRegion         The Rectangle that specifies the image area.
 122      * @param origin          The Point that specifies the origin.
 123      * @param parent          The parent (if any) of this raster.
 124      */
 125     public ShortInterleavedRaster(SampleModel sampleModel,
 126                                    DataBuffer dataBuffer,
 127                                    Rectangle aRegion,
 128                                    Point origin,
 129                                    ShortInterleavedRaster parent) {
 130 
 131         super(sampleModel, dataBuffer, aRegion, origin, parent);
 132         this.maxX = minX + width;
 133         this.maxY = minY + height;
 134 
 135         if(!(dataBuffer instanceof DataBufferUShort)) {
 136             throw new RasterFormatException("ShortInterleavedRasters must "+
 137                                             "have ushort DataBuffers");
 138         }
 139 
 140         DataBufferUShort dbus = (DataBufferUShort)dataBuffer;
 141         this.data = stealData(dbus, 0);
 142 
 143         // REMIND: need case for interleaved ComponentSampleModel
 144         if ((sampleModel instanceof PixelInterleavedSampleModel) ||
 145             (sampleModel instanceof ComponentSampleModel &&
 146              sampleModel.getNumBands() == 1)) {
 147             ComponentSampleModel csm = (ComponentSampleModel)sampleModel;
 148 
 149             this.scanlineStride = csm.getScanlineStride();
 150             this.pixelStride = csm.getPixelStride();
 151             this.dataOffsets = csm.getBandOffsets();
 152             int xOffset = aRegion.x - origin.x;
 153             int yOffset = aRegion.y - origin.y;
 154             for (int i = 0; i < getNumDataElements(); i++) {
 155                 dataOffsets[i] += xOffset*pixelStride+yOffset*scanlineStride;
 156             }
 157         } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
 158             SinglePixelPackedSampleModel sppsm =
 159                     (SinglePixelPackedSampleModel)sampleModel;
 160             this.scanlineStride = sppsm.getScanlineStride();
 161             this.pixelStride    = 1;
 162             this.dataOffsets = new int[1];
 163             this.dataOffsets[0] = dbus.getOffset();
 164             int xOffset = aRegion.x - origin.x;
 165             int yOffset = aRegion.y - origin.y;
 166             dataOffsets[0] += xOffset+yOffset*scanlineStride;
 167         } else {
 168             throw new RasterFormatException("ShortInterleavedRasters must "+
 169               "have PixelInterleavedSampleModel, SinglePixelPackedSampleModel"+
 170               " or 1 band ComponentSampleModel.  Sample model is "+
 171               sampleModel);
 172         }
 173         this.bandOffset = this.dataOffsets[0];
 174         verify();
 175     }
 176 
 177     /**
 178      * Returns a copy of the data offsets array. For each band the data offset
 179      * is the index into the band's data array, of the first sample of the
 180      * band.
 181      */
 182     public int[] getDataOffsets() {
 183         return dataOffsets.clone();


 713             throw new RasterFormatException("y lies outside the raster");
 714         }
 715         if ((x+width < x) || (x+width > this.minX + this.width)) {
 716             throw new RasterFormatException("(x + width) is outside of Raster");
 717         }
 718         if ((y+height < y) || (y+height > this.minY + this.height)) {
 719             throw new RasterFormatException("(y + height) is outside of Raster");
 720         }
 721 
 722         SampleModel sm;
 723 
 724         if (bandList != null)
 725             sm = sampleModel.createSubsetSampleModel(bandList);
 726         else
 727             sm = sampleModel;
 728 
 729         int deltaX = x0 - x;
 730         int deltaY = y0 - y;
 731 
 732         return new ShortInterleavedRaster(sm,
 733                                        dataBuffer,
 734                                        new Rectangle(x0, y0, width, height),
 735                                        new Point(sampleModelTranslateX+deltaX,
 736                                                  sampleModelTranslateY+deltaY),
 737                                        this);
 738     }
 739 
 740     /**
 741      * Creates a Raster with the same layout but using a different
 742      * width and height, and with new zeroed data arrays.
 743      */
 744     public WritableRaster createCompatibleWritableRaster(int w, int h) {
 745         if (w <= 0 || h <=0) {
 746             throw new RasterFormatException("negative "+
 747                                           ((w <= 0) ? "width" : "height"));
 748         }
 749 
 750         SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);
 751 
 752         return new ShortInterleavedRaster(sm, new Point(0, 0));
 753     }
   1 /*
   2  * Copyright (c) 1998, 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


  54  */
  55 public class ShortInterleavedRaster extends ShortComponentRaster {
  56 
  57     /** A cached copy of minX + width for use in bounds checks. */
  58     private int maxX;
  59 
  60     /** A cached copy of minY + height for use in bounds checks. */
  61     private int maxY;
  62 
  63     /**
  64      *  Constructs a ShortInterleavedRaster with the given SampleModel.
  65      *  The Raster's upper left corner is origin and it is the same
  66      *  size as the SampleModel.  A DataBuffer large enough to describe the
  67      *  Raster is automatically created.  SampleModel must be of type
  68      *  PixelInterleavedSampleModel or SinglePixelPackedSampleModel.
  69      *  @param sampleModel     The SampleModel that specifies the layout.
  70      *  @param origin          The Point that specified the origin.
  71      */
  72     public ShortInterleavedRaster(SampleModel sampleModel, Point origin) {
  73         this(sampleModel,
  74              (DataBufferUShort)sampleModel.createDataBuffer(),
  75              new Rectangle(origin.x,
  76                            origin.y,
  77                            sampleModel.getWidth(),
  78                            sampleModel.getHeight()),
  79              origin,
  80              null);
  81     }
  82 
  83     /**
  84      * Constructs a ShortInterleavedRaster with the given SampleModel
  85      * and DataBuffer.  The Raster's upper left corner is origin and
  86      * it is the same sizes the SampleModel.  The DataBuffer is not
  87      * initialized and must be a DataBufferUShort compatible with SampleModel.
  88      * SampleModel must be of type PixelInterleavedSampleModel or
  89      * SinglePixelPackedSampleModel.
  90      * @param sampleModel     The SampleModel that specifies the layout.
  91      * @param dataBuffer      The DataBufferUShort that contains the image data.
  92      * @param origin          The Point that specifies the origin.
  93      */
  94     public ShortInterleavedRaster(SampleModel sampleModel,
  95                                   DataBufferUShort dataBuffer,
  96                                   Point origin) {
  97         this(sampleModel,
  98              dataBuffer,
  99              new Rectangle(origin.x,
 100                            origin.y,
 101                            sampleModel.getWidth(),
 102                            sampleModel.getHeight()),
 103              origin,
 104              null);
 105     }
 106 
 107     /**
 108      * Constructs a ShortInterleavedRaster with the given SampleModel,
 109      * DataBuffer, and parent.  DataBuffer must be a DataBufferUShort and
 110      * SampleModel must be of type PixelInterleavedSampleModel or
 111      * SinglePixelPackedSampleModel.  When translated into the base Raster's
 112      * coordinate system, aRegion must be contained by the base Raster.
 113      * Origin is the coodinate in the new Raster's coordinate system of
 114      * the origin of the base Raster.  (The base Raster is the Raster's
 115      * ancestor which has no parent.)
 116      *
 117      * Note that this constructor should generally be called by other
 118      * constructors or create methods, it should not be used directly.
 119      * @param sampleModel     The SampleModel that specifies the layout.
 120      * @param dataBuffer      The DataBufferUShort that contains the image data.
 121      * @param aRegion         The Rectangle that specifies the image area.
 122      * @param origin          The Point that specifies the origin.
 123      * @param parent          The parent (if any) of this raster.
 124      */
 125     public ShortInterleavedRaster(SampleModel sampleModel,
 126                                   DataBufferUShort dataBuffer,
 127                                   Rectangle aRegion,
 128                                   Point origin,
 129                                   ShortInterleavedRaster parent) {
 130 
 131         super(sampleModel, dataBuffer, aRegion, origin, parent);
 132         this.maxX = minX + width;
 133         this.maxY = minY + height;
 134 
 135         this.data = stealData(dataBuffer, 0);






 136 
 137         // REMIND: need case for interleaved ComponentSampleModel
 138         if ((sampleModel instanceof PixelInterleavedSampleModel) ||
 139             (sampleModel instanceof ComponentSampleModel &&
 140              sampleModel.getNumBands() == 1)) {
 141             ComponentSampleModel csm = (ComponentSampleModel)sampleModel;
 142 
 143             this.scanlineStride = csm.getScanlineStride();
 144             this.pixelStride = csm.getPixelStride();
 145             this.dataOffsets = csm.getBandOffsets();
 146             int xOffset = aRegion.x - origin.x;
 147             int yOffset = aRegion.y - origin.y;
 148             for (int i = 0; i < getNumDataElements(); i++) {
 149                 dataOffsets[i] += xOffset*pixelStride+yOffset*scanlineStride;
 150             }
 151         } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
 152             SinglePixelPackedSampleModel sppsm =
 153                     (SinglePixelPackedSampleModel)sampleModel;
 154             this.scanlineStride = sppsm.getScanlineStride();
 155             this.pixelStride    = 1;
 156             this.dataOffsets = new int[1];
 157             this.dataOffsets[0] = dataBuffer.getOffset();
 158             int xOffset = aRegion.x - origin.x;
 159             int yOffset = aRegion.y - origin.y;
 160             dataOffsets[0] += xOffset+yOffset*scanlineStride;
 161         } else {
 162             throw new RasterFormatException("ShortInterleavedRasters must "+
 163               "have PixelInterleavedSampleModel, SinglePixelPackedSampleModel"+
 164               " or 1 band ComponentSampleModel.  Sample model is "+
 165               sampleModel);
 166         }
 167         this.bandOffset = this.dataOffsets[0];
 168         verify();
 169     }
 170 
 171     /**
 172      * Returns a copy of the data offsets array. For each band the data offset
 173      * is the index into the band's data array, of the first sample of the
 174      * band.
 175      */
 176     public int[] getDataOffsets() {
 177         return dataOffsets.clone();


 707             throw new RasterFormatException("y lies outside the raster");
 708         }
 709         if ((x+width < x) || (x+width > this.minX + this.width)) {
 710             throw new RasterFormatException("(x + width) is outside of Raster");
 711         }
 712         if ((y+height < y) || (y+height > this.minY + this.height)) {
 713             throw new RasterFormatException("(y + height) is outside of Raster");
 714         }
 715 
 716         SampleModel sm;
 717 
 718         if (bandList != null)
 719             sm = sampleModel.createSubsetSampleModel(bandList);
 720         else
 721             sm = sampleModel;
 722 
 723         int deltaX = x0 - x;
 724         int deltaY = y0 - y;
 725 
 726         return new ShortInterleavedRaster(sm,
 727                                        (DataBufferUShort)dataBuffer,
 728                                        new Rectangle(x0, y0, width, height),
 729                                        new Point(sampleModelTranslateX+deltaX,
 730                                                  sampleModelTranslateY+deltaY),
 731                                        this);
 732     }
 733 
 734     /**
 735      * Creates a Raster with the same layout but using a different
 736      * width and height, and with new zeroed data arrays.
 737      */
 738     public WritableRaster createCompatibleWritableRaster(int w, int h) {
 739         if (w <= 0 || h <=0) {
 740             throw new RasterFormatException("negative "+
 741                                           ((w <= 0) ? "width" : "height"));
 742         }
 743 
 744         SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);
 745 
 746         return new ShortInterleavedRaster(sm, new Point(0, 0));
 747     }
< prev index next >