Program: 187105
Project: 187105
Buggy source:
private void write(IIOMetadata sm,IIOImage iioimage,ImageWriteParam p,boolean writeHeader,boolean writeData) throws IOException {
  if (stream == null) {
    throw new IllegalStateException("String_Node_Str");
  }
  if (iioimage == null) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  if (iioimage.hasRaster() && !canWriteRasters()) {
    throw new UnsupportedOperationException("String_Node_Str");
  }
  this.image=iioimage.getRenderedImage();
  SampleModel sampleModel=image.getSampleModel();
  this.sourceXOffset=image.getMinX();
  this.sourceYOffset=image.getMinY();
  this.sourceWidth=image.getWidth();
  this.sourceHeight=image.getHeight();
  Rectangle imageBounds=new Rectangle(sourceXOffset,sourceYOffset,sourceWidth,sourceHeight);
  ColorModel colorModel=null;
  if (p == null) {
    this.param=getDefaultWriteParam();
    this.sourceBands=null;
    this.periodX=1;
    this.periodY=1;
    this.numBands=sampleModel.getNumBands();
    colorModel=image.getColorModel();
  }
 else {
    this.param=p;
    Rectangle sourceRegion=param.getSourceRegion();
    if (sourceRegion != null) {
      sourceRegion=sourceRegion.intersection(imageBounds);
      sourceXOffset=sourceRegion.x;
      sourceYOffset=sourceRegion.y;
      sourceWidth=sourceRegion.width;
      sourceHeight=sourceRegion.height;
    }
    int gridX=param.getSubsamplingXOffset();
    int gridY=param.getSubsamplingYOffset();
    this.sourceXOffset+=gridX;
    this.sourceYOffset+=gridY;
    this.sourceWidth-=gridX;
    this.sourceHeight-=gridY;
    this.periodX=param.getSourceXSubsampling();
    this.periodY=param.getSourceYSubsampling();
    int[] sBands=param.getSourceBands();
    if (sBands != null) {
      sourceBands=sBands;
      this.numBands=sourceBands.length;
    }
 else {
      this.numBands=sampleModel.getNumBands();
    }
    ImageTypeSpecifier destType=p.getDestinationType();
    if (destType != null) {
      ColorModel cm=destType.getColorModel();
      if (cm.getNumComponents() == numBands) {
        colorModel=cm;
      }
    }
    if (colorModel == null) {
      colorModel=image.getColorModel();
    }
  }
  this.imageType=new ImageTypeSpecifier(colorModel,sampleModel);
  ImageUtil.canEncodeImage(this,this.imageType);
  int destWidth=(sourceWidth + periodX - 1) / periodX;
  int destHeight=(sourceHeight + periodY - 1) / periodY;
  if (destWidth <= 0 || destHeight <= 0) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  clearAbortRequest();
  processImageStarted(0);
  int[] sampleSize=sampleModel.getSampleSize();
  long tot=0;
  for (int i=0; i < this.numBands; i++)   tot+=sampleSize[i];
  long sizeImage=(tot * this.sourceHeight * this.sourceWidth) / 8;
  long var=4294967296L;
  boolean isForceToBigTIFF=false;
  if (p instanceof TIFFImageWriteParam) {
    isForceToBigTIFF=((TIFFImageWriteParam)p).isForceToBigTIFF();
  }
  if (sizeImage > var || isForceToBigTIFF || isBtiff == true)   isBtiff=true;
 else   isBtiff=false;
  if (writeHeader) {
    this.streamMetadata=null;
    if (sm != null) {
      this.streamMetadata=(TIFFStreamMetadata)convertStreamMetadata(sm,param);
    }
    if (this.streamMetadata == null) {
      this.streamMetadata=(TIFFStreamMetadata)getDefaultStreamMetadata(param);
    }
    writeHeader();
    if (!isBtiff) {
      stream.seek(headerPosition + 4);
      nextSpace=(nextSpace + 3) & ~0x3;
      stream.writeInt((int)nextSpace);
    }
 else {
      stream.seek(headerPosition + 8);
      nextSpace=(nextSpace + 7) & ~0x7;
      stream.writeLong(nextSpace);
    }
  }
  this.imageMetadata=null;
  IIOMetadata im=iioimage.getMetadata();
  if (im != null) {
    if (im instanceof TIFFImageMetadata) {
      this.imageMetadata=((TIFFImageMetadata)im).getShallowClone();
    }
 else     if (Arrays.asList(im.getMetadataFormatNames()).contains(TIFFImageMetadata.nativeMetadataFormatName)) {
      this.imageMetadata=convertNativeImageMetadata(im);
    }
 else     if (im.isStandardMetadataFormatSupported()) {
      try {
        this.imageMetadata=convertStandardImageMetadata(im);
      }
 catch (      IIOInvalidTreeException e) {
      }
    }
  }
  if (this.imageMetadata == null) {
    this.imageMetadata=(TIFFImageMetadata)getDefaultImageMetadata(this.imageType,this.param);
  }
  setupMetadata(colorModel,sampleModel,destWidth,destHeight);
  compressor.setWriter(this);
  compressor.setMetadata(imageMetadata);
  compressor.setStream(stream);
  initializeScaleTables(sampleModel.getSampleSize());
  this.isBilevel=ImageUtil.isBinary(this.image.getSampleModel());
  this.isInverted=(nativePhotometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO && photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO) || (nativePhotometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO && photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO);
  this.isImageSimple=(isBilevel || (!isInverted && ImageUtil.imageIsContiguous(this.image))) && !isRescaling && sourceBands == null && periodX == 1 && periodY == 1 && colorConverter == null;
  TIFFIFD rootIFD=imageMetadata.getRootIFD();
  rootIFD.writeToStream(stream,isBtiff);
  this.nextIFDPointerPos=stream.getStreamPosition();
  stream.writeLong(0);
  long lastIFDPosition=rootIFD.getLastPosition();
  stream.seek(lastIFDPosition);
  if (lastIFDPosition > this.nextSpace) {
    this.nextSpace=lastIFDPosition;
  }
  if (!writeData) {
    return;
  }
  long stripOrTileByteCountsPosition=rootIFD.getStripOrTileByteCountsPosition();
  long stripOrTileOffsetsPosition=rootIFD.getStripOrTileOffsetsPosition();
  this.totalPixels=tileWidth * tileLength * tilesDown* tilesAcross;
  this.pixelsDone=0;
  for (int tj=0; tj < tilesDown; tj++) {
    for (int ti=0; ti < tilesAcross; ti++) {
      long pos=stream.getStreamPosition();
      Rectangle tileRect=new Rectangle(sourceXOffset + ti * tileWidth * periodX,sourceYOffset + tj * tileLength * periodY,tileWidth * periodX,tileLength * periodY);
      try {
        int byteCount=writeTile(tileRect,compressor);
        if (pos + byteCount > nextSpace) {
          nextSpace=pos + byteCount;
        }
        pixelsDone+=tileRect.width * tileRect.height;
        processImageProgress(100.0F * pixelsDone / totalPixels);
        stream.mark();
        stream.seek(stripOrTileOffsetsPosition);
        if (!isBtiff) {
          stream.writeInt((int)pos);
          stripOrTileOffsetsPosition+=4;
          stream.seek(stripOrTileByteCountsPosition);
          stream.writeInt(byteCount);
          stripOrTileByteCountsPosition+=4;
        }
 else {
          stream.writeLong(pos);
          stripOrTileOffsetsPosition+=8;
          stream.seek(stripOrTileByteCountsPosition);
          stream.writeLong(byteCount);
          stripOrTileByteCountsPosition+=8;
        }
        stream.reset();
      }
 catch (      IOException e) {
        throw new IIOException("String_Node_Str",e);
      }
      if (abortRequested()) {
        processWriteAborted();
        return;
      }
    }
  }
  processImageComplete();
}

