Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: make the tiff encoder spec conformant to avoid corrupted TIFF on MacOS #708

Open
stephane-archer opened this issue Jan 3, 2025 · 8 comments

Comments

@stephane-archer
Copy link

stephane-archer commented Jan 3, 2025

This image is 3840 × 2160:
testHorizontal.zip

The copyResize function silently produces corrupted TIFF output when resizing an image to a height that is not a multiple of the original height (e.g., 1234). However, it works correctly for heights like 720 or 1024.

    var resizeImage = img.copyResize(refImageCompatibleImage, height: 1234);
    img.Image editedImage = img.adjustColor(
      resizeImage,
      brightness: brightness,
    );
    Uint8List editedImageEncoded = img.encodeTiff(editedImage);
    File editedImageFile = await getNewTempFile("EditedImage", ".tiff");
    await editedImageFile.create(exclusive: true);
    await editedImageFile.writeAsBytes(editedImageEncoded);
    return editedImageFile.path;

Issues

  • Silent Failure: The function does not throw an error for unsupported heights.
  • Unexpected Behavior: The function should resize to any height, or document constraints and enforce them via exceptions.
@brendan-duncan
Copy link
Owner

I'm not seeing the error or corruption from copyResize. I ran a test on your image using

      final img =
      decodeJpg(File('test/_data/jpg/testHorizontal.jpg').readAsBytesSync())!;
      final i0 = copyResize(img, height: 1234);
      final editedImage = adjustColor(
        i0,
        brightness: 0.75,
      );
      expect(i0.width, equals(2194));
      expect(i0.height, equals(1234));
      File('$testOutputPath/transform/copyResize_jpg.tif')
        ..createSync(recursive: true)
        ..writeAsBytesSync(encodeTiff(editedImage));
    });

And that produced this TIFF,
copyResize_jpg.zip

@stephane-archer
Copy link
Author

stephane-archer commented Jan 4, 2025

@brendan-duncan the tiff produced that you linked can't be read in MacOS and result in error when displaying using flutter
Screenshot 2025-01-04 at 05 07 07
Screenshot 2025-01-04 at 05 09 46

@stephane-archer
Copy link
Author

stephane-archer commented Jan 4, 2025

I don't know what makes this tiff incompatible with MacOS, is there anything special with that tiff? Could it be an error in the files or a structure issue in the file? Bit Depth? Color Profile? Metadata?

@stephane-archer
Copy link
Author

there seems to be error in the produced file:

identify -verbose copyResize_jpg.tif
identify: Invalid TIFF directory; tags are not sorted in ascending order. `TIFFReadDirectoryCheckOrder' @ warning/tiff.c/TIFFWarnings/939.
identify: Nonstandard tile width 2194, convert file. `copyResize_jpg.tif' @ warning/tiff.c/TIFFWarnings/939.
identify: Nonstandard tile length 1234, convert file. `copyResize_jpg.tif' @ warning/tiff.c/TIFFWarnings/939.
identify: Nonstandard tile width 2194, convert file. `copyResize_jpg.tif' @ warning/tiff.c/TIFFWarnings/939.
identify: Nonstandard tile length 1234, convert file. `copyResize_jpg.tif' @ warning/tiff.c/TIFFWarnings/939.

@stephane-archer
Copy link
Author

tiffinfo copyResize_jpg.tif
TIFFReadDirectoryCheckOrder: Warning, Invalid TIFF directory; tags are not sorted in ascending order.
copyResize_jpg.tif: Warning, Nonstandard tile width 2194, convert file.
copyResize_jpg.tif: Warning, Nonstandard tile length 1234, convert file.

@stephane-archer
Copy link
Author

  • The warning Invalid TIFF directory; tags are not sorted in ascending order indicates that the TIFF file's tags are not in the standard order. macOS Preview may have stricter requirements for properly ordered tags compared to other platforms.
  • The warnings Nonstandard tile width 2194 and Nonstandard tile length 1234 suggest that the file uses non-standard tile sizes. These might not be handled correctly by macOS's TIFF reader, as standard tiles are typically powers of 2 (e.g., 256x256).

The last point is most likely the main issue and could explain why some resolutions work for copyResize.

@brendan-duncan
Copy link
Owner

I'll take a look at the TIFF encoder on macOS as soon as I get a chance. TIFF is such an awful format. It can do everything, but the problem is that it can do everything and no one can agree on what it should do.

@brendan-duncan
Copy link
Owner

According to libtiff, http://www.simplesystems.org/libtiff/functions/TIFFtile.html, tileWidth and tileHeight have to be a multiple of 16 to be spec conformant. I'll look into making that happen.

@stephane-archer stephane-archer changed the title copyResize Produces Corrupted Output for Arbitrary Heights make the tiff encoder spec conformant to avoid corrupted TIFF on MacOS Jan 6, 2025
@stephane-archer stephane-archer changed the title make the tiff encoder spec conformant to avoid corrupted TIFF on MacOS Bug: make the tiff encoder spec conformant to avoid corrupted TIFF on MacOS Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants