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 startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) 

Source Link

Document

Sets an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB color space, into a portion of the image data.

Usage

From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java

public static BufferedImage resizeNinePatchImage(Project project, ImageInformation information)
        throws IOException {
    BufferedImage image = ImageIO.read(information.getImageFile());
    int originalWidth = image.getWidth();
    int originalHeight = image.getHeight();
    if (originalWidth - 2 == information.getTargetWidth() && originalHeight - 2 == information.getTargetHeight()
            && MathUtils.floatEquals(information.getFactor(), 1f)) {
        return image;
    }/*from  w  ww. j  ava2  s  .c o  m*/

    int newWidth = information.getTargetWidth();
    int newHeight = information.getTargetHeight();
    if (newWidth <= 0 || newHeight <= 0) {
        newWidth = originalWidth;
        newHeight = originalHeight;
    }

    if (information.getFactor() >= 0) {
        newWidth = (int) (newWidth * information.getFactor());
        newHeight = (int) (newHeight * information.getFactor());
    }

    BufferedImage trimmedImage = trim9PBorder(image);
    ImageInformation trimmedImageInformation = ImageInformation.newBuilder(information)
            .setExportName(getExportName("trimmed", information.getExportName())).build(project);
    saveImageTempFile(trimmedImage, trimmedImageInformation);
    trimmedImage = resizeNormalImage(trimmedImage, newWidth, newHeight, trimmedImageInformation);
    saveImageTempFile(trimmedImage, ImageInformation.newBuilder(trimmedImageInformation)
            .setExportName(getExportName("trimmedResized", information.getExportName())).build(project));

    BufferedImage borderImage;

    int w = trimmedImage.getWidth();
    int h = trimmedImage.getHeight();

    try {
        borderImage = generateBordersImage(image, w, h);
    } catch (Exception e) {
        return null;
    }

    int[] rgbArray = new int[w * h];
    trimmedImage.getRGB(0, 0, w, h, rgbArray, 0, w);
    borderImage.setRGB(1, 1, w, h, rgbArray, 0, w);

    return borderImage;
}

From source file:org.uva.itast.blended.omr.OMRUtils.java

private static void writeResultImage(int stride, int height, int[] pixels, URI uri, String inputName,
        String suffix) {/*from w ww .j  a  va2s .c  o  m*/
    // Write the result
    BufferedImage result = new BufferedImage(stride, height, BufferedImage.TYPE_INT_ARGB);
    result.setRGB(0, 0, stride, height, pixels, 0, stride);

    // Use the current working directory for URLs
    String resultName = inputName;
    if ("http".equals(uri.getScheme())) {
        int pos = resultName.lastIndexOf('/');
        if (pos > 0) {
            resultName = '.' + resultName.substring(pos);
        }
    }
    int pos = resultName.lastIndexOf('.');
    if (pos > 0) {
        resultName = resultName.substring(0, pos);
    }
    resultName += suffix;
    OutputStream outStream = null;
    try {
        outStream = new FileOutputStream(resultName);
        ImageIO.write(result, "png", outStream);
    } catch (FileNotFoundException e) {
        System.err.println("Could not create " + resultName);
    } catch (IOException e) {
        System.err.println("Could not write to " + resultName);
    } finally {
        try {
            if (outStream != null) {
                outStream.close();
            }
        } catch (IOException ioe) {
            // continue
        }
    }
}

From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java

private static void enforceBorderColors(BufferedImage inputImage) {
    Graphics2D g = inputImage.createGraphics();
    g.setBackground(new Color(0, 0, 0, 0));
    g.clearRect(1, 1, inputImage.getWidth() - 2, inputImage.getHeight() - 2);
    g.dispose();//from   www  .  j  a  v  a 2 s  .  c o m
    int w = inputImage.getWidth();
    int h = inputImage.getHeight();
    int[] rgb = new int[w * h];

    inputImage.getRGB(0, 0, w, h, rgb, 0, w);

    for (int i = 0; i < rgb.length; i++) {
        if ((0xff000000 & rgb[i]) != 0) {
            rgb[i] = 0xff000000;
        }
    }
    inputImage.setRGB(0, 0, w, h, rgb, 0, w);
}

From source file:ColorPan.java

public void paint(Graphics g) {
    int width = getSize().width;
    int height = getSize().height;
    int[] data = new int[width * height];
    int i = 0;// w  w  w .j  a  v  a 2s  .c  o  m
    for (int y = 0; y < height; y++) {
        int red = (y * 255) / (height - 1);
        for (int x = 0; x < width; x++) {
            int green = (x * 255) / (width - 1);
            int blue = 128;
            data[i++] = (red << 16) | (green << 8) | blue;
        }
    }
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    image.setRGB(0, 0, width, height, data, 0, width);
    g.drawImage(image, 0, 0, this);
}

From source file:org.jcodec.codecs.mjpeg.MJPEGParser.java

BufferedImage buffered(DecodedImage decoded) {
    int w = decoded.getWidth();
    int h = decoded.getHeight();
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
    bi.setRGB(0, 0, w, h, decoded.getPixels(), 0, w);
    return bi;//from   www.  ja  v  a 2s .c om

}

