< prev index next >

src/java.desktop/share/classes/sun/awt/image/ByteComponentRaster.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.ComponentSampleModel;
  32 import java.awt.image.SinglePixelPackedSampleModel;
  33 import java.awt.image.DataBuffer;
  34 import java.awt.image.DataBufferByte;
  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 8-bit
  40  * data elements stored in close proximity to each other in a single byte
  41  * array.
  42  * The bit precision per data element is that
  43  * of the data type (that is, the bit precision for this Raster is 8).
  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 an
  47  * IndexColorModel if there is only one band.
  48  * <p>
  49  * For example, 3-3-2 RGB image data can be represented by a
  50  * ByteComponentRaster using a SinglePixelPackedSampleModel and
  51  * a ComponentColorModel.
  52  *
  53  */


  77     private int maxY;
  78 
  79     private static native void initIDs();
  80     static {
  81         /* ensure that the necessary native libraries are loaded */
  82         NativeLibLoader.loadLibraries();
  83         initIDs();
  84     }
  85 
  86     /**
  87      * Constructs a ByteComponentRaster with the given SampleModel.
  88      * The Raster's upper left corner is origin and it is the same
  89      * size as the SampleModel.  A DataBuffer large enough to describe the
  90      * Raster is automatically created.  SampleModel must be of type
  91      * SinglePixelPackedSampleModel or ComponentSampleModel.
  92      * @param sampleModel     The SampleModel that specifies the layout.
  93      * @param origin          The Point that specified the origin.
  94      */
  95     public ByteComponentRaster(SampleModel sampleModel, Point origin) {
  96         this(sampleModel,
  97              sampleModel.createDataBuffer(),
  98              new Rectangle(origin.x,
  99                            origin.y,
 100                            sampleModel.getWidth(),
 101                            sampleModel.getHeight()),
 102              origin,
 103              null);
 104     }
 105 
 106     /**
 107      * Constructs a ByteComponentRaster with the given SampleModel
 108      * and DataBuffer.  The Raster's upper left corner is origin and
 109      * it is the same size as the SampleModel.  The DataBuffer is not
 110      * initialized and must be a DataBufferByte compatible with SampleModel.
 111      * SampleModel must be of type SinglePixelPackedSampleModel
 112      * or ComponentSampleModel.
 113      * @param sampleModel     The SampleModel that specifies the layout.
 114      * @param dataBuffer      The DataBufferShort that contains the image data.
 115      * @param origin          The Point that specifies the origin.
 116      */
 117     public ByteComponentRaster(SampleModel sampleModel,
 118                                   DataBuffer dataBuffer,
 119                                   Point origin) {
 120         this(sampleModel,
 121              dataBuffer,
 122              new Rectangle(origin.x,
 123                            origin.y,
 124                            sampleModel.getWidth(),
 125                            sampleModel.getHeight()),
 126              origin,
 127              null);
 128     }
 129 
 130     /**
 131      * Constructs a ByteComponentRaster with the given SampleModel,
 132      * DataBuffer, and parent.  DataBuffer must be a DataBufferByte and
 133      * SampleModel must be of type SinglePixelPackedSampleModel
 134      * or ComponentSampleModel.
 135      * When translated into the base Raster's
 136      * coordinate system, aRegion must be contained by the base Raster.
 137      * Origin is the coordinate in the new Raster's coordinate system of
 138      * the origin of the base Raster.  (The base Raster is the Raster's
 139      * ancestor which has no parent.)
 140      *
 141      * Note that this constructor should generally be called by other
 142      * constructors or create methods, it should not be used directly.
 143      * @param sampleModel     The SampleModel that specifies the layout.
 144      * @param dataBuffer      The DataBufferShort that contains the image data.
 145      * @param aRegion         The Rectangle that specifies the image area.
 146      * @param origin          The Point that specifies the origin.
 147      * @param parent          The parent (if any) of this raster.
 148      */
 149     public ByteComponentRaster(SampleModel sampleModel,
 150                                   DataBuffer dataBuffer,
 151                                   Rectangle aRegion,
 152                                   Point origin,
 153                                   ByteComponentRaster parent) {
 154         super(sampleModel, dataBuffer, aRegion, origin, parent);
 155         this.maxX = minX + width;
 156         this.maxY = minY + height;
 157 
 158         if (!(dataBuffer instanceof DataBufferByte)) {
 159             throw new RasterFormatException("ByteComponentRasters must have " +
 160                                             "byte DataBuffers");
 161         }
 162 
 163         DataBufferByte dbb = (DataBufferByte)dataBuffer;
 164         this.data = stealData(dbb, 0);
 165         if (dbb.getNumBanks() != 1) {
 166             throw new
 167                 RasterFormatException("DataBuffer for ByteComponentRasters"+
 168                                       " must only have 1 bank.");
 169         }
 170         int dbOffset = dbb.getOffset();
 171 
 172         if (sampleModel instanceof ComponentSampleModel) {
 173             ComponentSampleModel ism = (ComponentSampleModel)sampleModel;
 174             this.type = IntegerComponentRaster.TYPE_BYTE_SAMPLES;
 175             this.scanlineStride = ism.getScanlineStride();
 176             this.pixelStride = ism.getPixelStride();
 177             this.dataOffsets = ism.getBandOffsets();
 178             int xOffset = aRegion.x - origin.x;
 179             int yOffset = aRegion.y - origin.y;
 180             for (int i = 0; i < getNumDataElements(); i++) {
 181                 dataOffsets[i] += dbOffset +
 182                     xOffset*pixelStride+yOffset*scanlineStride;
 183             }
 184         } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
 185             SinglePixelPackedSampleModel sppsm =
 186                     (SinglePixelPackedSampleModel)sampleModel;
 187             this.type = IntegerComponentRaster.TYPE_BYTE_PACKED_SAMPLES;
 188             this.scanlineStride = sppsm.getScanlineStride();
 189             this.pixelStride    = 1;
 190             this.dataOffsets = new int[1];


 806             throw new RasterFormatException("y lies outside the raster");
 807         }
 808         if ((x+width < x) || (x+width > this.minX + this.width)) {
 809             throw new RasterFormatException("(x + width) is outside of Raster");
 810         }
 811         if ((y+height < y) || (y+height > this.minY + this.height)) {
 812             throw new RasterFormatException("(y + height) is outside of Raster");
 813         }
 814 
 815         SampleModel sm;
 816 
 817         if (bandList != null)
 818             sm = sampleModel.createSubsetSampleModel(bandList);
 819         else
 820             sm = sampleModel;
 821 
 822         int deltaX = x0 - x;
 823         int deltaY = y0 - y;
 824 
 825         return new ByteComponentRaster(sm,
 826                                        dataBuffer,
 827                                        new Rectangle(x0, y0, width, height),
 828                                        new Point(sampleModelTranslateX+deltaX,
 829                                                  sampleModelTranslateY+deltaY),
 830                                        this);
 831     }
 832 
 833     /**
 834      * Creates a Raster with the same layout but using a different
 835      * width and height, and with new zeroed data arrays.
 836      */
 837     public WritableRaster createCompatibleWritableRaster(int w, int h) {
 838         if (w <= 0 || h <=0) {
 839             throw new RasterFormatException("negative "+
 840                                           ((w <= 0) ? "width" : "height"));
 841         }
 842 
 843         SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);
 844 
 845         return new ByteComponentRaster(sm , new Point(0,0));
 846 


   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.ComponentSampleModel;
  32 import java.awt.image.SinglePixelPackedSampleModel;

  33 import java.awt.image.DataBufferByte;
  34 import java.awt.Rectangle;
  35 import java.awt.Point;
  36 
  37 /**
  38  * This class defines a Raster with pixels consisting of one or more 8-bit
  39  * data elements stored in close proximity to each other in a single byte
  40  * array.
  41  * The bit precision per data element is that
  42  * of the data type (that is, the bit precision for this Raster is 8).
  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 an
  46  * IndexColorModel if there is only one band.
  47  * <p>
  48  * For example, 3-3-2 RGB image data can be represented by a
  49  * ByteComponentRaster using a SinglePixelPackedSampleModel and
  50  * a ComponentColorModel.
  51  *
  52  */


  76     private int maxY;
  77 
  78     private static native void initIDs();
  79     static {
  80         /* ensure that the necessary native libraries are loaded */
  81         NativeLibLoader.loadLibraries();
  82         initIDs();
  83     }
  84 
  85     /**
  86      * Constructs a ByteComponentRaster with the given SampleModel.
  87      * The Raster's upper left corner is origin and it is the same
  88      * size as the SampleModel.  A DataBuffer large enough to describe the
  89      * Raster is automatically created.  SampleModel must be of type
  90      * SinglePixelPackedSampleModel or ComponentSampleModel.
  91      * @param sampleModel     The SampleModel that specifies the layout.
  92      * @param origin          The Point that specified the origin.
  93      */
  94     public ByteComponentRaster(SampleModel sampleModel, Point origin) {
  95         this(sampleModel,
  96              (DataBufferByte)sampleModel.createDataBuffer(),
  97              new Rectangle(origin.x,
  98                            origin.y,
  99                            sampleModel.getWidth(),
 100                            sampleModel.getHeight()),
 101              origin,
 102              null);
 103     }
 104 
 105     /**
 106      * Constructs a ByteComponentRaster with the given SampleModel
 107      * and DataBuffer.  The Raster's upper left corner is origin and
 108      * it is the same size as the SampleModel.  The DataBuffer is not
 109      * initialized and must be a DataBufferByte compatible with SampleModel.
 110      * SampleModel must be of type SinglePixelPackedSampleModel
 111      * or ComponentSampleModel.
 112      * @param sampleModel     The SampleModel that specifies the layout.
 113      * @param dataBuffer      The DataBufferByte that contains the image data.
 114      * @param origin          The Point that specifies the origin.
 115      */
 116     public ByteComponentRaster(SampleModel sampleModel,
 117                                   DataBufferByte dataBuffer,
 118                                   Point origin) {
 119         this(sampleModel,
 120              dataBuffer,
 121              new Rectangle(origin.x,
 122                            origin.y,
 123                            sampleModel.getWidth(),
 124                            sampleModel.getHeight()),
 125              origin,
 126              null);
 127     }
 128 
 129     /**
 130      * Constructs a ByteComponentRaster with the given SampleModel,
 131      * DataBuffer, and parent.  DataBuffer must be a DataBufferByte and
 132      * SampleModel must be of type SinglePixelPackedSampleModel
 133      * or ComponentSampleModel.
 134      * When translated into the base Raster's
 135      * coordinate system, aRegion must be contained by the base Raster.
 136      * Origin is the coordinate in the new Raster's coordinate system of
 137      * the origin of the base Raster.  (The base Raster is the Raster's
 138      * ancestor which has no parent.)
 139      *
 140      * Note that this constructor should generally be called by other
 141      * constructors or create methods, it should not be used directly.
 142      * @param sampleModel     The SampleModel that specifies the layout.
 143      * @param dataBuffer      The DataBufferByte that contains the image data.
 144      * @param aRegion         The Rectangle that specifies the image area.
 145      * @param origin          The Point that specifies the origin.
 146      * @param parent          The parent (if any) of this raster.
 147      */
 148     public ByteComponentRaster(SampleModel sampleModel,
 149                                   DataBufferByte dataBuffer,
 150                                   Rectangle aRegion,
 151                                   Point origin,
 152                                   ByteComponentRaster parent) {
 153         super(sampleModel, dataBuffer, aRegion, origin, parent);
 154         this.maxX = minX + width;
 155         this.maxY = minY + height;
 156 
 157         this.data = stealData(dataBuffer, 0);
 158         if (dataBuffer.getNumBanks() != 1) {






 159             throw new
 160                 RasterFormatException("DataBuffer for ByteComponentRasters"+
 161                                       " must only have 1 bank.");
 162         }
 163         int dbOffset = dataBuffer.getOffset();
 164 
 165         if (sampleModel instanceof ComponentSampleModel) {
 166             ComponentSampleModel ism = (ComponentSampleModel)sampleModel;
 167             this.type = IntegerComponentRaster.TYPE_BYTE_SAMPLES;
 168             this.scanlineStride = ism.getScanlineStride();
 169             this.pixelStride = ism.getPixelStride();
 170             this.dataOffsets = ism.getBandOffsets();
 171             int xOffset = aRegion.x - origin.x;
 172             int yOffset = aRegion.y - origin.y;
 173             for (int i = 0; i < getNumDataElements(); i++) {
 174                 dataOffsets[i] += dbOffset +
 175                     xOffset*pixelStride+yOffset*scanlineStride;
 176             }
 177         } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
 178             SinglePixelPackedSampleModel sppsm =
 179                     (SinglePixelPackedSampleModel)sampleModel;
 180             this.type = IntegerComponentRaster.TYPE_BYTE_PACKED_SAMPLES;
 181             this.scanlineStride = sppsm.getScanlineStride();
 182             this.pixelStride    = 1;
 183             this.dataOffsets = new int[1];


 799             throw new RasterFormatException("y lies outside the raster");
 800         }
 801         if ((x+width < x) || (x+width > this.minX + this.width)) {
 802             throw new RasterFormatException("(x + width) is outside of Raster");
 803         }
 804         if ((y+height < y) || (y+height > this.minY + this.height)) {
 805             throw new RasterFormatException("(y + height) is outside of Raster");
 806         }
 807 
 808         SampleModel sm;
 809 
 810         if (bandList != null)
 811             sm = sampleModel.createSubsetSampleModel(bandList);
 812         else
 813             sm = sampleModel;
 814 
 815         int deltaX = x0 - x;
 816         int deltaY = y0 - y;
 817 
 818         return new ByteComponentRaster(sm,
 819                                        (DataBufferByte)dataBuffer,
 820                                        new Rectangle(x0, y0, width, height),
 821                                        new Point(sampleModelTranslateX+deltaX,
 822                                                  sampleModelTranslateY+deltaY),
 823                                        this);
 824     }
 825 
 826     /**
 827      * Creates a Raster with the same layout but using a different
 828      * width and height, and with new zeroed data arrays.
 829      */
 830     public WritableRaster createCompatibleWritableRaster(int w, int h) {
 831         if (w <= 0 || h <=0) {
 832             throw new RasterFormatException("negative "+
 833                                           ((w <= 0) ? "width" : "height"));
 834         }
 835 
 836         SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);
 837 
 838         return new ByteComponentRaster(sm , new Point(0,0));
 839 


< prev index next >