File Coverage

blib/lib/Image/Pbm.pm
Criterion Covered Total %
statement 37 37 100.0
branch 6 12 50.0
condition 2 6 33.3
subroutine 6 6 100.0
pod 2 2 100.0
total 53 63 84.1


line stmt bran cond sub pod time code
1             package Image::Pbm;
2              
3             our $VERSION = '0.04';
4              
5 2     2   27699 use strict;
  2         4  
  2         69  
6 2     2   10 use warnings;
  2         2  
  2         51  
7 2     2   1710 use Image::Xbm(); our @ISA = 'Image::Xbm';
  2         16011  
  2         90  
8 2     2   2308 use Image::PBMlib 2.00 ();
  2         32620  
  2         973  
9              
10             sub load
11             {
12 1     1 1 2107 my $self = shift ;
13 1 50 33     8 my $file = shift || $self->get(-file ) or die 'No file specified';
14              
15 1 50       46 open my $f, $file or die "Failed to open `$file': $!";
16 1         3 my $h = {};
17 1         1 my $p = [];
18 1         7 Image::PBMlib::readpnmfile( $f, $h, $p,'dec');
19 1 50       474 die "Failed to parse header in `$file': $h->{error}" if $h->{error};
20 1 50       5 die "Wrong magic number: ($h->{type})" if $h->{type} != 1;
21              
22 1         6 $self->_set( -file => $file );
23 1         8 $self->_set( -width => $h->{width} );
24 1         8 $self->_set(-height => $h->{height} );
25 1         7 $self->_set( -bits => pack 'b*', join '', map { @$_ } @$p );
  6         22  
26             }
27              
28             sub save
29             {
30 1     1 1 3237 my $self = shift;
31 1 50 33     7 my $file = shift || $self->get(-file ) or die 'No file specified';
32              
33             # I hate getter/setter! They may be helpful in languages
34             # which fail to hide the implementation of properties.
35 1         7 my ( $setch, $unsetch ) = $self->get(-setch,-unsetch );
36 1         31 $self->set(-file => $file,-setch => ' 1',-unsetch => ' 0');
37              
38 1 50       194 open my $f, ">$file" or die "Failed to open `$file': $!";
39 1         5 local $\ = "\n";
40 1         12 print $f 'P1';
41 1         4 print $f "# $file";
42 1         6 print $f $self->get(-width );
43 1         19 print $f $self->get(-height );
44 1         14 print $f $self->as_string;
45              
46 1         145 $self->set(-setch => $setch,-unsetch => $unsetch );
47             }
48              
49             1;
50              
51             =head1 NAME
52              
53             Image::Pbm - Load, create, manipulate and save pbm image files.
54              
55             =head1 SYNOPSIS
56              
57             use Image::Pbm();
58              
59             my $i = Image::Pbm->new(-width => 50, -height => 25 );
60             $i->line ( 2, 2, 22, 22 => 1 );
61             $i->rectangle( 4, 4, 40, 20 => 1 );
62             $i->ellipse ( 6, 6, 30, 15 => 1 );
63             $i->xybit ( 42, 22 => 1 );
64             print $i->as_string;
65             $i->save('test.pbm');
66              
67             $i = Image::Pbm->new(-file,'test.pbm');
68              
69             =head1 DESCRIPTION
70              
71             This module provides basic load, manipulate and save functionality for
72             the pbm file format. It inherits from C which provides additional
73             functionality.
74              
75             See L and L for a description of all
76             inherited methods.
77              
78             =head1 EXAMPLE
79              
80             Imagine, we have to create self-contained web pages (with embedded images).
81             Most browsers understand the xbm image format, but generating xbm files
82             requires a certain effort (or a full fledged graphics software package).
83             On the other hand, generating pbm files is easy. Indeed, it's more likely
84             that you use your favorite text editor instead of Image::Pbm for that task.
85             Reading pbm files is slightly more difficult.
86             That's where the Image::[PX]bm modules come into play:
87              
88             use Image::Pbm();
89              
90             Image::Pbm->new(-file,'test.pbm')
91             ->new_from_image('Image::Xbm')
92             ->save('test.xbm');
93              
94             Once we have xbm files, we can serve these images onto the Internet.
95             To embed these images into a web page, we can use the "data" URL scheme:
96              
97             http://www.ietf.org/rfc/rfc2397.txt
98              
99             which requires the standard %xx hex encoding of URLs:
100              
101             use URI::Escape();
102              
103             my $data = URI::Escape::uri_escape( $xbm );
104              
105             print qq();
106              
107             This works with Mozilla and Opera.
108             For Internet Explorer, we can use the following workaround:
109              
110             print <<"HTML";
111            
112              
113            
116              
117            
118             HTML
119              
120             This works with Mozilla too.
121              
122             =head1 TODO
123              
124             Contact Mark Summerfield because the inheritance hierarchy
125              
126             Image::Pbm <: Image::Xbm <: Image::Base
127              
128             is suboptimal and should look like
129              
130             Image::Xbm <:
131             Image::Bitmap <: Image::Base
132             Image::Pbm <:
133              
134             =head1 AUTHOR
135              
136             Steffen Goeldner
137              
138             =head1 COPYRIGHT
139              
140             Copyright (c) 2004, 2012 Steffen Goeldner. All rights reserved.
141              
142             This program is free software; you can redistribute it and/or
143             modify it under the same terms as Perl itself.
144              
145             =head1 SEE ALSO
146              
147             L, L, L, L.
148              
149             =cut