Program: 33372
Project: 33372
Buggy source:
private static int[] getBytes2Read(IFD ifd){
  TiffField<?> tiffField=ifd.getField(TiffTag.IMAGE_WIDTH.getValue());
  int imageWidth=tiffField.getDataAsLong()[0];
  tiffField=ifd.getField(TiffTag.IMAGE_LENGTH.getValue());
  int imageHeight=tiffField.getDataAsLong()[0];
  int samplesPerPixel=1;
  tiffField=ifd.getField(TiffTag.SAMPLES_PER_PIXEL.getValue());
  if (tiffField != null) {
    samplesPerPixel=tiffField.getDataAsLong()[0];
  }
  int bitsPerSample=1;
  tiffField=ifd.getField(TiffTag.BITS_PER_SAMPLE.getValue());
  if (tiffField != null) {
    bitsPerSample=tiffField.getDataAsLong()[0];
  }
  int tileWidth=-1;
  int tileLength=-1;
  TiffField<?> f_tileLength=ifd.getField(TiffTag.TILE_LENGTH.getValue());
  TiffField<?> f_tileWidth=ifd.getField(TiffTag.TILE_WIDTH.getValue());
  if (f_tileWidth != null) {
    tileWidth=f_tileWidth.getDataAsLong()[0];
    tileLength=f_tileLength.getDataAsLong()[0];
  }
  int rowsPerStrip=imageHeight;
  int rowWidth=imageWidth;
  TiffField<?> f_rowsPerStrip=ifd.getField(TiffTag.ROWS_PER_STRIP.getValue());
  if (f_rowsPerStrip != null)   rowsPerStrip=f_rowsPerStrip.getDataAsLong()[0];
  if (rowsPerStrip > imageHeight)   rowsPerStrip=imageHeight;
  if (tileWidth > 0) {
    rowsPerStrip=tileLength;
    rowWidth=tileWidth;
  }
  int planaryConfiguration=1;
  tiffField=ifd.getField(TiffTag.PLANAR_CONFIGURATTION.getValue());
  if (tiffField != null)   planaryConfiguration=tiffField.getDataAsLong()[0];
  int[] totalBytes2Read=new int[samplesPerPixel];
  if (planaryConfiguration == 1)   totalBytes2Read[0]=((rowWidth * bitsPerSample + 7) / 8) * samplesPerPixel * rowsPerStrip;
 else   totalBytes2Read[0]=totalBytes2Read[1]=totalBytes2Read[2]=((rowWidth * bitsPerSample + 7) / 8) * rowsPerStrip;
  int photoMetric=ifd.getField(TiffTag.PHOTOMETRIC_INTERPRETATION.getValue()).getDataAsLong()[0];
  if (photoMetric == TiffFieldEnum.PhotoMetric.YCbCr.getValue()) {
    int horizontalSampleFactor=2;
    int verticalSampleFactor=2;
    TiffField<?> f_YCbCrSubSampling=ifd.getField(TiffTag.YCbCr_SUB_SAMPLING.getValue());
    if (f_YCbCrSubSampling != null) {
      int[] sampleFactors=f_YCbCrSubSampling.getDataAsLong();
      horizontalSampleFactor=sampleFactors[0];
      verticalSampleFactor=sampleFactors[1];
    }
    if (samplesPerPixel != 3)     samplesPerPixel=3;
    int[] sampleBytesPerRow=new int[samplesPerPixel];
    sampleBytesPerRow[0]=(bitsPerSample * rowWidth + 7) / 8;
    sampleBytesPerRow[1]=(bitsPerSample * rowWidth / horizontalSampleFactor + 7) / 8;
    sampleBytesPerRow[2]=sampleBytesPerRow[1];
    int[] sampleRowsPerStrip=new int[samplesPerPixel];
    sampleRowsPerStrip[0]=rowsPerStrip;
    sampleRowsPerStrip[1]=rowsPerStrip / verticalSampleFactor;
    sampleRowsPerStrip[2]=sampleRowsPerStrip[1];
    totalBytes2Read[0]=sampleBytesPerRow[0] * sampleRowsPerStrip[0];
    totalBytes2Read[1]=sampleBytesPerRow[1] * sampleRowsPerStrip[1];
    totalBytes2Read[2]=totalBytes2Read[1];
    if (tiffField != null)     planaryConfiguration=tiffField.getDataAsLong()[0];
    if (planaryConfiguration == 1)     totalBytes2Read[0]=totalBytes2Read[0] + totalBytes2Read[1] + totalBytes2Read[2];
  }
  return totalBytes2Read;
}

