Example usage for java.awt.image WritableRaster getWidth

List of usage examples for java.awt.image WritableRaster getWidth

Introduction

In this page you can find the example usage for java.awt.image WritableRaster getWidth.

Prototype

public final int getWidth() 

Source Link

Document

Returns the width in pixels of the Raster.

Usage

From source file:tilt.image.Blob.java

/**
 * Is this blob surrounded by a wide band of white?
 * @param wr the raster to search in//from   www  . jav  a 2 s .c  o  m
 * @param hStandoff the amount of white horizontal standoff
 * @param vStandoff the amount of vertical white standoff
 * @return true if it is
 */
boolean hasWhiteStandoff(WritableRaster wr, int hStandoff, int vStandoff) {
    // 1. original blob bounds
    Rectangle inner = new Rectangle(topLeft().x, topLeft().y, getWidth(), getHeight());
    Rectangle outer = (Rectangle) inner.clone();
    // 2. outset rect by standoff
    if (outer.x >= hStandoff)
        outer.x -= hStandoff;
    else
        outer.x = 0;
    if (outer.y - vStandoff > 0)
        outer.y -= vStandoff;
    else
        outer.y = 0;
    if (outer.x + outer.width + hStandoff * 2 < wr.getWidth())
        outer.width += hStandoff * 2;
    else
        outer.width = wr.getWidth() - outer.x;
    if (outer.y + outer.height + vStandoff * 2 < wr.getHeight())
        outer.height += vStandoff * 2;
    else
        outer.height = wr.getHeight() - outer.y;
    // 3. test for black pixels in that area
    int[] iArray = new int[1];
    int maxY = outer.y + outer.height;
    int maxX = outer.x + outer.width;
    int nBlacks = 0;
    int maxRogues = opts.getInt(Options.Keys.maxRoguePixels);
    for (int y = outer.y; y < maxY; y++) {
        for (int x = outer.x; x < maxX; x++) {
            Point loc = new Point(x, y);
            if (!inner.contains(loc)) {
                wr.getPixel(x, y, iArray);
                if (iArray[0] == 0) {
                    if (nBlacks == maxRogues) {
                        return false;
                    } else
                        nBlacks++;
                }
            }
        }
    }
    return true;
}

From source file:tilt.image.Blob.java

/**
 * Check if there is one dirty dot not already seen.
 * @param wr the raster to search/*from w  ww  .j  ava 2s  .  co  m*/
 * @param start the first blackdot
 * @param iArray the array containing the dirty dot
 */
void checkDirtyDot(WritableRaster wr, Point start, int[] iArray) {
    hull = new ArrayList<>();
    // do it without recursion
    stack.push(start);
    while (!stack.empty()) {
        Point p = stack.pop();
        wr.getPixel(p.x, p.y, iArray);
        if (iArray[0] <= getBlackLevel(p)) {
            if (dirt != null)
                dirt.getPixel(p.x, p.y, iArray);
            else
                parent.getPixel(p.x, p.y, iArray);
            if (iArray[0] != 0) {
                iArray[0] = 0;
                // set one pixel known to be black 
                setBlackPixel(p.x, p.y, iArray);
                // top
                if (p.y > 0)
                    stack.push(new Point(p.x, p.y - 1));
                // bottom
                if (p.y < wr.getHeight() - 1)
                    stack.push(new Point(p.x, p.y + 1));
                // left
                if (p.x > 0)
                    stack.push(new Point(p.x - 1, p.y));
                // right
                if (p.x < wr.getWidth() - 1)
                    stack.push(new Point(p.x + 1, p.y));
                // bot-left
                if (p.y < wr.getHeight() - 1 && p.x > 0)
                    stack.push(new Point(p.x - 1, p.y + 1));
                // bot-right
                if (p.y < wr.getHeight() - 1 && p.x < wr.getWidth() - 1)
                    stack.push(new Point(p.x + 1, p.y + 1));
                // top-left
                if (p.y > 0 && p.x > 0)
                    stack.push(new Point(p.x - 1, p.y - 1));
                // top-right
                if (p.y > 0 && p.x < wr.getWidth() - 1)
                    stack.push(new Point(p.x + 1, p.y - 1));
            }
        } else
            hull.add(p);
    }
}

From source file:nitf.imageio.NITFReader.java

/**
 * Reads image data as bytes for the given region, and writes it to the
 * given writable raster/*  w  ww . j a va2  s.co  m*/
 * 
 * @param sourceRegion
 * @param sourceXSubsampling
 * @param sourceYSubsampling
 * @param bandOffsets
 * @param destinationOffset
 * @param imRas
 * @return Raster
 * @throws IOException
 */
