< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2013, 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.MultiPixelPackedSampleModel;
  32 import java.awt.image.DataBuffer;
  33 import java.awt.image.DataBufferByte;
  34 import java.awt.Rectangle;
  35 import java.awt.Point;
  36 
  37 /**
  38  * This class is useful for describing 1, 2, or 4 bit image data
  39  * elements.  This raster has one band whose pixels are packed
  40  * together into individual bytes in a single byte array.  This type
  41  * of raster can be used with an IndexColorModel. This raster uses a
  42  * MultiPixelPackedSampleModel.
  43  *
  44  */
  45 public class BytePackedRaster extends SunWritableRaster {
  46 
  47     /** The data bit offset for each pixel. */
  48     int           dataBitOffset;
  49 
  50     /** Scanline stride of the image data contained in this Raster. */
  51     int           scanlineStride;
  52 


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


1305         if ((x+width < x) || (x+width > this.minX + this.width)) {
1306             throw new RasterFormatException("(x + width) is outside of Raster");
1307         }
1308         if ((y+height < y) || (y+height > this.minY + this.height)) {
1309             throw new RasterFormatException("(y + height) is outside of Raster");
1310         }
1311 
1312         SampleModel sm;
1313 
1314         if (bandList != null) {
1315             sm = sampleModel.createSubsetSampleModel(bandList);
1316         }
1317         else {
1318             sm = sampleModel;
1319         }
1320 
1321         int deltaX = x0 - x;
1322         int deltaY = y0 - y;
1323 
1324         return new BytePackedRaster(sm,
1325                                     dataBuffer,
1326                                     new Rectangle(x0, y0, width, height),
1327                                     new Point(sampleModelTranslateX+deltaX,
1328                                               sampleModelTranslateY+deltaY),
1329                                     this);
1330     }
1331 
1332     /**
1333      * Creates a raster with the same layout but using a different
1334      * width and height, and with new zeroed data arrays.
1335      */
1336     public WritableRaster createCompatibleWritableRaster(int w, int h) {
1337         if (w <= 0 || h <=0) {
1338             throw new RasterFormatException("negative "+
1339                                           ((w <= 0) ? "width" : "height"));
1340         }
1341 
1342         SampleModel sm = sampleModel.createCompatibleSampleModel(w,h);
1343 
1344         return new BytePackedRaster(sm, new Point(0,0));
1345     }


   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.MultiPixelPackedSampleModel;

  32 import java.awt.image.DataBufferByte;
  33 import java.awt.Rectangle;
  34 import java.awt.Point;
  35 
  36 /**
  37  * This class is useful for describing 1, 2, or 4 bit image data
  38  * elements.  This raster has one band whose pixels are packed
  39  * together into individual bytes in a single byte array.  This type
  40  * of raster can be used with an IndexColorModel. This raster uses a
  41  * MultiPixelPackedSampleModel.
  42  *
  43  */
  44 public class BytePackedRaster extends SunWritableRaster {
  45 
  46     /** The data bit offset for each pixel. */
  47     int           dataBitOffset;
  48 
  49     /** Scanline stride of the image data contained in this Raster. */
  50     int           scanlineStride;
  51 


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





 158             throw new
 159                 RasterFormatException("DataBuffer for BytePackedRasters"+
 160                                       " must only have 1 bank.");
 161         }
 162         int dbOffset = dataBuffer.getOffset();
 163 
 164         if (sampleModel instanceof MultiPixelPackedSampleModel) {
 165             MultiPixelPackedSampleModel mppsm =
 166                 (MultiPixelPackedSampleModel)sampleModel;
 167             this.type = IntegerComponentRaster.TYPE_BYTE_BINARY_SAMPLES;
 168             pixelBitStride = mppsm.getPixelBitStride();
 169             if (pixelBitStride != 1 &&
 170                 pixelBitStride != 2 &&
 171                 pixelBitStride != 4) {
 172                 throw new RasterFormatException
 173                   ("BytePackedRasters must have a bit depth of 1, 2, or 4");
 174             }
 175             scanlineStride = mppsm.getScanlineStride();
 176             dataBitOffset = mppsm.getDataBitOffset() + dbOffset*8;
 177             int xOffset = aRegion.x - origin.x;
 178             int yOffset = aRegion.y - origin.y;
 179             dataBitOffset += xOffset*pixelBitStride + yOffset*scanlineStride*8;
 180             bitMask = (1 << pixelBitStride) -1;
 181             shiftOffset = 8 - pixelBitStride;
 182         } else {


1299         if ((x+width < x) || (x+width > this.minX + this.width)) {
1300             throw new RasterFormatException("(x + width) is outside of Raster");
1301         }
1302         if ((y+height < y) || (y+height > this.minY + this.height)) {
1303             throw new RasterFormatException("(y + height) is outside of Raster");
1304         }
1305 
1306         SampleModel sm;
1307 
1308         if (bandList != null) {
1309             sm = sampleModel.createSubsetSampleModel(bandList);
1310         }
1311         else {
1312             sm = sampleModel;
1313         }
1314 
1315         int deltaX = x0 - x;
1316         int deltaY = y0 - y;
1317 
1318         return new BytePackedRaster(sm,
1319                                     (DataBufferByte)dataBuffer,
1320                                     new Rectangle(x0, y0, width, height),
1321                                     new Point(sampleModelTranslateX+deltaX,
1322                                               sampleModelTranslateY+deltaY),
1323                                     this);
1324     }
1325 
1326     /**
1327      * Creates a raster with the same layout but using a different
1328      * width and height, and with new zeroed data arrays.
1329      */
1330     public WritableRaster createCompatibleWritableRaster(int w, int h) {
1331         if (w <= 0 || h <=0) {
1332             throw new RasterFormatException("negative "+
1333                                           ((w <= 0) ? "width" : "height"));
1334         }
1335 
1336         SampleModel sm = sampleModel.createCompatibleSampleModel(w,h);
1337 
1338         return new BytePackedRaster(sm, new Point(0,0));
1339     }


< prev index next >