Example usage for java.awt.image Raster getWidth

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

Introduction

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

Prototype

public final int getWidth() 

Source Link

Document

Returns the width in pixels of the Raster.

Usage

From source file:GraphicsUtil.java

public static void copyData_FALLBACK(Raster src, WritableRaster dst) {
    // System.out.println("Fallback copyData");

    int x0 = dst.getMinX();
    if (x0 < src.getMinX())
        x0 = src.getMinX();//www .j a  v a  2 s.c  o  m

    int y0 = dst.getMinY();
    if (y0 < src.getMinY())
        y0 = src.getMinY();

    int x1 = dst.getMinX() + dst.getWidth() - 1;
    if (x1 > src.getMinX() + src.getWidth() - 1)
        x1 = src.getMinX() + src.getWidth() - 1;

    int y1 = dst.getMinY() + dst.getHeight() - 1;
    if (y1 > src.getMinY() + src.getHeight() - 1)
        y1 = src.getMinY() + src.getHeight() - 1;

    int width = x1 - x0 + 1;
    int[] data = null;

    for (int y = y0; y <= y1; y++) {
        data = src.getPixels(x0, y, width, 1, data);
        dst.setPixels(x0, y, width, 1, data);
    }
}

From source file:GraphicsUtil.java

/**
 * An internal optimized version of copyData designed to work on
 * Integer packed data with a SinglePixelPackedSampleModel.  Only
 * the region of overlap between src and dst is copied.
 *
 * Calls to this should be preflighted with is_INT_PACK_Data
 * on both src and dest (requireAlpha can be false).
 *
 * @param src The source of the data//  ww w  . ja v a  2  s .c o  m
 * @param dst The destination for the data.
 */
public static void copyData_INT_PACK(Raster src, WritableRaster dst) {
    // System.out.println("Fast copyData");
    int x0 = dst.getMinX();
    if (x0 < src.getMinX())
        x0 = src.getMinX();

    int y0 = dst.getMinY();
    if (y0 < src.getMinY())
        y0 = src.getMinY();

    int x1 = dst.getMinX() + dst.getWidth() - 1;
    if (x1 > src.getMinX() + src.getWidth() - 1)
        x1 = src.getMinX() + src.getWidth() - 1;

    int y1 = dst.getMinY() + dst.getHeight() - 1;
    if (y1 > src.getMinY() + src.getHeight() - 1)
        y1 = src.getMinY() + src.getHeight() - 1;

    int width = x1 - x0 + 1;
    int height = y1 - y0 + 1;

    SinglePixelPackedSampleModel srcSPPSM;
    srcSPPSM = (SinglePixelPackedSampleModel) src.getSampleModel();

    final int srcScanStride = srcSPPSM.getScanlineStride();
    DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer();
    final int[] srcPixels = srcDB.getBankData()[0];
    final int srcBase = (srcDB.getOffset()
            + srcSPPSM.getOffset(x0 - src.getSampleModelTranslateX(), y0 - src.getSampleModelTranslateY()));

    SinglePixelPackedSampleModel dstSPPSM;
    dstSPPSM = (SinglePixelPackedSampleModel) dst.getSampleModel();

    final int dstScanStride = dstSPPSM.getScanlineStride();
    DataBufferInt dstDB = (DataBufferInt) dst.getDataBuffer();
    final int[] dstPixels = dstDB.getBankData()[0];
    final int dstBase = (dstDB.getOffset()
            + dstSPPSM.getOffset(x0 - dst.getSampleModelTranslateX(), y0 - dst.getSampleModelTranslateY()));

    if ((srcScanStride == dstScanStride) && (srcScanStride == width)) {
        // System.out.println("VERY Fast copyData");

        System.arraycopy(srcPixels, srcBase, dstPixels, dstBase, width * height);
    } else if (width > 128) {
        int srcSP = srcBase;
        int dstSP = dstBase;
        for (int y = 0; y < height; y++) {
            System.arraycopy(srcPixels, srcSP, dstPixels, dstSP, width);
            srcSP += srcScanStride;
            dstSP += dstScanStride;
        }
    } else {
        for (int y = 0; y < height; y++) {
            int srcSP = srcBase + y * srcScanStride;
            int dstSP = dstBase + y * dstScanStride;
            for (int x = 0; x < width; x++)
                dstPixels[dstSP++] = srcPixels[srcSP++];
        }
    }
}

