Skip to content

Commit

Permalink
TIFF bilevel fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Perry committed Feb 24, 2021
1 parent 7259351 commit d03b598
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
11 changes: 10 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ See also INFO/Changes-ver_2 for changes released for PDF::API2, and
incorporated into PDF::Builder.
See also INFO/Changes_2020 for earlier version 3 release logs.

3.022 2021-02-12
3.022 2021-02-24

* comment out SVG mention in Builder.pm, Page.pm & POD until finished

lib/PDF/Builder/Resource/XObject/Image/TIFF/File_GT.pm,
lib/PDF/Builder/Resource/XObject/Image/TIFF_GT.pm
Some improvements to deal with inverted colors (black/white) on certain
TIFF images. Now G3 and G4 (CCITT Fax) get flipped to the correct
colors. There are still some TIFF cases which are not fully properly
dealt with; we are still working on them.

lib/PDF/Builder/Annotation.pm, examples/040_annotation,
examples/041_annot_fileattach
Add "-opacity" option to control the opacity (opaqueness) of the icon,
Expand Down
14 changes: 9 additions & 5 deletions lib/PDF/Builder/Resource/XObject/Image/TIFF/File_GT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use strict;
use warnings;

# VERSION
my $LAST_UPDATE = '3.020'; # manually update whenever code is changed
my $LAST_UPDATE = '3.022'; # manually update whenever code is changed

