< 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
  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.ComponentSampleModel;
  32 import java.awt.image.PixelInterleavedSampleModel;
  33 import java.awt.image.SinglePixelPackedSampleModel;
  34 import java.awt.image.DataBuffer;
  35 import java.awt.image.DataBufferUShort;
  36 import java.awt.Rectangle;
  37 import java.awt.Point;
  38 
  39 /**
  40  * This class defines a Raster with pixels consisting of one or more 16-bit
  41  * data elements stored in close proximity to each other in a short integer
  42  * array.  The bit precision per data element is that
  43  * of the data type (that is, the bit precision for this Raster is 16).
  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 if there are multiple bands, or a
  47  * IndexColorModel if there is only one band.
  48  * <p>
  49  * For example, 5-6-5 RGB image data can be represented by a
  50  * ShortInterleavedRaster using a SinglePixelPackedSampleModel and
  51  * a ComponentColorModel.
  52  *
  53  *
  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
  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.ComponentSampleModel;
  32 import java.awt.image.PixelInterleavedSampleModel;
  33 import java.awt.image.SinglePixelPackedSampleModel;

  34 import java.awt.image.DataBufferUShort;
  35 import java.awt.Rectangle;
  36 import java.awt.Point;
  37 
  38 /**
  39  * This class defines a Raster with pixels consisting of one or more 16-bit
  40  * data elements stored in close proximity to each other in a short integer
  41  * array.  The bit precision per data element is that
  42  * of the data type (that is, the bit precision for this Raster is 16).
  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 if there are multiple bands, or a
  46  * IndexColorModel if there is only one band.
  47  * <p>
  48  * For example, 5-6-5 RGB image data can be represented by a
  49  * ShortInterleavedRaster using a SinglePixelPackedSampleModel and
  50  * a ComponentColorModel.
  51  *
  52  *
  53  */
  54 public class ShortInterleavedRaster extends ShortComponentRaster {
  55 
  56     /** A cached copy of minX + width for use in bounds checks. */
  57     private int maxX;
  58 
  59     /** A cached copy of minY + height for use in bounds checks. */
  60     private int maxY;
  61 
  62     /**
  63      *  Constructs a ShortInterleavedRaster with the given SampleModel.
  64      *  The Raster's upper left corner is origin and it is the same
  65      *  size as the SampleModel.  A DataBuffer large enough to describe the
  66      *  Raster is automatically created.  SampleModel must be of type
  67      *  PixelInterleavedSampleModel or SinglePixelPackedSampleModel.
  68      *  @param sampleModel     The SampleModel that specifies the layout.
  69      *  @param origin          The Point that specified the origin.
  70      */
  71     public ShortInterleavedRaster(SampleModel sampleModel, Point origin) {
  72         this(sampleModel,
  73              (DataBufferUShort) sampleModel.createDataBuffer(),
  74              new Rectangle(origin.x,
  75                            origin.y,
  76                            sampleModel.getWidth(),
  77                            sampleModel.getHeight()),
  78              origin,
  79              null);
  80     }
  81 
  82     /**
  83      * Constructs a ShortInterleavedRaster with the given SampleModel
  84      * and DataBuffer.  The Raster's upper left corner is origin and
  85      * it is the same sizes the SampleModel.  The DataBuffer is not
  86      * initialized and must be a DataBufferUShort compatible with SampleModel.
  87      * SampleModel must be of type PixelInterleavedSampleModel or
  88      * SinglePixelPackedSampleModel.
  89      * @param sampleModel     The SampleModel that specifies the layout.
  90      * @param dataBuffer      The DataBufferUShort that contains the image data.
  91      * @param origin          The Point that specifies the origin.
  92      */
  93     public ShortInterleavedRaster(SampleModel sampleModel,
  94                                   DataBufferUShort dataBuffer,
  95                                   Point origin)
  96     {
  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 
 132         super(sampleModel, dataBuffer, aRegion, origin, parent);
 133         this.maxX = minX + width;
 134         this.maxY = minY + height;
 135 
 136         this.data = stealData(dataBuffer, 0);






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


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