Program: 153990
Project: 153990
Buggy source:
/** 
 * Constructor for BMPImage
 * @param stream
 */
public BMPImage(InputStream stream){
  if (stream instanceof BufferedInputStream) {
    inputStream=(BufferedInputStream)stream;
  }
 else {
    inputStream=new BufferedInputStream(stream);
  }
  try {
    if (!(readUnsignedByte(inputStream) == 'B' && readUnsignedByte(inputStream) == 'M')) {
      throw new RuntimeException(JaiI18N.getString("String_Node_Str"));
    }
    bitmapFileSize=readDWord(inputStream);
    readWord(inputStream);
    readWord(inputStream);
    bitmapOffset=readDWord(inputStream);
    long size=readDWord(inputStream);
    if (size == 12) {
      width=readWord(inputStream);
      height=readWord(inputStream);
    }
 else {
      width=readLong(inputStream);
      height=readLong(inputStream);
    }
    int planes=readWord(inputStream);
    bitsPerPixel=readWord(inputStream);
    properties.put("String_Node_Str",new Integer(planes));
    properties.put("String_Node_Str",new Integer(bitsPerPixel));
    numBands=3;
    if (size == 12) {
      properties.put("String_Node_Str","String_Node_Str");
      if (bitsPerPixel == 1) {
        imageType=VERSION_2_1_BIT;
      }
 else       if (bitsPerPixel == 4) {
        imageType=VERSION_2_4_BIT;
      }
 else       if (bitsPerPixel == 8) {
        imageType=VERSION_2_8_BIT;
      }
 else       if (bitsPerPixel == 24) {
        imageType=VERSION_2_24_BIT;
      }
      int numberOfEntries=(int)((bitmapOffset - 14 - size) / 3);
      int sizeOfPalette=numberOfEntries * 3;
      palette=new byte[sizeOfPalette];
      inputStream.read(palette,0,sizeOfPalette);
      properties.put("String_Node_Str",palette);
    }
 else {
      compression=readDWord(inputStream);
      imageSize=readDWord(inputStream);
      long xPelsPerMeter=readLong(inputStream);
      long yPelsPerMeter=readLong(inputStream);
      long colorsUsed=readDWord(inputStream);
      long colorsImportant=readDWord(inputStream);
switch ((int)compression) {
case BI_RGB:
        properties.put("String_Node_Str","String_Node_Str");
      break;
case BI_RLE8:
    properties.put("String_Node_Str","String_Node_Str");
  break;
case BI_RLE4:
properties.put("String_Node_Str","String_Node_Str");
break;
case BI_BITFIELDS:
properties.put("String_Node_Str","String_Node_Str");
break;
}
properties.put("String_Node_Str",new Long(xPelsPerMeter));
properties.put("String_Node_Str",new Long(yPelsPerMeter));
properties.put("String_Node_Str",new Long(colorsUsed));
properties.put("String_Node_Str",new Long(colorsImportant));
if (size == 40) {
switch ((int)compression) {
case BI_RGB:
case BI_RLE8:
case BI_RLE4:
int numberOfEntries=(int)((bitmapOffset - 14 - size) / 4);
int sizeOfPalette=numberOfEntries * 4;
palette=new byte[sizeOfPalette];
inputStream.read(palette,0,sizeOfPalette);
properties.put("String_Node_Str",palette);
if (bitsPerPixel == 1) {
imageType=VERSION_3_1_BIT;
}
 else if (bitsPerPixel == 4) {
imageType=VERSION_3_4_BIT;
}
 else if (bitsPerPixel == 8) {
imageType=VERSION_3_8_BIT;
}
 else if (bitsPerPixel == 24) {
imageType=VERSION_3_24_BIT;
}
 else if (bitsPerPixel == 16) {
imageType=VERSION_3_NT_16_BIT;
redMask=0x7C00;
greenMask=0x3E0;
blueMask=0x1F;
properties.put("String_Node_Str",new Integer(redMask));
properties.put("String_Node_Str",new Integer(greenMask));
properties.put("String_Node_Str",new Integer(blueMask));
}
 else if (bitsPerPixel == 32) {
imageType=VERSION_3_NT_32_BIT;
redMask=0x00FF0000;
greenMask=0x0000FF00;
blueMask=0x000000FF;
properties.put("String_Node_Str",new Integer(redMask));
properties.put("String_Node_Str",new Integer(greenMask));
properties.put("String_Node_Str",new Integer(blueMask));
}
properties.put("String_Node_Str","String_Node_Str");
break;
case BI_BITFIELDS:
if (bitsPerPixel == 16) {
imageType=VERSION_3_NT_16_BIT;
}
 else if (bitsPerPixel == 32) {
imageType=VERSION_3_NT_32_BIT;
}
redMask=(int)readDWord(inputStream);
greenMask=(int)readDWord(inputStream);
blueMask=(int)readDWord(inputStream);
properties.put("String_Node_Str",new Integer(redMask));
properties.put("String_Node_Str",new Integer(greenMask));
properties.put("String_Node_Str",new Integer(blueMask));
if (colorsUsed != 0) {
sizeOfPalette=(int)colorsUsed * 4;
palette=new byte[sizeOfPalette];
inputStream.read(palette,0,sizeOfPalette);
properties.put("String_Node_Str",palette);
}
properties.put("String_Node_Str","String_Node_Str");
break;
default :
throw new RuntimeException(JaiI18N.getString("String_Node_Str"));
}
}
 else if (size == 108) {
properties.put("String_Node_Str","String_Node_Str");
redMask=(int)readDWord(inputStream);
greenMask=(int)readDWord(inputStream);
blueMask=(int)readDWord(inputStream);
alphaMask=(int)readDWord(inputStream);
long csType=readDWord(inputStream);
int redX=readLong(inputStream);
int redY=readLong(inputStream);
int redZ=readLong(inputStream);
int greenX=readLong(inputStream);
int greenY=readLong(inputStream);
int greenZ=readLong(inputStream);
int blueX=readLong(inputStream);
int blueY=readLong(inputStream);
int blueZ=readLong(inputStream);
long gammaRed=readDWord(inputStream);
long gammaGreen=readDWord(inputStream);
long gammaBlue=readDWord(inputStream);
int numberOfEntries=(int)((bitmapOffset - 14 - size) / 4);
int sizeOfPalette=numberOfEntries * 4;
palette=new byte[sizeOfPalette];
inputStream.read(palette,0,sizeOfPalette);
if (palette != null || palette.length != 0) {
properties.put("String_Node_Str",palette);
}
switch ((int)csType) {
case LCS_CALIBRATED_RGB:
properties.put("String_Node_Str","String_Node_Str");
properties.put("String_Node_Str",new Integer(redX));
properties.put("String_Node_Str",new Integer(redY));
properties.put("String_Node_Str",new Integer(redZ));
properties.put("String_Node_Str",new Integer(greenX));
properties.put("String_Node_Str",new Integer(greenY));
properties.put("String_Node_Str",new Integer(greenZ));
properties.put("String_Node_Str",new Integer(blueX));
properties.put("String_Node_Str",new Integer(blueY));
properties.put("String_Node_Str",new Integer(blueZ));
properties.put("String_Node_Str",new Long(gammaRed));
properties.put("String_Node_Str",new Long(gammaGreen));
properties.put("String_Node_Str",new Long(gammaBlue));
throw new RuntimeException(JaiI18N.getString("String_Node_Str"));
case LCS_sRGB:
properties.put("String_Node_Str","String_Node_Str");
break;
case LCS_CMYK:
properties.put("String_Node_Str","String_Node_Str");
throw new RuntimeException(JaiI18N.getString("String_Node_Str"));
}
if (bitsPerPixel == 1) {
imageType=VERSION_4_1_BIT;
}
 else if (bitsPerPixel == 4) {
imageType=VERSION_4_4_BIT;
}
 else if (bitsPerPixel == 8) {
imageType=VERSION_4_8_BIT;
}
 else if (bitsPerPixel == 16) {
imageType=VERSION_4_16_BIT;
if ((int)compression == BI_RGB) {
redMask=0x7C00;
greenMask=0x3E0;
blueMask=0x1F;
}
}
 else if (bitsPerPixel == 24) {
imageType=VERSION_4_24_BIT;
}
 else if (bitsPerPixel == 32) {
imageType=VERSION_4_32_BIT;
if ((int)compression == BI_RGB) {
redMask=0x00FF0000;
greenMask=0x0000FF00;
blueMask=0x000000FF;
}
}
properties.put("String_Node_Str",new Integer(redMask));
properties.put("String_Node_Str",new Integer(greenMask));
properties.put("String_Node_Str",new Integer(blueMask));
properties.put("String_Node_Str",new Integer(alphaMask));
}
 else {
properties.put("String_Node_Str","String_Node_Str");
throw new RuntimeException(JaiI18N.getString("String_Node_Str"));
}
}
}
 catch (IOException ioe) {
String message=JaiI18N.getString("String_Node_Str");
ImagingListenerProxy.errorOccurred(message,new ImagingException(message,ioe),this,false);
}
if (height > 0) {
isBottomUp=true;
}
 else {
isBottomUp=false;
height=Math.abs(height);
}
tileWidth=width;
tileHeight=height;
if (bitsPerPixel == 1 || bitsPerPixel == 4 || bitsPerPixel == 8) {
numBands=1;
if (bitsPerPixel == 8) {
sampleModel=RasterFactory.createPixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,width,height,numBands);
}
 else {
sampleModel=new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,width,height,bitsPerPixel);
}
byte r[], g[], b[];
int size;
if (imageType == VERSION_2_1_BIT || imageType == VERSION_2_4_BIT || imageType == VERSION_2_8_BIT) {
size=palette.length / 3;
if (size > 256) {
size=256;
}
int off;
r=new byte[size];
g=new byte[size];
b=new byte[size];
for (int i=0; i < size; i++) {
off=3 * i;
b[i]=palette[off];
g[i]=palette[off + 1];
r[i]=palette[off + 2];
}
}
 else {
size=palette.length / 4;
if (size > 256) {
size=256;
}
int off;
r=new byte[size];
g=new byte[size];
b=new byte[size];
for (int i=0; i < size; i++) {
off=4 * i;
b[i]=palette[off];
g[i]=palette[off + 1];
r[i]=palette[off + 2];
}
}
if (ImageCodec.isIndicesForGrayscale(r,g,b)) colorModel=ImageCodec.createComponentColorModel(sampleModel);
 else colorModel=new IndexColorModel(bitsPerPixel,size,r,g,b);
}
 else if (bitsPerPixel == 16) {
numBands=3;
sampleModel=new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT,width,height,new int[]{redMask,greenMask,blueMask});
colorModel=new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),16,redMask,greenMask,blueMask,0,false,DataBuffer.TYPE_USHORT);
}
 else if (bitsPerPixel == 32) {
numBands=alphaMask == 0 ? 3 : 4;
int[] bitMasks=numBands == 3 ? new int[]{redMask,greenMask,blueMask} : new int[]{redMask,greenMask,blueMask,alphaMask};
sampleModel=new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT,width,height,bitMasks);
colorModel=new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),32,redMask,greenMask,blueMask,alphaMask,false,DataBuffer.TYPE_INT);
}
 else {
numBands=3;
sampleModel=RasterFactory.createPixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,width,height,numBands);
colorModel=ImageCodec.createComponentColorModel(sampleModel);
}
}