use IO::File;
use Graphics::TIFF 7 ':all'; # already confirmed to be installed
Expand Down Expand Up @@ -77,19 +77,23 @@ sub readTags {
if ($self->{'filter'} == COMPRESSION_NONE) { # 1
delete $self->{'filter'};
# 2 modified Huffman RLE (COMPRESSION_CCITTRLE)
} elsif ($self->{'filter'} == COMPRESSION_CCITTFAX3 || $self->{'filter'} == COMPRESSION_CCITT_T4) { # 3
} elsif ($self->{'filter'} == COMPRESSION_CCITTFAX3 ||
$self->{'filter'} == COMPRESSION_CCITT_T4) { # 3
$self->{'ccitt'} = $self->{'filter'};
$self->{'filter'} = 'CCITTFaxDecode';
} elsif ($self->{'filter'} == COMPRESSION_CCITTFAX4 || $self->{'filter'} == COMPRESSION_CCITT_T6) { # 4
} elsif ($self->{'filter'} == COMPRESSION_CCITTFAX4 ||
$self->{'filter'} == COMPRESSION_CCITT_T6) { # 4
# G4 same code as G3
$self->{'ccitt'} = $self->{'filter'};
$self->{'filter'} = 'CCITTFaxDecode';
} elsif ($self->{'filter'} == COMPRESSION_LZW) { # 5
$self->{'filter'} = 'LZWDecode';
} elsif ($self->{'filter'} == COMPRESSION_OJPEG || $self->{'filter'} == COMPRESSION_JPEG) { # 6 JPEG is 'new' JPEG?
} elsif ($self->{'filter'} == COMPRESSION_OJPEG ||
$self->{'filter'} == COMPRESSION_JPEG) { # 6 JPEG is 'new' JPEG?
$self->{'filter'} = 'DCTDecode';
# 7 'new' JPEG
} elsif ($self->{'filter'} == COMPRESSION_ADOBE_DEFLATE || $self->{'filter'} == COMPRESSION_DEFLATE) { # 8 same? see 32946
} elsif ($self->{'filter'} == COMPRESSION_ADOBE_DEFLATE ||
$self->{'filter'} == COMPRESSION_DEFLATE) { # 8 same? see 32946
$self->{'filter'} = 'FlateDecode';
# 9 T.85
# 10 T.43
Expand Down
29 changes: 11 additions & 18 deletions lib/PDF/Builder/Resource/XObject/Image/TIFF_GT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use warnings;
#no warnings 'uninitialized';

# VERSION
my $LAST_UPDATE = '3.021'; # manually update whenever code is changed
my $LAST_UPDATE = '3.022'; # manually update whenever code is changed

use Compress::Zlib;

Expand Down Expand Up @@ -129,7 +129,7 @@ sub new {
$tif->close();

return $self;
}
} # end of new()

=item $mode = $tif->usesLib()
Expand All @@ -152,6 +152,7 @@ sub usesLib {
return $self->{'usesGT'}->val();
}

# non-CCITT compression methods
sub handle_generic {
my ($self, $pdf, $tif, %opts) = @_;
my ($stripcount, $buffer);
Expand All @@ -164,6 +165,10 @@ sub handle_generic {
$dict->{'BitsPerComponent'} = PDFNum($tif->{'bitsPerSample'});
$dict->{'Colors'} = PDFNum($tif->{'colorSpace'} eq 'DeviceGray'?1 :3);

## uncompressed needs to be flipped
#if (!defined $tif->{'filter'}) {
# $self->{'Decode'} = PDFArray(PDFNum(1), PDFNum(0));
#}
$stripcount = $tif->{'object'}->NumberOfStrips();
$buffer = '';
for my $i (0 .. $stripcount - 1) {
Expand Down Expand Up @@ -586,9 +591,10 @@ sub handle_ccitt {
$decode->{'K'} = (($tif->{'ccitt'} == 4 || (defined $tif->{'g3Options'} && $tif->{'g3Options'} & 0x1))? PDFNum(-1): PDFNum(0));
$decode->{'Columns'} = PDFNum($tif->{'imageWidth'});
$decode->{'Rows'} = PDFNum($tif->{'imageHeight'});
# not sure why whiteIsZero needs to be flipped around???
$decode->{'BlackIs1'} = PDFBool($tif->{'whiteIsZero'} == 0? 1: 0);
$decode->{'BlackIs1'} = PDFBool($tif->{'whiteIsZero'} == 1? 1: 0);
$decode->{'DamagedRowsBeforeError'} = PDFNum(100);
# all CCITT Fax need to flip black/white
$self->{'Decode'} = PDFArray(PDFNum(1), PDFNum(0));

# g3Options bit 0 = 0 for 1-Dimensional, = 1 for 2-Dimensional MR
# aka T4Options bit 1 = 0 (compressed data only)
Expand Down Expand Up @@ -640,12 +646,6 @@ sub handle_ccitt {
sub read_tiff {
my ($self, $pdf, $tif, %opts) = @_;

# not sure why blackIsZero needs to be flipped around???
if (defined $tif->{'blackIsZero'}) {
$tif->{'blackIsZero'} = $tif->{'blackIsZero'} == 1? 0: 1;
$tif->{'whiteIsZero'} = $tif->{'blackIsZero'} == 1? 0: 1;
}

$self->width($tif->{'imageWidth'});
$self->height($tif->{'imageHeight'});

Expand All @@ -669,19 +669,12 @@ sub read_tiff {
$self->{'Interpolate'} = PDFBool(1);
$self->bits_per_component($tif->{'bitsPerSample'});

# swaps 0 and 1 ([0 1] -> [1 0]) in certain cases
if (($tif->{'whiteIsZero'}||0) == 1 &&
($tif->{'filter'}||'') ne 'CCITTFaxDecode') {
$self->{'Decode'} = PDFArray(PDFNum(1), PDFNum(0));
}

## debug dump $tif
#foreach (sort keys %$tif) {
# if (defined $tif->{$_}) {
# print "\$tif->{'$_'} = '$tif->{$_}'\n";
# } else {
# print "\$tif->{'$_'} = ?\n";
# }
#}
# check filters and handle separately
if (defined $tif->{'filter'} and $tif->{'filter'} eq 'CCITTFaxDecode') {
$self->handle_ccitt($pdf, $tif, %opts);
Expand Down

0 comments on commit d03b598

Please sign in to comment.