Example usage for java.awt.image BufferedImage setRGB

List of usage examples for java.awt.image BufferedImage setRGB

Introduction

In this page you can find the example usage for java.awt.image BufferedImage setRGB.

Prototype

public void setRGB(int x, int y, int rgb) 

Source Link

Document

Sets a pixel in this BufferedImage to the specified RGB value.

Usage

From source file:edu.stanford.epad.epadws.handlers.dicom.DSOUtil.java

private static File copyEmptyTiffFile(File original, String newFileName, int width, int height) {

    BufferedImage orjImg;// w w w .  ja  va  2s .  c o m
    try {
        orjImg = ImageIO.read(original);

        if (width != orjImg.getWidth() || height != orjImg.getHeight()) {
            log.warning("Width and height not right. Old width:" + width + " updated:" + orjImg.getWidth()
                    + " old height:" + height + " updated:" + orjImg.getHeight());
            width = orjImg.getWidth();
            height = orjImg.getHeight();

        }
    } catch (IOException e1) {
        log.warning("failed to read original tiff;", e1);
    }

    File newFile = null;
    try {
        long len = original.length();
        long rgbLen = width * height * 4;
        long greyLen = width * height;
        long bwLen = width * height / 8;
        int imagetype = BufferedImage.TYPE_BYTE_BINARY;
        if (len > greyLen)
            imagetype = BufferedImage.TYPE_BYTE_GRAY;
        if (len > rgbLen)
            imagetype = BufferedImage.TYPE_4BYTE_ABGR;
        newFile = File.createTempFile(newFileName, ".tif");
        //log.info("Creating empty tiff:" + newFile.getAbsolutePath() + " width:" + width + " height:" + height);
        BufferedImage bufferedImage = new BufferedImage(width, height, imagetype);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bufferedImage.setRGB(x, y, Color.black.getRGB());
            }
        }
        ImageIO.write(bufferedImage, "tif", newFile);
    } catch (IOException e) {
        log.warning("Error creating empty TIFF  file" + newFile.getAbsolutePath());
    }
    return newFile;
}

From source file:tests.TestDrawing.java

@Test
public void testDrawing() {
    BufferedImage img = null;
    BufferedImage imgs[] = new BufferedImage[2];

    try {/*from  w w w . jav  a 2 s  .  c  o  m*/
        imgs[0] = ImageIO.read(new File("../captures/4.jpg")); // eventually C:\\ImageTest\\pic2.jpg
        imgs[1] = ImageIO.read(new File("../captures/5.jpg")); // eventually C:\\ImageTest\\pic2.jpg
        img = ImageIO.read(new File("../captures/5.jpg")); // eventually C:\\ImageTest\\pic2.jpg
        for (int x = 0; x < img.getWidth(); x++) {
            for (int y = 0; y < img.getHeight(); y++) {
                int rgbDiff = RgbCalculator.absoluteDifference(imgs[0].getRGB(x, y), imgs[1].getRGB(x, y));
                img.setRGB(x, y, rgbDiff);
            }
        }

        Graphics2D g2d = img.createGraphics();
        g2d.setBackground(Color.BLUE);
        g2d.setColor(Color.RED);

        Vector3D[] corners = new Vector3D[] { new Vector3D(new double[] { 172, 78, 0 }),
                new Vector3D(new double[] { 455, 71, 0 }), new Vector3D(new double[] { 507, 350, 0 }),
                new Vector3D(new double[] { 143, 362, 0 }) };
        //draw lines from corner to corner
        for (int c = 0; c < corners.length; c++) {
            Vector3D v1 = corners[c];
            Vector3D v2 = corners[(c + 1) % corners.length];
            g2d.drawLine((int) v1.getX(), (int) v1.getY(), (int) v2.getX(), (int) v2.getY());
        }

        for (int x = 0; x < img.getWidth(); x++) {
            for (int y = 0; y < img.getHeight(); y++) {
                Vector3D currentPos = new Vector3D(new double[] { x, y, 0 });
                double[] normalizedCoordinates = new double[2];
                {
                    //normalize x coordinate
                    Line floor = new Line(corners[0], corners[3], 0.5);
                    Line roof = new Line(corners[1], corners[2], 0.5);
                    double floorDistance = floor.distance(currentPos) * 1 / corners[0].distance(corners[3]);
                    double roofDistance = roof.distance(currentPos) * 1 / corners[1].distance(corners[2]);
                    normalizedCoordinates[0] = interpolate(0, 1,
                            floorDistance / (floorDistance + roofDistance));
                }
                {
                    //normalize y coordinate
                    Line floor = new Line(corners[0], corners[1], 0.5);
                    Line roof = new Line(corners[2], corners[3], 0.5);
                    double floorDistance = floor.distance(currentPos) * 1 / corners[0].distance(corners[1]);
                    double roofDistance = roof.distance(currentPos) * 1 / corners[2].distance(corners[3]);
                    normalizedCoordinates[1] = interpolate(0, 1,
                            floorDistance / (floorDistance + roofDistance));
                }

                boolean isEdge = false;
                for (int d = 0; d < 2; d++) {
                    double val = normalizedCoordinates[d];
                    if (Math.abs((double) ((int) (val * 8)) - (val * 8)) < 0.05) {
                        isEdge = true;
                    }
                }
                if (isEdge) {
                    //color corners red:
                    img.setRGB(x, y, Color.RED.getRGB());
                }

            }

        }

        JFrame dialog = new JFrame();
        ImagePanel imagePanel = new ImagePanel(img);
        dialog.add(imagePanel);
        imagePanel.setSize(100, 100);
        dialog.pack();
        dialog.setSize(300, 300);
        dialog.setVisible(true);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    assertTrue("hello", true);
}

From source file:io.selendroid.android.impl.AbstractDevice.java

public byte[] takeScreenshot() throws AndroidDeviceException {
    if (device == null) {
        throw new AndroidDeviceException("Device not accessible via ddmlib.");
    }/*from w  ww.j av a2  s .  com*/
    RawImage rawImage;
    try {
        rawImage = device.getScreenshot();
    } catch (IOException ioe) {
        throw new AndroidDeviceException("Unable to get frame buffer: " + ioe.getMessage());
    } catch (TimeoutException e) {
        e.printStackTrace();
        throw new AndroidDeviceException(e.getMessage());
    } catch (AdbCommandRejectedException e) {
        e.printStackTrace();
        throw new AndroidDeviceException(e.getMessage());
    }

    // device/adb not available?
    if (rawImage == null)
        return null;

    BufferedImage image = new BufferedImage(rawImage.width, rawImage.height, BufferedImage.TYPE_INT_ARGB);

    int index = 0;
    int IndexInc = rawImage.bpp >> 3;
    for (int y = 0; y < rawImage.height; y++) {
        for (int x = 0; x < rawImage.width; x++) {
            int value = rawImage.getARGB(index);
            index += IndexInc;
            image.setRGB(x, y, value);
        }
    }
    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    try {
        if (!ImageIO.write(image, "png", stream)) {
            throw new IOException("Failed to find png writer");
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new AndroidDeviceException(e.getMessage());
    }
    byte[] raw = null;
    try {
        stream.flush();
        raw = stream.toByteArray();
        stream.close();
    } catch (IOException e) {
        throw new RuntimeException("I/O Error while capturing screenshot: " + e.getMessage());
    } finally {
        Closeable closeable = (Closeable) stream;
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }

    return raw;
}

From source file:yaphyre.core.films.ImageFile.java

private BufferedImage createImageFromSamples(double gamma) {

    BufferedImage image = new BufferedImage(xResolution, yResolution, BufferedImage.TYPE_INT_RGB);

    samples.asMap().entrySet().forEach(entry -> {

        Collection<Color> colorSamples = entry.getValue();
        Color sampleColor = colorSamples.stream().reduce(Color.BLACK, Color::add)
                .multiply(1d / colorSamples.size());
        sampleColor = ((gamma != 1d) ? sampleColor.pow(gamma) : sampleColor).clip();

        // flip the image camera: 0,0 is bottom left, BufferedImage: 0,0 is top left
        Point2D samplePoint = entry.getKey();
        int imageX = (int) samplePoint.getU();
        int imageY = (yResolution - 1) - ((int) samplePoint.getV());

        image.setRGB(imageX, imageY, createARGBfromColor(sampleColor));

    });/*from  w w  w  . ja v a2 s  .c om*/

    return image;
}

From source file:org.polymap.rhei.batik.engine.svg.Svg2Png.java

private static BufferedImage shiftHue(BufferedImage img, ImageConfiguration imageConfiguration) {
    int height = img.getHeight();
    int width = img.getWidth();
    int imageType = 0;
    if (imageConfiguration.getColorType() == COLOR_TYPE.MONOCHROM) {
        imageType = BufferedImage.TYPE_BYTE_BINARY;
    } else if (imageConfiguration.getColorType() == COLOR_TYPE.GRAY) {
        imageType = BufferedImage.TYPE_BYTE_GRAY;
    } else if (imageConfiguration.getColorType() == COLOR_TYPE.RGB) {
        imageType = BufferedImage.TYPE_INT_RGB;
    } else {/*w ww. java  2  s .  co m*/
        imageType = BufferedImage.TYPE_INT_ARGB;
    }

    BufferedImage finalThresholdImage = new BufferedImage(width, height, imageType);

    Float hueDelta = null;
    Float saturationDelta = null;
    Float brightnessDelta = null;
    if (imageConfiguration.getRgb() != null) {
        hueDelta = degreeToPercent(imageConfiguration.getRgb().getHSB()[0]);
        saturationDelta = imageConfiguration.getRgb().getHSB()[1];
        brightnessDelta = imageConfiguration.getRgb().getHSB()[2];
    } else {
        hueDelta = imageConfiguration.getAdjHue();
        saturationDelta = imageConfiguration.getAdjSaturation();
        brightnessDelta = imageConfiguration.getAdjBrightness();
    }

    if (hueDelta == null) {
        hueDelta = 0f;
    }
    if (saturationDelta == null) {
        saturationDelta = 0f;
    }
    if (brightnessDelta == null) {
        brightnessDelta = 0f;
    }
    for (int x = 0; x < width; x++) {
        try {
            for (int y = 0; y < height; y++) {
                int rgb = img.getRGB(x, y);
                Color color = new Color(rgb);
                float[] hsb = getHsb(imageConfiguration, color);
                hsb = applyDeltas(imageConfiguration, hueDelta, saturationDelta, brightnessDelta, hsb);
                hsb = replaceColors(imageConfiguration, color, hsb);
                int newRGB = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
                finalThresholdImage.setRGB(x, y, newRGB);
                if (imageType == BufferedImage.TYPE_INT_ARGB) {
                    setAlpha(finalThresholdImage, x, y, rgb);
                    finalThresholdImage = makeColorsTransparent(finalThresholdImage, imageConfiguration,
                            new Color(newRGB));
                }
            }
        } catch (Exception e) {
            e.getMessage();
        }
    }

    return finalThresholdImage;
}

From source file:io.selendroid.standalone.android.impl.AbstractDevice.java

public byte[] takeScreenshot() throws AndroidDeviceException {
    if (device == null) {
        throw new AndroidDeviceException("Device not accessible via ddmlib.");
    }//ww w . j  a  v a2  s . c  om
    RawImage rawImage;
    try {
        rawImage = device.getScreenshot();
    } catch (IOException ioe) {
        throw new AndroidDeviceException("Unable to get frame buffer: " + ioe.getMessage());
    } catch (TimeoutException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
        throw new AndroidDeviceException(e.getMessage());
    } catch (AdbCommandRejectedException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
        throw new AndroidDeviceException(e.getMessage());
    }

    // device/adb not available?
    if (rawImage == null)
        return null;

    BufferedImage image = new BufferedImage(rawImage.width, rawImage.height, BufferedImage.TYPE_3BYTE_BGR);

    int index = 0;
    int IndexInc = rawImage.bpp >> 3;
    for (int y = 0; y < rawImage.height; y++) {
        for (int x = 0; x < rawImage.width; x++) {
            image.setRGB(x, y, rawImage.getARGB(index));
            index += IndexInc;
        }
    }
    return toByteArray(image);
}

From source file:org.stanwood.nwn2.gui.view.UIIconView.java

@Override
public void paintUIObject(Graphics g) {
    int x = getX();
    int y = getY();
    try {//from  w  ww  .j  av a2s .  co m
        BufferedImage img = getIconManager().getIcon(icon.getImg());
        int width = getWidth();
        int height = getHeight();

        if (img.getHeight() != height && img.getWidth() != width) {
            Image newImg = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics ig = img.getGraphics();
            ig.drawImage(newImg, 0, 0, null);
        }

        if (icon.getColor() != null) {
            Color colour = getColor(icon.getColor());
            for (int w = 0; w < img.getWidth(); w++) {
                for (int h = 0; h < img.getHeight(); h++) {
                    Color rgb = ColorUtil.blend(new Color(img.getRGB(w, h)), colour);
                    img.setRGB(w, h, rgb.getRGB());
                }
            }
        }

        g.drawImage(img, x, y, null);
    } catch (Exception e) {
        log.error(e.getMessage());
        drawMissingIcon(x, y, getWidth(), getHeight(), g);
    }
}

From source file:jtrace.Scene.java

/**
 * Render scene.//from  w  ww  . jav a2  s  .co  m
 *
 * @param width Width of resulting image.
 * @param height Height of resulting image.
 * @param maxRecursionDepth
 *
 * @return BufferedImage containing rendering.
 */
public BufferedImage render(int width, int height, int maxRecursionDepth) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);

    this.maxRecursionDepth = maxRecursionDepth;

    Random random = new Random();

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {

            //System.out.format("x:%d y%d\n", x,y);

            Ray ray = camera.getRay(width, height, x, y);

            // Reset recursion depth:
            recursionDepth = 0;

            //debugThisRay = random.nextDouble()<debugFrac;

            // Trace ray through scene:
            Colour pixelColour = traceRay(ray);

            image.setRGB(x, y, pixelColour.getInt());

        }
    }

    return image;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage createColorMappedBufferedImage(float[] data, DefaultColorMap1D cMap, float low,
        float up, int w, int h, int startX, int stopX, int startY, int stopY, boolean vFlip, boolean hFlip) {
    int myW = stopX - startX + 1;
    int myH = stopY - startY + 1;
    BufferedImage out = new BufferedImage(myW, myH, BufferedImage.TYPE_INT_RGB);
    int[] cmapLUT = cMap.getRGBColorTable();
    int cmapLUTSize = cmapLUT.length - 1;
    float cs = (float) cmapLUTSize / (up - low);
    int c;// w  w  w. j  av  a  2  s . c  o m

    for (int j = 0; j < myH; j++) {
        for (int i = 0; i < myW; i++) {
            c = (int) ((data[(j + startY) * w + i + startX] - low) * cs);
            if (c < 0) {
                c = 0;
            }
            if (c > cmapLUTSize) {
                c = cmapLUTSize;
            }
            if (vFlip && hFlip) {
                out.setRGB(myW - 1 - i, myH - 1 - j, cmapLUT[c]);
            } else if (vFlip && !hFlip) {
                out.setRGB(i, myH - 1 - j, cmapLUT[c]);
            } else if (!vFlip && hFlip) {
                out.setRGB(myW - 1 - i, j, cmapLUT[c]);
            } else {
                out.setRGB(i, j, cmapLUT[c]);
            }
        }
    }
    return out;
}

From source file:com.mightypocket.ashot.AndroDemon.java

private ImageEx renderImage(RawImage screenshot, boolean landscape, boolean ccw) {
    BufferedImage image = gc.createCompatibleImage(screenshot.width, screenshot.height);

    int offset = p.getInt(PREF_SAVE_SKIP_OFFSET, 0);
    int size = screenshot.width * screenshot.height;

    if (lastRawImage == null || lastRawImage.length != size) {
        lastRawImage = new int[size];
    }/*from www  . jav a 2s.c  o m*/

    int index = 0;
    int indexInc = screenshot.bpp >> 3;

    boolean duplicate = true;

    int value, pos;

    if (!ccw && landscape) {
        index = (size - 1) * indexInc;
        indexInc = -indexInc;
    }

    for (int y = 0; y < screenshot.height; y++) {
        for (int x = 0; x < screenshot.width; x++, index += indexInc) {
            value = screenshot.getARGB(index);
            image.setRGB(x, y, value);
            pos = x + y * screenshot.width;

            if (duplicate && y >= offset)
                duplicate = duplicate && (lastRawImage[pos] == value);

            lastRawImage[pos] = value;
        }
    }

    ImageEx res = new ImageEx(image);
    res.setDuplicate(duplicate);

    return res;
}