From source file:fr.gael.drb.cortex.topic.sentinel3.jai.operator.QuicklookSlstrRIF.java

private BufferedImage toGrayScale(Raster in, PixelCorrection c, boolean invertColors, double lowerBound,
        double upperBound) {
    double offset = -lowerBound;
    double scaleFactor = 256. / (upperBound - lowerBound);
    int width = in.getWidth();
    int height = in.getHeight();

    // generate//  www.  j a  v a2s. co m
    BufferedImage out = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            int pixel = checkAndApplyCorrection(in.getSample(i, j, 0), c);
            if (pixel == c.nodata) {
                if (invertColors)
                    out.setRGB(i, j, new Color(255, 255, 255).getRGB());
                else
                    out.setRGB(i, j, new Color(0, 0, 0).getRGB());
                continue;
            }

            double normalized = (pixel + offset) * scaleFactor;
            int gray = (int) (Math.max(0, Math.min(255, normalized)));
            if (invertColors)
                gray = 255 - gray;
            out.setRGB(i, j, new Color(gray, gray, gray).getRGB());
        }
    }
    return out;
}

From source file:fr.gael.drb.cortex.topic.sentinel3.jai.operator.QuicklookSlstrRIF.java

/**
 * Compute the natural color QL from passed images. This method only 
 * assembles provided images as red/green/blue 8 bits channels.
 * @param red red channel image/*from w w w .j  a  va2s.c o  m*/
 * @param green green channel image
 * @param blue blue channel image
 * @return the assembled image.
 */
private RenderedImage naturalColors(Raster red, PixelCorrection rc, Raster green, PixelCorrection gc,
        Raster blue, PixelCorrection bc) {

    BufferedImage bred = toGrayScale(red, rc, false, false);
    BufferedImage bgreen = toGrayScale(green, gc, false, false);
    BufferedImage bblue = toGrayScale(blue, bc, false, false);

    BufferedImage quicklook = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_RGB);

    for (int j = 0; j < red.getHeight(); j++) {
        for (int i = 0; i < red.getWidth(); i++) {
            int cred = new Color(bred.getRGB(i, j)).getRed();
            int cgreen = new Color(bgreen.getRGB(i, j)).getGreen();
            int cblue = new Color(bblue.getRGB(i, j)).getBlue();

            quicklook.setRGB(i, j, new Color(cred, cgreen, cblue).getRGB());
        }
    }

    return quicklook;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploaderThumbnailerTester_2.java

private boolean isBlackEdge(int fromX, int toX, int fromY, int toY, Raster imageData) {
    int edgeTotal = 0;
    try {//  w  w w.ja  v  a2 s  .co m
        for (int col = fromX; col <= toX; col++) {
            for (int row = fromY; row <= toY; row++) {
                edgeTotal += sumPixel(imageData, col, row);
            }
        }
    } catch (Exception e) {
        log.error("can't sum edge: fromX=" + fromX + ", toX=" + toX + ", fromY=" + fromY + ", toY=" + toY
                + ", imageWidth=" + imageData.getWidth() + ", imageHeight=" + imageData.getHeight() + ": " + e);
    }

    log.debug("edge total = " + edgeTotal);
    return edgeTotal < EDGE_THRESHOLD;
}

From source file:main.MapKit.java

