image-tiff
image-tiff copied to clipboard
CCITT group 4 (Fax4) decoding support
This adds support for decoding CCIT group 4 tiff images by using the fax
crate.
I've found that the photometric interpretation tag is ignored by all the extant decoders I've found, and attempting to correctly interpret it will break some subset of images. Unfortunate, but I suspect it's best to follow the herd here?
Probably check Tag::PhotometricInterpretation
to decide what values are black and white.
OK. I've done some research and found that
- other image processors (imagemagick, Apple Preview) ignore the photometric interpretation tag and always assume that it's
WhiteIsZero
- fax4-encoded images I've found in the wild (which are all, unfortunately, confidential images of business checks) have the tag set correctly
WhiteIsZero
- imagemagick creates fax4-encoded tiffs with photometric interpretation incorrectly as
BlackIsZero
but that doesn't matter because everything that loads the files assumesWhiteIsZero
.
To conform with how everything in the wild I'd argue we just keep this hardcoded the way it is?
hahaha. Yea, I think just keep it as is.
Ideally, I'd like to see the create_reader
act more lazily. The best way would be to have incremental decoding, but the fax
crate doesn't seem to support that. An intermediate step might be to generate the list of color transitions, and then lazily expand that into the provided output buffer
I don't see a reason why incremental decoding could not be implemented.
It would be pretty easy to write something like
impl Decoder<R: Read> {
fn new(reader: R) -> Self;
fn advance(&mut self) -> Result<(), io::Error>;
fn pels(&self) -> Pels;
}
Yeah, I think an interface like that would work. Then it could be wrapped in another object that implemented Read
by tracking how far into the row had been read and expanded Pels pixel by pixel
Appreciate the feedback and all the back-and-forth here. I'm happy to help out as much as I can here to push this forward, but I don't want to step on @s3bk's feet if he's working on these changes.
@stephenjudkins I pushed changes to the fax repo. There are strange differences with the last line again. I need to re-encode my samples with libtiff to check if the error is in the sample data or my code.
But you should be able to use the code to adapt this PR to the next version.