Program: 33353
Project: 33353
Buggy source:
private void jpegCompress(int[] pixels,int imageWidth,int imageHeight,boolean grayscale) throws Exception {
  int rowsPerStrip=imageHeight / 2 + 1;
  int jpegQuality=90;
  boolean writeICCProfile=false;
  PhotoMetric photoMetric=PhotoMetric.YCbCr;
  if (tiffOptions != null) {
    jpegQuality=tiffOptions.getJPEGQuality();
    if (tiffOptions.getPhotoMetric() != PhotoMetric.UNKNOWN) {
      photoMetric=tiffOptions.getPhotoMetric();
    }
    writeICCProfile=tiffOptions.writeICCProfile();
  }
  int numOfSamples=0;
  if (grayscale) {
    photoMetric=PhotoMetric.BLACK_IS_ZERO;
    numOfSamples=1;
  }
 else   if (photoMetric == PhotoMetric.RGB || photoMetric == PhotoMetric.YCbCr) {
    numOfSamples=3;
    ifd.addField(new RationalField(TiffTag.REFERENCE_BLACK_WHITE.getValue(),new int[]{0,255,128,255,128,255}));
    if (photoMetric == PhotoMetric.YCbCr)     ifd.addField(new ShortField(TiffTag.YCbCr_SUB_SAMPLING.getValue(),new short[]{1,1}));
  }
 else   if (photoMetric == PhotoMetric.SEPARATED) {
    numOfSamples=4;
  }
 else {
    throw new UnsupportedOperationException("String_Node_Str");
  }
  short[] bitsPerSample=new short[numOfSamples];
  Arrays.fill(bitsPerSample,(short)8);
  ifd.addField(new ShortField(TiffTag.PHOTOMETRIC_INTERPRETATION.getValue(),new short[]{(short)photoMetric.getValue()}));
  ifd.addField(new ShortField(TiffTag.SAMPLES_PER_PIXEL.getValue(),new short[]{(short)numOfSamples}));
  ifd.addField(new ShortField(TiffTag.BITS_PER_SAMPLE.getValue(),bitsPerSample));
  JPEGWriter jpgWriter=new JPEGWriter();
  ImageMeta.ImageMetaBuilder builder=new ImageMeta.ImageMetaBuilder();
  builder.colorType(ColorType.GRAY_SCALE);
  JPEGOptions jpegOptions=new JPEGOptions();
  jpegOptions.setQuality(jpegQuality);
  jpegOptions.setColorSpace(photoMetric.getValue());
  jpegOptions.setTiffFlavor(true);
  jpegOptions.setIncludeTables(false);
  builder.imageOptions(jpegOptions);
  jpgWriter.setImageMeta(builder.build());
  ByteArrayOutputStream bout=new ByteArrayOutputStream();
  jpgWriter.writeDefaultJPEGTables(bout);
  ifd.addField(new UndefinedField(TiffTag.JPEG_TABLES.getValue(),bout.toByteArray()));
  long startOffset=randomOS.getStreamPointer();
  jpgWriter.write(Arrays.copyOfRange(pixels,0,imageWidth * (imageHeight / 2 + 1)),imageWidth,imageHeight / 2 + 1,randomOS);
  long finishOffset=randomOS.getStreamPointer();
  int totalOut=(int)(finishOffset - startOffset);
  update(totalOut);
  startOffset=finishOffset;
  jpgWriter.write(Arrays.copyOfRange(pixels,imageWidth * (imageHeight / 2 + 1),pixels.length),imageWidth,imageHeight - imageHeight / 2 - 1,randomOS);
  finishOffset=randomOS.getStreamPointer();
  totalOut=(int)(finishOffset - startOffset);
  update(totalOut);
  if (photoMetric == PhotoMetric.SEPARATED && writeICCProfile) {
    byte[] icc_profile=jpgWriter.getCMYK_ICC_Profile();
    if (icc_profile != null) {
      ifd.addField(new UndefinedField(TiffTag.ICC_PROFILE.getValue(),icc_profile));
    }
  }
  ifd.addField(new ShortField(TiffTag.PLANAR_CONFIGURATTION.getValue(),new short[]{(short)PlanarConfiguration.CONTIGUOUS.getValue()}));
  ifd.addField(new ShortField(TiffTag.COMPRESSION.getValue(),new short[]{(short)Compression.JPG.getValue()}));
  ifd.addField(new ShortField(TiffTag.ROWS_PER_STRIP.getValue(),new short[]{(short)rowsPerStrip}));
}