@Override
public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel,
        RenderingHints hints) {/*from w w w .j a va  2s . c o m*/
    return new CompositeContext() {
        @Override
        public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
            if (src.getSampleModel().getDataType() != DataBuffer.TYPE_INT
                    || dstIn.getSampleModel().getDataType() != DataBuffer.TYPE_INT
                    || dstOut.getSampleModel().getDataType() != DataBuffer.TYPE_INT) {
                throw new IllegalStateException("Source and destination must store pixels as INT.");
            }

            int width = Math.min(src.getWidth(), dstIn.getWidth());
            int height = Math.min(src.getHeight(), dstIn.getHeight());

            int[] srcPixel = new int[4];
            int[] dstPixel = new int[4];
            int[] srcPixels = new int[width];
            int[] dstPixels = new int[width];

            for (int y = 0; y < height; y++) {
                src.getDataElements(0, y, width, 1, srcPixels);
                dstIn.getDataElements(0, y, width, 1, dstPixels);

                for (int x = 0; x < width; x++) {
                    // pixels are stored as INT_ARGB
                    // our arrays are [R, G, B, A]
                    int pixel = srcPixels[x];
                    srcPixel[0] = (pixel >> 16) & 0xFF;
                    srcPixel[1] = (pixel >> 8) & 0xFF;
                    srcPixel[2] = (pixel >> 0) & 0xFF;
                    srcPixel[3] = (pixel >> 24) & 0xFF;

                    pixel = dstPixels[x];
                    dstPixel[0] = (pixel >> 16) & 0xFF;
                    dstPixel[1] = (pixel >> 8) & 0xFF;
                    dstPixel[2] = (pixel >> 0) & 0xFF;
                    dstPixel[3] = (pixel >> 24) & 0xFF;

                    int[] result = new int[] { (srcPixel[0] * dstPixel[0]) >> 8,
                            (srcPixel[1] * dstPixel[1]) >> 8, (srcPixel[2] * dstPixel[2]) >> 8,
                            (srcPixel[3] * dstPixel[3]) >> 8 };

                    // mixes the result with the opacity
                    dstPixels[x] = (result[3]) << 24 | (result[0]) << 16 | (result[1]) << 8 | (result[2]);
                }
                dstOut.setDataElements(0, y, width, 1, dstPixels);
            }
        }

        @Override
        public void dispose() {
            // empty
        }
    };
}

From source file:com.occamlab.te.parsers.ImageParser.java

