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 void setBGRPixels ( byte [ ] bgrPixels , BufferedImage img , int x , int y , int w , int h ) { int imageType = img . getType ( ) ; WritableRaster raster = img . getRaster ( ) ; 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 ) ; } else if ( imageType == BufferedImage . TYPE_INT_ARGB || imageType == BufferedImage . TYPE_INT_ARGB_PRE ) { pixels = bytes2int ( bgrPixels , 3 , 0 , 1 , 2 ) ; } else { pixels = bytes2int ( bgrPixels , 0 , 1 , 2 ) ; } 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 { img . setRGB ( x , y , w , h , pixels , 0 , w ) ; } } }
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