| 1 | package Image::TextMode::Reader::XBin; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | extends 'Image::TextMode::Reader'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # Header byte constants | ||||||||||||||||||||||||||||
| 9 | 2 | my $PALETTE = 1; | |||||||||||||||||||||||||||
| 10 | 2 | my $FONT = 2; | |||||||||||||||||||||||||||
| 11 | 2 | my $COMPRESSED = 4; | |||||||||||||||||||||||||||
| 12 | 2 | my $NON_BLINK = 8; | |||||||||||||||||||||||||||
| 13 | 2 | my $FIVETWELVE_CHARS = 16; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Compression type constants | ||||||||||||||||||||||||||||
| 16 | 2 | my $NO_COMPRESSION = 0; | |||||||||||||||||||||||||||
| 17 | 2 | my $CHARACTER_COMPRESSION = 64; | |||||||||||||||||||||||||||
| 18 | 2 | my $ATTRIBUTE_COMPRESSION = 128; | |||||||||||||||||||||||||||
| 19 | 2 | my $FULL_COMPRESSION = 192; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Compression byte constants | ||||||||||||||||||||||||||||
| 22 | 2 | my $COMPRESSION_TYPE = 192; | |||||||||||||||||||||||||||
| 23 | 2 | my $COMPRESSION_COUNTER = 63; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | my $header_template = 'A4 C v v C C'; | |||||||||||||||||||||||||||
| 26 | 2 | my $eof_char = chr( 26 ); | |||||||||||||||||||||||||||
| 27 | 2 | my @header_fields = qw( id eofchar width height fontsize flags ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub _read { | ||||||||||||||||||||||||||||
| 30 | 3 | ▶ | my ( $self, $image, $fh, $options ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_read: called CC=7 SCAR=20.5
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 3 | my $headerdata; | |||||||||||||||||||||||||||
| 33 | 3 | my $headerlength = read( $fh, $headerdata, 11 ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # does it start with the right data? | ||||||||||||||||||||||||||||
| 36 | 3 | ▶ | croak 'Not an XBin file.' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$headerlength == 11 and $headerdata =~ /^XBIN$eof_char/s
MC/DC0%0/2
$headerlength == 11 and $headerdata =~ /^XBIN$eof_char/s
$headerlength == 11
$headerdata =~ /^XBIN$eof_char/s
Truth table33%1/3
A and B
A= $headerlength == 11B= $headerdata =~ /^XBIN$eof_char/s
| |||||||||||||||||||||||||||||
| 37 | unless $headerlength == 11 and $headerdata =~ m{^XBIN$eof_char}s; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # parse header data | ||||||||||||||||||||||||||||
| 40 | 3 | _read_header( $image, $headerdata ); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3 | ▶ | if ( $image->header->{ flags } & $PALETTE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 3 | my $paldata; | |||||||||||||||||||||||||||
| 44 | 3 | read( $fh, $paldata, 48 ); | |||||||||||||||||||||||||||
| 45 | 3 | _parse_palette( $image, $paldata ); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | ▶ | if ( $image->header->{ flags } & $FONT ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 3 | my $fontsize = $image->header->{ fontsize }; | |||||||||||||||||||||||||||
| 50 | my $chars = $fontsize | ||||||||||||||||||||||||||||
| 51 | 3 | ▶ | * ( $image->header->{ flags } & $FIVETWELVE_CHARS ? 512 : 256 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 3 | my $fontdata; | |||||||||||||||||||||||||||
| 53 | 3 | read( $fh, $fontdata, $chars ); | |||||||||||||||||||||||||||
| 54 | 3 | _parse_font( $image, $fontdata ); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 3 | ▶ | if ( $image->header->{ flags } & $COMPRESSED ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | _parse_compressed( $image, $fh ); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||
| 61 | 3 | _parse_uncompressed( $image, $fh ); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | return $image; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub _read_header { | ||||||||||||||||||||||||||||
| 68 | 3 | ▶ | my ( $image, $content ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_read_header: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 3 | my %header; | |||||||||||||||||||||||||||
| 71 | 3 | @header{ @header_fields } = unpack( $header_template, $content ); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 3 | $image->header( \%header ); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub _parse_font { | ||||||||||||||||||||||||||||
| 77 | 3 | ▶ | my ( $image, $data ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_parse_font: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 78 | 3 | my $height = $image->header->{ fontsize }; | |||||||||||||||||||||||||||
| 79 | 3 | my @chars; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 3 | for ( 0 .. ( length( $data ) / $height ) - 1 ) { | |||||||||||||||||||||||||||
| 82 | 768 | push @chars, | |||||||||||||||||||||||||||
| 83 | [ unpack( 'C*', substr( $data, $_ * $height, $height ) ) ]; | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | $image->font( | ||||||||||||||||||||||||||||
| 87 | 3 | Image::TextMode::Font->new( | |||||||||||||||||||||||||||
| 88 | { width => 8, | ||||||||||||||||||||||||||||
| 89 | height => $height, | ||||||||||||||||||||||||||||
| 90 | chars => \@chars, | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | ) | ||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub _parse_palette { | ||||||||||||||||||||||||||||
| 97 | 3 | ▶ | my ( $image, $data ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_parse_palette: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 3 | my @values = unpack( 'C*', $data ); | |||||||||||||||||||||||||||
| 100 | 3 | my @palette; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 3 | for my $i ( 0 .. @values / 3 - 1 ) { | |||||||||||||||||||||||||||
| 103 | 48 | my $offset = $i * 3; | |||||||||||||||||||||||||||
| 104 | 48 | $palette[ $i ] = [ | |||||||||||||||||||||||||||
| 105 | $values[ $offset ] << 2 | $values[ $offset ] >> 4, | ||||||||||||||||||||||||||||
| 106 | $values[ $offset + 1 ] << 2 | $values[ $offset + 1 ] >> 4, | ||||||||||||||||||||||||||||
| 107 | $values[ $offset + 2 ] << 2 | $values[ $offset + 2 ] >> 4, | ||||||||||||||||||||||||||||
| 108 | ]; | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | $image->palette( | ||||||||||||||||||||||||||||
| 112 | 3 | Image::TextMode::Palette->new( { colors => \@palette } ) ); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub _parse_compressed { | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | my ( $image, $fh ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_parse_compressed: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my $x = 0; | |||||||||||||||||||||||||||
| 119 | 0 | my $y = 0; | |||||||||||||||||||||||||||
| 120 | 0 | my $width = $image->header->{ width }; | |||||||||||||||||||||||||||
| 121 | 0 | my $height = $image->header->{ height }; | |||||||||||||||||||||||||||
| 122 | 0 | my $info; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | READ: while ( read( $fh, $info, 1 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | $info = unpack( 'C', $info ); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | my $type = $info & $COMPRESSION_TYPE; | |||||||||||||||||||||||||||
| 128 | 0 | my $counter = ( $info & $COMPRESSION_COUNTER ) + 1; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my ( $char, $attr ); | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | while ( $counter-- ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ( $type == $NO_COMPRESSION ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 133 | 0 | read( $fh, $char, 1 ); | |||||||||||||||||||||||||||
| 134 | 0 | read( $fh, $attr, 1 ); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | elsif ( $type == $CHARACTER_COMPRESSION ) { | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | read( $fh, $char, 1 ) if !defined $char; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | read( $fh, $attr, 1 ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | elsif ( $type == $ATTRIBUTE_COMPRESSION ) { | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | read( $fh, $attr, 1 ) if !defined $attr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | read( $fh, $char, 1 ); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | else { # $FULL_COMPRESSION | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | read( $fh, $char, 1 ) if !defined $char; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | read( $fh, $attr, 1 ) if !defined $attr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | my $pchar = unpack( 'a', $char ); | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | $image->putpixel( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | { char => length $pchar ? $pchar : ' ', | ||||||||||||||||||||||||||||
| 152 | attr => scalar unpack( 'C', $attr ) | ||||||||||||||||||||||||||||
| 153 | }, | ||||||||||||||||||||||||||||
| 154 | $x, $y, | ||||||||||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | $x++; | |||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ( $x == $width ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | $x = 0; | |||||||||||||||||||||||||||
| 160 | 0 | $y++; | |||||||||||||||||||||||||||
| 161 | 0 | ▶ | last READ if $y == $height; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | sub _parse_uncompressed { | ||||||||||||||||||||||||||||
| 168 | 3 | ▶ | my ( $image, $fh ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_parse_uncompressed: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 3 | my ( $x, $y ) = ( 0, 0 ); | |||||||||||||||||||||||||||
| 171 | 3 | my $chardata; | |||||||||||||||||||||||||||
| 172 | 3 | my $width = $image->header->{ width }; | |||||||||||||||||||||||||||
| 173 | 3 | my $height = $image->header->{ height }; | |||||||||||||||||||||||||||
| 174 | 3 | ▶ | while ( read( $fh, $chardata, 2 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 240 | my @data = unpack( 'aC', $chardata ); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 240 | $image->putpixel( { char => $data[ 0 ], attr => $data[ 1 ] }, | |||||||||||||||||||||||||||
| 178 | $x, $y, ); | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 240 | $x++; | |||||||||||||||||||||||||||
| 181 | 240 | ▶ | if ( $x == $width ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 3 | $x = 0; | |||||||||||||||||||||||||||
| 183 | 3 | $y++; | |||||||||||||||||||||||||||
| 184 | 3 | ▶ | last if $y == $height; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head1 NAME | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Image::TextMode::Reader::XBin - Reads XBin files | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Provides reading capabilities for the XBin format. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Brian Cassidy E<lt>bricas@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Copyright 2008-2014 by Brian Cassidy | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 206 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 2 | 1; | |||||||||||||||||||||||||||