| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package MAS::TIFF::Field; | ||
| 5 | |||
| 6 | # http://www.fileformat.info/format/tiff/egff.htm | ||
| 7 | my %tags = ( | ||
| 8 | 254 => 'NewSubFileType', | ||
| 9 | 255 => 'SubFileType', | ||
| 10 | 256 => 'ImageWidth', | ||
| 11 | 257 => 'ImageLength', # aka ImageHeight | ||
| 12 | 258 => 'BitsPerSample', | ||
| 13 | 259 => 'Compression', | ||
| 14 | 262 => 'PhotometricInterpretation', | ||
| 15 | 263 => 'Thresholding', | ||
| 16 | 264 => 'CellWidth', | ||
| 17 | 265 => 'CellLength', | ||
| 18 | 266 => 'FillOrder', | ||
| 19 | 269 => 'DocumentName', | ||
| 20 | 270 => 'ImageDescription', | ||
| 21 | 271 => 'Make', | ||
| 22 | 272 => 'Model', | ||
| 23 | 273 => 'StripOffsets', | ||
| 24 | 274 => 'Orientation', | ||
| 25 | 277 => 'SamplesPerPixel', | ||
| 26 | 278 => 'RowsPerStrip', | ||
| 27 | 279 => 'StripByteCounts', | ||
| 28 | 280 => 'MinSampleValue', | ||
| 29 | 281 => 'MaxSampleValue', | ||
| 30 | 282 => 'XResolution', | ||
| 31 | 283 => 'YResolution', | ||
| 32 | 284 => 'PlanarConfiguration', | ||
| 33 | 285 => 'PageName', | ||
| 34 | 286 => 'XPosition', | ||
| 35 | 287 => 'YPosition', | ||
| 36 | 288 => 'FreeOffsets', | ||
| 37 | 289 => 'FreeByteCounts', | ||
| 38 | 290 => 'GrayResponseUnit', | ||
| 39 | 291 => 'GrayResponseCurve', | ||
| 40 | 292 => 'T4Options', # Before TIFF 6.0, was called Group3Options | ||
| 41 | 293 => 'T6Options', # Before TIFF 6.0, was called Group3Options | ||
| 42 | 296 => 'ResolutionUnit', | ||
| 43 | 297 => 'PageNumber', | ||
| 44 | 300 => 'ColorResponseUnit', | ||
| 45 | 301 => 'TransferFunction', # Before TIFF 6.0, was called ColorResponseCurve | ||
| 46 | 305 => 'Software', | ||
| 47 | 306 => 'DateTime', | ||
| 48 | 315 => 'Artist', | ||
| 49 | 316 => 'HostComputer', | ||
| 50 | 317 => 'Predictor', | ||
| 51 | 318 => 'WhitePoint', | ||
| 52 | 319 => 'PrimaryChromaticities', | ||
| 53 | 320 => 'ColorMap', | ||
| 54 | 321 => 'HalftoneHints', | ||
| 55 | 322 => 'TileWidth', | ||
| 56 | 323 => 'TileLength', | ||
| 57 | 324 => 'TileOffsets', | ||
| 58 | 325 => 'TileByteCounts', | ||
| 59 | |||
| 60 | # TIFF Class F, not TIFF 6.0 | ||
| 61 | 326 => 'BadFaxLines', | ||
| 62 | 327 => 'CleanFaxData', | ||
| 63 | 328 => 'ConsecutiveBadFaxLines', | ||
| 64 | |||
| 65 | 332 => 'InkSet', | ||
| 66 | 333 => 'InkNames', | ||
| 67 | 334 => 'NumberOfInks', | ||
| 68 | 336 => 'DotRange', | ||
| 69 | 337 => 'TargetPrinter', | ||
| 70 | 338 => 'ExtraSamples', | ||
| 71 | 339 => 'SampleFormat', | ||
| 72 | 340 => 'SMinSampleValue', | ||
| 73 | 341 => 'SMaxSampleValue', | ||
| 74 | 342 => 'TransferRange', | ||
| 75 | 512 => 'JPEGProc', | ||
| 76 | 513 => 'JPEGInterchangeFormat', | ||
| 77 | 514 => 'JPEGInterchangeFormatLength', | ||
| 78 | 515 => 'JPEGRestartInterval', | ||
| 79 | 517 => 'JPEGLosslessPredictors', | ||
| 80 | 518 => 'JPEGPointTransforms', | ||
| 81 | 519 => 'JPEGQTables', | ||
| 82 | 520 => 'JPEGDCTTables', | ||
| 83 | 521 => 'JPEGACTTables', | ||
| 84 | 529 => 'YCbCrCoefficiets', | ||
| 85 | 530 => 'YCbCrSubSampling', | ||
| 86 | 531 => 'YCbCrPositioning', | ||
| 87 | 532 => 'ReferenceBlackAndWhite', | ||
| 88 | 700 => 'XMP', # http://www.awaresystems.be/imaging/tiff/tifftags/extension.html | ||
| 89 | 33432 => 'Copyright', | ||
| 90 | 34377 => 'Photoshop Image Resource Blocks', # http://www.digitalpreservation.gov/formats/content/tiff_tags.shtml | ||
| 91 | 34665 => 'Exif IFD', # http://www.digitalpreservation.gov/formats/content/tiff_tags.shtml | ||
| 92 | ); | ||
| 93 | |||
| 94 | sub read_from_io { | ||
| 95 | my $class = shift; | ||
| 96 | my $io = shift; | ||
| 97 | |||
| 98 | my $tag_id = $io->read_word; | ||
| 99 | my $tag_name = $tags{$tag_id}; | ||
| 100 | |||
| 101 | my $data_type = MAS::TIFF::DataType->read_from_io($io); | ||
| 102 | my $data_count = $io->read_dword; | ||
| 103 | my $data_raw = $io->read(4); | ||
| 104 | |||
| 105 | my $self = bless { | ||
| 106 | IO => $io, | ||
| 107 | ID => $tag_id, | ||
| 108 | NAME => $tag_name, | ||
| 109 | TYPE => $data_type, | ||
| 110 | COUNT => $data_count, | ||
| 111 | RAW => $data_raw, | ||
| 112 | }, $class; | ||
| 113 | |||
| 114 | return $self; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub id { return shift->{ID} } | ||
| 118 | sub name { return shift->{NAME} } | ||
| 119 | sub type { return shift->{TYPE} } | ||
| 120 | sub count { return shift->{COUNT} } | ||
| 121 | sub raw { return shift->{RAW} } | ||
| 122 | |||
| 123 | sub size { | ||
| 124 | my $self = shift; | ||
| 125 | |||
| 126 | if (exists $self->{SIZE}) { | ||
| 127 | return $self->{SIZE}; | ||
| 128 | } | ||
| 129 | |||
| 130 | my $size = $self->type->size * $self->count; | ||
| 131 | |||
| 132 | $self->{SIZE} = $size; | ||
| 133 | |||
| 134 | return $size; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub offset { | ||
| 138 | my $self = shift; | ||
| 139 | |||
| 140 | if (exists $self->{OFFSET}) { | ||
| 141 | return $self->{OFFSET}; | ||
| 142 | } | ||
| 143 | |||
| 144 | my $offset; | ||
| 145 | |||
| 146 | if ($self->size <= 4) { | ||
| 147 | $offset = undef; | ||
| 148 | } | ||
| 149 | else { | ||
| 150 | if ($self->{IO}->byte_order eq 'I') { | ||
| 151 | $offset = unpack('L<', $self->raw); | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | $offset = unpack('L>', $self->raw); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | $self->{OFFSET} = $offset; | ||
| 159 | |||
| 160 | return $offset; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub template { | ||
| 164 | my $self = shift; | ||
| 165 | return $self->type->template($self->count, $self->{IO}->byte_order); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub value_at { | ||
| 169 | my $self = shift; | ||
| 170 | my $index = shift; | ||
| 171 | |||
| 172 | die "Index must be defined" unless defined $index; | ||
| 173 | |||
| 174 | my $values = $self->all_values; | ||
| 175 | |||
| 176 | return $values->[$index]; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub all_values { | ||
| 180 | my $self = shift; | ||
| 181 | |||
| 182 | my $values; | ||
| 183 | |||
| 184 | if (not exists $self->{VALUES}) { | ||
| 185 | my $size = $self->type->size * $self->count; | ||
| 186 | |||
| 187 | my $bytes; | ||
| 188 | |||
| 189 | if ($size <= 4) { | ||
| 190 | $bytes = $self->{RAW}; | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | $bytes = $self->{IO}->read($size, $self->offset); | ||
| 194 | } | ||
| 195 | |||
| 196 | my $unpacked = [ unpack($self->template, $bytes) ]; | ||
| 197 | |||
| 198 | $values = $self->type->post_process($unpacked); | ||
| 199 | |||
| 200 | $self->{VALUES} = $values; | ||
| 201 | } | ||
| 202 | else { | ||
| 203 | $values = $self->{VALUES}; | ||
| 204 | } | ||
| 205 | |||
| 206 | return $values; | ||
| 207 | } | ||
| 208 | |||
| 209 | 1; |