protected void readRaster(int imageIndex, Rectangle sourceRegion, Rectangle destRegion, int sourceXSubsampling,
        int sourceYSubsampling, int[] bandOffsets, int pixelSize, Point destinationOffset, WritableRaster imRas)
        throws IOException {
    checkIndex(imageIndex);

    try {
        ImageSubheader subheader = record.getImages()[imageIndex].getSubheader();
        int numCols = subheader.getNumCols().getIntData();
        int numRows = subheader.getNumRows().getIntData();

        // try to optimize the read call by reading in the entire
        // image at once
        if ((destRegion.height * sourceYSubsampling) == numRows
                && (destRegion.width * sourceXSubsampling) == numCols) {
            readFullImage(imageIndex, destRegion, sourceXSubsampling, sourceYSubsampling, bandOffsets,
                    pixelSize, imRas);
            return;
        }
        // the general purpose case
        else {
            int colBytes = destRegion.width * pixelSize;

            int dstMinX = imRas.getMinX();
            int dstMaxX = dstMinX + imRas.getWidth() - 1;
            int dstMinY = imRas.getMinY();
            int dstMaxY = dstMinY + imRas.getHeight() - 1;
            // int swap = 0;

            int nBands = subheader.getBandCount();

            /*
             * NOTE: This is a "fix" that will be removed once the
             * underlying NITRO library gets patched. Currently, if you make
             * a request of a single band, it doesn't matter which band you
             * request - the data from the first band will be returned
             * regardless. This is obviously wrong. To thwart this, we will
             * read all bands, then scale down what we return to the user
             * based on their actual request.
             */
            int[] requestBands = new int[nBands];
            for (int i = 0; i < nBands; ++i)
                requestBands[i] = i;

            byte[][] rowBuf = new byte[requestBands.length][colBytes];

            // make a SubWindow from the params
            // TODO may want to read by blocks or rows to make faster and
            // more
            // memory efficient
            SubWindow window;
            window = new SubWindow();
            window.setNumBands(requestBands.length);
            window.setBandList(requestBands);
            window.setNumCols(destRegion.width);
            window.setNumRows(1);
            window.setStartCol(sourceRegion.x);
            window.setStartRow(sourceRegion.y);

            // the NITRO library can do the subsampling for us
            if (sourceYSubsampling != 1 || sourceXSubsampling != 1) {
                DownSampler downSampler = new PixelSkipDownSampler(sourceYSubsampling, sourceXSubsampling);
                window.setDownSampler(downSampler);
            }

            // String pixelJustification = record.getImages()[imageIndex]
            // .getSubheader().getPixelJustification().getStringData()
            // .trim();
            // swap = pixelJustification.equals("R") ? 1 : 0;

            List<ByteBuffer> bandBufs = new ArrayList<ByteBuffer>();
            for (int i = 0; i < requestBands.length; ++i) {
                ByteBuffer bandBuf = null;
                bandBuf = ByteBuffer.wrap(rowBuf[i]);
                // bandBuf.order(ByteOrder.nativeOrder());
                // bandBuf.order(swap == 0 ? ByteOrder.BIG_ENDIAN
                // : ByteOrder.LITTLE_ENDIAN);
                bandBufs.add(bandBuf);
            }

            nitf.ImageReader imageReader = getImageReader(imageIndex);
            for (int srcY = 0; srcY < sourceRegion.height; srcY++) {
                if (sourceYSubsampling != 1 && (srcY % sourceYSubsampling) != 0)
                    continue;

                window.setStartRow(sourceRegion.y + srcY);

                // Read the row
                try {
                    imageReader.read(window, rowBuf);
                } catch (NITFException e) {
                    throw new IIOException("Error reading line " + srcY, e);
                }

                // Determine where the row will go in the destination
                int dstY = destinationOffset.y + srcY / sourceYSubsampling;
                if (dstY < dstMinY) {
                    continue; // The row is above imRas
                }
                if (dstY > dstMaxY) {
                    break; // We're done with the image
                }

                // Copy each (subsampled) source pixel into imRas
                for (int srcX = 0, dstX = destinationOffset.x; srcX < colBytes; srcX += pixelSize, dstX++) {
                    if (dstX < dstMinX) {
                        continue;
                    }
                    if (dstX > dstMaxX) {
                        break;
                    }

                    for (int i = 0; i < bandOffsets.length; ++i) {
                        ByteBuffer bandBuf = bandBufs.get(bandOffsets[i]);

                        switch (pixelSize) {
                        case 1:
                            imRas.setSample(dstX, dstY, i, bandBuf.get(srcX));
                            break;
                        case 2:
                            imRas.setSample(dstX, dstY, i, bandBuf.getShort(srcX));
                            break;
                        case 4:
                            imRas.setSample(dstX, dstY, i, bandBuf.getFloat(srcX));
                            break;
                        case 8:
                            imRas.setSample(dstX, dstY, i, bandBuf.getDouble(srcX));
                            break;
                        }
                    }
                }
            }
        }
    } catch (NITFException e1) {
        throw new IOException(ExceptionUtils.getStackTrace(e1));
    }
}