title: GIF image becomes wrong after ImageIO read() and write() operations
body: <p>I have this code. It simply reads a GIF file, redraws it with background and output to a new GIF file.</p>
  
  <p>The problem is the result file becomes strange. I have no idea why it becomes poor quality. The problem doesn't happen on JPG files. How to fix it?</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics2D;
  import java.awt.Rectangle;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  import javax.imageio.ImageIO;
  
  public class ImageTest {
  
      public static void main(String[] args) {
          f();
      }
  
      private static final String EXTENSION = "gif";
      private static final String FILENAME = "pinkHeart";
      private static final String PATH = "/Users/hieugioi/Downloads/";
  
      public static void f() {
          File file = new File(PATH + FILENAME + "." + EXTENSION);
  
          try {
              final BufferedImage originalImage = ImageIO.read(file);
  
              int imageType = getImageType(originalImage);
              final BufferedImage buff = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), imageType);
              final Graphics2D g = buff.createGraphics();
  
              Color backgroundColor = Color.GRAY;
              g.setColor(backgroundColor);
              g.fill(new Rectangle(0, 0, buff.getWidth(), buff.getHeight()));
              g.drawImage(originalImage, null, 0, 0);
  
              File out = new File(PATH + FILENAME + "Out." + EXTENSION);
              ImageIO.write(buff, EXTENSION, out);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public static int getImageType(BufferedImage img) {
          int imageType = img.getType();
          if (imageType == BufferedImage.TYPE_CUSTOM) {
              if (img.getAlphaRaster() != null) {
                  imageType = BufferedImage.TYPE_INT_ARGB_PRE;
              } else {
                  imageType = BufferedImage.TYPE_INT_RGB;
              }
          } else if (imageType == BufferedImage.TYPE_BYTE_INDEXED &amp;&amp; img.getColorModel().hasAlpha()) {
              imageType = BufferedImage.TYPE_INT_ARGB_PRE;
          }
          return imageType;
      }
  }
  </code></pre>
  
  <p>Input image (pinkHeart.gif):</p>
  
  <p><a href="https://i.stack.imgur.com/Zvwuq.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Zvwuq.gif" alt="enter image description here"></a></p>
  
  <p>Output image (pinkHeartOut.gif):</p>
  
  <p><a href="https://i.stack.imgur.com/ItpiZ.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ItpiZ.gif" alt="enter image description here"></a></p>
  
  <p><strong>Update case 2</strong></p>
  
  <p>Input image (example.gif):</p>
  
  <p><a href="https://i.stack.imgur.com/slEVb.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/slEVb.gif" alt="enter image description here"></a></p>
  
  <p>Output image (exampleOut.gif): The output's yellow totally disappears!</p>
  
  <p><a href="https://i.stack.imgur.com/q7nub.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q7nub.gif" alt="enter image description here"></a></p>