-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixel4.pl
More file actions
193 lines (157 loc) · 3.59 KB
/
pixel4.pl
File metadata and controls
193 lines (157 loc) · 3.59 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
184
185
186
187
188
189
190
191
192
193
use IO::Socket::INET;
use GD;
#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));
$num_children = $ARGV[3];
$num_children = 9 if (!defined($num_children));
$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";
$section_size_x = int($img_width / sqrt($num_children));
$section_size_y = int($img_height / sqrt($num_children));
undef($image);
$child_number = 0;
STARTFORK:
#Fork Here and have the children render their own parts.
$start = "";
$end = "";
$start_height = 0;
$end_height = 0;
$child_number = 0;
undef($children);
#############################################################
$px = 0;
$py = 0;
$run = 1;
while($child_number < $num_children)
{
$pid = fork();
if ($pid == 0)
{
$start = $section_size_x * $px;
$end = $section_size_x * ($px + 1);
$start_height = $section_size_y * $py;
$end_height = $section_size_y * ($py + 1);
print "$child_number) I will take rows $start x $end TO $start_height x $end_height\n";
StartMinion();
last;
}
else
{
$children[$child_number] = $pid;
}
if ($px >= (sqrt($num_children)-1))
{
$px = 0;
$py++;
}
else
{
$px++;
}
$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
$| = 1;
my ($socket,$data);
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
while(1)
{
for($i=$start;$i<$end;$i++)
{
for($j=$start_height;$j<$end_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");
}
}
$socket->send($data);
undef($data);
}
}
sub KillMinions
{
for($k=0;$k<$num_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);
}