private static void processBufferedImage(BufferedImage buffimage, String formatName, NodeList nodes)
        throws Exception {
    HashMap<Object, Object> bandMap = new HashMap<Object, Object>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getLocalName().equals("subimage")) {
                Element e = (Element) node;
                int x = Integer.parseInt(e.getAttribute("x"));
                int y = Integer.parseInt(e.getAttribute("y"));
                int w = Integer.parseInt(e.getAttribute("width"));
                int h = Integer.parseInt(e.getAttribute("height"));
                processBufferedImage(buffimage.getSubimage(x, y, w, h), formatName, e.getChildNodes());
            } else if (node.getLocalName().equals("checksum")) {
                CRC32 checksum = new CRC32();
                Raster raster = buffimage.getRaster();
                DataBufferByte buffer;
                if (node.getParentNode().getLocalName().equals("subimage")) {
                    WritableRaster outRaster = raster.createCompatibleWritableRaster();
                    buffimage.copyData(outRaster);
                    buffer = (DataBufferByte) outRaster.getDataBuffer();
                } else {
                    buffer = (DataBufferByte) raster.getDataBuffer();
                }/*from w w  w  . j  a va 2  s  .  c  om*/
                int numbanks = buffer.getNumBanks();
                for (int j = 0; j < numbanks; j++) {
                    checksum.update(buffer.getData(j));
                }
                Document doc = node.getOwnerDocument();
                node.appendChild(doc.createTextNode(Long.toString(checksum.getValue())));
            } else if (node.getLocalName().equals("count")) {
                String band = ((Element) node).getAttribute("bands");
                String sample = ((Element) node).getAttribute("sample");
                if (sample.equals("all")) {
                    bandMap.put(band, null);
                } else {
                    HashMap<Object, Object> sampleMap = (HashMap<Object, Object>) bandMap.get(band);
                    if (sampleMap == null) {
                        if (!bandMap.containsKey(band)) {
                            sampleMap = new HashMap<Object, Object>();
                            bandMap.put(band, sampleMap);
                        }
                    }
                    sampleMap.put(Integer.decode(sample), new Integer(0));
                }
            } else if (node.getLocalName().equals("transparentNodata")) { // 2011-08-24
                                                                          // PwD
                String transparentNodata = checkTransparentNodata(buffimage, node);
                node.setTextContent(transparentNodata);
            }
        }
    }

    Iterator bandIt = bandMap.keySet().iterator();
    while (bandIt.hasNext()) {
        String band_str = (String) bandIt.next();
        int band_indexes[];
        if (buffimage.getType() == BufferedImage.TYPE_BYTE_BINARY
                || buffimage.getType() == BufferedImage.TYPE_BYTE_GRAY) {
            band_indexes = new int[1];
            band_indexes[0] = 0;
        } else {
            band_indexes = new int[band_str.length()];
            for (int i = 0; i < band_str.length(); i++) {
                if (band_str.charAt(i) == 'A')
                    band_indexes[i] = 3;
                if (band_str.charAt(i) == 'B')
                    band_indexes[i] = 2;
                if (band_str.charAt(i) == 'G')
                    band_indexes[i] = 1;
                if (band_str.charAt(i) == 'R')
                    band_indexes[i] = 0;
            }
        }

        Raster raster = buffimage.getRaster();
        java.util.HashMap sampleMap = (java.util.HashMap) bandMap.get(band_str);
        boolean addall = (sampleMap == null);
        if (sampleMap == null) {
            sampleMap = new java.util.HashMap();
            bandMap.put(band_str, sampleMap);
        }

        int minx = raster.getMinX();
        int maxx = minx + raster.getWidth();
        int miny = raster.getMinY();
        int maxy = miny + raster.getHeight();
        int bands[][] = new int[band_indexes.length][raster.getWidth()];

        for (int y = miny; y < maxy; y++) {
            for (int i = 0; i < band_indexes.length; i++) {
                raster.getSamples(minx, y, maxx, 1, band_indexes[i], bands[i]);
            }
            for (int x = minx; x < maxx; x++) {
                int sample = 0;
                for (int i = 0; i < band_indexes.length; i++) {
                    sample |= bands[i][x] << ((band_indexes.length - i - 1) * 8);
                }

                Integer sampleObj = new Integer(sample);

                boolean add = addall;
                if (!addall) {
                    add = sampleMap.containsKey(sampleObj);
                }
                if (add) {
                    Integer count = (Integer) sampleMap.get(sampleObj);
                    if (count == null) {
                        count = new Integer(0);
                    }
                    count = new Integer(count.intValue() + 1);
                    sampleMap.put(sampleObj, count);
                }
            }
        }
    }

    Node node = nodes.item(0);
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getLocalName().equals("count")) {
                String band = ((Element) node).getAttribute("bands");
                String sample = ((Element) node).getAttribute("sample");
                HashMap sampleMap = (HashMap) bandMap.get(band);
                Document doc = node.getOwnerDocument();
                if (sample.equals("all")) {
                    Node parent = node.getParentNode();
                    Node prevSibling = node.getPreviousSibling();
                    Iterator sampleIt = sampleMap.keySet().iterator();
                    Element countnode = null;
                    int digits;
                    String prefix;
                    switch (buffimage.getType()) {
                    case BufferedImage.TYPE_BYTE_BINARY:
                        digits = 1;
                        prefix = "";
                        break;
                    case BufferedImage.TYPE_BYTE_GRAY:
                        digits = 2;
                        prefix = "0x";
                        break;
                    default:
                        prefix = "0x";
                        digits = band.length() * 2;
                    }
                    while (sampleIt.hasNext()) {
                        countnode = doc.createElementNS(node.getNamespaceURI(), "count");
                        Integer sampleInt = (Integer) sampleIt.next();
                        Integer count = (Integer) sampleMap.get(sampleInt);
                        if (band.length() > 0) {
                            countnode.setAttribute("bands", band);
                        }
                        countnode.setAttribute("sample", prefix + HexString(sampleInt.intValue(), digits));
                        Node textnode = doc.createTextNode(count.toString());
                        countnode.appendChild(textnode);
                        parent.insertBefore(countnode, node);
                        if (sampleIt.hasNext()) {
                            if (prevSibling != null && prevSibling.getNodeType() == Node.TEXT_NODE) {
                                parent.insertBefore(prevSibling.cloneNode(false), node);
                            }
                        }
                    }
                    parent.removeChild(node);
                    node = countnode;
                } else {
                    Integer count = (Integer) sampleMap.get(Integer.decode(sample));
                    if (count == null)
                        count = new Integer(0);
                    Node textnode = doc.createTextNode(count.toString());
                    node.appendChild(textnode);
                }
            }
        }
        node = node.getNextSibling();
    }
}