Fixed version (ground truth):
/** 
 * Constructor for BMPImage
 * @param stream
 */
public BMPImage(InputStream stream){
  if (stream instanceof BufferedInputStream) {
    inputStream=(BufferedInputStream)stream;
  }
 else {
    inputStream=new BufferedInputStream(stream);
  }
  try {
    inputStream.mark(Integer.MAX_VALUE);
    if (!(readUnsignedByte(inputStream) == 'B' && readUnsignedByte(inputStream) == 'M')) {
      throw new RuntimeException(JaiI18N.getString("String_Node_Str"));
    }
    bitmapFileSize=readDWord(inputStream);
    readWord(inputStream);
    readWord(inputStream);
    bitmapOffset=readDWord(inputStream);
    long size=readDWord(inputStream);
    if (size == 12) {
      width=readWord(inputStream);
      height=readWord(inputStream);
    }
 else {
      width=readLong(inputStream);
      height=readLong(inputStream);
    }
    int planes=readWord(inputStream);
    bitsPerPixel=readWord(inputStream);
    properties.put("String_Node_Str",new Integer(planes));
    properties.put("String_Node_Str",new Integer(bitsPerPixel));
    numBands=3;
    if (size == 12) {
      properties.put("String_Node_Str","String_Node_Str");
      if (bitsPerPixel == 1) {
        imageType=VERSION_2_1_BIT;
      }
 else       if (bitsPerPixel == 4) {
        imageType=VERSION_2_4_BIT;
      }
 else       if (bitsPerPixel == 8) {
        imageType=VERSION_2_8_BIT;
      }
 else       if (bitsPerPixel == 24) {
        imageType=VERSION_2_24_BIT;
      }
      int numberOfEntries=(int)((bitmapOffset - 14 - size) / 3);
      int sizeOfPalette=numberOfEntries * 3;
      palette=new byte[sizeOfPalette];
      inputStream.read(palette,0,sizeOfPalette);
      properties.put("String_Node_Str",palette);
    }
 else {
      compression=readDWord(inputStream);
      imageSize=readDWord(inputStream);
      long xPelsPerMeter=readLong(inputStream);
      long yPelsPerMeter=readLong(inputStream);
      long colorsUsed=readDWord(inputStream);
      long colorsImportant=readDWord(inputStream);
switch ((int)compression) {
case BI_RGB:
        properties.put("String_Node_Str","String_Node_Str");
      break;
case BI_RLE8:
    properties.put("String_Node_Str","String_Node_Str");
  break;
case BI_RLE4:
properties.put("String_Node_Str","String_Node_Str");
break;
case BI_BITFIELDS:
properties.put("String_Node_Str","String_Node_Str");
break;
}
properties.put("String_Node_Str",new Long(xPelsPerMeter));
properties.put("String_Node_Str",new Long(yPelsPerMeter));
properties.put("String_Node_Str",new Long(colorsUsed));
properties.put("String_Node_Str",new Long(colorsImportant));
if (size == 40) {
switch ((int)compression) {
case BI_RGB:
case BI_RLE8:
case BI_RLE4:
int numberOfEntries=(int)((bitmapOffset - 14 - size) / 4);
int sizeOfPalette=numberOfEntries * 4;
palette=new byte[sizeOfPalette];
inputStream.read(palette,0,sizeOfPalette);
properties.put("String_Node_Str",palette);
if (bitsPerPixel == 1) {
imageType=VERSION_3_1_BIT;
}
 else if (bitsPerPixel == 4) {
imageType=VERSION_3_4_BIT;
}
 else if (bitsPerPixel == 8) {
imageType=VERSION_3_8_BIT;
}
 else if (bitsPerPixel == 24) {
imageType=VERSION_3_24_BIT;
}
 else if (bitsPerPixel == 16) {
imageType=VERSION_3_NT_16_BIT;
redMask=0x7C00;
greenMask=0x3E0;
blueMask=0x1F;
properties.put("String_Node_Str",new Integer(redMask));
properties.put("String_Node_Str",new Integer(greenMask));
properties.put("String_Node_Str",new Integer(blueMask));
}
 else if (bitsPerPixel == 32) {
imageType=VERSION_3_NT_32_BIT;
redMask=0x00FF0000;
greenMask=0x0000FF00;
blueMask=0x000000FF;
properties.put("String_Node_Str",new Integer(redMask));
properties.put("String_Node_Str",new Integer(greenMask));
properties.put("String_Node_Str",new Integer(blueMask));
}
properties.put("String_Node_Str","String_Node_Str");
break;
case BI_BITFIELDS:
if (bitsPerPixel == 16) {
imageType=VERSION_3_NT_16_BIT;
}
 else if (bitsPerPixel == 32) {
imageType=VERSION_3_NT_32_BIT;
}
redMask=(int)readDWord(inputStream);
greenMask=(int)readDWord(inputStream);
blueMask=(int)readDWord(inputStream);
properties.put("String_Node_Str",new Integer(redMask));
properties.put("String_Node_Str",new Integer(greenMask));
properties.put("String_Node_Str",new Integer(blueMask));
if (colorsUsed != 0) {
sizeOfPalette=(int)colorsUsed * 4;
palette=new byte[sizeOfPalette];
inputStream.read(palette,0,sizeOfPalette);
properties.put("String_Node_Str",palette);
}
properties.put("String_Node_Str","String_Node_Str");
break;
default :
throw new RuntimeException(JaiI18N.getString("String_Node_Str"));
}
}
 else if (size == 108) {
properties.put("String_Node_Str","String_Node_Str");
redMask=(int)readDWord(inputStream);
greenMask=(int)readDWord(inputStream);
blueMask=(int)readDWord(inputStream);
alphaMask=(int)readDWord(inputStream);
long csType=readDWord(inputStream);
int redX=readLong(inputStream);
int redY=readLong(inputStream);
int redZ=readLong(inputStream);
int greenX=readLong(inputStream);
int greenY=readLong(inputStream);
int greenZ=readLong(inputStream);
int blueX=readLong(inputStream);
int blueY=readLong(inputStream);
int blueZ=readLong(inputStream);
long gammaRed=readDWord(inputStream);
long gammaGreen=readDWord(inputStream);
long gammaBlue=readDWord(inputStream);
int numberOfEntries=(int)((bitmapOffset - 14 - size) / 4);
int sizeOfPalette=numberOfEntries * 4;
palette=new byte[sizeOfPalette];
inputStream.read(palette,0,sizeOfPalette);
if (palette != null || palette.length != 0) {
properties.put("String_Node_Str",palette);
}
switch ((int)csType) {
case LCS_CALIBRATED_RGB:
properties.put("String_Node_Str","String_Node_Str");
properties.put("String_Node_Str",new Integer(redX));
properties.put("String_Node_Str",new Integer(redY));
properties.put("String_Node_Str",new Integer(redZ));
properties.put("String_Node_Str",new Integer(greenX));
properties.put("String_Node_Str",new Integer(greenY));
properties.put("String_Node_Str",new Integer(greenZ));
properties.put("String_Node_Str",new Integer(blueX));
properties.put("String_Node_Str",new Integer(blueY));
properties.put("String_Node_Str",new Integer(blueZ));
properties.put("String_Node_Str",new Long(gammaRed));
properties.put("String_Node_Str",new Long(gammaGreen));
properties.put("String_Node_Str",new Long(gammaBlue));
throw new RuntimeException(JaiI18N.getString("String_Node_Str"));
case LCS_sRGB:
properties.put("String_Node_Str","String_Node_Str");
break;
case LCS_CMYK:
properties.put("String_Node_Str","String_Node_Str");
throw new RuntimeException(JaiI18N.getString("String_Node_Str"));
}
if (bitsPerPixel == 1) {
imageType=VERSION_4_1_BIT;
}
 else if (bitsPerPixel == 4) {
imageType=VERSION_4_4_BIT;
}
 else if (bitsPerPixel == 8) {
imageType=VERSION_4_8_BIT;
}
 else if (bitsPerPixel == 16) {
imageType=VERSION_4_16_BIT;
if ((int)compression == BI_RGB) {
redMask=0x7C00;
greenMask=0x3E0;
blueMask=0x1F;
}
}
 else if (bitsPerPixel == 24) {
imageType=VERSION_4_24_BIT;
}
 else if (bitsPerPixel == 32) {
imageType=VERSION_4_32_BIT;
if ((int)compression == BI_RGB) {
redMask=0x00FF0000;
greenMask=0x0000FF00;
blueMask=0x000000FF;
}
}
properties.put("String_Node_Str",new Integer(redMask));
properties.put("String_Node_Str",new Integer(greenMask));
properties.put("String_Node_Str",new Integer(blueMask));
properties.put("String_Node_Str",new Integer(alphaMask));
}
 else {
properties.put("String_Node_Str","String_Node_Str");
throw new RuntimeException(JaiI18N.getString("String_Node_Str"));
}
}
}
 catch (IOException ioe) {
String message=JaiI18N.getString("String_Node_Str");
ImagingListenerProxy.errorOccurred(message,new ImagingException(message,ioe),this,false);
}
if (height > 0) {
isBottomUp=true;
}
 else {
isBottomUp=false;
height=Math.abs(height);
}
tileWidth=width;
tileHeight=height;
if (bitsPerPixel == 1 || bitsPerPixel == 4 || bitsPerPixel == 8) {
numBands=1;
if (bitsPerPixel == 8) {
sampleModel=RasterFactory.createPixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,width,height,numBands);
}
 else {
sampleModel=new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,width,height,bitsPerPixel);
}
byte r[], g[], b[];
int size;
if (imageType == VERSION_2_1_BIT || imageType == VERSION_2_4_BIT || imageType == VERSION_2_8_BIT) {
size=palette.length / 3;
if (size > 256) {
size=256;
}
int off;
r=new byte[size];
g=new byte[size];
b=new byte[size];
for (int i=0; i < size; i++) {
off=3 * i;
b[i]=palette[off];
g[i]=palette[off + 1];
r[i]=palette[off + 2];
}
}
 else {
size=palette.length / 4;
if (size > 256) {
size=256;
}
int off;
r=new byte[size];
g=new byte[size];
b=new byte[size];
for (int i=0; i < size; i++) {
off=4 * i;
b[i]=palette[off];
g[i]=palette[off + 1];
r[i]=palette[off + 2];
}
}
if (ImageCodec.isIndicesForGrayscale(r,g,b)) colorModel=ImageCodec.createComponentColorModel(sampleModel);
 else colorModel=new IndexColorModel(bitsPerPixel,size,r,g,b);
}
 else if (bitsPerPixel == 16) {
numBands=3;
sampleModel=new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT,width,height,new int[]{redMask,greenMask,blueMask});
colorModel=new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),16,redMask,greenMask,blueMask,0,false,DataBuffer.TYPE_USHORT);
}
 else if (bitsPerPixel == 32) {
numBands=alphaMask == 0 ? 3 : 4;
int[] bitMasks=numBands == 3 ? new int[]{redMask,greenMask,blueMask} : new int[]{redMask,greenMask,blueMask,alphaMask};
sampleModel=new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT,width,height,bitMasks);
colorModel=new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),32,redMask,greenMask,blueMask,alphaMask,false,DataBuffer.TYPE_INT);
}
 else {
numBands=3;
sampleModel=RasterFactory.createPixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,width,height,numBands);
colorModel=ImageCodec.createComponentColorModel(sampleModel);
}
try {
inputStream.reset();
inputStream.skip(bitmapOffset);
}
 catch (IOException ioe) {
String message=JaiI18N.getString("String_Node_Str");
ImagingListenerProxy.errorOccurred(message,new ImagingException(message,ioe),this,false);
}
}