GeniusTrader/Graphics/Driver/ImageMagick.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 238 CC 15 SCAR 36.1
CC
15
Cov
0%
string
110.0
polygon
12.0
filled_polygon
12.0
CRAP 36.8
1 package Finance::GeniusTrader::Graphics::Driver::ImageMagick;
2  
3 # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber
4 # This file is distributed under the terms of the General Public License
5 # version 2 or (at your option) any later version.
6  
7 use strict;
8 use Finance::GeniusTrader::Graphics::Driver;
9 use Finance::GeniusTrader::Graphics::Zone;
10 use Image::Magick;
11  
12 our @ISA = qw(Finance::GeniusTrader::Graphics::Driver);
13  
14 =head1 Finance::GeniusTrader::Graphics::Driver::ImageMagick
15  
16 This driver implements the drawing primitives using the ImageMagick Perl extension.
17  
18 =cut
19  
20 # PUBLIC DRIVER INTERFACE
21 sub new {
22 my $type = shift;
23 my $class = ref($type) || $type;
24 return bless {}, $class;
25 }
26  
27 sub create_picture {
28 my ($self, $zone) = @_;
29 my $size = $zone->external_width() . 'x' . $zone->external_height();
30 my $i = Image::Magick->new(size=>$size);
31 $i->ReadImage('xc:white');
32 my $p = { "img" => $i, "zone" => $zone, "linewidth" => 1 };
33 return $p;
34 }
35  
36 sub line {
37 my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_;
38 my ($xpt1, $ypt1) = _convert_coord($p, $x1, $y1);
39 my ($xpt2, $ypt2) = _convert_coord($p, $x2, $y2);
40 my $points = $xpt1 . ',' . $ypt1 . ' ' . $xpt2 . ',' . $ypt2;
41 $p->{'img'}->Draw(fill=>_get_color($p, $color), antialias=>0, primitive=>'line', points=>$points, strokewidth=>$p->{'linewidth'});
42 }
43  
44 sub antialiased_line {
45 my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_;
46 my ($xpt1, $ypt1) = _convert_coord($p, $x1, $y1);
47 my ($xpt2, $ypt2) = _convert_coord($p, $x2, $y2);
48 my $points = $xpt1 . ',' . $ypt1 . ' ' . $xpt2 . ',' . $ypt2;
49 $p->{'img'}->Draw(fill=>_get_color($p, $color), antialias=>1, primitive=>'line', points=>$points, strokewidth=>$p->{'linewidth'});
50 }
51  
52 sub dashed_line {
53 my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_;
54
55 # Not yet implemented
56 }
57  
58 sub rectangle {
59 my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_;
60 my ($xpt1, $ypt1) = _convert_coord($p, $x1, $y1);
61 my ($xpt2, $ypt2) = _convert_coord($p, $x2, $y2);
62 my $points = $xpt1 . ',' . $ypt1 . ' ' . $xpt2 . ',' . $ypt2;
63 $p->{'img'}->Draw(stroke=>_get_color($p, $color), primitive=>'rectangle', points=>$points, fill=>'none', strokewidth=>$p->{'linewidth'});
64 }
65  
66 sub filled_rectangle {
67 my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_;
68 my ($xpt1, $ypt1) = _convert_coord($p, $x1, $y1);
69 my ($xpt2, $ypt2) = _convert_coord($p, $x2, $y2);
70 my $points = $xpt1 . ',' . $ypt1 . ' ' . $xpt2 . ',' . $ypt2;
71 $p->{'img'}->Draw(fill=>_get_color($p, $color), bordercolor=>_get_color($p, $color), stroke=>_get_color($p, $color), primitive=>'rectangle', points=>$points);
72 }
73  
74 sub polygon {
75 my ($self, $p, $color, @points) = @_;
76 my $data;
77  
78 if(scalar(@points)) {
79 for (my $i = 0; $i < scalar(@points); $i++) {
80 my ($x1, $x2) = _convert_coord($p, $points[$i][0], $points[$i][1]);
81 $data .= $x1 . ',' . $x2;
82 $data .= ' ' if ($i != (scalar(@points) - 1));
83 }
84 $p->{'img'}->Draw(stroke=>_get_color($p, $color), primitive=>'polygon', points=>$data, fill=>'none', strokewidth=>$p->{'linewidth'});
85 }
86 }
87  
88 sub filled_polygon {
89 my ($self, $p, $color, @points) = @_;
90 my $data;
91  
92 if(scalar(@points)) {
93 for (my $i = 0; $i < scalar(@points); $i++) {
94 my ($x1, $x2) = _convert_coord($p, $points[$i][0], $points[$i][1]);
95 $data .= $x1 . ',' . $x2;
96 $data .= ' ' if ($i != (scalar(@points) - 1));
97 }
98 $p->{'img'}->Draw(fill=>_get_color($p, $color), bordercolor=>_get_color($p, $color), stroke=>_get_color($p, $color), primitive=>'polygon', points=>$data);
99 }
100 }
101  
102 sub circle {
103 my ($self, $p, $x, $y, $width, $height, $color) = @_;
104  
105 my ($x_, $y_) = _convert_coord($p, $x, $y);
106 $width = int($width/2);
107 $height = int($height/2);
108 my $data = "$x_,$y_ $width,$height 0,360";
109 $p->{'img'}->Draw(stroke=>_get_color($p, $color), primitive=>'ellipse', points=>$data, fill=>'none', strokewidth=>$p->{'linewidth'});
110 }
111  
112 sub string {
113 my ($self, $p, $name, $size, $color, $x, $y, $text, $halign,
114 $valign, $orientation) = @_;
115 my ($offset_x, $offset_y) = (0, 0);
116 $halign = $ALIGN_CENTER if (! defined($halign));
117 $valign = $ALIGN_BOTTOM if (! defined($valign));
118 $orientation = $ORIENTATION_RIGHT if (! defined($orientation));
119  
120 my $rotate = 0;
121 $rotate = 90 if ($orientation eq $ORIENTATION_DOWN);
122 $rotate = -90 if ($orientation eq $ORIENTATION_UP);
123
124 my ($character_width, $character_height, $ascender, $descender, $text_width, $text_height, $maximum_horizontal_advance) =
125 $p->{'img'}->QueryFontMetrics(font=>$name, pointsize=>$size, fill=>_get_color($p, $color), text=>$text, rotate=>$rotate, antialias=>'true');
126  
127 #die "Can't generate text : $@" if (! scalar(@b));
128 if ($orientation eq $ORIENTATION_UP) {
129 #($halign eq $ALIGN_CENTER) && ($offset_y = );
130 #($halign eq $ALIGN_LEFT) && ($offset_y = );
131 #($halign eq $ALIGN_RIGHT) && ($offset_y = );
132 #($valign eq $ALIGN_CENTER) && ($offset_x = );
133 #($valign eq $ALIGN_TOP) && ($offset_x = );
134 #($valign eq $ALIGN_BOTTOM) && ($offset_x = );
135 } elsif ($orientation eq $ORIENTATION_DOWN) {
136 ($halign eq $ALIGN_CENTER) && ($offset_y = -int($text_width / 2));
137 ($halign eq $ALIGN_LEFT) && ($offset_y = -$text_width);
138 ($halign eq $ALIGN_RIGHT) && ($offset_y = 0);
139 ($valign eq $ALIGN_CENTER) && ($offset_x = -$text_height - int($text_height / 2));
140 ($valign eq $ALIGN_TOP) && ($offset_x = -$text_height);
141 ($valign eq $ALIGN_BOTTOM) && ($offset_y = -$text_height - $text_height);
142 } elsif ($orientation eq $ORIENTATION_LEFT) {
143 #($halign eq $ALIGN_CENTER) && ($offset_x = );
144 #($halign eq $ALIGN_LEFT) && ($offset_x = );
145 #($halign eq $ALIGN_RIGHT) && ($offset_x = );
146 #($valign eq $ALIGN_CENTER) && ($offset_y = );
147 #($valign eq $ALIGN_TOP) && ($offset_y = );
148 #($valign eq $ALIGN_BOTTOM) && ($offset_y = );
149 } elsif ($orientation eq $ORIENTATION_RIGHT) {
150 ($halign eq $ALIGN_CENTER) && ($offset_x = -int($text_width / 2));
151 ($halign eq $ALIGN_LEFT) && ($offset_x = 0);
152 ($halign eq $ALIGN_RIGHT) && ($offset_x = -$text_width);
153 ($valign eq $ALIGN_CENTER) && ($offset_y = int($text_height / 2));
154 ($valign eq $ALIGN_TOP) && ($offset_y = $text_height);
155 ($valign eq $ALIGN_BOTTOM) && ($offset_y = 0);
156 }
157 my ($nx, $ny) = _convert_coord($p, $x, $y);
158 $p->{'img'}->Annotate(font=>$name, pointsize=>$size, fill=>_get_color($p, $color), text=>$text, x=>$nx + $offset_x, y=>$ny + $offset_y, rotate=>$rotate, antialias=>'true');
159 }
160  
161 # OUTPUT FUNCTIONS
162 sub save_to {
163 my ($self, $p, $filename) = @_;
164 $p->{'img'}->Write(filename=>$filename, compression=>'None');
165 }
166  
167 sub dump {
168 my ($self, $p) = @_;
169 binmode STDOUT;
170 $p->{'img'}->Write('png:-');
171 }
172  
173 # PRIVATE FUNCTIONS
174 sub _get_color {
175 my ($p, $color) = @_;
176 my ($red, $green, $blue) = @{$color};
177 my $alpha = 0;
178 $alpha = $color->[3] if ($#{$color}>=3);
179 my $coln = sprintf ("#%02X%02X%02X%02X", $red, $green, $blue, $alpha);
180 return $coln;
181 }
182  
183 sub _convert_coord {
184 my ($p, $x, $y) = @_;
185 return ($x, $p->{'zone'}->external_height() - $y - 1);
186 }
187  
188 1;