< prev index next >

src/java.desktop/share/classes/sun/awt/image/IntegerComponentRaster.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.SinglePixelPackedSampleModel;
  32 import java.awt.image.DataBuffer;
  33 import java.awt.image.DataBufferInt;
  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 32-bit
  39  * data elements stored in close proximity to each other in a integer array.
  40  * The bit precision per data element is that
  41  * of the data type (that is, the bit precision for this raster is 32).
  42  * There is only one pixel stride and one scanline stride for all
  43  * bands.  For a given pixel, all samples fit in N data elements and these
  44  * N data elements hold samples for only one pixel.  This type of Raster
  45  * can be used with a PackedColorModel.
  46  * <p>
  47  * For example, if there is only one data element per pixel, a
  48  * SinglePixelPackedSampleModel can be used to represent multiple
  49  * bands with a PackedColorModel (including a DirectColorModel) for
  50  * color interpretation.
  51  *
  52  */


  93 
  94     private static native void initIDs();
  95     static {
  96         /* ensure that the necessary native libraries are loaded */
  97         NativeLibLoader.loadLibraries();
  98         initIDs();
  99     }
 100 
 101     /**
 102      *  Constructs a IntegerComponentRaster with the given SampleModel.
 103      *  The Raster's upper left corner is origin and it is the same
 104      *  size as the SampleModel.  A DataBuffer large enough to describe the
 105      *  Raster is automatically created.  SampleModel must be of type
 106      *  SinglePixelPackedSampleModel.
 107      *  @param sampleModel     The SampleModel that specifies the layout.
 108      *  @param origin          The Point that specified the origin.
 109      */
 110     public IntegerComponentRaster(SampleModel sampleModel,
 111                                      Point origin) {
 112         this(sampleModel,
 113              sampleModel.createDataBuffer(),
 114              new Rectangle(origin.x,
 115                            origin.y,
 116                            sampleModel.getWidth(),
 117                            sampleModel.getHeight()),
 118              origin,
 119              null);
 120     }
 121 
 122     /**
 123      * Constructs a IntegerComponentRaster with the given SampleModel
 124      * and DataBuffer.  The Raster's upper left corner is origin and
 125      * it is the same sizes the SampleModel.  The DataBuffer is not
 126      * initialized and must be a DataBufferInt compatible with SampleModel.
 127      * SampleModel must be of type SinglePixelPackedSampleModel.
 128      * @param sampleModel     The SampleModel that specifies the layout.
 129      * @param dataBuffer      The DataBufferInt that contains the image data.
 130      * @param origin          The Point that specifies the origin.
 131      */
 132     public IntegerComponentRaster(SampleModel sampleModel,
 133                                      DataBuffer dataBuffer,
 134                                      Point origin) {
 135         this(sampleModel,
 136              dataBuffer,
 137              new Rectangle(origin.x,
 138                            origin.y,
 139                            sampleModel.getWidth(),
 140                            sampleModel.getHeight()),
 141              origin,
 142              null);
 143     }
 144 
 145    /**
 146      * Constructs a IntegerComponentRaster with the given SampleModel,
 147      * DataBuffer, and parent.  DataBuffer must be a DataBufferInt and
 148      * SampleModel must be of type SinglePixelPackedSampleModel.
 149      * When translated into the base Raster's
 150      * coordinate system, aRegion must be contained by the base Raster.
 151      * Origin is the coodinate in the new Raster's coordinate system of
 152      * the origin of the base Raster.  (The base Raster is the Raster's
 153      * ancestor which has no parent.)
 154      *
 155      * Note that this constructor should generally be called by other
 156      * constructors or create methods, it should not be used directly.
 157      * @param sampleModel     The SampleModel that specifies the layout.
 158      * @param dataBuffer      The DataBufferInt that contains the image data.
 159      * @param aRegion         The Rectangle that specifies the image area.
 160      * @param origin          The Point that specifies the origin.
 161      * @param parent          The parent (if any) of this raster.
 162      */
 163     public IntegerComponentRaster(SampleModel sampleModel,
 164                                      DataBuffer dataBuffer,
 165                                      Rectangle aRegion,
 166                                      Point origin,
 167                                      IntegerComponentRaster parent){
 168         super(sampleModel,dataBuffer,aRegion,origin,parent);
 169         this.maxX = minX + width;
 170         this.maxY = minY + height;
 171         if (!(dataBuffer instanceof DataBufferInt)) {
 172            throw new RasterFormatException("IntegerComponentRasters must have" +
 173                 "integer DataBuffers");
 174         }
 175         DataBufferInt dbi = (DataBufferInt)dataBuffer;
 176         if (dbi.getNumBanks() != 1) {
 177             throw new
 178                 RasterFormatException("DataBuffer for IntegerComponentRasters"+
 179                                       " must only have 1 bank.");
 180         }
 181         this.data = stealData(dbi, 0);
 182 
 183         if (sampleModel instanceof SinglePixelPackedSampleModel) {
 184             SinglePixelPackedSampleModel sppsm =
 185                     (SinglePixelPackedSampleModel)sampleModel;
 186             int[] boffsets = sppsm.getBitOffsets();
 187             boolean notByteBoundary = false;
 188             for (int i=1; i < boffsets.length; i++) {
 189                 if ((boffsets[i]%8) != 0) {
 190                     notByteBoundary = true;
 191                 }
 192             }
 193             this.type = (notByteBoundary
 194                          ? IntegerComponentRaster.TYPE_INT_PACKED_SAMPLES
 195                          : IntegerComponentRaster.TYPE_INT_8BIT_SAMPLES);
 196 
 197             this.scanlineStride = sppsm.getScanlineStride();
 198             this.pixelStride    = 1;
 199             this.dataOffsets = new int[1];
 200             this.dataOffsets[0] = dbi.getOffset();
 201             this.bandOffset = this.dataOffsets[0];
 202             int xOffset = aRegion.x - origin.x;
 203             int yOffset = aRegion.y - origin.y;
 204             dataOffsets[0] += xOffset+yOffset*scanlineStride;
 205             this.numDataElems = sppsm.getNumDataElements();
 206         } else {
 207             throw new RasterFormatException("IntegerComponentRasters must have"+
 208                                             " SinglePixelPackedSampleModel");
 209         }
 210 
 211         verify();
 212     }
 213 
 214 
 215     /**
 216      * Returns a copy of the data offsets array. For each band the data offset
 217      * is the index into the band's data array, of the first sample of the
 218      * band.
 219      */
 220     public int[] getDataOffsets() {


 552             throw new RasterFormatException("y lies outside raster");
 553         }
 554         if ((x+width < x) || (x+width > this.minX + this.width)) {
 555             throw new RasterFormatException("(x + width) is outside raster");
 556         }
 557         if ((y+height < y) || (y+height > this.minY + this.height)) {
 558             throw new RasterFormatException("(y + height) is outside raster");
 559         }
 560 
 561         SampleModel sm;
 562 
 563         if (bandList != null)
 564             sm = sampleModel.createSubsetSampleModel(bandList);
 565         else
 566             sm = sampleModel;
 567 
 568         int deltaX = x0 - x;
 569         int deltaY = y0 - y;
 570 
 571         return new IntegerComponentRaster(sm,
 572                                           dataBuffer,
 573                                           new Rectangle(x0,y0,width,height),
 574                                           new Point(sampleModelTranslateX+deltaX,
 575                                                     sampleModelTranslateY+deltaY),
 576                                           this);
 577     }
 578 
 579 
 580     /**
 581      * Creates a subraster given a region of the raster.  The x and y
 582      * coordinates specify the horizontal and vertical offsets
 583      * from the upper-left corner of this raster to the upper-left corner
 584      * of the subraster.  A subset of the bands of the parent raster may
 585      * be specified. If this is null, then all the bands are present in the
 586      * subRaster. Note that the subraster will reference the same
 587      * DataBuffer as the parent raster, but using different offsets.
 588      * @param x               X offset.
 589      * @param y               Y offset.
 590      * @param width           Width (in pixels) of the subraster.
 591      * @param height          Height (in pixels) of the subraster.
 592      * @param x0              Translated X origin 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.SinglePixelPackedSampleModel;

  32 import java.awt.image.DataBufferInt;
  33 import java.awt.Rectangle;
  34 import java.awt.Point;
  35 
  36 /**
  37  * This class defines a Raster with pixels consisting of one or more 32-bit
  38  * data elements stored in close proximity to each other in a integer array.
  39  * The bit precision per data element is that
  40  * of the data type (that is, the bit precision for this raster is 32).
  41  * There is only one pixel stride and one scanline stride for all
  42  * bands.  For a given pixel, all samples fit in N data elements and these
  43  * N data elements hold samples for only one pixel.  This type of Raster
  44  * can be used with a PackedColorModel.
  45  * <p>
  46  * For example, if there is only one data element per pixel, a
  47  * SinglePixelPackedSampleModel can be used to represent multiple
  48  * bands with a PackedColorModel (including a DirectColorModel) for
  49  * color interpretation.
  50  *
  51  */


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




 172             throw new
 173                 RasterFormatException("DataBuffer for IntegerComponentRasters"+
 174                                       " must only have 1 bank.");
 175         }
 176         this.data = stealData(dataBuffer, 0);
 177 
 178         if (sampleModel instanceof SinglePixelPackedSampleModel) {
 179             SinglePixelPackedSampleModel sppsm =
 180                     (SinglePixelPackedSampleModel)sampleModel;
 181             int[] boffsets = sppsm.getBitOffsets();
 182             boolean notByteBoundary = false;
 183             for (int i=1; i < boffsets.length; i++) {
 184                 if ((boffsets[i]%8) != 0) {
 185                     notByteBoundary = true;
 186                 }
 187             }
 188             this.type = (notByteBoundary
 189                          ? IntegerComponentRaster.TYPE_INT_PACKED_SAMPLES
 190                          : IntegerComponentRaster.TYPE_INT_8BIT_SAMPLES);
 191 
 192             this.scanlineStride = sppsm.getScanlineStride();
 193             this.pixelStride    = 1;
 194             this.dataOffsets = new int[1];
 195             this.dataOffsets[0] = dataBuffer.getOffset();
 196             this.bandOffset = this.dataOffsets[0];
 197             int xOffset = aRegion.x - origin.x;
 198             int yOffset = aRegion.y - origin.y;
 199             dataOffsets[0] += xOffset+yOffset*scanlineStride;
 200             this.numDataElems = sppsm.getNumDataElements();
 201         } else {
 202             throw new RasterFormatException("IntegerComponentRasters must have"+
 203                                             " SinglePixelPackedSampleModel");
 204         }
 205 
 206         verify();
 207     }
 208 
 209 
 210     /**
 211      * Returns a copy of the data offsets array. For each band the data offset
 212      * is the index into the band's data array, of the first sample of the
 213      * band.
 214      */
 215     public int[] getDataOffsets() {


 547             throw new RasterFormatException("y lies outside raster");
 548         }
 549         if ((x+width < x) || (x+width > this.minX + this.width)) {
 550             throw new RasterFormatException("(x + width) is outside raster");
 551         }
 552         if ((y+height < y) || (y+height > this.minY + this.height)) {
 553             throw new RasterFormatException("(y + height) is outside raster");
 554         }
 555 
 556         SampleModel sm;
 557 
 558         if (bandList != null)
 559             sm = sampleModel.createSubsetSampleModel(bandList);
 560         else
 561             sm = sampleModel;
 562 
 563         int deltaX = x0 - x;
 564         int deltaY = y0 - y;
 565 
 566         return new IntegerComponentRaster(sm,
 567                                           (DataBufferInt)dataBuffer,
 568                                           new Rectangle(x0,y0,width,height),
 569                                           new Point(sampleModelTranslateX+deltaX,
 570                                                     sampleModelTranslateY+deltaY),
 571                                           this);
 572     }
 573 
 574 
 575     /**
 576      * Creates a subraster given a region of the raster.  The x and y
 577      * coordinates specify the horizontal and vertical offsets
 578      * from the upper-left corner of this raster to the upper-left corner
 579      * of the subraster.  A subset of the bands of the parent raster may
 580      * be specified. If this is null, then all the bands are present in the
 581      * subRaster. Note that the subraster will reference the same
 582      * DataBuffer as the parent raster, but using different offsets.
 583      * @param x               X offset.
 584      * @param y               Y offset.
 585      * @param width           Width (in pixels) of the subraster.
 586      * @param height          Height (in pixels) of the subraster.
 587      * @param x0              Translated X origin of the subRaster.


< prev index next >