| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package MAS::TIFF::IO; | ||
| 5 | |||
| 6 | sub new { | ||
| 7 | my $class = shift; | ||
| 8 | my $path = shift; | ||
| 9 | |||
| 10 | my $fh = FileHandle->new($path, 'r'); | ||
| 11 | |||
| 12 | die 'Could not open' unless defined $fh; | ||
| 13 | |||
| 14 | binmode($fh); | ||
| 15 | |||
| 16 | my $self = bless { | ||
| 17 | FH => $fh, | ||
| 18 | }, $class; | ||
| 19 | |||
| 20 | my $buf = $self->read(4); | ||
| 21 | |||
| 22 | my $byte_order; | ||
| 23 | |||
| 24 | if ($buf eq "MM\0\x2a") { | ||
| 25 | $byte_order = 'M'; # big-endian | ||
| 26 | } | ||
| 27 | elsif ($buf eq "II\x2a\0") { | ||
| 28 | $byte_order = 'I'; # little-endian | ||
| 29 | } | ||
| 30 | else { | ||
| 31 | die "Not a TIFF file. Header: '%s', version %x %x\n", substr($buf, 0, 2), ord substr($buf, 2, 1), ord substr($buf, 3, 1); | ||
| 32 | } | ||
| 33 | |||
| 34 | $self->{BYTE_ORDER} = $byte_order; | ||
| 35 | |||
| 36 | return $self; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub read { | ||
| 40 | my $self = shift; | ||
| 41 | my $size = shift; | ||
| 42 | my $seek = shift; | ||
| 43 | |||
| 44 | $self->fh->seek($seek, 0) if defined $seek; | ||
| 45 | |||
| 46 | my $buf; | ||
| 47 | my $n = $self->fh->read($buf, $size); | ||
| 48 | die "Error reading: $!" unless defined $n; | ||
| 49 | die "Unable to read $size bytes!" unless $n == $size; | ||
| 50 | |||
| 51 | return $buf; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub close { | ||
| 55 | my $self = shift; | ||
| 56 | |||
| 57 | my $fh = $self->fh; | ||
| 58 | $fh->close; | ||
| 59 | delete $self->{FH}; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub fh { return shift->{FH} } | ||
| 63 | sub byte_order { return shift->{BYTE_ORDER} } | ||
| 64 | |||
| 65 | sub read_word { | ||
| 66 | my $self = shift; | ||
| 67 | my $seek = shift; | ||
| 68 | |||
| 69 | $self->fh->seek($seek, 0) if defined $seek; | ||
| 70 | |||
| 71 | my $buf; | ||
| 72 | |||
| 73 | my $n = $self->fh->read($buf, 2); | ||
| 74 | die "Error reading file: $!" unless defined $n; | ||
| 75 | die "Could not read 2 bytes from file!" unless $n == 2; | ||
| 76 | |||
| 77 | my $result = 0; | ||
| 78 | |||
| 79 | if ($self->byte_order eq 'M') { | ||
| 80 | for (my $i = 0; $i < 2; ++$i) { | ||
| 81 | $result *= 256; | ||
| 82 | $result += ord substr($buf, $i, 1); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | for (my $i = 1; $i >= 0; --$i) { | ||
| 87 | $result *= 256; | ||
| 88 | $result += ord substr($buf, $i, 1); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | return $result; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub read_dword { | ||
| 96 | my $self = shift; | ||
| 97 | my $seek = shift; | ||
| 98 | |||
| 99 | $self->fh->seek($seek, 0) if defined $seek; | ||
| 100 | |||
| 101 | my $buf; | ||
| 102 | |||
| 103 | my $n = $self->fh->read($buf, 4); | ||
| 104 | die "Error reading file: $!" unless defined $n; | ||
| 105 | die "Could not read 4 bytes from file!" unless $n == 4; | ||
| 106 | |||
| 107 | my $result = 0; | ||
| 108 | |||
| 109 | if ($self->byte_order eq 'M') { | ||
| 110 | for (my $i = 0; $i < 4; ++$i) { | ||
| 111 | $result *= 256; | ||
| 112 | $result += ord substr($buf, $i, 1); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | else { | ||
| 116 | for (my $i = 3; $i >= 0; --$i) { | ||
| 117 | $result *= 256; | ||
| 118 | $result += ord substr($buf, $i, 1); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | return $result; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub read_rational { | ||
| 126 | my $self = shift; | ||
| 127 | my $seek = shift; | ||
| 128 | |||
| 129 | $self->fh->seek($seek, 0) if defined $seek; | ||
| 130 | |||
| 131 | my $numerator = $self->read_dword; | ||
| 132 | my $denominator = $self->read_dword; | ||
| 133 | |||
| 134 | return bless [$numerator, $denominator], 'MAS::TIFF::Rational'; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub read_ascii { | ||
| 138 | my $self = shift; | ||
| 139 | my ($count, $seek) = @_; | ||
| 140 | |||
| 141 | $self->fh->seek($seek, 0) if defined $seek; | ||
| 142 | |||
| 143 | my $buf; | ||
| 144 | |||
| 145 | my $n = $self->fh->read($buf, $count); | ||
| 146 | die "Error reading file: $!" unless defined $n; | ||
| 147 | die "Could not read $count bytes from file!" unless $n == $count; | ||
| 148 | |||
| 149 | return unpack("Z*", $buf); | ||
| 150 | } | ||
| 151 | |||
| 152 | 1; |