-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixel5.pl
More file actions
183 lines (153 loc) · 3.6 KB
/
pixel5.pl
File metadata and controls
183 lines (153 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use IO::Socket::INET;
use GD;
use POSIX;
#For the next version load the text into an array not just the hex color.
#Load the lenght of a packet into their own array so I can just push it out.
#When loading the array set it up to have different drawing methods instead of line by line draw.
#$imagefile = "C:/Users/Awol/Pictures/m2018logo.png";
$imagefile = $ARGV[0];
if (substr($imagefile,-4) eq ".png")
{
$image = GD::Image->newFromPng($imagefile, 1);
$img_type = "PNG";
}
else
{
$image = GD::Image->new($imagefile);
$img_type = "OTHER";
}
$start_x = $ARGV[1];
$start_x = 0 if (!defined($start_x));
$start_y = $ARGV[2];
$start_y = 0 if (!defined($start_y));
$screen_width = 1280;
$screen_height = 720;
#Load Image into an 2d Array
for($i=0;$i<$image->width;$i++)
{
for($j=0;$j<$image->height;$j++)
{
$index = $image->getPixel($i,$j);
@rgb = $image->rgb($index);
$hex = rgbToHex(@rgb);
if (($img_type eq "PNG" && $index >= 1<<24))
{
$img[$i][$j] = "tran";
}
else
{
$img[$i][$j] = $hex;
}
}
}
$img_width = $image->width;
$img_height = $image->height;
print "Image Size = $img_width x $img_height\n";
undef($image);
print "Creating Protocal String\n";
$data = "";
for($i=0;$i<$img_width;$i++)
{
for($j=0;$j<$img_height;$j++)
{
$new_x = $start_x + $j;
$new_y = $start_y + $i;
$hex = $img[$j][$i];
$data = $data . "PX $new_x $new_y $hex\n" if ($hex ne "tran");
}
}
print "Done Creating String\n";
$temp = length($data);
print "$temp\n";
$data_parts = ceil($temp / 1400);
print "$data_parts will be created\n";
#Split the $data string into TCP packet size chucks
$slice = ceil($data_parts / 9);
@parts = "";
for($i=0;$i<$data_parts;$i++)
{
$start_str = $i * 1400;
$parts[$i] = substr($data,$start_str,1400);
#print "$i - " .length($parts[$i]). "\n";
}
STARTFORK:
#Fork Here and have the children render their own parts.
$child_number = 0;
@children = "";
#############################################################
$run = 1;
for ($i=0;$i<$data_parts;$i++)
{
$stuff_2_send = $stuff_2_send . $parts[$i];
if (($i % $slice) == 0)
{
$pid = fork();
if ($pid == 0)
{
print "$child_number) I will take rows ". length($stuff_2_send) ."\n";
StartMinion($stuff_2_send);
last;
}
else
{
$children[$child_number] = $pid;
$stuff_2_send = "";
$child_number++;
}
}
}
#############################################################
#If I am the parent I need to do something else.
if ($pid != 0)
{
print "Master awaiting Commands\n";
while(1)
{
$cmd=<STDIN>;
chomp($cmd);
@cmdline = split(/ /,$cmd);
#print "You typed $cmd\n";
if ($cmdline[0] eq "quit")
{
KillMinions();
exit;
}
if ($cmdline[0] eq "move")
{
print "moving!\n";
$start_x = $cmdline[1];
$start_y = $cmdline[2];
KillMinions();
goto STARTFORK;
}
}
}
sub StartMinion
{
# We call IO::Socket::INET->new() to create the TCP Socket
# flush after every write
my $data2send = shift(@_);
#$| = 1;
my ($socket);
print "Minion $child_number Running\n";
$socket = new IO::Socket::INET (PeerAddr => 'maglan-srv-blade05.lan.magfest.net:1234', Proto => 'tcp') or die "ERROR in Socket Creation : $!\n";
#send operation
#Create an string of the protocal and save.
while(1)
{
$socket->send($data2send);
}
}
sub KillMinions
{
for($k=0;$k<@children;$k++)
{
kill(9,$children[$k]);
}
}
sub rgbToHex {
$red=$_[0];
$green=$_[1];
$blue=$_[2];
return sprintf("%2.2X%2.2X%2.2X",$red,$green,$blue);
}