code: static byte[] getPixels(BufferedImage img, int x, int y, int w, int h, byte[] pixels) {
          if (w == 0 || h == 0) {
              return new byte[0];
          }
  
          if (pixels == null) {
              pixels = new byte[w * h];
          } else if (pixels.length < w * h) {
              throw new IllegalArgumentException("pixels array must have a length >= w*h");
          }
  
          int imageType = img.getType();
  
          if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
              Raster raster = img.getRaster();
  
              return (byte[]) raster.getDataElements(x, y, w, h, pixels);
          } else {
              throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
          }
      }
docs: <p>Returns an array of pixels, stored as integers, from a <code>
  BufferedImage</code>. The pixels are grabbed from a rectangular area
  defined by a location and two dimensions. Calling this method on an image
  of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and
  <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
  
  @param  img    the source image
  @param  x      the x location at which to start grabbing pixels
  @param  y      the y location at which to start grabbing pixels
  @param  w      the width of the rectangle of pixels to grab
  @param  h      the height of the rectangle of pixels to grab
  @param  pixels a pre-allocated array of pixels of size w*h; can be null
  
  @return <code>pixels</code> if non-null, a new array of integers
  otherwise