Fixed version (ground truth):
private void write(IIOMetadata sm,IIOImage iioimage,ImageWriteParam p,boolean writeHeader,boolean writeData) throws IOException {
  if (stream == null) {
    throw new IllegalStateException("String_Node_Str");
  }
  if (iioimage == null) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  if (iioimage.hasRaster() && !canWriteRasters()) {
    throw new UnsupportedOperationException("String_Node_Str");
  }
  this.image=iioimage.getRenderedImage();
  SampleModel sampleModel=image.getSampleModel();
  this.sourceXOffset=image.getMinX();
  this.sourceYOffset=image.getMinY();
  this.sourceWidth=image.getWidth();
  this.sourceHeight=image.getHeight();
  Rectangle imageBounds=new Rectangle(sourceXOffset,sourceYOffset,sourceWidth,sourceHeight);
  ColorModel colorModel=null;
  if (p == null) {
    this.param=getDefaultWriteParam();
    this.sourceBands=null;
    this.periodX=1;
    this.periodY=1;
    this.numBands=sampleModel.getNumBands();
    colorModel=image.getColorModel();
  }
 else {
    this.param=p;
    Rectangle sourceRegion=param.getSourceRegion();
    if (sourceRegion != null) {
      sourceRegion=sourceRegion.intersection(imageBounds);
      sourceXOffset=sourceRegion.x;
      sourceYOffset=sourceRegion.y;
      sourceWidth=sourceRegion.width;
      sourceHeight=sourceRegion.height;
    }
    int gridX=param.getSubsamplingXOffset();
    int gridY=param.getSubsamplingYOffset();
    this.sourceXOffset+=gridX;
    this.sourceYOffset+=gridY;
    this.sourceWidth-=gridX;
    this.sourceHeight-=gridY;
    this.periodX=param.getSourceXSubsampling();
    this.periodY=param.getSourceYSubsampling();
    int[] sBands=param.getSourceBands();
    if (sBands != null) {
      sourceBands=sBands;
      this.numBands=sourceBands.length;
    }
 else {
      this.numBands=sampleModel.getNumBands();
    }
    ImageTypeSpecifier destType=p.getDestinationType();
    if (destType != null) {
      ColorModel cm=destType.getColorModel();
      if (cm.getNumComponents() == numBands) {
        colorModel=cm;
      }
    }
    if (colorModel == null) {
      colorModel=image.getColorModel();
    }
  }
  this.imageType=new ImageTypeSpecifier(colorModel,sampleModel);
  ImageUtil.canEncodeImage(this,this.imageType);
  int destWidth=(sourceWidth + periodX - 1) / periodX;
  int destHeight=(sourceHeight + periodY - 1) / periodY;
  if (destWidth <= 0 || destHeight <= 0) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  clearAbortRequest();
  processImageStarted(0);
  int[] sampleSize=sampleModel.getSampleSize();
  long tot=0;
  for (int i=0; i < this.numBands; i++)   tot+=sampleSize[i];
  long sizeImage=(tot * this.sourceHeight * this.sourceWidth) / 8;
  long var=4294967296L;
  boolean isForceToBigTIFF=false;
  if (p instanceof TIFFImageWriteParam) {
    isForceToBigTIFF=((TIFFImageWriteParam)p).isForceToBigTIFF();
  }
  if (sizeImage > var || isForceToBigTIFF || isBtiff == true)   isBtiff=true;
 else   isBtiff=false;
  if (writeHeader) {
    this.streamMetadata=null;
    if (sm != null) {
      this.streamMetadata=(TIFFStreamMetadata)convertStreamMetadata(sm,param);
    }
    if (this.streamMetadata == null) {
      this.streamMetadata=(TIFFStreamMetadata)getDefaultStreamMetadata(param);
    }
    writeHeader();
    if (!isBtiff) {
      stream.seek(headerPosition + 4);
      nextSpace=(nextSpace + 3) & ~0x3;
      stream.writeInt((int)nextSpace);
    }
 else {
      stream.seek(headerPosition + 8);
      nextSpace=(nextSpace + 7) & ~0x7;
      stream.writeLong(nextSpace);
    }
  }
  this.imageMetadata=null;
  IIOMetadata im=iioimage.getMetadata();
  if (im != null) {
    if (im instanceof TIFFImageMetadata) {
      this.imageMetadata=((TIFFImageMetadata)im).getShallowClone();
    }
 else     if (Arrays.asList(im.getMetadataFormatNames()).contains(TIFFImageMetadata.nativeMetadataFormatName)) {
      this.imageMetadata=convertNativeImageMetadata(im);
    }
 else     if (im.isStandardMetadataFormatSupported()) {
      try {
        this.imageMetadata=convertStandardImageMetadata(im);
      }
 catch (      IIOInvalidTreeException e) {
      }
    }
  }
  if (this.imageMetadata == null) {
    this.imageMetadata=(TIFFImageMetadata)getDefaultImageMetadata(this.imageType,this.param);
  }
  setupMetadata(colorModel,sampleModel,destWidth,destHeight);
  compressor.setWriter(this);
  compressor.setMetadata(imageMetadata);
  compressor.setStream(stream);
  initializeScaleTables(sampleModel.getSampleSize());
  this.isBilevel=ImageUtil.isBinary(this.image.getSampleModel());
  this.isInverted=(nativePhotometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO && photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO) || (nativePhotometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO && photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO);
  this.isImageSimple=(isBilevel || (!isInverted && ImageUtil.imageIsContiguous(this.image))) && !isRescaling && sourceBands == null && periodX == 1 && periodY == 1 && colorConverter == null;
  TIFFIFD rootIFD=imageMetadata.getRootIFD();
  rootIFD.writeToStream(stream,isBtiff);
  this.nextIFDPointerPos=stream.getStreamPosition();
  if (!isBtiff) {
    stream.writeInt(0);
  }
 else {
    stream.writeLong(0);
  }
  long lastIFDPosition=rootIFD.getLastPosition();
  stream.seek(lastIFDPosition);
  if (lastIFDPosition > this.nextSpace) {
    this.nextSpace=lastIFDPosition;
  }
  if (!writeData) {
    return;
  }
  long stripOrTileByteCountsPosition=rootIFD.getStripOrTileByteCountsPosition();
  long stripOrTileOffsetsPosition=rootIFD.getStripOrTileOffsetsPosition();
  this.totalPixels=tileWidth * tileLength * tilesDown* tilesAcross;
  this.pixelsDone=0;
  for (int tj=0; tj < tilesDown; tj++) {
    for (int ti=0; ti < tilesAcross; ti++) {
      long pos=stream.getStreamPosition();
      Rectangle tileRect=new Rectangle(sourceXOffset + ti * tileWidth * periodX,sourceYOffset + tj * tileLength * periodY,tileWidth * periodX,tileLength * periodY);
      try {
        int byteCount=writeTile(tileRect,compressor);
        if (pos + byteCount > nextSpace) {
          nextSpace=pos + byteCount;
        }
        pixelsDone+=tileRect.width * tileRect.height;
        processImageProgress(100.0F * pixelsDone / totalPixels);
        stream.mark();
        stream.seek(stripOrTileOffsetsPosition);
        if (!isBtiff) {
          stream.writeInt((int)pos);
          stripOrTileOffsetsPosition+=4;
          stream.seek(stripOrTileByteCountsPosition);
          stream.writeInt(byteCount);
          stripOrTileByteCountsPosition+=4;
        }
 else {
          stream.writeLong(pos);
          stripOrTileOffsetsPosition+=8;
          stream.seek(stripOrTileByteCountsPosition);
          stream.writeLong(byteCount);
          stripOrTileByteCountsPosition+=8;
        }
        stream.reset();
      }
 catch (      IOException e) {
        throw new IIOException("String_Node_Str",e);
      }
      if (abortRequested()) {
        processWriteAborted();
        return;
      }
    }
  }
  processImageComplete();
}