Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: working with png data by marioroy
in thread working with png data by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-04-26 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found