From source file:TextureByReference.java

public static BufferedImage convertImage(BufferedImage bImage, int type) {
    int width = bImage.getWidth();
    int height = bImage.getHeight();
    BufferedImage newImage = new BufferedImage(width, height, type);
    int[] rgbArray = new int[width * height];
    bImage.getRGB(0, 0, width, height, rgbArray, 0, width);
    newImage.setRGB(0, 0, width, height, rgbArray, 0, width);
    return newImage;
}

From source file:io.warp10.script.processing.Pencode.java

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
    Object top = stack.pop();/*  w w w.jav a  2 s.c  o m*/

    if (!(top instanceof processing.core.PGraphics)) {
        throw new WarpScriptException(getName() + " operates on a PGraphics instance.");
    }

    processing.core.PGraphics pg = (processing.core.PGraphics) top;

    pg.endDraw();

    BufferedImage bimage = new BufferedImage(pg.pixelWidth, pg.pixelHeight, BufferedImage.TYPE_INT_ARGB);
    bimage.setRGB(0, 0, pg.pixelWidth, pg.pixelHeight, pg.pixels, 0, pg.pixelWidth);
    Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("png");
    ImageWriter writer = null;
    if (iter.hasNext()) {
        writer = iter.next();
    }
    ImageWriteParam param = writer.getDefaultWriteParam();
    IIOMetadata metadata = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream output = new BufferedOutputStream(baos);

    try {
        writer.setOutput(ImageIO.createImageOutputStream(output));
        writer.write(metadata, new IIOImage(bimage, null, metadata), param);
    } catch (IOException ioe) {
        throw new WarpScriptException(getName() + " error while encoding PGraphics.", ioe);
    }

    writer.dispose();

    StringBuilder sb = new StringBuilder("data:image/png;base64,");
    sb.append(Base64.encodeBase64String(baos.toByteArray()));

    stack.push(sb.toString());

    //
    // Re-issue a 'beginDraw' so we can continue using the PGraphics instance
    //

    pg.beginDraw();

    return stack;
}

From source file:itemrender.client.rendering.FBOHelper.java

public void saveToFile(File file) {
    // Bind framebuffer texture
    GlStateManager.bindTexture(textureID);

    GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    int width = GL11.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH);
    int height = GL11.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 0, GL11.GL_TEXTURE_HEIGHT);

    IntBuffer texture = BufferUtils.createIntBuffer(width * height);
    GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, texture);

    int[] texture_array = new int[width * height];
    texture.get(texture_array);//  ww  w. j a va2  s.  c o m

    BufferedImage image = new BufferedImage(renderTextureSize, renderTextureSize, BufferedImage.TYPE_INT_ARGB);
    image.setRGB(0, 0, renderTextureSize, renderTextureSize, texture_array, 0, width);

    file.mkdirs();
    try {
        ImageIO.write(image, "png", file);
    } catch (Exception e) {
        // Do nothing
    }
}

From source file:itemrender.client.rendering.FBOHelper.java

public String getBase64() {
    // Bind framebuffer texture
    GlStateManager.bindTexture(textureID);

    GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    int width = GL11.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH);
    int height = GL11.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 0, GL11.GL_TEXTURE_HEIGHT);

    IntBuffer texture = BufferUtils.createIntBuffer(width * height);
    GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, texture);

    int[] texture_array = new int[width * height];
    texture.get(texture_array);//from   w  w  w. ja  v  a2 s.  c om

    BufferedImage image = new BufferedImage(renderTextureSize, renderTextureSize, BufferedImage.TYPE_INT_ARGB);
    image.setRGB(0, 0, renderTextureSize, renderTextureSize, texture_array, 0, width);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        ImageIO.write(image, "PNG", out);
    } catch (IOException e) {
        // Do nothing
    }

    return Base64.encodeBase64String(out.toByteArray());
}

From source file:org.apache.flex.compiler.internal.embedding.transcoders.JPEGTranscoder.java

private byte[] bufferedImageToJPEG(ImageInfo imageInfo, int[] pixels) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(imageInfo.width, imageInfo.height,
            BufferedImage.TYPE_INT_ARGB);
    bufferedImage.setRGB(0, 0, imageInfo.width, imageInfo.height, pixels, 0, imageInfo.width);

    ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
    ImageWriteParam writeParam = writer.getDefaultWriteParam();
    ColorModel colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
    ImageTypeSpecifier imageTypeSpecifier = new ImageTypeSpecifier(colorModel,
            colorModel.createCompatibleSampleModel(1, 1));
    writeParam.setDestinationType(imageTypeSpecifier);
    writeParam.setSourceBands(new int[] { 0, 1, 2 });
    writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

    float q = 1.0f;
    if (quality != null)
        q = quality.floatValue();/*from   w w  w .j  a va  2 s. c  o m*/
    writeParam.setCompressionQuality(q);

    DAByteArrayOutputStream buffer = new DAByteArrayOutputStream();
    writer.setOutput(new MemoryCacheImageOutputStream(buffer));

    IIOImage ioImage = new IIOImage(bufferedImage, null, null);

    writer.write(null, ioImage, writeParam);
    writer.dispose();

    return buffer.getDirectByteArray();
}