From source file:org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderImageIO.java

private BufferedImage getFallbackBufferedImage(ImageReader reader, int pageIndex, ImageReadParam param)
        throws IOException {
    //Work-around found at: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4799903
    //There are some additional ideas there if someone wants to go further.

    // Try reading a Raster (no color conversion).
    Raster raster = reader.readRaster(pageIndex, param);

    // Arbitrarily select a BufferedImage type.
    int imageType;
    switch (raster.getNumBands()) {
    case 1:/*from w w w.j a  va2s .  c  o  m*/
        imageType = BufferedImage.TYPE_BYTE_GRAY;
        break;
    case 3:
        imageType = BufferedImage.TYPE_3BYTE_BGR;
        break;
    case 4:
        imageType = BufferedImage.TYPE_4BYTE_ABGR;
        break;
    default:
        throw new UnsupportedOperationException();
    }

    // Create a BufferedImage.
    BufferedImage bi = new BufferedImage(raster.getWidth(), raster.getHeight(), imageType);

    // Set the image data.
    bi.getRaster().setRect(raster);
    return bi;
}

From source file:org.esa.s2tbx.dataio.jp2.internal.JP2TileOpImage.java

private void computeRectDirect(WritableRaster dest, Rectangle destRect) {
    try (OpenJP2Decoder decoder = new OpenJP2Decoder(this.cacheDir, this.imageFile, this.bandIndex,
            this.dataType, getLevel(), 20, tileIndex)) {
        Raster readTileImage = null;
        final DataBuffer dataBuffer = dest.getDataBuffer();
        int tileWidth = this.getTileWidth();
        int tileHeight = this.getTileHeight();
        final int fileTileX = destRect.x / tileLayout.tileWidth;
        final int fileTileY = destRect.y / tileLayout.tileHeight;
        int fileTileOriginX = destRect.x - fileTileX * tileLayout.tileWidth;
        int fileTileOriginY = destRect.y - fileTileY * tileLayout.tileHeight;
        Dimension dimensions = decoder.getImageDimensions();
        Rectangle fileTileRect = new Rectangle(0, 0, dimensions.width, dimensions.height);

        if (fileTileOriginX == 0 && tileLayout.tileWidth == tileWidth && fileTileOriginY == 0
                && tileLayout.tileHeight == tileHeight && tileWidth * tileHeight == dataBuffer.getSize()) {
            readTileImage = decoder.read(null);
        } else {//from www.j  a v  a2 s.  c o  m
            final Rectangle tileRect = new Rectangle(fileTileOriginX, fileTileOriginY, tileWidth, tileHeight);
            final Rectangle intersection = fileTileRect.intersection(tileRect);
            if (!intersection.isEmpty()) {
                readTileImage = decoder.read(intersection);
            }
        }
        if (readTileImage != null) {
            Raster readBandRaster = readTileImage.createChild(0, 0, readTileImage.getWidth(),
                    readTileImage.getHeight(), 0, 0, bands);
            dest.setDataElements(dest.getMinX(), dest.getMinY(), readBandRaster);
        }

    } catch (IOException e) {
        logger.severe(e.getMessage());
    }
}

From source file:org.geoserver.jai.ConcurrentTileFactory.java

/**
 * Recycle the given tile.//from www  .j av  a 2  s.c o  m
 */
public void recycleTile(Raster tile) {
    if (tile.getWidth() != tile.getHeight() || tile.getWidth() > 512) {
        return;
    }
    recycledArrays.recycleTile(tile);
}