Example usage for java.awt.image WritableRaster setDataElements

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

Introduction

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

Prototype

public void setDataElements(int x, int y, Raster inRaster) 

Source Link

Document

Sets the data for a rectangle of pixels from an input Raster.

Usage

From source file:CheckAlignmentOpImage.java

public Raster computeTile(int x, int y) {
    Raster r1 = source1.getTile(x, y);
    Raster r2 = source2.getTile(x, y);

    int xBounds = r1.getWidth();
    if (r2.getWidth() < xBounds)
        xBounds = r2.getWidth();//from  w  ww . ja va  2 s  . c  om
    int yBounds = r1.getHeight();
    if (r2.getHeight() < yBounds)
        yBounds = r2.getHeight();

    WritableRaster wr;
    wr = r1.createCompatibleWritableRaster(xBounds, yBounds);

    int tmpi;
    int tmpj;
    for (int i = 0; i < wr.getWidth(); i++)
        for (int j = 0; j < wr.getHeight(); j++) {
            tmpi = i / samplingPeriod;
            tmpj = j / samplingPeriod;
            if ((tmpi % 2 == 0) && (tmpj % 2 == 0))
                wr.setDataElements(i, j, r2.getDataElements(i, j, null));
            else if ((tmpi % 2 != 0) && (tmpj % 2 != 0))
                wr.setDataElements(i, j, r2.getDataElements(i, j, null));
            else
                wr.setDataElements(i, j, r1.getDataElements(i, j, null));
        }
    return wr;
}

From source file:RasterImageTest.java

/**
 * Makes the Mandelbrot image./*from www  . j  a v a  2 s  . c o m*/
 * @param width the width
 * @parah height the height
 * @return the image
 */
public BufferedImage makeMandelbrot(int width, int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    WritableRaster raster = image.getRaster();
    ColorModel model = image.getColorModel();

    Color fractalColor = Color.red;
    int argb = fractalColor.getRGB();
    Object colorData = model.getDataElements(argb, null);

    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {
            double a = XMIN + i * (XMAX - XMIN) / width;
            double b = YMIN + j * (YMAX - YMIN) / height;
            if (!escapesToInfinity(a, b))
                raster.setDataElements(i, j, colorData);
        }
    return image;
}

From source file:haven.Utils.java

public static BufferedImage outline(BufferedImage img, Color col) {
    Coord sz = imgsz(img).add(2, 2);/*from  w w w.  j  a  va2  s.c  o m*/
    BufferedImage ol = TexI.mkbuf(sz);
    Object fcol = ol.getColorModel().getDataElements(col.getRGB(), null);
    Raster src = img.getRaster();
    WritableRaster dst = ol.getRaster();
    for (int y = 0; y < sz.y; y++) {
        for (int x = 0; x < sz.x; x++) {
            boolean t;
            if ((y == 0) || (x == 0) || (y == sz.y - 1) || (x == sz.x - 1)) {
                t = true;
            } else {
                t = src.getSample(x - 1, y - 1, 3) < 250;
            }
            if (!t)
                continue;
            if (((x > 1) && (y > 0) && (y < sz.y - 1) && (src.getSample(x - 2, y - 1, 3) >= 250))
                    || ((x > 0) && (y > 1) && (x < sz.x - 1) && (src.getSample(x - 1, y - 2, 3) >= 250))
                    || ((x < sz.x - 2) && (y > 0) && (y < sz.y - 1) && (src.getSample(x, y - 1, 3) >= 250))
                    || ((x > 0) && (y < sz.y - 2) && (x < sz.x - 1) && (src.getSample(x - 1, y, 3) >= 250)))
                dst.setDataElements(x, y, fcol);
        }
    }
    return (ol);
}

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 w  w  w.ja  v  a 2 s.  c  om
            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.apache.pdfbox.pdmodel.graphics.image.SampledImageReader.java

