Program: 33358
Project: 33358
Buggy source:
/** 
 * Retrieves RGB values from DataBuffer. This one is slower than getRGB()
 * @param image input BufferedImage 
 * @return integer array - default RGB color space representation
 */
public static int[] getRGB2(BufferedImage image){
  int type=image.getType();
  Raster raster=image.getRaster();
  DataBuffer dataBuffer=raster.getDataBuffer();
  int numOfBanks=dataBuffer.getNumBanks();
  int dataType=dataBuffer.getDataType();
  int imageWidth=image.getWidth();
  int imageHeight=image.getHeight();
  int imageSize=imageWidth * imageHeight;
  int[] rgbs=new int[imageSize];
switch (dataType) {
case DataBuffer.TYPE_INT:
    rgbs=((DataBufferInt)dataBuffer).getData(0);
  if (type == BufferedImage.TYPE_INT_ARGB_PRE) {
    for (int i=0; i < imageSize; i++) {
      float alpha=255.0f / ((rgbs[i] >> 24) & 0xff);
      byte red=(byte)(((rgbs[i] >> 16) & 0xff) * alpha);
      byte green=(byte)(((rgbs[i] >> 8) & 0xff) * alpha);
      byte blue=(byte)((rgbs[i] & 0xff) * alpha);
      rgbs[i]=(rgbs[i] & 0xff000000) | ((red & 0xff) << 16) | ((green & 0xff) << 8)| (blue & 0xff);
    }
  }
 else   if (type == BufferedImage.TYPE_INT_BGR) {
    for (int i=0; i < rgbs.length; i++) {
      int blue=(rgbs[i] >> 16) & 0xff;
      int green=(rgbs[i] >> 8) & 0xff;
      int red=rgbs[i] & 0xff;
      rgbs[i]=0xff000000 | (red << 16) | (green << 8)| blue;
    }
  }
 else   if (type == BufferedImage.TYPE_INT_RGB) {
    for (int i=0; i < rgbs.length; i++)     rgbs[i]=0xff000000 | rgbs[i];
  }
 else   if (type == BufferedImage.TYPE_INT_ARGB) {
    ;
  }
return rgbs;
case DataBuffer.TYPE_BYTE:
byte[][] bpixels=((DataBufferByte)dataBuffer).getBankData();
if (type == BufferedImage.TYPE_BYTE_INDEXED || type == BufferedImage.TYPE_BYTE_BINARY) {
IndexColorModel indexModel=(IndexColorModel)image.getColorModel();
int mapSize=indexModel.getMapSize();
byte[] reds=new byte[mapSize];
byte[] greens=new byte[mapSize];
byte[] blues=new byte[mapSize];
byte[] alphas=new byte[mapSize];
int[] palette=new int[mapSize];
indexModel.getReds(reds);
indexModel.getGreens(greens);
indexModel.getBlues(blues);
indexModel.getAlphas(alphas);
for (int i=0; i < mapSize; i++) palette[i]=(alphas[i] & 0xff) << 24 | (reds[i] & 0xff) << 16 | (greens[i] & 0xff) << 8 | blues[i] & 0xff;
int bitsPerPixel=raster.getSampleModel().getSampleSize(0);
int i=0, index=0, padding=0, safeEnd=0;
switch (bitsPerPixel) {
case 8:
for (; i < imageSize; i++) rgbs[i]=palette[bpixels[0][i] & 0xff];
break;
case 4:
padding=imageWidth % 2;
safeEnd=imageWidth - padding;
for (int j=0; j < imageHeight; j++) {
for (int k=0; k < safeEnd; k+=2, i++) {
rgbs[index++]=palette[(bpixels[0][i] >>> 4) & 0x0f];
rgbs[index++]=palette[bpixels[0][i] & 0x0f];
}
if (padding != 0) rgbs[index++]=palette[(bpixels[0][i] >>> 4) & 0x0f];
}
break;
case 2:
padding=imageWidth % 4;
safeEnd=imageWidth - padding;
for (int j=0; j < imageHeight; j++) {
for (int k=0; k < safeEnd; k+=4, i++) {
for (int l=6; l >= 0; l-=2, index++) rgbs[index]=palette[(bpixels[0][i] >>> l) & 0x03];
}
if (padding != 0) {
for (int m=0, n=6; m < padding; m++, n-=2, index++) rgbs[index]=palette[(bpixels[0][i] >>> n) & 0x03];
i++;
}
}
break;
case 1:
padding=imageWidth % 8;
safeEnd=imageWidth - padding;
int maxValue=8 - padding;
for (int j=0; j < imageHeight; j++) {
for (int k=0; k < safeEnd; k+=8, i++) {
for (int l=7; l >= 0; l--, index++) rgbs[index]=palette[(bpixels[0][i] >>> l) & 0x01];
}
if (padding != 0) {
for (int m=7; m >= maxValue; m--, index++) rgbs[index]=palette[(bpixels[0][i] >>> m) & 0x01];
i++;
}
}
break;
default :
System.out.println(bitsPerPixel + "String_Node_Str");
}
}
 else if (type == BufferedImage.TYPE_4BYTE_ABGR) {
if (numOfBanks == 1) {
for (int i=0, index=0; i < imageSize; i++) rgbs[i]=((bpixels[0][index++] & 0xff) << 24) | (bpixels[0][index++] & 0xff) | ((bpixels[0][index++] & 0xff) << 8)| ((bpixels[0][index++] & 0xff) << 16);
}
 else {
for (int i=0; i < imageSize; i++) rgbs[i]=((bpixels[0][i] & 0xff) << 24) | (bpixels[1][i] & 0xff) | ((bpixels[2][i] & 0xff) << 8)| ((bpixels[3][i] & 0xff) << 16);
}
}
 else if (type == BufferedImage.TYPE_3BYTE_BGR) {
if (numOfBanks == 1) {
for (int i=0, index=0; i < imageSize; i++) rgbs[i]=0xff000000 | (bpixels[0][index++] & 0xff) | ((bpixels[0][index++] & 0xff) << 8)| ((bpixels[0][index++] & 0xff) << 16);
}
 else {
for (int i=0; i < imageSize; i++) rgbs[i]=0xff000000 | (bpixels[0][i] & 0xff) | ((bpixels[1][i] & 0xff) << 8)| ((bpixels[2][i] & 0xff) << 16);
}
}
 else if (type == BufferedImage.TYPE_4BYTE_ABGR_PRE) {
if (numOfBanks == 1) {
for (int i=0, index=0; i < imageSize; i++, index+=4) {
float alpha=255.0f * (bpixels[0][index] & 0xff);
byte blue=(byte)((bpixels[0][index + 1] & 0xff) * alpha);
byte green=(byte)((bpixels[0][index + 2] & 0xff) * alpha);
byte red=(byte)((bpixels[0][index + 3] & 0xff) * alpha);
rgbs[i]=((bpixels[0][index] & 0xff) << 24) | ((red & 0xff) << 16) | ((green & 0xff) << 8)| (blue & 0xff);
}
}
 else {
for (int i=0; i < imageSize; i++) {
float alpha=255.0f * (bpixels[0][i] & 0xff);
byte blue=(byte)((bpixels[1][i] & 0xff) * alpha);
byte green=(byte)((bpixels[2][i] & 0xff) * alpha);
byte red=(byte)((bpixels[3][i] & 0xff) * alpha);
rgbs[i]=((bpixels[0][i] & 0xff) << 24) | ((red & 0xff) << 16) | ((green & 0xff) << 8)| (blue & 0xff);
}
}
}
 else if (type == BufferedImage.TYPE_BYTE_GRAY) {
for (int i=0; i < imageSize; i++) rgbs[i]=0xff000000 | ((bpixels[0][i] & 0xff) << 16) | ((bpixels[0][i] & 0xff) << 8)| (bpixels[0][i] & 0xff);
}
return rgbs;
case DataBuffer.TYPE_USHORT:
short[] spixels=((DataBufferUShort)dataBuffer).getBankData()[0];
if (type == BufferedImage.TYPE_USHORT_GRAY) {
for (int i=0; i < imageSize; i++) {
int gray=((spixels[i] >> 8) & 0xff);
rgbs[i]=0xff000000 | (gray << 16) | (gray << 8)| gray;
}
}
 else if (type == BufferedImage.TYPE_USHORT_565_RGB) {
for (int i=0; i < imageSize; i++) {
int red=((spixels[i] >> 11) & 0x1f);
int green=((spixels[i] >> 5) & 0x3f);
int blue=(spixels[i] & 0x1f);
rgbs[i]=0xff000000 | (red << 19) | (green << 10)| (blue << 3);
}
}
 else if (type == BufferedImage.TYPE_USHORT_555_RGB) {
for (int i=0; i < imageSize; i++) {
int red=((spixels[i] >>> 10) & 0x1f);
int green=((spixels[i] >>> 5) & 0x1f);
int blue=(spixels[i] & 0x1f);
rgbs[i]=0xff000000 | (red << 19) | (green << 11)| (blue << 3);
}
}
return rgbs;
default :
throw new UnsupportedOperationException("String_Node_Str" + type);
}
}

