Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: working with png data

by marioroy (Prior)
on Sep 10, 2019 at 13:42 UTC ( [id://11105966]=note: print w/replies, xml ) Need Help??


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 ......

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11105966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found