| 1 | package Image::Pbm; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Image::Xbm(); our @ISA = 'Image::Xbm'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Image::PBMlib 2.00 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | sub load | ||||||||||||||||||||||||||||
| 11 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=6 SCAR=18.8
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 1 | my $self = shift ; | |||||||||||||||||||||||||||
| 13 | 1 | ▶ | my $file = shift || $self->get(-file ) or die 'No file specified'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
shift() || $self->get('-file')
MC/DC0%0/2
shift() || $self->get('-file')
shift()
$self->get('-file')
Truth table33%1/3
A || B
A= shift()B= $self->get('-file')
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | open my $f, $file or die "Failed to open `$file': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 1 | my $h = {}; | |||||||||||||||||||||||||||
| 17 | 1 | my $p = []; | |||||||||||||||||||||||||||
| 18 | 1 | Image::PBMlib::readpnmfile( $f, $h, $p,'dec'); | |||||||||||||||||||||||||||
| 19 | 1 | ▶ | die "Failed to parse header in `$file': $h->{error}" if $h->{error}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | die "Wrong magic number: ($h->{type})" if $h->{type} != 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | $self->_set( -file => $file ); | |||||||||||||||||||||||||||
| 23 | 1 | $self->_set( -width => $h->{width} ); | |||||||||||||||||||||||||||
| 24 | 1 | $self->_set(-height => $h->{height} ); | |||||||||||||||||||||||||||
| 25 | 1 | $self->_set( -bits => pack 'b*', join '', map { @$_ } @$p ); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub save | ||||||||||||||||||||||||||||
| 29 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
save: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 31 | 1 | ▶ | my $file = shift || $self->get(-file ) or die 'No file specified'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
shift() || $self->get('-file')
MC/DC0%0/2
shift() || $self->get('-file')
shift()
$self->get('-file')
Truth table33%1/3
A || B
A= shift()B= $self->get('-file')
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # I hate getter/setter! They may be helpful in languages | ||||||||||||||||||||||||||||
| 34 | # which fail to hide the implementation of properties. | ||||||||||||||||||||||||||||
| 35 | 1 | my ( $setch, $unsetch ) = $self->get(-setch,-unsetch ); | |||||||||||||||||||||||||||
| 36 | 1 | $self->set(-file => $file,-setch => ' 1',-unsetch => ' 0'); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | open my $f, ">$file" or die "Failed to open `$file': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 1 | local $\ = "\n"; | |||||||||||||||||||||||||||
| 40 | 1 | print $f 'P1'; | |||||||||||||||||||||||||||
| 41 | 1 | print $f "# $file"; | |||||||||||||||||||||||||||
| 42 | 1 | print $f $self->get(-width ); | |||||||||||||||||||||||||||
| 43 | 1 | print $f $self->get(-height ); | |||||||||||||||||||||||||||
| 44 | 1 | print $f $self->as_string; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | $self->set(-setch => $setch,-unsetch => $unsetch ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | 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<Image::Xbm> which provides additional | ||||||||||||||||||||||||||||
| 73 | functionality. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | See L<Image::Base> and L<Image::Xbm> 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(<img src="data:image/x-xbitmap,$data">); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | This works with Mozilla and Opera. | ||||||||||||||||||||||||||||
| 108 | For Internet Explorer, we can use the following workaround: | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | print <<"HTML"; | ||||||||||||||||||||||||||||
| 111 | <pre id="xbm" style="display: none;">$xbm</pre> | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | <script> | ||||||||||||||||||||||||||||
| 114 | function xbm() { return document.getElementById('xbm').innerHTML; } | ||||||||||||||||||||||||||||
| 115 | </script> | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | <img src="javascript:xbm()"> | ||||||||||||||||||||||||||||
| 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 <sgoeldner@cpan.org> | ||||||||||||||||||||||||||||
| 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<perl>, L<Image::Base>, L<Image::Xbm>, L<Image::PBMlib>. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||