src/main/java/com/xceptance/neodymium/visual/ai/util/Helper.java
/***
     * Convert a BufferedImage to a 2D double array. This method did not use the getRBG method it calculate the values
     * native with bit shifting of the Integer values of the pixel. The alpha channel is also in consideration and get
     * get special treatment.
     * 
     * @param image
     *            BufferedImage
     * @return result double[][] matrix of the image
     */
Convert a BufferedImage to a 2D double array. This method did not use the getRBG method it calculate the values
native with bit shifting of the Integer values of the pixel. The alpha channel is also in consideration and get
get special treatment.

@param image
BufferedImage
@return result double[][] matrix of the image
Convert a BufferedImage to a 2D double array . This method did not use the getRBG method it calculate the values native with bit shifting of the Integer values of the pixel . The alpha channel is also in consideration and get get special treatment . @param image BufferedImage @return result double [] [] matrix of the image
public static double[][] convertTo2DWithoutUsingGetRGB(BufferedImage image)
    {
        final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        final int width = image.getWidth();
        final int height = image.getHeight();
        final boolean hasAlphaChannel = image.getAlphaRaster() != null;
        double[][] result = new double[height][width];
        if (hasAlphaChannel)
        {
            final int pixelLength = 4;
            for (int pixel = 0, row = 0, col = 0; pixel < pixels.length / 2; pixel += pixelLength)
            {
                int argb = 0;
                argb += (((int) pixels[pixel] & 0xff) << 24);
                argb += ((int) pixels[pixel + 1] & 0xff);
                argb += (((int) pixels[pixel + 2] & 0xff) << 8);
                argb += (((int) pixels[pixel + 3] & 0xff) << 16);
                result[row][col] = argb;
                col++;
                if (col == width)
                {
                    col = 0;
                    row++;
                }
            }
        }
        else
        {
            final int pixelLength = 3;
            for (int pixel = 0, row = 0, col = 0; pixel < pixels.length / 2; pixel += pixelLength)
            {
                int argb = 0;
                argb += -16777216;
                argb += ((int) pixels[pixel] & 0xff);
                argb += (((int) pixels[pixel + 1] & 0xff) << 8);
                argb += (((int) pixels[pixel + 2] & 0xff) << 16);
                result[row][col] = argb;
                col++;
                if (col == width)
                {
                    col = 0;
                    row++;
                }
            }
        }
        return result;
    }
public static double [ ] [ ] convertTo2DWithoutUsingGetRGB ( BufferedImage image ) { final byte [ ] pixels = ( ( DataBufferByte ) image . getRaster ( ) . getDataBuffer ( ) ) . getData ( ) ; final int width = image . getWidth ( ) ; final int height = image . getHeight ( ) ; final boolean hasAlphaChannel = image . getAlphaRaster ( ) != null ; double [ ] [ ] result = new double [ height ] [ width ] ; if ( hasAlphaChannel ) { final int pixelLength = 4 ; for ( int pixel = 0 , row = 0 , col = 0 ; pixel < pixels . length / 2 ; pixel += pixelLength ) { int argb = 0 ; argb += ( ( ( int ) pixels [ pixel ] & 0xff ) << 24 ) ; argb += ( ( int ) pixels [ pixel + 1 ] & 0xff ) ; argb += ( ( ( int ) pixels [ pixel + 2 ] & 0xff ) << 8 ) ; argb += ( ( ( int ) pixels [ pixel + 3 ] & 0xff ) << 16 ) ; result [ row ] [ col ] = argb ; col ++ ; if ( col == width ) { col = 0 ; row ++ ; } } } else { final int pixelLength = 3 ; for ( int pixel = 0 , row = 0 , col = 0 ; pixel < pixels . length / 2 ; pixel += pixelLength ) { int argb = 0 ; argb += - 16777216 ; argb += ( ( int ) pixels [ pixel ] & 0xff ) ; argb += ( ( ( int ) pixels [ pixel + 1 ] & 0xff ) << 8 ) ; argb += ( ( ( int ) pixels [ pixel + 2 ] & 0xff ) << 16 ) ; result [ row ] [ col ] = argb ; col ++ ; if ( col == width ) { col = 0 ; row ++ ; } } } return result ; }
Convert a BufferedImage to a 2D double array.
Convert a BufferedImage to a 2D double array .
// alpha // blue // green // red // 255 alpha // blue // green // red