Fixed version (ground truth):
private static int[] getBytes2Read(IFD ifd){
  TiffField<?> tiffField=ifd.getField(TiffTag.IMAGE_WIDTH.getValue());
  int imageWidth=tiffField.getDataAsLong()[0];
  tiffField=ifd.getField(TiffTag.IMAGE_LENGTH.getValue());
  int imageHeight=tiffField.getDataAsLong()[0];
  int horizontalSampleFactor=2;
  int verticalSampleFactor=2;
  int photoMetric=ifd.getField(TiffTag.PHOTOMETRIC_INTERPRETATION.getValue()).getDataAsLong()[0];
  if (photoMetric == TiffFieldEnum.PhotoMetric.YCbCr.getValue()) {
    TiffField<?> f_YCbCrSubSampling=ifd.getField(TiffTag.YCbCr_SUB_SAMPLING.getValue());
    if (f_YCbCrSubSampling != null) {
      int[] sampleFactors=f_YCbCrSubSampling.getDataAsLong();
      horizontalSampleFactor=sampleFactors[0];
      verticalSampleFactor=sampleFactors[1];
    }
    imageWidth=((imageWidth + horizontalSampleFactor - 1) / horizontalSampleFactor) * horizontalSampleFactor;
    imageHeight=((imageHeight + verticalSampleFactor - 1) / verticalSampleFactor) * verticalSampleFactor;
  }
  int samplesPerPixel=1;
  tiffField=ifd.getField(TiffTag.SAMPLES_PER_PIXEL.getValue());
  if (tiffField != null) {
    samplesPerPixel=tiffField.getDataAsLong()[0];
  }
  int bitsPerSample=1;
  tiffField=ifd.getField(TiffTag.BITS_PER_SAMPLE.getValue());
  if (tiffField != null) {
    bitsPerSample=tiffField.getDataAsLong()[0];
  }
  int tileWidth=-1;
  int tileLength=-1;
  TiffField<?> f_tileLength=ifd.getField(TiffTag.TILE_LENGTH.getValue());
  TiffField<?> f_tileWidth=ifd.getField(TiffTag.TILE_WIDTH.getValue());
  if (f_tileWidth != null) {
    tileWidth=f_tileWidth.getDataAsLong()[0];
    tileLength=f_tileLength.getDataAsLong()[0];
  }
  int rowsPerStrip=imageHeight;
  int rowWidth=imageWidth;
  TiffField<?> f_rowsPerStrip=ifd.getField(TiffTag.ROWS_PER_STRIP.getValue());
  if (f_rowsPerStrip != null)   rowsPerStrip=f_rowsPerStrip.getDataAsLong()[0];
  if (rowsPerStrip > imageHeight)   rowsPerStrip=imageHeight;
  if (tileWidth > 0) {
    rowsPerStrip=tileLength;
    rowWidth=tileWidth;
  }
  int planaryConfiguration=1;
  tiffField=ifd.getField(TiffTag.PLANAR_CONFIGURATTION.getValue());
  if (tiffField != null)   planaryConfiguration=tiffField.getDataAsLong()[0];
  int[] totalBytes2Read=new int[samplesPerPixel];
  if (planaryConfiguration == 1)   totalBytes2Read[0]=((rowWidth * bitsPerSample + 7) / 8) * samplesPerPixel * rowsPerStrip;
 else   totalBytes2Read[0]=totalBytes2Read[1]=totalBytes2Read[2]=((rowWidth * bitsPerSample + 7) / 8) * rowsPerStrip;
  if (photoMetric == TiffFieldEnum.PhotoMetric.YCbCr.getValue()) {
    if (samplesPerPixel != 3)     samplesPerPixel=3;
    int[] sampleBytesPerRow=new int[samplesPerPixel];
    sampleBytesPerRow[0]=(bitsPerSample * rowWidth + 7) / 8;
    sampleBytesPerRow[1]=(bitsPerSample * rowWidth / horizontalSampleFactor + 7) / 8;
    sampleBytesPerRow[2]=sampleBytesPerRow[1];
    int[] sampleRowsPerStrip=new int[samplesPerPixel];
    sampleRowsPerStrip[0]=rowsPerStrip;
    sampleRowsPerStrip[1]=rowsPerStrip / verticalSampleFactor;
    sampleRowsPerStrip[2]=sampleRowsPerStrip[1];
    totalBytes2Read[0]=sampleBytesPerRow[0] * sampleRowsPerStrip[0];
    totalBytes2Read[1]=sampleBytesPerRow[1] * sampleRowsPerStrip[1];
    totalBytes2Read[2]=totalBytes2Read[1];
    if (tiffField != null)     planaryConfiguration=tiffField.getDataAsLong()[0];
    if (planaryConfiguration == 1)     totalBytes2Read[0]=totalBytes2Read[0] + totalBytes2Read[1] + totalBytes2Read[2];
  }
  return totalBytes2Read;
}