Fixed version (ground truth):
/** 
 * Retrieves RGB values from DataBuffer. This one is slower than getRGB()
 * @param image input BufferedImage 
 * @return integer array - default RGB color space representation
 */
public static int[] getRGB2(BufferedImage image){
  int type=image.getType();
  Raster raster=image.getRaster();
  DataBuffer dataBuffer=raster.getDataBuffer();
  int numOfBanks=dataBuffer.getNumBanks();
  int dataType=dataBuffer.getDataType();
  int imageWidth=image.getWidth();
  int imageHeight=image.getHeight();
  int imageSize=imageWidth * imageHeight;
  int[] rgbs=new int[imageSize];
switch (dataType) {
case DataBuffer.TYPE_INT:
    rgbs=((DataBufferInt)dataBuffer).getData(0);
  if (type == BufferedImage.TYPE_INT_ARGB_PRE) {
    for (int i=0; i < imageSize; i++) {
      float alpha=255.0f / ((rgbs[i] >> 24) & 0xff);
      byte red=(byte)(((rgbs[i] >> 16) & 0xff) * alpha);
      byte green=(byte)(((rgbs[i] >> 8) & 0xff) * alpha);
      byte blue=(byte)((rgbs[i] & 0xff) * alpha);
      rgbs[i]=(rgbs[i] & 0xff000000) | ((red & 0xff) << 16) | ((green & 0xff) << 8)| (blue & 0xff);
    }
  }
 else   if (type == BufferedImage.TYPE_INT_BGR) {
    for (int i=0; i < rgbs.length; i++) {
      int blue=(rgbs[i] >> 16) & 0xff;
      int green=(rgbs[i] >> 8) & 0xff;
      int red=rgbs[i] & 0xff;
      rgbs[i]=0xff000000 | (red << 16) | (green << 8)| blue;
    }
  }
 else   if (type == BufferedImage.TYPE_INT_RGB) {
    for (int i=0; i < rgbs.length; i++)     rgbs[i]=0xff000000 | rgbs[i];
  }
 else   if (type == BufferedImage.TYPE_INT_ARGB) {
    ;
  }
return rgbs;
case DataBuffer.TYPE_BYTE:
byte[][] bpixels=((DataBufferByte)dataBuffer).getBankData();
if (type == BufferedImage.TYPE_BYTE_INDEXED || type == BufferedImage.TYPE_BYTE_BINARY) {
IndexColorModel indexModel=(IndexColorModel)image.getColorModel();
int mapSize=indexModel.getMapSize();
byte[] reds=new byte[mapSize];
byte[] greens=new byte[mapSize];
byte[] blues=new byte[mapSize];
byte[] alphas=new byte[mapSize];
int[] palette=new int[mapSize];
indexModel.getReds(reds);
indexModel.getGreens(greens);
indexModel.getBlues(blues);
indexModel.getAlphas(alphas);
for (int i=0; i < mapSize; i++) palette[i]=(alphas[i] & 0xff) << 24 | (reds[i] & 0xff) << 16 | (greens[i] & 0xff) << 8 | blues[i] & 0xff;
int bitsPerPixel=raster.getSampleModel().getSampleSize(0);
int i=0, index=0, padding=0, safeEnd=0;
switch (bitsPerPixel) {
case 8:
for (; i < imageSize; i++) rgbs[i]=palette[bpixels[0][i] & 0xff];
break;
case 4:
padding=imageWidth % 2;
safeEnd=imageWidth - padding;
for (int j=0; j < imageHeight; j++) {
for (int k=0; k < safeEnd; k+=2, i++) {
rgbs[index++]=palette[(bpixels[0][i] >>> 4) & 0x0f];
rgbs[index++]=palette[bpixels[0][i] & 0x0f];
}
if (padding != 0) rgbs[index++]=palette[(bpixels[0][i] >>> 4) & 0x0f];
}
break;
case 2:
padding=imageWidth % 4;
safeEnd=imageWidth - padding;
for (int j=0; j < imageHeight; j++) {
for (int k=0; k < safeEnd; k+=4, i++) {
for (int l=6; l >= 0; l-=2, index++) rgbs[index]=palette[(bpixels[0][i] >>> l) & 0x03];
}
if (padding != 0) {
for (int m=0, n=6; m < padding; m++, n-=2, index++) rgbs[index]=palette[(bpixels[0][i] >>> n) & 0x03];
i++;
}
}
break;
case 1:
padding=imageWidth % 8;
safeEnd=imageWidth - padding;
int maxValue=8 - padding;
for (int j=0; j < imageHeight; j++) {
for (int k=0; k < safeEnd; k+=8, i++) {
for (int l=7; l >= 0; l--, index++) rgbs[index]=palette[(bpixels[0][i] >>> l) & 0x01];
}
if (padding != 0) {
for (int m=7; m >= maxValue; m--, index++) rgbs[index]=palette[(bpixels[0][i] >>> m) & 0x01];
i++;
}
}
break;
default :
System.out.println(bitsPerPixel + "String_Node_Str");
}
}
 else if (type == BufferedImage.TYPE_4BYTE_ABGR) {
if (numOfBanks == 1) {
for (int i=0, index=0; i < imageSize; i++) rgbs[i]=((bpixels[0][index++] & 0xff) << 24) | (bpixels[0][index++] & 0xff) | ((bpixels[0][index++] & 0xff) << 8)| ((bpixels[0][index++] & 0xff) << 16);
}
 else {
for (int i=0; i < imageSize; i++) rgbs[i]=((bpixels[0][i] & 0xff) << 24) | (bpixels[1][i] & 0xff) | ((bpixels[2][i] & 0xff) << 8)| ((bpixels[3][i] & 0xff) << 16);
}
}
 else if (type == BufferedImage.TYPE_3BYTE_BGR) {
if (numOfBanks == 1) {
for (int i=0, index=0; i < imageSize; i++) rgbs[i]=0xff000000 | (bpixels[0][index++] & 0xff) | ((bpixels[0][index++] & 0xff) << 8)| ((bpixels[0][index++] & 0xff) << 16);
}
 else {
for (int i=0; i < imageSize; i++) rgbs[i]=0xff000000 | (bpixels[0][i] & 0xff) | ((bpixels[1][i] & 0xff) << 8)| ((bpixels[2][i] & 0xff) << 16);
}
}
 else if (type == BufferedImage.TYPE_4BYTE_ABGR_PRE) {
if (numOfBanks == 1) {
for (int i=0, index=0; i < imageSize; i++, index+=4) {
float alpha=255.0f * (bpixels[0][index] & 0xff);
byte blue=(byte)((bpixels[0][index + 1] & 0xff) * alpha);
byte green=(byte)((bpixels[0][index + 2] & 0xff) * alpha);
byte red=(byte)((bpixels[0][index + 3] & 0xff) * alpha);
rgbs[i]=((bpixels[0][index] & 0xff) << 24) | ((red & 0xff) << 16) | ((green & 0xff) << 8)| (blue & 0xff);
}
}
 else {
for (int i=0; i < imageSize; i++) {
float alpha=255.0f * (bpixels[0][i] & 0xff);
byte blue=(byte)((bpixels[1][i] & 0xff) * alpha);
byte green=(byte)((bpixels[2][i] & 0xff) * alpha);
byte red=(byte)((bpixels[3][i] & 0xff) * alpha);
rgbs[i]=((bpixels[0][i] & 0xff) << 24) | ((red & 0xff) << 16) | ((green & 0xff) << 8)| (blue & 0xff);
}
}
}
 else if (type == BufferedImage.TYPE_BYTE_GRAY) {
for (int i=0; i < imageSize; i++) rgbs[i]=0xff000000 | ((bpixels[0][i] & 0xff) << 16) | ((bpixels[0][i] & 0xff) << 8)| (bpixels[0][i] & 0xff);
}
return rgbs;
case DataBuffer.TYPE_USHORT:
short[] spixels=((DataBufferUShort)dataBuffer).getBankData()[0];
if (type == BufferedImage.TYPE_USHORT_GRAY) {
for (int i=0; i < imageSize; i++) {
int gray=((spixels[i] >> 8) & 0xff);
rgbs[i]=0xff000000 | (gray << 16) | (gray << 8)| gray;
}
}
 else if (type == BufferedImage.TYPE_USHORT_565_RGB) {
for (int i=0; i < imageSize; i++) {
int red=((spixels[i] >> 11) & 0x1f);
int green=((spixels[i] >> 5) & 0x3f);
int blue=(spixels[i] & 0x1f);
rgbs[i]=0xff000000 | (red << 19) | (green << 10)| (blue << 3);
}
}
 else if (type == BufferedImage.TYPE_USHORT_555_RGB) {
for (int i=0; i < imageSize; i++) {
int red=((spixels[i] >>> 10) & 0x1f);
int green=((spixels[i] >>> 5) & 0x1f);
int blue=(spixels[i] & 0x1f);
rgbs[i]=0xff000000 | (red << 19) | (green << 11)| (blue << 3);
}
}
return rgbs;
default :
return image.getRGB(0,0,imageWidth,imageHeight,rgbs,0,imageWidth);
}
}