File Coverage

blib/lib/Image/TextMode/Reader/IDF.pm
Criterion Covered Total %
statement 49 51 96.0
branch 5 8 62.5
condition n/a
subroutine 4 4 100.0
pod n/a
total 58 63 92.0


line stmt bran cond sub pod time code
1             package Image::TextMode::Reader::IDF;
2              
3 2     2   879 use Moo;
  2         4  
  2         13  
4              
5             extends 'Image::TextMode::Reader';
6              
7             my $header_template = 'A4 v v v v';
8             my @header_fields = qw( id x0 y0 x1 y1 );
9              
10             sub _read {
11 3     3   5 my ( $self, $image, $fh, $options ) = @_;
12              
13 3         4 my $buffer;
14 3         17 read( $fh, $buffer, 12 );
15              
16 3         4 my %header;
17 3         30 @header{ @header_fields } = unpack( $header_template, $buffer );
18              
19 3         27 $image->header( \%header );
20              
21             # font and palette data are stored at the bottom of the file
22 3         1000 seek( $fh, -48 - 4096, 2 );
23 3 50       63 if ( $image->has_sauce ) {
24 0         0 my $s = $image->sauce;
25 0 0       0 seek( $fh, -129 - ( $s->comment_count ? 5 + 64 * $s->comment_count : 0 ), 1 );
26             }
27              
28 3         119 my $max = tell( $fh );
29              
30 3         45 read( $fh, $buffer, 4096 );
31 3         9 _parse_font( $image, $buffer );
32              
33 3         4980 read( $fh, $buffer, 48 );
34 3         12 _parse_palette( $image, $buffer );
35              
36 3         3076 seek( $fh, 12, 0 );
37 3         6 my ( $x, $y ) = ( 0, 0 );
38 3         56 my $width = $image->header->{ x1 };
39 3         21 while ( tell $fh < $max ) {
40 92         146 read( $fh, $buffer, 2 );
41 92         142 my $info = unpack( 'v', $buffer );
42              
43 92         87 my $len = 1;
44 92 100       144 if ( $info == 1 ) {
45 2         4 read( $fh, $info, 2 );
46 2         3 $len = unpack( 'v', $info ) & 255;
47 2         3 read( $fh, $buffer, 2 );
48             }
49              
50 92         191 my @data = unpack( 'aC', $buffer );
51 92         120 for ( 1 .. $len ) {
52 240         659 $image->putpixel( { char => $data[ 0 ], attr => $data[ 1 ] },
53             $x, $y );
54 240         7641 $x++;
55 240 100       586 if ( $x > $width ) {
56 3         5 $x = 0;
57 3         11 $y++;
58             }
59             }
60             }
61              
62 3         35 return $image;
63             }
64              
65             sub _parse_palette {
66 3     3   7 my ( $image, $data ) = @_;
67              
68 3         19 my @pal = unpack( 'C*', $data );
69 3         7 my @colors;
70 3         6 for ( 0 .. 15 ) {
71 48         36 my $offset = $_ * 3;
72 48         58 push @colors, [ map { $_ << 2 | $_ >> 4 } @pal[ $offset .. $offset + 2 ] ],;
  144         173  
73             }
74              
75             $image->palette(
76 3         47 Image::TextMode::Palette->new( { colors => \@colors } ) );
77             }
78              
79             sub _parse_font {
80 3     3   6 my ( $image, $data ) = @_;
81 3         4 my $height = 16;
82 3         3 my @chars;
83              
84 3         13 for ( 0 .. ( length( $data ) / $height ) - 1 ) {
85 768         2085 push @chars,
86             [ unpack( 'C*', substr( $data, $_ * $height, $height ) ) ];
87             }
88              
89             $image->font(
90 3         50 Image::TextMode::Font->new(
91             { width => 8,
92             height => $height,
93             chars => \@chars,
94             }
95             )
96             );
97             }
98              
99             =head1 NAME
100              
101             Image::TextMode::Reader::IDF - Reads IDF files
102              
103             =head1 DESCRIPTION
104              
105             Provides reading capabilities for the IDF format.
106              
107             =head1 AUTHOR
108              
109             Brian Cassidy Ebricas@cpan.orgE
110              
111             =head1 COPYRIGHT AND LICENSE
112              
113             Copyright 2008-2014 by Brian Cassidy
114              
115             This library is free software; you can redistribute it and/or modify
116             it under the same terms as Perl itself.
117              
118             =cut
119              
120             1;