dragon66 / icafe

Java library for reading, writing, converting and manipulating images and metadata
Eclipse Public License 1.0
204 stars 58 forks source link

TIFFReader damages TIFF file when the BufferedImage content is accessed. #66

Closed llech closed 6 years ago

llech commented 6 years ago

Actual behaviour:

I've created a simple test for reading the content of the TIFF:

    TIFFReader reader = new TIFFReader();
    FileInputStream input = new FileInputStream(file);

    reader.read(input);
    input.close();
    int i = 1;
    for (BufferedImage bi : reader.getFrames()) {
      final int imageWidth = bi.getWidth();
      final int imageHeight = bi.getHeight();
      System.out.println("Next TIFF page found, width: "+imageWidth+", height: "+imageHeight);
      File target = new File("target/tiff-frame"+i+".jpg");
      BufferedImage copyImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
      Graphics2D g2d= copyImage.createGraphics();
      g2d.drawImage(bi, 0, 0, null);
      g2d.dispose();
      ImageIO.write(copyImage, "jpeg", file);
      i++;
    }

This code invokes no operations modifying the content of original file, however after invoking it, the content of the original file was destroyed. All frames were replaced with black background.

Expected behaviour:

The TIFFReader reads the file content, and not modifies it in any way, especially the destructive one.

dragon66 commented 6 years ago

@llech I believe the issue you found is because you write to the same image file from which you read the image.

Note: TIFFReader is not functioning completely and if it failed to read some image, the result is a all-black blob but this is not the cause of the corruption for the original image.

Wen