Fixed version (ground truth):
private void jpegCompress(int[] pixels,int imageWidth,int imageHeight,boolean grayscale) throws Exception {
  int rowsPerStrip=imageHeight / 2 + 1;
  int jpegQuality=90;
  boolean writeICCProfile=false;
  PhotoMetric photoMetric=PhotoMetric.YCbCr;
  if (tiffOptions != null) {
    jpegQuality=tiffOptions.getJPEGQuality();
    if (tiffOptions.getPhotoMetric() != PhotoMetric.UNKNOWN) {
      photoMetric=tiffOptions.getPhotoMetric();
    }
    writeICCProfile=tiffOptions.writeICCProfile();
  }
  int numOfSamples=0;
  if (grayscale) {
    photoMetric=PhotoMetric.BLACK_IS_ZERO;
    numOfSamples=1;
  }
 else   if (photoMetric == PhotoMetric.RGB || photoMetric == PhotoMetric.YCbCr) {
    numOfSamples=3;
    ifd.addField(new RationalField(TiffTag.REFERENCE_BLACK_WHITE.getValue(),new int[]{0,255,128,255,128,255}));
    if (photoMetric == PhotoMetric.YCbCr)     ifd.addField(new ShortField(TiffTag.YCbCr_SUB_SAMPLING.getValue(),new short[]{1,1}));
  }
 else   if (photoMetric == PhotoMetric.SEPARATED) {
    numOfSamples=4;
  }
 else {
    throw new UnsupportedOperationException("String_Node_Str");
  }
  short[] bitsPerSample=new short[numOfSamples];
  Arrays.fill(bitsPerSample,(short)8);
  ifd.addField(new ShortField(TiffTag.PHOTOMETRIC_INTERPRETATION.getValue(),new short[]{(short)photoMetric.getValue()}));
  ifd.addField(new ShortField(TiffTag.SAMPLES_PER_PIXEL.getValue(),new short[]{(short)numOfSamples}));
  ifd.addField(new ShortField(TiffTag.BITS_PER_SAMPLE.getValue(),bitsPerSample));
  JPEGWriter jpgWriter=new JPEGWriter();
  ImageMeta.ImageMetaBuilder builder=new ImageMeta.ImageMetaBuilder();
  if (grayscale)   builder.colorType(ColorType.GRAY_SCALE);
  JPEGOptions jpegOptions=new JPEGOptions();
  jpegOptions.setQuality(jpegQuality);
  jpegOptions.setColorSpace(photoMetric.getValue());
  jpegOptions.setTiffFlavor(true);
  jpegOptions.setIncludeTables(false);
  builder.imageOptions(jpegOptions);
  jpgWriter.setImageMeta(builder.build());
  ByteArrayOutputStream bout=new ByteArrayOutputStream();
  jpgWriter.writeDefaultJPEGTables(bout);
  ifd.addField(new UndefinedField(TiffTag.JPEG_TABLES.getValue(),bout.toByteArray()));
  long startOffset=randomOS.getStreamPointer();
  jpgWriter.write(Arrays.copyOfRange(pixels,0,imageWidth * (imageHeight / 2 + 1)),imageWidth,imageHeight / 2 + 1,randomOS);
  long finishOffset=randomOS.getStreamPointer();
  int totalOut=(int)(finishOffset - startOffset);
  update(totalOut);
  startOffset=finishOffset;
  jpgWriter.write(Arrays.copyOfRange(pixels,imageWidth * (imageHeight / 2 + 1),pixels.length),imageWidth,imageHeight - imageHeight / 2 - 1,randomOS);
  finishOffset=randomOS.getStreamPointer();
  totalOut=(int)(finishOffset - startOffset);
  update(totalOut);
  if (photoMetric == PhotoMetric.SEPARATED && writeICCProfile) {
    byte[] icc_profile=jpgWriter.getCMYK_ICC_Profile();
    if (icc_profile != null) {
      ifd.addField(new UndefinedField(TiffTag.ICC_PROFILE.getValue(),icc_profile));
    }
  }
  ifd.addField(new ShortField(TiffTag.PLANAR_CONFIGURATTION.getValue(),new short[]{(short)PlanarConfiguration.CONTIGUOUS.getValue()}));
  ifd.addField(new ShortField(TiffTag.COMPRESSION.getValue(),new short[]{(short)Compression.JPG.getValue()}));
  ifd.addField(new ShortField(TiffTag.ROWS_PER_STRIP.getValue(),new short[]{(short)rowsPerStrip}));
}