Baz has asked for the wisdom of the Perl Monks concerning the following question:

#!C:/Perl/bin/perl -w use strict; #$| = 1; use CGI::Carp "fatalsToBrowser"; use GD; use CGI ":all"; my $im = GD::Image->newFromPng("m.png"); # get the image size my ($width, $height) = $im->getBounds(); my $result; my $index; my $r; my $g; my $b; my $color; for (my $x = 0; $x < $height; $x++) { $result .= "<BR>"; for (my $y = 0; $y < $width; $y++) { $index = $im->getPixel($y, $x); ($r,$g,$b) = $im->rgb($index); $color = sprintf "%02x%02x%02x", $r, $g, $b; $result.= font({color=>"$color"},"."); } } print header; print "$result\n"; print end_html;
Gives -
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
......................................................................

Replies are listed 'Best First'.
Re: ascii art trouble
by BrowserUk (Patriarch) on Mar 16, 2003 at 07:02 UTC

    You appear to have found some buffer limit in either CGI.pm, or more probably, in the AS implementation of that module. The limit appears to come into affect at the boundary between 61439 and 61440 bytes?

    This is the limit on my NT system using AS 633. YMMV.

    Try the following 4 one-liners

    perl -e"print 'x' x 61439;" perl -e"print 'x' x 61440;" perl -MCGI -e"print 'x' x 61439;" perl -MCGI -e"print 'x' x 61440;"

    On my system, the first three fill the screen with 'x's, but the fourth prints nothing at all. I haven't been able to reproduce the exact corruption you experience, but try redirecting the output to a file and you will probably find the corruption gone.

    One possible work-around is to output $result in chunks of 60000 chars or so. At least this works on my system

    #! perl -sw use strict; #$| = 1; use GD; use CGI ":all"; my $im = GD::Image->newFromPng("m.png"); my ($width, $height) = $im->getBounds(); my $result; for my $x (0 .. $width) { $result .= "<BR>"; for my $y (0 .. $height) { $result .= font( { color=> sprintf "%02x%02x%02x", $im->rgb($im->getPixel($y, + $x)) },'.'); } } print header,$/; print substr($result, $_*60000, 60000) for 0 .. int(length($result)/60 +000); print end_html;

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: ascii art trouble
by BrowserUk (Patriarch) on Mar 16, 2003 at 12:35 UTC

    As a footnote to your original problem, you might consider reducing the size of the html generated by wrapping consecutive runs of pixels of the same color in a single set of FONT tags, rather than each pixel individually.

    Using a very simple 17x11 printer icon in two colors, this reduced the size of the html from 6,039 bytes to 1,587. And on a moderately complex picture, 128x96 pixels it reduced from 360,870 bytes to 89,270.

    Remove the two comment cards from the following version of your code to see one way of acheiving this.

    #! perl -sw use strict; #$| = 1; use GD; use CGI ":all"; my $im = GD::Image->newFromPng("m.png"); my ($width, $height) = $im->getBounds(); my $result; for my $y (0 .. $height) { $result .= "<BR>\n"; for (my $x=0; $x < $width; $x++) { my $color = $im->getPixel($x, $y); my $n=1; # ++$n while $color == $im->getPixel(++$x, $y) and $x <= $width +; $result .= font( { color=> sprintf "%02x%02x%02x", $im->rgb($c +olor) } ,'#' x $n ); # --$x; } } print header,$/; print substr($result, $_*60000, 60000) for 0 .. int(length($result)/60 +000); print end_html;

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: ascii art trouble
by diotalevi (Canon) on Mar 15, 2003 at 23:45 UTC

    And that looks exactly correct - I see <font color="663333">.</font> in your HTML source which appears correct. It just so happens that your color variations are difficult to see. I tried replacing your dots with ampersands and could see something a bit clearer except it was still difficult.

    My advise to you - improve the contrast.


    Seeking Green geeks in Minnesota

      Hmmm...I'm testing this on my PC which runs Windows XP. I'm using Apache to view the script output, and I use ActivePerl to execute my scripts. Any suggestions as to why I'm having this problem? Heres a snippet of my html output, in source form -
      color="330000">.</font><font color="330000">.</font><font color="33000 +0">.</font><font color="330033">.</font><font color="333333">.</font> +<font color="333333">.</font><font color="333333">.</font><font color +="333333">.</font><font color="333333">.</font><BR><font color="33000 +0">.</font><font color="330000">.</font><font color="330000">.</font> +<font color="330000">.</font><font color="330000">.</font><font color +="330000">.</font><font color="330000">.</font><f z   w +E V I C E \ T C P I P _ { 7 7 7 7 7 7 7 7 - 7 7 7 7 - 7 7 7 7 - 7 7 7 + 7 - 7 7 7 7 7 7 7 7 7 7 7 7 } lor="330000">.</font><font color="33 +0000">.</font><font color="330000">.</font><font color="330000">.</fo +nt><font color="330000">.</font><font color="330000">.</font><font co +lor="330000">.</font><font color="330000">.</font><font color="330000 +">.</font><font color="330000">.</font><font color="330033">.</font>< +font color="330033">.</font><font color="330000">.</font><font

        The other stuff isn't in your script and shouldn't happen. Its likely a bug in one of Apache, Windows XP or your browser. Or even more likely just something with your individual install of Windows.


        Seeking Green geeks in Minnesota

Re: ascii art trouble
by jasonk (Parson) on Mar 15, 2003 at 23:35 UTC

    The answer is 'an elephant'.

    Now, what was the question?

      I think someone edited my post because it looked fine when I posted it first, anyway heres a piece of the output which is wrong -

      ..............</y T@ z="996666">..........................
      .......<f z   wE V I C E \ T C P I P _ { 7 7 7 7 7 7 7 7 - 7 7 7 7 - 7 7 7 7 - 7 7 7 7 - 7 7 7 7 7 7 7 7 7 7 7 7 } lor="330000">.......


      I should be getting all dots, but I seem to be getting glitches like the above in my output - is this a memory issue?

        Your code looks fine to me, are you sure this problem isn't on the browser side? I'd be especially suspicious of the browser if your script is hosted on a non-windows server, as the gibberish looks very windows-like.


        We're not surrounded, we're in a target-rich environment!