1351 lines · 53.5 KB · Java
RawDownload
/* * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.awt.image;import java.awt.image.Raster;import java.awt.image.WritableRaster;import java.awt.image.RasterFormatException;import java.awt.image.SampleModel;import java.awt.image.ComponentSampleModel;import java.awt.image.PixelInterleavedSampleModel;import java.awt.image.SinglePixelPackedSampleModel;import java.awt.image.DataBuffer;import java.awt.image.DataBufferByte;import java.awt.Rectangle;import java.awt.Point; /** * This class defines a Raster with pixels consisting of one or more * 8-bit data elements stored in close proximity to each other in a * single byte array. * <p> * The bit precision per data element is that of the data type (that * is, the bit precision for this Raster is 8).  There is only one * pixel stride and one scanline stride for all bands.  This type of * Raster can be used with a ComponentColorModel if there are multiple * bands, or an IndexColorModel if there is only one band. * */public class ByteInterleavedRaster extends ByteComponentRaster {     /** True if the data offsets range from 0 to (pixelStride - 1) in order. */    boolean inOrder;     /**     * The DataBuffer offset, minus sampleModelTranslateX*pixelStride,     * minus sampleModelTranslateY*scanlineStride, used to calculate     * pixel offsets.     */    int dbOffset;    int dbOffsetPacked;     /** True if a SinglePixelPackedSampleModel is being used. */    boolean packed = false;     /** If packed == true, the SampleModel's bit masks. */    int[] bitMasks;     /** If packed == true, the SampleModel's bit offsets. */    int[] bitOffsets;     /** A cached copy of minX + width for use in bounds checks. */    private int maxX;     /** A cached copy of minY + height for use in bounds checks. */    private int maxY;     /**     * Constructs a ByteInterleavedRaster with the given SampleModel.     * The Raster's upper left corner is origin and it is the same     * size as the SampleModel.  A DataBuffer large enough to describe the     * Raster is automatically created.  SampleModel must be of type     * SinglePixelPackedSampleModel or InterleavedSampleModel.     * @param sampleModel     The SampleModel that specifies the layout.     * @param origin          The Point that specified the origin.     */    public ByteInterleavedRaster(SampleModel sampleModel, Point origin) {        this(sampleModel,             sampleModel.createDataBuffer(),             new Rectangle(origin.x,                           origin.y,                           sampleModel.getWidth(),                           sampleModel.getHeight()),             origin,             null);    }     /**     * Constructs a ByteInterleavedRaster with the given SampleModel     * and DataBuffer.  The Raster's upper left corner is origin and     * it is the same size as the SampleModel.  The DataBuffer is not     * initialized and must be a DataBufferByte compatible with SampleModel.     * SampleModel must be of type SinglePixelPackedSampleModel     * or InterleavedSampleModel.     * @param sampleModel     The SampleModel that specifies the layout.     * @param dataBuffer      The DataBufferShort that contains the image data.     * @param origin          The Point that specifies the origin.     */    public ByteInterleavedRaster(SampleModel sampleModel,                                  DataBuffer dataBuffer,                                  Point origin) {        this(sampleModel,             dataBuffer,             new Rectangle(origin.x,                           origin.y,                           sampleModel.getWidth(),                           sampleModel.getHeight()),             origin,             null);    }     /*** Analyzes a ComponentSampleModel to determine if it can function     * as a PixelInterleavedSampleModel.  In order to do so, it must use     * only bank 0 of its DataBuffer, and the data offsets must span a range     * of less than pixelStride.     *     * <p> These properties are trivially true for a 1-banded SampleModel.     */    private boolean isInterleaved(ComponentSampleModel sm) {        // Analyze ComponentSampleModel to determine if it has the        // properties of a PixelInterleavedSampleModel         int numBands = sampleModel.getNumBands();        if (numBands == 1) {            return true;        }         // Determine banks used        int[] bankIndices = sm.getBankIndices();        for (int i = 0; i < numBands; i++) {            if (bankIndices[i] != 0) {                return false;            }        }         // Determine range of band offsets        int[] bandOffsets = sm.getBandOffsets();        int minOffset = bandOffsets[0];        int maxOffset = minOffset;        for (int i = 1; i < numBands; i++) {            int offset = bandOffsets[i];            if (offset < minOffset) {                minOffset = offset;            }            if (offset > maxOffset) {                maxOffset = offset;            }        }        if (maxOffset - minOffset >= sm.getPixelStride()) {            return false;        }         return true;    }     /**     * Constructs a ByteInterleavedRaster with the given SampleModel,     * DataBuffer, and parent.  DataBuffer must be a DataBufferByte and     * SampleModel must be of type SinglePixelPackedSampleModel     * or InterleavedSampleModel.     * When translated into the base Raster's     * coordinate system, aRegion must be contained by the base Raster.     * Origin is the coordinate in the new Raster's coordinate system of     * the origin of the base Raster.  (The base Raster is the Raster's     * ancestor which has no parent.)     *     * Note that this constructor should generally be called by other     * constructors or create methods, it should not be used directly.     * @param sampleModel     The SampleModel that specifies the layout.     * @param dataBuffer      The DataBufferShort that contains the image data.     * @param aRegion         The Rectangle that specifies the image area.     * @param origin          The Point that specifies the origin.     * @param parent          The parent (if any) of this raster.     */    public ByteInterleavedRaster(SampleModel sampleModel,                                  DataBuffer dataBuffer,                                  Rectangle aRegion,                                  Point origin,                                  ByteInterleavedRaster parent) {        super(sampleModel, dataBuffer, aRegion, origin, parent);        this.maxX = minX + width;        this.maxY = minY + height;         if (!(dataBuffer instanceof DataBufferByte)) {            throw new RasterFormatException("ByteInterleavedRasters must have " +                                            "byte DataBuffers");        }         DataBufferByte dbb = (DataBufferByte)dataBuffer;        this.data = stealData(dbb, 0);         int xOffset = aRegion.x - origin.x;        int yOffset = aRegion.y - origin.y;        if (sampleModel instanceof PixelInterleavedSampleModel ||            (sampleModel instanceof ComponentSampleModel &&             isInterleaved((ComponentSampleModel)sampleModel))) {            ComponentSampleModel csm = (ComponentSampleModel)sampleModel;            this.scanlineStride = csm.getScanlineStride();            this.pixelStride = csm.getPixelStride();            this.dataOffsets = csm.getBandOffsets();            for (int i = 0; i < getNumDataElements(); i++) {                dataOffsets[i] += xOffset*pixelStride+yOffset*scanlineStride;            }        } else if (sampleModel instanceof SinglePixelPackedSampleModel) {            SinglePixelPackedSampleModel sppsm =                    (SinglePixelPackedSampleModel)sampleModel;            this.packed = true;            this.bitMasks = sppsm.getBitMasks();            this.bitOffsets = sppsm.getBitOffsets();            this.scanlineStride = sppsm.getScanlineStride();            this.pixelStride = 1;            this.dataOffsets = new int[1];            this.dataOffsets[0] = dbb.getOffset();            dataOffsets[0] += xOffset*pixelStride+yOffset*scanlineStride;        } else {            throw new RasterFormatException("ByteInterleavedRasters must " +              "have PixelInterleavedSampleModel, SinglePixelPackedSampleModel"+              " or interleaved ComponentSampleModel.  Sample model is " +              sampleModel);        }        this.bandOffset = this.dataOffsets[0];         this.dbOffsetPacked = dataBuffer.getOffset() -            sampleModelTranslateY*scanlineStride -            sampleModelTranslateX*pixelStride;        this.dbOffset = dbOffsetPacked -            (xOffset*pixelStride+yOffset*scanlineStride);         // Set inOrder to true if the data elements are in order and        // have no gaps between them        this.inOrder = false;        if (numDataElements == pixelStride) {            inOrder = true;            for (int i = 1; i < numDataElements; i++) {                if (dataOffsets[i] - dataOffsets[0] != i) {                    inOrder = false;                    break;                }            }        }         verify(false);    }     /**     * Returns a copy of the data offsets array. For each band the data offset     * is the index into the band's data array, of the first sample of the     * band.     */    public int[] getDataOffsets() {        return (int[]) dataOffsets.clone();    }     /**     * Returns the data offset for the specified band.  The data offset     * is the index into the data array     * in which the first sample of the first scanline is stored.     * @param band  The band whose offset is returned.     */    public int getDataOffset(int band) {        return dataOffsets[band];    }     /**     * Returns the scanline stride -- the number of data array elements between     * a given sample and the sample in the same column of the next row in the     * same band.     */    public int getScanlineStride() {        return scanlineStride;    }     /**     * Returns pixel stride -- the number of data array elements between two     * samples for the same band on the same scanline.     */    public int getPixelStride() {        return pixelStride;    }     /**     * Returns a reference to the data array.     */    public byte[] getDataStorage() {        return data;    }     /**     * Returns the data elements for all bands at the specified     * location.     * An ArrayIndexOutOfBounds exception will be thrown at runtime     * if the pixel coordinate is out of bounds.     * A ClassCastException will be thrown if the input object is non null     * and references anything other than an array of transferType.     * @param x        The X coordinate of the pixel location.     * @param y        The Y coordinate of the pixel location.     * @param outData  An object reference to an array of type defined by     *                 getTransferType() and length getNumDataElements().     *                 If null an array of appropriate type and size will be     *                 allocated.     * @return         An object reference to an array of type defined by     *                 getTransferType() with the request pixel data.     */    public Object getDataElements(int x, int y, Object obj) {        if ((x < this.minX) || (y < this.minY) ||            (x >= this.maxX) || (y >= this.maxY)) {            throw new ArrayIndexOutOfBoundsException                ("Coordinate out of bounds!");        }        byte outData[];        if (obj == null) {            outData = new byte[numDataElements];        } else {            outData = (byte[])obj;        }        int off = (y-minY)*scanlineStride +                  (x-minX)*pixelStride;         for (int band = 0; band < numDataElements; band++) {            outData[band] = data[dataOffsets[band] + off];        }         return outData;    }     /**     * Returns an array of data elements from the specified rectangular     * region.     * An ArrayIndexOutOfBounds exception will be thrown at runtime     * if the pixel coordinates are out of bounds.     * A ClassCastException will be thrown if the input object is non null     * and references anything other than an array of transferType.     * <pre>     *       byte[] bandData = (byte[])raster.getDataElements(x, y, w, h, null);     *       int numDataElements = raster.getNumDataElements();     *       byte[] pixel = new byte[numDataElements];     *       // To find a data element at location (x2, y2)     *       System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,     *                        pixel, 0, numDataElements);     * </pre>     * @param x        The X coordinate of the upper left pixel location.     * @param y        The Y coordinate of the upper left pixel location.     * @param width    Width of the pixel rectangle.     * @param height   Height of the pixel rectangle.     * @param outData  An object reference to an array of type defined by     *                 getTransferType() and length w*h*getNumDataElements().     *                 If null an array of appropriate type and size will be     *                 allocated.     * @return         An object reference to an array of type defined by     *                 getTransferType() with the request pixel data.     */    public Object getDataElements(int x, int y, int w, int h, Object obj) {        return getByteData(x, y, w, h, (byte[])obj);    }     /**     * Returns a byte array of data elements from the specified rectangular     * region for the specified band.     * An ArrayIndexOutOfBounds exception will be thrown at runtime     * if the pixel coordinates are out of bounds.     * <pre>     *       byte[] bandData = raster.getByteData(x, y, w, h, null);     *       // To find the data element at location (x2, y2)     *       byte bandElement = bandData[((y2-y)*w + (x2-x))];     * </pre>     * @param x        The X coordinate of the upper left pixel location.     * @param y        The Y coordinate of the upper left pixel location.     * @param width    Width of the pixel rectangle.     * @param height   Height of the pixel rectangle.     * @param band     The band to return.     * @param outData  If non-null, data elements for all bands     *                 at the specified location are returned in this array.     * @return         Data array with data elements for all bands.     */    public byte[] getByteData(int x, int y, int w, int h,                              int band, byte[] outData) {        // Bounds check for 'band' will be performed automatically        if ((x < this.minX) || (y < this.minY) ||            (x + w > this.maxX) || (y + h > this.maxY)) {            throw new ArrayIndexOutOfBoundsException                ("Coordinate out of bounds!");        }        if (outData == null) {            outData = new byte[w*h];        }        int yoff = (y-minY)*scanlineStride +                   (x-minX)*pixelStride + dataOffsets[band];        int xoff;        int off = 0;        int xstart;        int ystart;         if (pixelStride == 1) {            if (scanlineStride == w) {                System.arraycopy(data, yoff, outData, 0, w*h);            } else {                for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {                    System.arraycopy(data, yoff, outData, off, w);                    off += w;                }            }        } else {            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {                xoff = yoff;                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {                    outData[off++] = data[xoff];                }            }        }         return outData;    }     /**     * Returns a byte array of data elements from the specified rectangular     * region.     * An ArrayIndexOutOfBounds exception will be thrown at runtime     * if the pixel coordinates are out of bounds.     * <pre>     *       byte[] bandData = raster.getByteData(x, y, w, h, null);     *       int numDataElements = raster.getnumDataElements();     *       byte[] pixel = new byte[numDataElements];     *       // To find a data element at location (x2, y2)     *       System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,     *                        pixel, 0, numDataElements);     * </pre>     * @param x        The X coordinate of the upper left pixel location.     * @param y        The Y coordinate of the upper left pixel location.     * @param width    Width of the pixel rectangle.     * @param height   Height of the pixel rectangle.     * @param outData  If non-null, data elements for all bands     *                 at the specified location are returned in this array.     * @return         Data array with data elements for all bands.     */    public byte[] getByteData(int x, int y, int w, int h, byte[] outData) {        if ((x < this.minX) || (y < this.minY) ||            (x + w > this.maxX) || (y + h > this.maxY)) {            throw new ArrayIndexOutOfBoundsException                ("Coordinate out of bounds!");        }        if (outData == null) {            outData = new byte[numDataElements*w*h];        }        int yoff = (y-minY)*scanlineStride +                   (x-minX)*pixelStride;        int xoff;        int off = 0;        int xstart;        int ystart;         if (inOrder) {            yoff += dataOffsets[0];            int rowBytes = w*pixelStride;            if (scanlineStride == rowBytes) {                System.arraycopy(data, yoff, outData, off, rowBytes*h);            } else {                for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {                    System.arraycopy(data, yoff, outData, off, rowBytes);                    off += rowBytes;                }            }        } else if (numDataElements == 1) {            yoff += dataOffsets[0];            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {                xoff = yoff;                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {                    outData[off++] = data[xoff];                }            }        } else if (numDataElements == 2) {            yoff += dataOffsets[0];            int d1 = dataOffsets[1] - dataOffsets[0];            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {                xoff = yoff;                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {                    outData[off++] = data[xoff];                    outData[off++] = data[xoff + d1];                }            }        } else if (numDataElements == 3) {            yoff += dataOffsets[0];            int d1 = dataOffsets[1] - dataOffsets[0];            int d2 = dataOffsets[2] - dataOffsets[0];            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {                xoff = yoff;                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {                    outData[off++] = data[xoff];                    outData[off++] = data[xoff + d1];                    outData[off++] = data[xoff + d2];

Lines 1–500 of 1351.