image-rs / image-tiff

TIFF decoding and encoding library in pure Rust
MIT License
120 stars 78 forks source link

TIFF files with unassociated extra samples are unsupported. #183

Open Aerocatia opened 2 years ago

Aerocatia commented 2 years ago

TIFF files with 2 samples per pixel (Luminance + Alpha) are unsupported by this library. Such TIFF files are the default output of imagemagick if the input is grayscale.

Attached is an example. example_tiff.zip

fintelia commented 2 years ago

Do you know where specifically the error is coming from? GrayA is listed as a supported color type and I believe that most of the code doesn't care how many samples per pixel there are.

HeroicKatora commented 2 years ago

It seems, imagemagick does not encode it as a truly alpha component but as a mask:

$ tiffinfo *
TIFF Directory at offset 0x80008 (524296)
  Image Width: 512 Image Length: 512
  Resolution: 1, 1 (unitless)
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: min-is-black
  Extra Samples: 1<unassoc-alpha>
  FillOrder: msb-to-lsb
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 2
  Rows/Strip: 512
  Planar Configuration: single image plane
  Page Number: 0-1
  White Point: 0.3127-0.329
  PrimaryChromaticities: 0.640000,0.330000,0.300000,0.600000,0.150000,0.060000

This is probably intended in this case (pistol_scope_mask.tif) but quite possibly the reason that it's not commonly encountered or reported as such.

The library doesn't guess the meaning of extra channels and unassociated extra samples aren't supported.

sophie-h commented 3 months ago

I guess this is the same issue?

Format error decoding Tiff: Format error: invalid JPEG format: Invalid number of channels (4) for RGB data

If so, is the solution to change the check to "at least 3 channels" and just use the first 3 channels?

Triggered by Transparency_example.tiff generated via https://www.freeconvert.com

Downstream issue https://gitlab.gnome.org/sophie-h/glycin/-/issues/78

sophie-h commented 3 months ago
$ tiffinfo Transparency_example.tiff 
=== TIFF directory 0 ===
TIFF Directory at offset 0x8 (8)
  Image Width: 194 Image Length: 68
  Bits/Sample: 8
  Compression Scheme: JPEG
  Photometric Interpretation: RGB color
  Extra Samples: 1<unassoc-alpha>
  FillOrder: msb-to-lsb
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 4
  Rows/Strip: 68
  Planar Configuration: single image plane
  Page Number: 0-1
  White Point: 0.3127-0.329
  PrimaryChromaticities: 0.640000,0.330000,0.300000,0.600000,0.150000,0.060000
  JPEG Tables: (289 bytes)
sophie-h commented 3 months ago

Maybe we can just use SamplesPerPixel – ExtraSamples as a default and special-case (1) where the premultiplication has to be undone? (Haven't looked at the code yet.)

0 = Unspecified data

1 = Associated alpha data (with pre-multiplied color)

2 = Unassociated alpha data