Program: 33371
Project: 33371
Buggy source:
public static int[] getUncompressedStripByteCounts(IFD ifd,int strips){
  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 bytesPerRow=((bitsPerSample * rowWidth + 7) / 8) * samplesPerPixel;
  int planaryConfiguration=1;
  tiffField=ifd.getField(TiffTag.PLANAR_CONFIGURATTION.getValue());
  if (tiffField != null)   planaryConfiguration=tiffField.getDataAsLong()[0];
  if (planaryConfiguration == 2) {
    bytesPerRow=(bitsPerSample * rowWidth + 7) / 8;
  }
  int bytesPerStrip=bytesPerRow * rowsPerStrip;
  int[] counts=new int[strips];
  int photoMetric=ifd.getField(TiffTag.PHOTOMETRIC_INTERPRETATION.getValue()).getDataAsLong()[0];
  if (photoMetric != TiffFieldEnum.PhotoMetric.YCbCr.getValue()) {
    Arrays.fill(counts,bytesPerStrip);
    if (tileWidth < 0) {
      int lastStripBytes=bytesPerRow * imageHeight - bytesPerStrip * (strips - 1);
      counts[counts.length - 1]=lastStripBytes;
      if (planaryConfiguration == 2) {
        int stripsPerSample=strips / samplesPerPixel;
        lastStripBytes=bytesPerRow * imageHeight - bytesPerStrip * (stripsPerSample - 1);
        if (lastStripBytes > 0) {
          for (int i=0, stripOffset=stripsPerSample - 1; i < samplesPerPixel; i++) {
            counts[stripOffset]=lastStripBytes;
            stripOffset+=stripsPerSample;
          }
        }
      }
    }
  }
 else {
    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];
    int[] columnHeight=new int[samplesPerPixel];
    columnHeight[0]=imageHeight;
    columnHeight[1]=imageHeight / verticalSampleFactor;
    columnHeight[2]=columnHeight[1];
    if (planaryConfiguration == 1) {
      bytesPerStrip=sampleBytesPerRow[0] * sampleRowsPerStrip[0] + sampleBytesPerRow[1] * sampleRowsPerStrip[1] + sampleBytesPerRow[2] * sampleRowsPerStrip[2];
      Arrays.fill(counts,bytesPerStrip);
      if (tileWidth < 0) {
        int lastStripBytes=(sampleBytesPerRow[0] * columnHeight[0] + sampleBytesPerRow[1] * columnHeight[1] + sampleBytesPerRow[2] * columnHeight[2]) - bytesPerStrip * (strips - 1);
        counts[counts.length - 1]=lastStripBytes;
      }
    }
 else {
      int[] sampleBytesPerStrip=new int[samplesPerPixel];
      sampleBytesPerStrip[0]=sampleRowsPerStrip[0] * sampleBytesPerRow[0];
      sampleBytesPerStrip[1]=sampleRowsPerStrip[1] * sampleBytesPerRow[1];
      sampleBytesPerStrip[2]=sampleBytesPerStrip[1];
      int stripsPerSample=strips / samplesPerPixel;
      int startOffset=0;
      int endOffset=stripsPerSample;
      for (int i=0; i < samplesPerPixel; i++) {
        Arrays.fill(counts,startOffset,endOffset,sampleBytesPerStrip[i]);
        startOffset=endOffset;
        endOffset+=stripsPerSample;
      }
      if (tileWidth < 0) {
        int[] lastStripBytes=new int[samplesPerPixel];
        lastStripBytes[0]=sampleBytesPerRow[0] * columnHeight[0] - sampleBytesPerStrip[0] * (stripsPerSample - 1);
        lastStripBytes[1]=sampleBytesPerRow[1] * columnHeight[1] - sampleBytesPerStrip[1] * (stripsPerSample - 1);
        lastStripBytes[2]=lastStripBytes[1];
        startOffset=stripsPerSample - 1;
        for (int i=0; i < samplesPerPixel; i++) {
          counts[startOffset]=lastStripBytes[i];
          startOffset=stripsPerSample;
        }
      }
    }
  }
  return counts;
}

Fixed version (ground truth):
public static int[] getUncompressedStripByteCounts(IFD ifd,int strips){
  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 bytesPerRow=((bitsPerSample * rowWidth + 7) / 8) * samplesPerPixel;
  int planaryConfiguration=1;
  tiffField=ifd.getField(TiffTag.PLANAR_CONFIGURATTION.getValue());
  if (tiffField != null)   planaryConfiguration=tiffField.getDataAsLong()[0];
  if (planaryConfiguration == 2) {
    bytesPerRow=(bitsPerSample * rowWidth + 7) / 8;
  }
  int bytesPerStrip=bytesPerRow * rowsPerStrip;
  int[] counts=new int[strips];
  if (photoMetric != TiffFieldEnum.PhotoMetric.YCbCr.getValue()) {
    Arrays.fill(counts,bytesPerStrip);
    if (tileWidth < 0) {
      int lastStripBytes=bytesPerRow * imageHeight - bytesPerStrip * (strips - 1);
      counts[counts.length - 1]=lastStripBytes;
      if (planaryConfiguration == 2) {
        int stripsPerSample=strips / samplesPerPixel;
        lastStripBytes=bytesPerRow * imageHeight - bytesPerStrip * (stripsPerSample - 1);
        if (lastStripBytes > 0) {
          for (int i=0, stripOffset=stripsPerSample - 1; i < samplesPerPixel; i++) {
            counts[stripOffset]=lastStripBytes;
            stripOffset+=stripsPerSample;
          }
        }
      }
    }
  }
 else {
    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];
    int[] columnHeight=new int[samplesPerPixel];
    columnHeight[0]=imageHeight;
    columnHeight[1]=imageHeight / verticalSampleFactor;
    columnHeight[2]=columnHeight[1];
    if (planaryConfiguration == 1) {
      bytesPerStrip=sampleBytesPerRow[0] * sampleRowsPerStrip[0] + sampleBytesPerRow[1] * sampleRowsPerStrip[1] + sampleBytesPerRow[2] * sampleRowsPerStrip[2];
      Arrays.fill(counts,bytesPerStrip);
      if (tileWidth < 0) {
        int lastStripBytes=(sampleBytesPerRow[0] * columnHeight[0] + sampleBytesPerRow[1] * columnHeight[1] + sampleBytesPerRow[2] * columnHeight[2]) - bytesPerStrip * (strips - 1);
        counts[counts.length - 1]=lastStripBytes;
      }
    }
 else {
      int[] sampleBytesPerStrip=new int[samplesPerPixel];
      sampleBytesPerStrip[0]=sampleRowsPerStrip[0] * sampleBytesPerRow[0];
      sampleBytesPerStrip[1]=sampleRowsPerStrip[1] * sampleBytesPerRow[1];
      sampleBytesPerStrip[2]=sampleBytesPerStrip[1];
      int stripsPerSample=strips / samplesPerPixel;
      int startOffset=0;
      int endOffset=stripsPerSample;
      for (int i=0; i < samplesPerPixel; i++) {
        Arrays.fill(counts,startOffset,endOffset,sampleBytesPerStrip[i]);
        startOffset=endOffset;
        endOffset+=stripsPerSample;
      }
      if (tileWidth < 0) {
        int[] lastStripBytes=new int[samplesPerPixel];
        lastStripBytes[0]=sampleBytesPerRow[0] * columnHeight[0] - sampleBytesPerStrip[0] * (stripsPerSample - 1);
        lastStripBytes[1]=sampleBytesPerRow[1] * columnHeight[1] - sampleBytesPerStrip[1] * (stripsPerSample - 1);
        lastStripBytes[2]=lastStripBytes[1];
        startOffset=stripsPerSample - 1;
        for (int i=0; i < samplesPerPixel; i++) {
          counts[startOffset]=lastStripBytes[i];
          startOffset+=stripsPerSample;
        }
      }
    }
  }
  return counts;
}