Builder/Resource/XObject/Image/TIFF/File_GT.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 162 CC 20 SCAR 49.6
CC
20
Cov
0%
readTags
240.0
new
42.0
close
2.0
CRAP 143.1
1 package PDF::Builder::Resource::XObject::Image::TIFF::File_GT;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '3.028'; # VERSION
7 our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
8  
9 use IO::File;
10 use Graphics::TIFF ':all'; # already confirmed to be installed
11  
12 =head1 NAME
13  
14 PDF::Builder::Resource::XObject::Image::TIFF::File_GT - Support routines for TIFF image library (Graphics::TIFF enabled)
15  
16 =head1 METHODS
17  
18 =head2 new
19  
20 PDF::Builder::Resource::XObject::Image::TIFF::File_GT->new()
21  
22 =over
23  
24 Create an image object from TIFF input, using the Graphics::TIFF library.
25 Remember to use the Builder.pm method image_tiff for this functionality.
26  
27 =back
28  
29 =cut
30  
31 sub new {
32 my ($class, $file) = @_;
33  
34 my $self = {};
35 bless ($self, $class);
36 my $file_is_temp = 0;
37 if (ref($file)) {
38 # if a file was requested, it wasn't found. if a GLOB (opened
39 # filehandle), try outputting to a temp file and using that
40 if (ref($file) eq 'GLOB') {
41 # treat $file as an open filehandle
42 local $/;
43 use File::Temp qw/ tempfile /;
44 my $tempdata = <$file>;
45 my ($fh, $tempname) = tempfile();
46 binmode($fh);
47 print $fh $tempdata;
48 close($fh);
49 $file->seek(0, 0);
50 $file_is_temp = 1;
51 $file = $tempname; # presumably existing $file no longer needed
52 } else {
53 # presumably requested a real file, but couldn't find it
54 die "Error: unknown handle type ".ref($file)."\n";
55 }
56 } else {
57 # presumably a file path sent in. see if exists and can read
58 if (! -r $file) {
59 die "Error: requested TIFF file $file not found";
60 }
61 }
62 $self->{'object'} = Graphics::TIFF->Open($file, 'r');
63 $self->readTags();
64 # if temporary file, get rid of it
65 if ($file_is_temp) {
66 $file =~ s#\\#/#g; # on Windows, \ separators may cause problems
67 if (!unlink($file)) {
68 print "Note: failed to erase temporary file $file\n";
69 }
70 }
71  
72 return $self;
73 }
74  
75 sub close { ## no critic
76 my $self = shift;
77  
78 $self->{'object'}->Close();
79 delete $self->{'object'};
80 return;
81 }
82  
83 # presumably this is handling byte order and bit order OK (for tags). we
84 # need to look at whether anything in the strip data needs bit/byte swapping.
85 #
86 # see list in Graphics::TIFF.pm
87 # not all of these are used, but PDF::Builder apps may use them
88 sub readTags {
89 my $self = shift;
90  
91 # pixels per row (number of columns)
92 $self->{'imageWidth'} = $self->{'object'}->GetField(TIFFTAG_IMAGEWIDTH);
93 # number of rows of pixels (height). DO NOT CONFUSE with imageLength!
94 $self->{'imageHeight'} = $self->{'object'}->GetField(TIFFTAG_IMAGELENGTH);
95  
96 # strips per image is floor(imageLength+RowsPerStrip-1/RowsPerStrip)
97 # calculated by method NumberOfStrips()
98 $self->{'RowsPerStrip'} = $self->{'object'}->GetField(TIFFTAG_ROWSPERSTRIP);
99 # TIFFTAG_PLANARCONFIGURATION = 1 if pixel components stored contiguously,
100 # = 2 if pixel components stored in sep. planes
101 # N = STRIPS PER IMAGE if PLANAR CONFIGURATION = 1
102 # multiply by SAMPLES PER PIXEL for 2
103 # offset is bytes from beginning of file (ReadRawStrip should use)
104 $self->{'imageOffset'} = $self->{'object'}->GetField(TIFFTAG_STRIPOFFSETS);
105 # STRIP BYTE COUNTS is bytes of compressed data per strip
106 # do NOT confuse with imageHeight! same N as imageOffset
107 $self->{'imageLength'} = $self->{'object'}->GetField(TIFFTAG_STRIPBYTECOUNTS);
108  
109 # ------ describe each pixel
110 # components per pixel (e.g., 1 for mono/gray/palette, 3 for RGB)
111 $self->{'SamplesPerPixel'} = $self->{'object'}->GetField(TIFFTAG_SAMPLESPERPIXEL);
112 # extra components, such as opacity/alpha
113 $self->{'ExtraSamples'} = $self->{'object'}->GetField(TIFFTAG_EXTRASAMPLES);
114 # bits per component (R, G, B, and A can have different number of bits)
115 # N (Samples per Pixel) components
116 $self->{'bitsPerSample'} = $self->{'object'}->GetField(TIFFTAG_BITSPERSAMPLE);
117 # 1 (common) most sig bit to least, 2 (rare) least sig bit to most (flip!)
118 $self->{'fillOrder'} = $self->{'object'}->GetField(TIFFTAG_FILLORDER);
119  
120 # ------ compression method
121 $self->{'filter'} = $self->{'object'}->GetField(TIFFTAG_COMPRESSION);
122 if ($self->{'filter'} == COMPRESSION_NONE) { # 1
123 delete $self->{'filter'};
124 # 2 modified Huffman RLE (COMPRESSION_CCITTRLE)
125 } elsif ($self->{'filter'} == COMPRESSION_CCITTFAX3 ||
126 $self->{'filter'} == COMPRESSION_CCITT_T4) { # 3
127 $self->{'ccitt'} = $self->{'filter'};
128 $self->{'filter'} = 'CCITTFaxDecode';
129 } elsif ($self->{'filter'} == COMPRESSION_CCITTFAX4 ||
130 $self->{'filter'} == COMPRESSION_CCITT_T6) { # 4
131 # G4 same code as G3
132 $self->{'ccitt'} = $self->{'filter'};
133 $self->{'filter'} = 'CCITTFaxDecode';
134 } elsif ($self->{'filter'} == COMPRESSION_LZW) { # 5
135 $self->{'filter'} = 'LZWDecode';
136 } elsif ($self->{'filter'} == COMPRESSION_OJPEG ||
137 $self->{'filter'} == COMPRESSION_JPEG) { # 6 JPEG is 'new' JPEG?
138 $self->{'filter'} = 'DCTDecode';
139 # 7 'new' JPEG
140 } elsif ($self->{'filter'} == COMPRESSION_ADOBE_DEFLATE ||
141 $self->{'filter'} == COMPRESSION_DEFLATE) { # 8 same? see 32946
142 $self->{'filter'} = 'FlateDecode';
143 # 9 T.85
144 # 10 T.43
145 # 32766 COMPRESSION_NEXT
146 # 32771 COMPRESSION_CCITTRLEW
147 } elsif ($self->{'filter'} == COMPRESSION_PACKBITS) { # 32773
148 $self->{'filter'} = 'RunLengthDecode';
149 # 32809 COMPRESSION_THUNDERSCAN
150 # 32895 COMPRESSION_IT8CTPAD
151 # 32896 COMPRESSION_IT8LW
152 # 32897 COMPRESSION_IT8MP
153 # 32898 COMPRESSION_IT8BL
154 # 32908 COMPRESSION_PIXARFILM
155 # 32909 COMPRESSION_PIXARLOG
156 # 32946 COMPRESSION_DEFLATE
157 # 32947 COMPRESSION_DCS
158 # 34661 COMPRESSION_JBIG
159 # 34676 COMPRESSION_SGILOG
160 # 34677 COMPRESSION_SGILOG24
161 # 34712 COMPRESSION_JP2000
162 } else {
163 die "Unknown/unsupported TIFF compression method with id '".$self->{'filter'}."'.\n";
164 }
165  
166 # ------ interpretation of color values
167 $self->{'colorSpace'} = $self->{'object'}->GetField(TIFFTAG_PHOTOMETRIC);
168 if ($self->{'colorSpace'} == PHOTOMETRIC_MINISWHITE) { # 0=WhiteIsZero
169 $self->{'colorSpace'} = 'DeviceGray';
170 $self->{'whiteIsZero'} = 1;
171 $self->{'blackIsZero'} = 0;
172 } elsif ($self->{'colorSpace'} == PHOTOMETRIC_MINISBLACK) { # 1=BlackIsZero
173 $self->{'colorSpace'} = 'DeviceGray';
174 $self->{'blackIsZero'} = 1;
175 $self->{'whiteIsZero'} = 0;
176 } elsif ($self->{'colorSpace'} == PHOTOMETRIC_RGB) { # 2=RGB in that order
177 $self->{'colorSpace'} = 'DeviceRGB';
178 } elsif ($self->{'colorSpace'} == PHOTOMETRIC_PALETTE) { # 3=palette index
179 $self->{'colorSpace'} = 'Indexed';
180 #} elsif ($self->{'colorSpace'} == PHOTOMETRIC_MASK) { # 4=transparency mask
181 # $self->{'colorSpace'} = 'TransMask';
182 } elsif ($self->{'colorSpace'} == PHOTOMETRIC_SEPARATED) { # 5=(us.) CMYK
183 $self->{'colorSpace'} = 'DeviceCMYK';
184 } elsif ($self->{'colorSpace'} == PHOTOMETRIC_YCBCR) { # 6=YCbCr
185 $self->{'colorSpace'} = 'DeviceRGB';
186 } elsif ($self->{'colorSpace'} == PHOTOMETRIC_CIELAB) { # 8=CIE L*a*b
187 $self->{'colorSpace'} = 'Lab';
188 # 9=CIE L*a*b alternate encoding (ICC)
189 # 10=CIE L*a*b alternate encoding (ITU-T Rec T.42)
190 # 32803=CFA
191 # 32844=Pixar LogL
192 # 32845=Pixar LogLuv
193 # 34892=LinearRaw
194 } else {
195 die "Unknown/unsupported TIFF photometric interpretation with id '".$self->{'colorSpace'}."'.\n";
196 }
197  
198 # TIFFTAG_THRESHHOLDING force grays to black or white
199 # TIFFTAG_CELLWIDTH dithering or halftoning matrix cell width
200 # TIFFTAG_CELLLENGTH dithering or halftoning matrix cell height
201 # palette RGB table definition
202 $self->{'colorMap'} = [ $self->{'object'}->GetField(TIFFTAG_COLORMAP) ];
203 # TIFFTAG_GRAYRESPONSEUNIT describe integer->float mapping
204 # TIFFTAG_GRAYRESPONSECURVE optical density of Gray curve at each point
205  
206 $self->{'imageDescription'} = $self->{'object'}->GetField(TIFFTAG_IMAGEDESCRIPTION);
207 # TIFFTAG_MAKE scanner or camera manufacturer
208 # TIFFTAG_MODEL scanner or camera model or name
209 # TIFFTAG_SOFTWARE name/version of softwar that produced TIFF
210 # TIFFTAG_DATETIME YYYY:MM:DD HH:MM:SS\0 string, creation timestamp
211 # TIFFTAG_ARTIST who created the image (was used for copyright, too)
212 # TIFFTAG_COPYRIGHT full copyright statement for the image
213 # TIFFTAG_HOSTCOMPUTER describe computer/OS used to create image
214  
215 $self->{'xRes'} = $self->{'object'}->GetField(TIFFTAG_XRESOLUTION);
216 $self->{'yRes'} = $self->{'object'}->GetField(TIFFTAG_YRESOLUTION);
217 $self->{'resUnit'} = $self->{'object'}->GetField(TIFFTAG_RESOLUTIONUNIT);
218 # TIFFTAG_ORIENTATION = 1 data starts at top left
219 # may also be known as T4Options
220 $self->{'g3Options'} = $self->{'object'}->GetField(TIFFTAG_GROUP3OPTIONS);
221 # may also be known as T6Options or Group4Options
222 $self->{'g4Options'} = $self->{'object'}->GetField(TIFFTAG_GROUP4OPTIONS);
223  
224 $self->{'Predictor'} = $self->{'object'}->GetField(TIFFTAG_PREDICTOR);
225 $self->{'imageId'} = $self->{'object'}->GetField(TIFFTAG_OPIIMAGEID);
226 # TIFFTAG_SUBFILETYPE = 3 bits for various subfiles within image file
227 # TIFFTAG_OSUBFILETYPE = 3 values concerning image types
228 # TIFFTAG_MINSAMPLEVALUE = SamplesPerPixel min value
229 # TIFFTAG_MAXSAMPLEVALUE = SamplesPerPixel max value
230 # TIFFTAG_FREEOFFSETS (ignore, early memory management attempt)
231 # TIFFTAG_FREEBYTECOUNTS (ignore, early memory mmanagement attempt)
232  
233 return $self;
234 }
235  
236 1;