Program: 154104
Project: 154104
Buggy source:
/** 
 * Encodes a RenderedImage and writes the output to the OutputStream associated with this ImageEncoder.
 */
public void encode(RenderedImage im) throws IOException {
  SampleModel sampleModel=im.getSampleModel();
  ColorModel colorModel=im.getColorModel();
  int numBands=colorModel.getNumColorComponents();
  int transType=sampleModel.getTransferType();
  if (((transType != DataBuffer.TYPE_BYTE) && !CodecUtils.isPackedByteImage(im)) || ((numBands != 1) && (numBands != 3))) {
    throw new RuntimeException(JaiI18N.getString("String_Node_Str"));
  }
  int cspaceType=colorModel.getColorSpace().getType();
  if (cspaceType != ColorSpace.TYPE_GRAY && cspaceType != ColorSpace.TYPE_RGB) {
    throw new Error(JaiI18N.getString("String_Node_Str"));
  }
  BufferedImage bi;
  if (im instanceof BufferedImage) {
    bi=(BufferedImage)im;
  }
 else {
    Raster ras;
    if (im.getNumXTiles() == 1 && im.getNumYTiles() == 1) {
      ras=im.getTile(im.getMinTileX(),im.getMinTileY());
    }
 else {
      ras=im.getData();
    }
    WritableRaster wRas;
    if (ras instanceof WritableRaster) {
      wRas=(WritableRaster)ras;
    }
 else {
      wRas=Raster.createWritableRaster(ras.getSampleModel(),ras.getDataBuffer(),new Point(ras.getSampleModelTranslateX(),ras.getSampleModelTranslateY()));
    }
    if (wRas.getMinX() != 0 || wRas.getMinY() != 0 || wRas.getWidth() != im.getWidth() || wRas.getHeight() != im.getHeight())     wRas=wRas.createWritableChild(wRas.getMinX(),wRas.getMinY(),im.getWidth(),im.getHeight(),0,0,null);
    bi=new BufferedImage(colorModel,wRas,false,null);
  }
  if (colorModel instanceof IndexColorModel) {
    IndexColorModel icm=(IndexColorModel)colorModel;
    bi=icm.convertToIntDiscrete(bi.getRaster(),false);
  }
  com.sun.image.codec.jpeg.JPEGEncodeParam j2dEP=com.sun.image.codec.jpeg.JPEGCodec.getDefaultJPEGEncodeParam(bi);
  if (jaiEP != null) {
    modifyEncodeParam(jaiEP,j2dEP,numBands);
  }
  com.sun.image.codec.jpeg.JPEGImageEncoder encoder;
  encoder=com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(output,j2dEP);
  try {
    encoder.encode(bi);
  }
 catch (  IOException e) {
    String message=JaiI18N.getString("String_Node_Str");
    ImagingListenerProxy.errorOccurred(message,new ImagingException(message,e),this,false);
  }
}

Fixed version (ground truth):
/** 
 * Encodes a RenderedImage and writes the output to the OutputStream associated with this ImageEncoder.
 */
public void encode(RenderedImage im) throws IOException {
  SampleModel sampleModel=im.getSampleModel();
  ColorModel colorModel=im.getColorModel();
  int numBands=colorModel.getNumColorComponents();
  int transType=sampleModel.getTransferType();
  if (((transType != DataBuffer.TYPE_BYTE) && !CodecUtils.isPackedByteImage(im)) || ((numBands != 1) && (numBands != 3))) {
    throw new RuntimeException(JaiI18N.getString("String_Node_Str"));
  }
  int cspaceType=colorModel.getColorSpace().getType();
  if (cspaceType != ColorSpace.TYPE_GRAY && cspaceType != ColorSpace.TYPE_RGB) {
    throw new Error(JaiI18N.getString("String_Node_Str"));
  }
  BufferedImage bi;
  if (im instanceof BufferedImage) {
    bi=(BufferedImage)im;
  }
 else {
    Raster ras;
    if (im.getNumXTiles() == 1 && im.getNumYTiles() == 1) {
      ras=im.getTile(im.getMinTileX(),im.getMinTileY());
    }
 else {
      ras=im.getData();
    }
    WritableRaster wRas;
    if (ras instanceof WritableRaster) {
      wRas=(WritableRaster)ras;
    }
 else {
      wRas=Raster.createWritableRaster(ras.getSampleModel(),ras.getDataBuffer(),new Point(ras.getSampleModelTranslateX(),ras.getSampleModelTranslateY()));
    }
    if (wRas.getMinX() != 0 || wRas.getMinY() != 0 || wRas.getWidth() != im.getWidth() || wRas.getHeight() != im.getHeight())     wRas=wRas.createWritableChild(wRas.getMinX(),wRas.getMinY(),im.getWidth(),im.getHeight(),0,0,null);
    bi=new BufferedImage(colorModel,wRas,false,null);
  }
  if (colorModel instanceof IndexColorModel) {
    IndexColorModel icm=(IndexColorModel)colorModel;
    bi=icm.convertToIntDiscrete(bi.getRaster(),false);
    if (bi.getSampleModel().getNumBands() == 4) {
      WritableRaster rgbaRas=bi.getRaster();
      WritableRaster rgbRas=rgbaRas.createWritableChild(0,0,bi.getWidth(),bi.getHeight(),0,0,new int[]{0,1,2});
      PackedColorModel pcm=(PackedColorModel)bi.getColorModel();
      int bits=pcm.getComponentSize(0) + pcm.getComponentSize(1) + pcm.getComponentSize(2);
      DirectColorModel dcm=new DirectColorModel(bits,pcm.getMask(0),pcm.getMask(1),pcm.getMask(2));
      bi=new BufferedImage(dcm,rgbRas,false,null);
    }
  }
  com.sun.image.codec.jpeg.JPEGEncodeParam j2dEP=com.sun.image.codec.jpeg.JPEGCodec.getDefaultJPEGEncodeParam(bi);
  if (jaiEP != null) {
    modifyEncodeParam(jaiEP,j2dEP,numBands);
  }
  com.sun.image.codec.jpeg.JPEGImageEncoder encoder;
  encoder=com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(output,j2dEP);
  try {
    encoder.encode(bi);
  }
 catch (  IOException e) {
    String message=JaiI18N.getString("String_Node_Str");
    ImagingListenerProxy.errorOccurred(message,new ImagingException(message,e),this,false);
  }
}