| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Image::TextMode::Reader::Tundra; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 1009 | use Moo; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 13 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | extends 'Image::TextMode::Reader'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | sub _read { | 
| 8 | 3 |  |  | 3 |  | 7 | my ( $self, $image, $fh, $options ) = @_; | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 3 |  |  |  |  | 3 | my ( $buffer, %header ); | 
| 11 | 3 |  |  |  |  | 46 | read( $fh, $buffer, 1 ); | 
| 12 | 3 |  |  |  |  | 23 | $header{ int_id } = unpack( 'C', $buffer ); | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 3 |  |  |  |  | 6 | read( $fh, $buffer, 8 ); | 
| 15 | 3 |  |  |  |  | 12 | $header{ id } = unpack( 'A8', $buffer ); | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 3 |  |  |  |  | 43 | $image->header( \%header ); | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 3 |  | 50 |  |  | 1158 | my $width = $options->{ width } || 80; | 
| 20 | 3 |  |  |  |  | 49 | my $pal = Image::TextMode::Palette->new; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 3 |  |  |  |  | 96 | my ( $x, $y, $attr, $fg, $bg, $pal_index ) = ( 0 ) x 6; | 
| 23 | 3 |  |  |  |  | 42 | $pal->colors->[ $pal_index++ ] = [ 0, 0, 0 ]; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 3 |  |  |  |  | 1011 | while ( read( $fh, $buffer, 1 ) ) { | 
| 26 | 90 | 50 |  |  |  | 286 | last if tell( $fh ) > $options->{ filesize }; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 90 |  |  |  |  | 131 | my $command = ord( $buffer ); | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 90 | 100 |  |  |  | 160 | if ( $command == 1 ) {    # position | 
| 31 | 2 |  |  |  |  | 4 | read( $fh, $buffer, 8 ); | 
| 32 | 2 |  |  |  |  | 9 | ( $y, $x ) = unpack( 'N*', $buffer ); | 
| 33 | 2 |  |  |  |  | 7 | next; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 88 |  |  |  |  | 82 | my $char; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 88 | 100 |  |  |  | 285 | if ( $command == 2 ) {    # fg | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 39 | 2 |  |  |  |  | 4 | read( $fh, $char,   1 ); | 
| 40 | 2 |  |  |  |  | 4 | read( $fh, $buffer, 4 ); | 
| 41 | 2 |  |  |  |  | 4 | my $rgb = unpack( 'N', $buffer ); | 
| 42 | 2 |  |  |  |  | 2 | $fg = $pal_index++; | 
| 43 | 2 |  |  |  |  | 39 | $pal->colors->[ $fg ] = [ | 
| 44 |  |  |  |  |  |  | ( $rgb >> 16 ) & 0x000000ff, | 
| 45 |  |  |  |  |  |  | ( $rgb >> 8 ) & 0x000000ff, | 
| 46 |  |  |  |  |  |  | $rgb & 0x000000ff, | 
| 47 |  |  |  |  |  |  | ]; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | elsif ( $command == 4 ) {    # bg | 
| 50 | 0 |  |  |  |  | 0 | read( $fh, $char,   1 ); | 
| 51 | 0 |  |  |  |  | 0 | read( $fh, $buffer, 4 ); | 
| 52 | 0 |  |  |  |  | 0 | my $rgb = unpack( 'N', $buffer ); | 
| 53 | 0 |  |  |  |  | 0 | $bg = $pal_index++; | 
| 54 | 0 |  |  |  |  | 0 | $pal->colors->[ $bg ] = [ | 
| 55 |  |  |  |  |  |  | ( $rgb >> 16 ) & 0x000000ff, | 
| 56 |  |  |  |  |  |  | ( $rgb >> 8 ) & 0x000000ff, | 
| 57 |  |  |  |  |  |  | $rgb & 0x000000ff, | 
| 58 |  |  |  |  |  |  | ]; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  | elsif ( $command == 6 ) {    # fg + bg | 
| 61 | 86 |  |  |  |  | 147 | read( $fh, $char,   1 ); | 
| 62 | 86 |  |  |  |  | 102 | read( $fh, $buffer, 8 ); | 
| 63 | 86 |  |  |  |  | 313 | my @rgb = unpack( 'N*', $buffer ); | 
| 64 | 86 |  |  |  |  | 120 | $fg = $pal_index++; | 
| 65 | 86 |  |  |  |  | 2750 | $pal->colors->[ $fg ] = [ | 
| 66 |  |  |  |  |  |  | ( $rgb[ 0 ] >> 16 ) & 0x000000ff, | 
| 67 |  |  |  |  |  |  | ( $rgb[ 0 ] >> 8 ) & 0x000000ff, | 
| 68 |  |  |  |  |  |  | $rgb[ 0 ] & 0x000000ff, | 
| 69 |  |  |  |  |  |  | ]; | 
| 70 | 86 |  |  |  |  | 621 | $bg = $pal_index++; | 
| 71 | 86 |  |  |  |  | 2358 | $pal->colors->[ $bg ] = [ | 
| 72 |  |  |  |  |  |  | ( $rgb[ 1 ] >> 16 ) & 0x000000ff, | 
| 73 |  |  |  |  |  |  | ( $rgb[ 1 ] >> 8 ) & 0x000000ff, | 
| 74 |  |  |  |  |  |  | $rgb[ 1 ] & 0x000000ff, | 
| 75 |  |  |  |  |  |  | ]; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 88 | 50 |  |  |  | 692 | if ( !$char ) { | 
| 79 | 0 |  |  |  |  | 0 | $char = chr( $command ); | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 88 |  |  |  |  | 539 | $image->putpixel( { char => $char, fg => $fg, bg => $bg }, $x, $y ); | 
| 83 | 88 |  |  |  |  | 5055 | $x++; | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 88 | 100 |  |  |  | 480 | if ( $x == $width ) { | 
| 86 | 1 |  |  |  |  | 3 | $x = 0; | 
| 87 | 1 |  |  |  |  | 16 | $y++; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 3 |  |  |  |  | 60 | $image->palette( $pal ); | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 3 |  |  |  |  | 1114 | return $image; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =head1 NAME | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | Image::TextMode::Reader::Tundra - Reads Tundra files | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | Provides reading capabilities for the Tundra format. | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =head1 AUTHOR | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | Brian Cassidy Ebricas@cpan.orgE | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | Copyright 2008-2015 by Brian Cassidy | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify | 
| 113 |  |  |  |  |  |  | it under the same terms as Perl itself. | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =cut | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | 1; |