Java BufferedImage Operation setBGRPixels(byte[] bgrPixels, BufferedImage img, int x, int y, int w, int h)

Here you can find the source of setBGRPixels(byte[] bgrPixels, BufferedImage img, int x, int y, int w, int h)

Description

converts and copies byte packed BGR or ABGR into the img buffer, the img type may vary (e.g.

License

BSD License

Declaration

public static void setBGRPixels(byte[] bgrPixels, BufferedImage img, int x, int y, int w, int h) 

Method Source Code

//package com.java2s;
/*/*from   www .j  ava  2 s  . c om*/
 * Copyright 2013, Morten Nobel-Joergensen
 *
 * License: The BSD 3-Clause License
 * http://opensource.org/licenses/BSD-3-Clause
 */

import java.awt.image.BufferedImage;

import java.awt.image.WritableRaster;

public class Main {
    /**
     * converts and copies byte packed  BGR or ABGR into the img buffer,
     * the img type may vary (e.g. RGB or BGR, int or byte packed)
     * but the number of components (w/o alpha, w alpha, gray) must match
     * <p/>
     * does not unmange the image for all (A)RGN and (A)BGR and gray imaged
     */
    public static void setBGRPixels(byte[] bgrPixels, BufferedImage img, int x, int y, int w, int h) {
        int imageType = img.getType();
        WritableRaster raster = img.getRaster();
        //int ttype= raster.getTransferType();
        if (imageType == BufferedImage.TYPE_3BYTE_BGR || imageType == BufferedImage.TYPE_4BYTE_ABGR
                || imageType == BufferedImage.TYPE_4BYTE_ABGR_PRE || imageType == BufferedImage.TYPE_BYTE_GRAY) {
            raster.setDataElements(x, y, w, h, bgrPixels);
        } else {
            int[] pixels;
            if (imageType == BufferedImage.TYPE_INT_BGR) {
                pixels = bytes2int(bgrPixels, 2, 1, 0); // bgr -->  bgr
            } else if (imageType == BufferedImage.TYPE_INT_ARGB || imageType == BufferedImage.TYPE_INT_ARGB_PRE) {
                pixels = bytes2int(bgrPixels, 3, 0, 1, 2); // abgr -->  argb
            } else {
                pixels = bytes2int(bgrPixels, 0, 1, 2); // bgr -->  rgb
            }
            if (w == 0 || h == 0) {
                return;
            } else if (pixels.length < w * h) {
                throw new IllegalArgumentException("pixels array must have a length" + " >= w*h");
            }
            if (imageType == BufferedImage.TYPE_INT_ARGB || imageType == BufferedImage.TYPE_INT_RGB
                    || imageType == BufferedImage.TYPE_INT_ARGB_PRE || imageType == BufferedImage.TYPE_INT_BGR) {
                raster.setDataElements(x, y, w, h, pixels);
            } else {
                // Unmanages the image
                img.setRGB(x, y, w, h, pixels, 0, w);
            }
        }
    }

    public static int[] bytes2int(byte[] in, int index1, int index2, int index3) {
        int[] out = new int[in.length / 3];
        for (int i = 0; i < out.length; i++) {
            int index = i * 3;
            int b1 = (in[index + index1] & 0xff) << 16;
            int b2 = (in[index + index2] & 0xff) << 8;
            int b3 = in[index + index3] & 0xff;
            out[i] = b1 | b2 | b3;
        }
        return out;
    }

    public static int[] bytes2int(byte[] in, int index1, int index2, int index3, int index4) {
        int[] out = new int[in.length / 4];
        for (int i = 0; i < out.length; i++) {
            int index = i * 4;
            int b1 = (in[index + index1] & 0xff) << 24;
            int b2 = (in[index + index2] & 0xff) << 16;
            int b3 = (in[index + index3] & 0xff) << 8;
            int b4 = in[index + index4] & 0xff;
            out[i] = b1 | b2 | b3 | b4;
        }
        return out;
    }
}

Related

  1. scanFill(BufferedImage sourceImage)
  2. scrapeColors(BufferedImage image)
  3. setArrayAlpha(BufferedImage image, int[] array)
  4. setArrayColor(BufferedImage image, int[][] array)
  5. setBackgroud(BufferedImage image, Color backgroundColor)
  6. setBrightnessFactor(BufferedImage img, float multiple, BufferedImage dest)
  7. setColor(BufferedImage image, int x, int y, int[] color, String colorModel)
  8. setCompressionType(ImageWriteParam param, BufferedImage image)
  9. setImageIntPixels(BufferedImage image, boolean allowDeoptimizingDirectRead, IntBuffer pixels)