926 lines · 36.6 KB · Java
RawDownload
        setDataElements(dstOffX, dstOffY, width, height, inRaster);    }     /**     * Stores the Raster data at the specified location.     * @param dstX The absolute X coordinate of the destination pixel     * that will receive a copy of the upper-left pixel of the     * inRaster     * @param dstY The absolute Y coordinate of the destination pixel     * that will receive a copy of the upper-left pixel of the     * inRaster     * @param width      The number of pixels to store horizontally     * @param height     The number of pixels to store vertically     * @param inRaster   Raster of data to place at x,y location.     */    private void setDataElements(int dstX, int dstY,                                 int width, int height,                                 Raster inRaster) {        // Assume bounds checking has been performed previously        if (width <= 0 || height <= 0) {            return;        }         int srcOffX = inRaster.getMinX();        int srcOffY = inRaster.getMinY();        Object tdata = null;         if (inRaster instanceof ByteComponentRaster) {            ByteComponentRaster bct = (ByteComponentRaster) inRaster;            byte[] bdata = bct.getDataStorage();            // REMIND: Do something faster!            if (numDataElements == 1) {                int toff = bct.getDataOffset(0);                int tss  = bct.getScanlineStride();                 int srcOffset = toff;                int dstOffset = dataOffsets[0]+(dstY-minY)*scanlineStride+                                               (dstX-minX);                  if (pixelStride == bct.getPixelStride()) {                    width *= pixelStride;                    for (int tmpY=0; tmpY < height; tmpY++) {                        System.arraycopy(bdata, srcOffset,                                         data, dstOffset, width);                        srcOffset += tss;                        dstOffset += scanlineStride;                    }                    markDirty();                    return;                }            }        }         for (int startY=0; startY < height; startY++) {            // Grab one scanline at a time            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,                                             width, 1, tdata);            setDataElements(dstX, dstY+startY, width, 1, tdata);        }    }     /**     * Stores an array of data elements into 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.     * The data elements in the     * data array are assumed to be packed.  That is, a data element     * for the nth band at location (x2, y2) would be found at:     * <pre>     *      inData[((y2-y)*w + (x2-x))*numDataElements + n]     * </pre>     * @param x        The X coordinate of the upper left pixel location.     * @param y        The Y coordinate of the upper left pixel location.     * @param w        Width of the pixel rectangle.     * @param h        Height of the pixel rectangle.     * @param inData   An object reference to an array of type defined by     *                 getTransferType() and length w*h*getNumDataElements()     *                 containing the pixel data to place between x,y and     *                 x+h, y+h.     */    public void setDataElements(int x, int y, int w, int h, Object obj) {        if ((x < this.minX) || (y < this.minY) ||            (x + w > this.maxX) || (y + h > this.maxY)) {            throw new ArrayIndexOutOfBoundsException                ("Coordinate out of bounds!");        }        byte inData[] = (byte[])obj;        int yoff = (y-minY)*scanlineStride +                   (x-minX)*pixelStride;        int xoff;        int off = 0;        int xstart;        int ystart;         if (numDataElements == 1) {            int srcOffset = 0;            int dstOffset = yoff + dataOffsets[0];            for (ystart=0; ystart < h; ystart++) {                xoff = yoff;                System.arraycopy(inData, srcOffset,                                 data, dstOffset, w);                 srcOffset += w;                dstOffset += scanlineStride;            }            markDirty();            return;        }         for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {            xoff = yoff;            for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {                for (int c = 0; c < numDataElements; c++) {                    data[dataOffsets[c] + xoff] = inData[off++];                }            }        }         markDirty();    }     /**     * Stores a byte array of data elements into the specified rectangular     * region for the specified band.     * An ArrayIndexOutOfBounds exception will be thrown at runtime     * if the pixel coordinates are out of bounds.     * The data elements in the     * data array are assumed to be packed.  That is, a data element     * at location (x2, y2) would be found at:     * <pre>     *      inData[((y2-y)*w + (x2-x)) + n]     * </pre>     * @param x        The X coordinate of the upper left pixel location.     * @param y        The Y coordinate of the upper left pixel location.     * @param w        Width of the pixel rectangle.     * @param h        Height of the pixel rectangle.     * @param band     The band to set.     * @param inData   The data elements to be stored.     */    public void putByteData(int x, int y, int w, int h,                            int band, byte[] inData) {        // 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!");        }        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(inData, 0, data, yoff, w*h);            }            else {                for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {                    System.arraycopy(inData, off, data, yoff, w);                    off += w;                }            }        }        else {            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {                xoff = yoff;                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {                    data[xoff] = inData[off++];                }            }        }         markDirty();    }     /**     * Stores a byte array of data elements into the specified rectangular     * region.     * An ArrayIndexOutOfBounds exception will be thrown at runtime     * if the pixel coordinates are out of bounds.     * The data elements in the     * data array are assumed to be packed.  That is, a data element     * for the nth band at location (x2, y2) would be found at:     * <pre>     *      inData[((y2-y)*w + (x2-x))*numDataElements + n]     * </pre>     * @param x        The X coordinate of the upper left pixel location.     * @param y        The Y coordinate of the upper left pixel location.     * @param w        Width of the pixel rectangle.     * @param h        Height of the pixel rectangle.     * @param inData   The data elements to be stored.     */    public void putByteData(int x, int y, int w, int h, byte[] inData) {        if ((x < this.minX) || (y < this.minY) ||            (x + w > this.maxX) || (y + h > this.maxY)) {            throw new ArrayIndexOutOfBoundsException                ("Coordinate out of bounds!");        }        int yoff = (y-minY)*scanlineStride +                   (x-minX)*pixelStride;         int xoff;        int off = 0;        int xstart;        int ystart;         if (numDataElements == 1) {            yoff += dataOffsets[0];            if (pixelStride == 1) {                if (scanlineStride == w) {                    System.arraycopy(inData, 0, data, yoff, w*h);                }                else {                    for (ystart=0; ystart < h; ystart++) {                        System.arraycopy(inData, off, data, yoff, w);                        off += w;                        yoff += scanlineStride;                    }                }            }            else {                for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {                    xoff = yoff;                    for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {                        data[xoff] = inData[off++];                    }                }            }        }        else {            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {                xoff = yoff;                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {                    for (int c = 0; c < numDataElements; c++) {                        data[dataOffsets[c] + xoff] = inData[off++];                    }                }            }        }         markDirty();    }     /**     * Creates a subraster given a region of the raster.  The x and y     * coordinates specify the horizontal and vertical offsets     * from the upper-left corner of this raster to the upper-left corner     * of the subraster.  A subset of the bands of the parent Raster may     * be specified.  If this is null, then all the bands are present in the     * subRaster. A translation to the subRaster may also be specified.     * Note that the subraster will reference the same     * DataBuffer as the parent raster, but using different offsets.     * @param x               X offset.     * @param y               Y offset.     * @param width           Width (in pixels) of the subraster.     * @param height          Height (in pixels) of the subraster.     * @param x0              Translated X origin of the subraster.     * @param y0              Translated Y origin of the subraster.     * @param bandList        Array of band indices.     * @exception RasterFormatException     *            if the specified bounding box is outside of the parent raster.     */    public Raster createChild(int x, int y,                              int width, int height,                              int x0, int y0, int[] bandList) {        WritableRaster newRaster = createWritableChild(x, y,                                                       width, height,                                                       x0, y0,                                                       bandList);        return (Raster) newRaster;    }     /**     * Creates a Writable subRaster given a region of the Raster. The x and y     * coordinates specify the horizontal and vertical offsets     * from the upper-left corner of this Raster to the upper-left corner     * of the subRaster.  A subset of the bands of the parent Raster may     * be specified.  If this is null, then all the bands are present in the     * subRaster. A translation to the subRaster may also be specified.     * Note that the subRaster will reference the same     * DataBuffer as the parent Raster, but using different offsets.     * @param x               X offset.     * @param y               Y offset.     * @param width           Width (in pixels) of the subraster.     * @param height          Height (in pixels) of the subraster.     * @param x0              Translated X origin of the subraster.     * @param y0              Translated Y origin of the subraster.     * @param bandList        Array of band indices.     * @exception RasterFormatException     *            if the specified bounding box is outside of the parent Raster.     */    public WritableRaster createWritableChild(int x, int y,                                              int width, int height,                                              int x0, int y0,                                              int[] bandList) {        if (x < this.minX) {            throw new RasterFormatException("x lies outside the raster");        }        if (y < this.minY) {            throw new RasterFormatException("y lies outside the raster");        }        if ((x+width < x) || (x+width > this.minX + this.width)) {            throw new RasterFormatException("(x + width) is outside of Raster");        }        if ((y+height < y) || (y+height > this.minY + this.height)) {            throw new RasterFormatException("(y + height) is outside of Raster");        }         SampleModel sm;         if (bandList != null)            sm = sampleModel.createSubsetSampleModel(bandList);        else            sm = sampleModel;         int deltaX = x0 - x;        int deltaY = y0 - y;         return new ByteComponentRaster(sm,                                       dataBuffer,                                       new Rectangle(x0, y0, width, height),                                       new Point(sampleModelTranslateX+deltaX,                                                 sampleModelTranslateY+deltaY),                                       this);    }     /**     * Creates a Raster with the same layout but using a different     * width and height, and with new zeroed data arrays.     */    public WritableRaster createCompatibleWritableRaster(int w, int h) {        if (w <= 0 || h <=0) {            throw new RasterFormatException("negative "+                                          ((w <= 0) ? "width" : "height"));        }         SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);         return new ByteComponentRaster(sm , new Point(0,0));     }     /**     * Creates a Raster with the same layout and the same     * width and height, and with new zeroed data arrays.  If     * the Raster is a subRaster, this will call     * createCompatibleRaster(width, height).     */    public WritableRaster createCompatibleWritableRaster() {        return createCompatibleWritableRaster(width,height);    }     /**     * Verify that the layout parameters are consistent with     * the data.  If strictCheck     * is false, this method will check for ArrayIndexOutOfBounds conditions.  If     * strictCheck is true, this method will check for additional error     * conditions such as line wraparound (width of a line greater than     * the scanline stride).     * @return   String   Error string, if the layout is incompatible with     *                    the data.  Otherwise returns null.     */    private void verify (boolean strictCheck) {        // Make sure data for Raster is in a legal range        for (int i=0; i < dataOffsets.length; i++) {            if (dataOffsets[i] < 0) {                throw new RasterFormatException("Data offsets for band "+i+                                                "("+dataOffsets[i]+                                                ") must be >= 0");            }        }         int maxSize = 0;        int size;         for (int i=0; i < numDataElements; i++) {            size = (height-1)*scanlineStride + (width-1)*pixelStride +                dataOffsets[i];            if (size > maxSize) {                maxSize = size;            }        }        if (data.length < maxSize) {            throw new RasterFormatException("Data array too small (should be "+                                          maxSize+" )");        }    }     public String toString() {        return new String ("ByteComponentRaster: width = "+width+" height = "                           + height                           +" #numDataElements "+numDataElements                           //  +" xOff = "+xOffset+" yOff = "+yOffset                           +" dataOff[0] = "+dataOffsets[0]);    } //    /**//     * For debugging...  prints a region of a one-band ByteComponentRaster//     *///    public void print(int x, int y, int w, int h) {//        // REMIND:  Only works for 1 band!//        System.out.println(this);//        int offset = dataOffsets[0] + y*scanlineStride + x*pixelStride;//        int off;//        for (int yoff=0; yoff < h; yoff++, offset += scanlineStride) {//            off = offset;//            System.out.print("Line "+(y+yoff)+": ");//            for (int xoff = 0; xoff < w; xoff++, off+= pixelStride) {//                String s = Integer.toHexString(data[off]);//                if (s.length() == 8) {//                    s = s.substring(6,8);//                }//                System.out.print(s+" ");//            }//            System.out.println("");//        }//    }  }

Lines 501–926 of 926. Previous lines