blib/lib/Bio/Graphics/Glyph/image.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 243 CC 26 SCAR 33.0
CC
26
Cov
0%
get_image
72.0
_guess_format
42.0
image_path
30.0
CRAP 27.2
1 package Bio::Graphics::Glyph::image;
2  
3 use strict;
4 use GD;
5 use base 'Bio::Graphics::Glyph::generic';
6 our @ISA;
7  
8 #
9 # |--------------------| true position ('height' high)
10 # . .
11 # . . diagonal (vertical spacing high)
12 # . .
13 # +--------------------------+
14 # | |
15 # | |
16 # | | image
17 # | |
18 # | |
19 # | |
20 # +--------------------------+
21  
22 use constant VERTICAL_SPACING => 20;
23  
24 sub my_description {
25 return <<END;
26 This glyph inserts an image into the track at the indicated feature
27 coordinates. The image can be in PNG, JPEG, GIF or GD format, and can
28 be either 8-bit or 24-bit ("truecolor"). The image can be located on
29 the local filesystem or located at a remote URL (provided that you
30 have the LWP module installed).
31  
32 When working with photographic images, you may wish to have
33 Bio::Graphics::Panel create 24-bit (truecolor) images in order to
34 avoid running out of colors. The symptom of this is that images appear
35 posterized. To turn on truecolor images, pass the -truecolor option to
36 Bio::Graphics::Panel.
37 END
38 }
39 sub my_options {
40 {
41 image => [
42 'string',
43 undef,
44 'Specify the image path or URL to use for the feature.',
45 'If no image option is specified, then the glyph will look',
46 'inside the feature itself for an image path or URL in a tag named "image"',
47 ],
48 image_prefix => [
49 'string',
50 undef,
51 'A string to prepend to each image path.',
52 'You may use this to prepend a directory path or a partial URL.'],
53 vertical_spacing => [
54 'integer',
55 20,
56 'Vertical distance from the box that shows the physical span of the',
57 'feature to the top of the picture, in pixels.'],
58 glyph_delegate => [
59 'string',
60 'generic',
61 'The glyph to use for the part of the glyph that shows the physical',
62 'span of features.']
63 }
64 }
65  
66 sub demo_feature {
67 my $self = shift;
68 my $ex_image =
69 'http://www.catch-fly.com/sites/awhittington/_files/Image/Drosophila-melanogaster.jpg';
70 return Bio::Graphics::Feature->new(-start=>1,
71 -end=>500,
72 -name=>$ex_image,
73 -attributes => {
74 image=>$ex_image,
75 },
76 );
77 }
78  
79 sub new {
80 my $self = shift->SUPER::new(@_);
81 $self->{image} = $self->get_image();
82 return $self;
83 }
84  
85 sub get_image {
86 my $self = shift;
87 my ($format,$image) = eval { $self->image_data };
88 unless ($image) {
89 warn $@ if $@;
90 return;
91 }
92 my $gd = $format eq 'image/png' ? GD::Image->newFromPngData($image,1)
93 : $format eq 'image/jpeg' ? GD::Image->newFromJpegData($image,1)
94 : $format eq 'image/gif' ? GD::Image->newFromGifData($image)
95 : $format eq 'image/gd' ? GD::Image->newFromGdData($image)
96 : $format eq 'image/gd2' ? GD::Image->newFromGd2Data($image)
97 : $self->throw("This module cannot handle images of type $format");
98 return $gd;
99 }
100  
101 sub _guess_format {
102 my $self = shift;
103 my $path = shift;
104 return 'image/png' if $path =~ /\.png$/i;
105 return 'image/jpeg' if $path =~ /\.jpe?g$/i;
106 return 'image/gif' if $path =~ /\.gif(87)?$/i;
107 return 'image/gd' if $path =~ /\.gd$/i;
108 return 'image/gd2' if $path =~ /\.gd2$/i;
109 my ($extension) = $path =~ /\.(\w+)$/; #cop-out
110 return $extension;
111 }
112  
113 sub image_path {
114 my $self = shift;
115 my $feature = $self->feature or $self->throw("no feature!");
116 my $dirname = $self->image_dir;
117 my $basename = $self->option('image');
118  
119 # can't get it from callback, so try looking for an 'image' attribute
120 if (!$basename && $feature->can('has_tag') && $feature->has_tag('image')) {
121 ($basename) = $feature->get_tag_values('image');
122 }
123  
124 return unless $basename;
125 return $basename if $basename =~ m!^\w+:/!; # looks like a URL
126 return $basename if $basename =~ m!^/!; # looks like an abs path
127 return "$dirname/$basename";
128 }
129  
130 sub image_data {
131 my $self = shift;
132 my $path = $self->image_path or return;
133  
134 if ($path =~ m!^\w+:/!) { # looks like a URL
135 require LWP::UserAgent;
136 my $ua = LWP::UserAgent->new(env_proxy => 1);
137 my $response = $ua->get($path);
138 if ($response->is_success) {
139 return ($response->content_type,$response->content);
140 } else {
141 $self->throw($response->status_line);
142 }
143  
144  
145 } else {
146 my $content_type = $self->_guess_format($path);
147 open F,$path or $self->throw("Can't open $path: $!");
148 binmode F;
149 my $data;
150 $data .= $_ while read(F,$_,1024);
151 close F;
152 return ($content_type,$data);
153 }
154 }
155  
156 sub pad_left {
157 my $self = shift;
158 my $pad = $self->SUPER::pad_left;
159 my $image = $self->{image} or return $pad;
160 my $width_needed = ($image->width - $self->width)/2;
161 return $pad > $width_needed ? $pad : $width_needed;
162 }
163  
164 sub pad_right {
165 my $self = shift;
166 my $pad = $self->SUPER::pad_right;
167 my $image = $self->{image} or return $pad;
168 my $width_needed = ($image->width - $self->width)/2;
169 return $pad > $width_needed ? $pad : $width_needed;
170 }
171  
172 sub pad_bottom {
173 my $self = shift;
174 my $pb = $self->SUPER::pad_bottom;
175 my $image = $self->{image} or return $pb;
176 $pb += $self->vertical_spacing;
177 $pb += $image->height;
178 return $pb;
179 }
180  
181 sub vertical_spacing {
182 my $self = shift;
183 my $vs = $self->option('vertical_spacing');
184 return $vs if defined $vs;
185 return VERTICAL_SPACING;
186 }
187  
188 sub draw_description {
189 my $self = shift;
190 my ($gd,$left,$top,$partno,$total_parts) = @_;
191 $self->SUPER::draw_description($gd,$left,$top,$partno,$total_parts);
192 }
193  
194 sub image_dir {
195 my $self = shift;
196 return $self->option('image_prefix');
197 }
198  
199 sub draw_component {
200 my $self = shift;
201 my $gd = shift;
202 my($x1,$y1,$x2,$y2) = $self->bounds(@_);
203  
204 my $delegate = $self->option('glyph_delegate') || 'generic';
205 if ($delegate eq 'generic') {
206 $self->SUPER::draw_component($gd,@_);
207 } else {
208 eval "require Bio::Graphics::Glyph::$delegate";
209 local @ISA = ("Bio::Graphics::Glyph::$delegate");
210 my $method = "Bio::Graphics::Glyph::${delegate}::draw_component";
211 $self->$method($gd,@_);
212 }
213  
214 my $image = $self->{image} or return;
215  
216 my $fgcolor = $self->fgcolor;
217 my $bgcolor = $self->bgcolor;
218 my $height = $self->option('height');
219 my $half = 4;
220 my $vs = $self->vertical_spacing;
221  
222 my $delta = (($x2-$x1) - $image->width)/2;
223 my($x,$y) = ($x1+$delta,$y1+$vs+$self->height);
224 if ($gd->can('copy') && !$gd->isa('GD::SVG::Image')) {
225 $gd->copy($image,$x,$y,0,0,$image->width,$image->height) ;
226 }
227 elsif ($gd->isa('GD::SVG::Image')
228 && $self->image_path =~ m!^(ftp|http)+:/!) { # a URL
229 my ($img,$id) = $gd->_prep($x,$y);
230 $img->image('x' => $x,
231 'y' => $y,
232 width => $image->width,
233 height => $image->height,
234 id => $id,
235 'xlink:href' => $self->image_path);
236 }
237 else {
238 my $gray = $self->panel->translate_color('gray');
239 $gd->filledRectangle($x,$y,$x+$image->width,$y+$image->height,$gray);
240 }
241  
242 if ($vs > 0) {
243 $gd->line($x1,$y2+2,$x1,$y2+$half,$fgcolor);
244 $gd->line($x2,$y2+2,$x2,$y2+$half,$fgcolor);
245 $gd->line($x1,$y2+$half,$x,$y-$half,$fgcolor);
246 $gd->line($x2,$y2+$half,$x+$image->width-1,$y-$half,$fgcolor);
247 $gd->line($x,$y-$half,$x,$y-2,$fgcolor);
248 $gd->line($x+$image->width-1,$y-$half,$x+$image->width-1,$y-2,$fgcolor);
249 }
250 }
251  
252 1;
253  
254 __END__