private static BufferedImage fromAny(PDImage pdImage, WritableRaster raster, COSArray colorKey)
        throws IOException {
    final PDColorSpace colorSpace = pdImage.getColorSpace();
    final int numComponents = colorSpace.getNumberOfComponents();
    final int width = pdImage.getWidth();
    final int height = pdImage.getHeight();
    final int bitsPerComponent = pdImage.getBitsPerComponent();
    final float[] decode = getDecodeArray(pdImage);

    // read bit stream
    ImageInputStream iis = null;// ww w.j  a  v a  2  s . c  o  m
    try {
        // create stream
        iis = new MemoryCacheImageInputStream(pdImage.createInputStream());
        final float sampleMax = (float) Math.pow(2, bitsPerComponent) - 1f;
        final boolean isIndexed = colorSpace instanceof PDIndexed;

        // init color key mask
        float[] colorKeyRanges = null;
        BufferedImage colorKeyMask = null;
        if (colorKey != null) {
            colorKeyRanges = colorKey.toFloatArray();
            colorKeyMask = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
        }

        // calculate row padding
        int padding = 0;
        if (width * numComponents * bitsPerComponent % 8 > 0) {
            padding = 8 - (width * numComponents * bitsPerComponent % 8);
        }

        // read stream
        byte[] srcColorValues = new byte[numComponents];
        byte[] alpha = new byte[1];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                boolean isMasked = true;
                for (int c = 0; c < numComponents; c++) {
                    int value = (int) iis.readBits(bitsPerComponent);

                    // color key mask requires values before they are decoded
                    if (colorKeyRanges != null) {
                        isMasked &= value >= colorKeyRanges[c * 2] && value <= colorKeyRanges[c * 2 + 1];
                    }

                    // decode array
                    final float dMin = decode[c * 2];
                    final float dMax = decode[(c * 2) + 1];

                    // interpolate to domain
                    float output = dMin + (value * ((dMax - dMin) / sampleMax));

                    if (isIndexed) {
                        // indexed color spaces get the raw value, because the TYPE_BYTE
                        // below cannot be reversed by the color space without it having
                        // knowledge of the number of bits per component
                        srcColorValues[c] = (byte) Math.round(output);
                    } else {
                        // interpolate to TYPE_BYTE
                        int outputByte = Math
                                .round(((output - Math.min(dMin, dMax)) / Math.abs(dMax - dMin)) * 255f);

                        srcColorValues[c] = (byte) outputByte;
                    }
                }
                raster.setDataElements(x, y, srcColorValues);

                // set alpha channel in color key mask, if any
                if (colorKeyMask != null) {
                    alpha[0] = (byte) (isMasked ? 255 : 0);
                    colorKeyMask.getRaster().setDataElements(x, y, alpha);
                }
            }

            // rows are padded to the nearest byte
            iis.readBits(padding);
        }

        // use the color space to convert the image to RGB
        BufferedImage rgbImage = colorSpace.toRGBImage(raster);

        // apply color mask, if any
        if (colorKeyMask != null) {
            return applyColorKeyMask(rgbImage, colorKeyMask);
        } else {
            return rgbImage;
        }
    } finally {
        if (iis != null) {
            iis.close();
        }
    }
}

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

private void computeRectIndirect(WritableRaster dest, Rectangle destRect) {
    try {//from  w  w  w.  j  a  va  2s.co  m
        Path tile = decompressTile(tileIndex, getLevel());
        RenderedImage readTileImage = null;
        if (tile != null) {
            try (ImageReader imageReader = new ImageReader(tile)) {
                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;
                Rectangle fileTileRect = tileDims.get(tile);
                if (fileTileRect == null) {
                    fileTileRect = new Rectangle(0, 0, imageReader.getImageWidth(),
                            imageReader.getImageHeight());
                    tileDims.put(tile, fileTileRect);
                }
                if (fileTileOriginX == 0 && tileLayout.tileWidth == tileWidth && fileTileOriginY == 0
                        && tileLayout.tileHeight == tileHeight
                        && tileWidth * tileHeight == dataBuffer.getSize()) {
                    readTileImage = imageReader.read();
                } else {
                    final Rectangle tileRect = new Rectangle(fileTileOriginX, fileTileOriginY, tileWidth,
                            tileHeight);
                    final Rectangle intersection = fileTileRect.intersection(tileRect);
                    if (!intersection.isEmpty()) {
                        readTileImage = imageReader.read(intersection);
                    }
                }
                if (readTileImage != null) {
                    Raster readBandRaster = readTileImage.getData().createChild(0, 0, readTileImage.getWidth(),
                            readTileImage.getHeight(), 0, 0, new int[] { bandIndex });
                    dest.setDataElements(dest.getMinX(), dest.getMinY(), readBandRaster);
                }
            } catch (IOException e) {
                logger.severe(e.getMessage());
            }
        }
    } catch (IOException e) {
        logger.severe(e.getMessage());
    }
}