< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1997, 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
  23  * questions.
  24  */
  25 
  26 package sun.awt.image;
  27 import java.awt.image.Raster;
  28 import java.awt.image.WritableRaster;
  29 import java.awt.image.RasterFormatException;
  30 import java.awt.image.SampleModel;
  31 import java.awt.image.BandedSampleModel;
  32 import java.awt.image.DataBuffer;
  33 import java.awt.image.DataBufferUShort;
  34 import java.awt.Rectangle;
  35 import java.awt.Point;
  36 
  37 /**
  38  * This class defines a Raster with pixels consisting of multiple 16-bit
  39  * samples stored in separate arrays for each band.  Operations on
  40  * sets of pixels are performed on a given band of each pixel
  41  * in the set before moving on to the next band.  The arrays used
  42  * for storage may be distinct or shared between some or all of
  43  * the bands.
  44  * There is only one pixel stride and one scanline stride for all
  45  * bands.  This type of Raster can be used with a
  46  * ComponentColorModel. This class requires a BandedSampleModel.
  47  *
  48  */
  49 public class ShortBandedRaster extends SunWritableRaster {
  50 
  51     /** Data offsets for each band of image data. */
  52     int[]         dataOffsets;


  58     short[][]     data;
  59 
  60     /** A cached copy of minX + width for use in bounds checks. */
  61     private int maxX;
  62 
  63     /** A cached copy of minY + height for use in bounds checks. */
  64     private int maxY;
  65 
  66     /**
  67      * Constructs a ShortBandedRaster with the given SampleModel.
  68      * The Raster's upper left corner is origin and it is the same
  69      * size as the SampleModel.  A DataBuffer large enough to describe the
  70      * Raster is automatically created.  SampleModel must be of type
  71      * BandedSampleModel.
  72      * @param sampleModel     The SampleModel that specifies the layout.
  73      * @param origin          The Point that specified the origin.
  74      */
  75     public ShortBandedRaster(SampleModel sampleModel,
  76                                 Point origin) {
  77         this(sampleModel,
  78              sampleModel.createDataBuffer(),
  79              new Rectangle(origin.x,
  80                            origin.y,
  81                            sampleModel.getWidth(),
  82                            sampleModel.getHeight()),
  83              origin,
  84              null);
  85     }
  86 
  87     /**
  88      * Constructs a ShortBandedRaster with the given SampleModel
  89      * and DataBuffer.  The Raster's upper left corner is origin and
  90      * it is the same size as the SampleModel.  The DataBuffer is not
  91      * initialized and must be a DataBufferUShort compatible with SampleModel.
  92      * SampleModel must be of type BandedSampleModel.
  93      * @param sampleModel     The SampleModel that specifies the layout.
  94      * @param dataBuffer      The DataBufferUShort that contains the image data.
  95      * @param origin          The Point that specifies the origin.
  96      */
  97     public ShortBandedRaster(SampleModel sampleModel,
  98                                 DataBuffer dataBuffer,
  99                                 Point origin) {
 100         this(sampleModel, dataBuffer,
 101              new Rectangle(origin.x, origin.y,
 102                            sampleModel.getWidth(),
 103                            sampleModel.getHeight()),
 104              origin, null);
 105     }
 106 
 107     /**
 108      * Constructs a ShortBandedRaster with the given SampleModel,
 109      * DataBuffer, and parent.  DataBuffer must be a DataBufferUShort and
 110      * SampleModel must be of type BandedSampleModel.
 111      * When translated into the base Raster's
 112      * coordinate system, aRegion must be contained by the base Raster.
 113      * Origin is the coordinate 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 ShortBandedRaster(SampleModel sampleModel,
 126                                 DataBuffer dataBuffer,
 127                                 Rectangle aRegion,
 128                                 Point origin,
 129                                 ShortBandedRaster parent) {
 130 
 131         super(sampleModel, dataBuffer, aRegion, origin, parent);
 132         this.maxX = minX + width;
 133         this.maxY = minY + height;
 134         if (!(dataBuffer instanceof DataBufferUShort)) {
 135            throw new RasterFormatException("ShortBandedRaster must have " +
 136                 "ushort DataBuffers");
 137         }
 138         DataBufferUShort dbus = (DataBufferUShort)dataBuffer;
 139 
 140         if (sampleModel instanceof BandedSampleModel) {
 141             BandedSampleModel bsm = (BandedSampleModel)sampleModel;
 142             this.scanlineStride = bsm.getScanlineStride();
 143             int bankIndices[] = bsm.getBankIndices();
 144             int bandOffsets[] = bsm.getBandOffsets();
 145             int dOffsets[] = dbus.getOffsets();
 146             dataOffsets = new int[bankIndices.length];
 147             data = new short[bankIndices.length][];
 148             int xOffset = aRegion.x - origin.x;
 149             int yOffset = aRegion.y - origin.y;
 150             for (int i = 0; i < bankIndices.length; i++) {
 151                data[i] = stealData(dbus, bankIndices[i]);
 152                dataOffsets[i] = dOffsets[bankIndices[i]] +
 153                    xOffset + yOffset*scanlineStride + bandOffsets[i];
 154             }
 155         } else {
 156             throw new RasterFormatException("ShortBandedRasters must have "+
 157                 "BandedSampleModels");
 158         }
 159         verify();
 160     }
 161 
 162     /**
 163      * Returns a copy of the data offsets array. For each band the data offset
 164      * is the index into the band's data array, of the first sample of the
 165      * band.
 166      */
 167     public int[] getDataOffsets() {
 168         return dataOffsets.clone();
 169     }
 170 
 171     /**


 653             throw new RasterFormatException("y lies outside raster");
 654         }
 655         if ((x+width < x) || (x+width > this.minX + this.width)) {
 656             throw new RasterFormatException("(x + width) is outside of Raster");
 657         }
 658         if ((y+height < y) || (y+height > this.minY + this.height)) {
 659             throw new RasterFormatException("(y + height) is outside of Raster");
 660         }
 661 
 662         SampleModel sm;
 663 
 664         if (bandList != null)
 665             sm = sampleModel.createSubsetSampleModel(bandList);
 666         else
 667             sm = sampleModel;
 668 
 669         int deltaX = x0 - x;
 670         int deltaY = y0 - y;
 671 
 672         return new ShortBandedRaster(sm,
 673                                      dataBuffer,
 674                                      new Rectangle(x0, y0, width, height),
 675                                      new Point(sampleModelTranslateX+deltaX,
 676                                                sampleModelTranslateY+deltaY),
 677                                      this);
 678 
 679     }
 680 
 681     /**
 682      * Creates a subraster given a region of the raster.  The x and y
 683      * coordinates specify the horizontal and vertical offsets
 684      * from the upper-left corner of this raster to the upper-left corner
 685      * of the subraster.  A subset of the bands of the parent Raster may
 686      * be specified.  If this is null, then all the bands are present in the
 687      * subRaster. A translation to the subRaster may also be specified.
 688      * Note that the subraster will reference the same
 689      * DataBuffers as the parent raster, but using different offsets.
 690      * @param x               X offset.
 691      * @param y               Y offset.
 692      * @param width           Width (in pixels) of the subraster.
 693      * @param height          Height (in pixels) of the subraster.


   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
  23  * questions.
  24  */
  25 
  26 package sun.awt.image;
  27 import java.awt.image.Raster;
  28 import java.awt.image.WritableRaster;
  29 import java.awt.image.RasterFormatException;
  30 import java.awt.image.SampleModel;
  31 import java.awt.image.BandedSampleModel;

  32 import java.awt.image.DataBufferUShort;
  33 import java.awt.Rectangle;
  34 import java.awt.Point;
  35 
  36 /**
  37  * This class defines a Raster with pixels consisting of multiple 16-bit
  38  * samples stored in separate arrays for each band.  Operations on
  39  * sets of pixels are performed on a given band of each pixel
  40  * in the set before moving on to the next band.  The arrays used
  41  * for storage may be distinct or shared between some or all of
  42  * the bands.
  43  * There is only one pixel stride and one scanline stride for all
  44  * bands.  This type of Raster can be used with a
  45  * ComponentColorModel. This class requires a BandedSampleModel.
  46  *
  47  */
  48 public class ShortBandedRaster extends SunWritableRaster {
  49 
  50     /** Data offsets for each band of image data. */
  51     int[]         dataOffsets;


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





 133 
 134         if (sampleModel instanceof BandedSampleModel) {
 135             BandedSampleModel bsm = (BandedSampleModel)sampleModel;
 136             this.scanlineStride = bsm.getScanlineStride();
 137             int bankIndices[] = bsm.getBankIndices();
 138             int bandOffsets[] = bsm.getBandOffsets();
 139             int dOffsets[] = dataBuffer.getOffsets();
 140             dataOffsets = new int[bankIndices.length];
 141             data = new short[bankIndices.length][];
 142             int xOffset = aRegion.x - origin.x;
 143             int yOffset = aRegion.y - origin.y;
 144             for (int i = 0; i < bankIndices.length; i++) {
 145                data[i] = stealData(dataBuffer, bankIndices[i]);
 146                dataOffsets[i] = dOffsets[bankIndices[i]] +
 147                    xOffset + yOffset*scanlineStride + bandOffsets[i];
 148             }
 149         } else {
 150             throw new RasterFormatException("ShortBandedRasters must have "+
 151                 "BandedSampleModels");
 152         }
 153         verify();
 154     }
 155 
 156     /**
 157      * Returns a copy of the data offsets array. For each band the data offset
 158      * is the index into the band's data array, of the first sample of the
 159      * band.
 160      */
 161     public int[] getDataOffsets() {
 162         return dataOffsets.clone();
 163     }
 164 
 165     /**


 647             throw new RasterFormatException("y lies outside raster");
 648         }
 649         if ((x+width < x) || (x+width > this.minX + this.width)) {
 650             throw new RasterFormatException("(x + width) is outside of Raster");
 651         }
 652         if ((y+height < y) || (y+height > this.minY + this.height)) {
 653             throw new RasterFormatException("(y + height) is outside of Raster");
 654         }
 655 
 656         SampleModel sm;
 657 
 658         if (bandList != null)
 659             sm = sampleModel.createSubsetSampleModel(bandList);
 660         else
 661             sm = sampleModel;
 662 
 663         int deltaX = x0 - x;
 664         int deltaY = y0 - y;
 665 
 666         return new ShortBandedRaster(sm,
 667                                      (DataBufferUShort)dataBuffer,
 668                                      new Rectangle(x0, y0, width, height),
 669                                      new Point(sampleModelTranslateX+deltaX,
 670                                                sampleModelTranslateY+deltaY),
 671                                      this);
 672 
 673     }
 674 
 675     /**
 676      * Creates a subraster given a region of the raster.  The x and y
 677      * coordinates specify the horizontal and vertical offsets
 678      * from the upper-left corner of this raster to the upper-left corner
 679      * of the subraster.  A subset of the bands of the parent Raster may
 680      * be specified.  If this is null, then all the bands are present in the
 681      * subRaster. A translation to the subRaster may also be specified.
 682      * Note that the subraster will reference the same
 683      * DataBuffers as the parent raster, but using different offsets.
 684      * @param x               X offset.
 685      * @param y               Y offset.
 686      * @param width           Width (in pixels) of the subraster.
 687      * @param height          Height (in pixels) of the subraster.


< prev index next >