http://www.perlmonks.org?node_id=11105966


in reply to working with png data

Greetings,

Running parallel is possible for both examples. The latter makes use of relay (for orderly) and gather capabilities in MCE.

First Example

use strict; use warnings; use GD::Simple; use MCE::Map; my $png = GD::Image->newFromPng('test.png'); my ($width,$height) = $png->getBounds; MCE::Map->init( max_workers => 4 ); my @sw = mce_map { my ($y,$str) = ($_,''); for my $x (0..$width - 1) { $str .= getPixel($x,$y); } $str; } 0..$height - 1; MCE::Map->finish(); # print $_, "\n" for @sw; sub getPixel { my ($index) = $png->getPixel($_[0],$_[1]); my ($r,$g,$b) = $png->rgb($index); my $p = ($b > 128 || $r > 128) ? 0 : 1; return $p; }

Second Example

use strict; use warnings; use GD::Simple; use MCE; my $png = GD::Image->newFromPng('test.png'); my ($bx,$by) = $png->getBounds; my @col; # foreground colors for (0..$png->colorsTotal) { my ($r,$g,$b) = $png->rgb($_); push (@col,$_) unless ($b>128 || $r>128); } my $pdata = ''; MCE->new( max_workers => 4, chunk_size => 1, input_data => \@col, init_relay => 1, # loads MCE::Relay gather => sub { # this runs inside the parent $pdata |= $_[0]; }, user_func => sub { # run parallel my $val = ~$png->wbmp($_); # send the val to the parent process # relay makes it run serially and orderly MCE::relay { MCE->gather($val); }; } )->run(); our $zlen = int(($bx + 7) / 8); # number of Bytes per line my $pos = 6; my $y = 0; # position after Header of WBMP my @sw; while ($pos < length($pdata)) { $sw[$y] = ''; for (1..$zlen) { # convert binary data into string $sw[$y] .= sprintf("%08b",ord(substr($pdata,$pos++,1))); } # print $sw[$y], "\n"; $y++; }

The examples run well. Output matches the OP's non-parallel demonstrations.

Regards, Mario

Replies are listed 'Best First'.
Re^2: working with png data
by Anonymous Monk on Sep 10, 2019 at 19:52 UTC
    Thanks. Thats also a cool idea ......