Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Table Pictures

by fenonn (Chaplain)
on Jun 11, 2001 at 20:57 UTC ( [id://87579]=CUFP: print w/replies, xml ) Need Help??

When I started my current job in web development at the university I attend, I was required to do a lot of work with HTML tables. I was put through a “table hell” as they call it. It happens that the last emplyee that was hired gets to make the “table hell” for the next incoming employee. I didn’t help create something really wild but the job of making this “table hell” gave me an evil idea. I wondered if I could create an HTML table that had all 1x1 pixel table data with nothing but a background color. That way I could, in a very inefficient way, display graphics using only HTML. I hadn’t learned perl at the time so I just keep the idea in my head.

Shortly thereafter I started learning perl, and decide to revisit this idea because it sounded like a somewhat easy task in perl. I decided that the best place to get data would be from and BMP file because of the lack of compression. After about ten minutes of coding I produced this very useless script that takes an ordinary windows BMP file and converts it into a HTML document.

#!/usr/bin/perl -w use strict; my $bmp = "pic.bmp"; my $html = "pic.html"; my $temp; open (IN, "<$bmp") || die; while(<IN>) { $temp .= $_; } close IN; my $col = (ord(substr $temp,18,1)) + (256*(ord(substr $temp,19,1))); my $row = (ord(substr $temp,22,1)) + (256*(ord(substr $temp,23,1))); my $numread = $col * 3; while ( ($numread % 4) != 0 ) { $numread++; } open (OUT, ">$html") || die; print OUT "<html><head></head><body><table border='0' cellpadding='0' +cellspacing='0'>"; for (my $i = ($row-1); $i >= 0; $i--) { print OUT "<tr>"; for my $j (0..($col-1)) { print OUT "<td height='1' width='1' bgcolor='#"; for (my $x = 2; $x >= 0; $x--) { my $temp1 = ord(substr($temp,54+$i*$numread+$j*3+$x,3)); print OUT sprintf "%02x",$temp1; } print OUT "'></td>"; } print OUT "</tr>"; } print OUT "</table></body></html>"; close OUT;
Beware running this script as it has almost no use. Also note that the output of a 400 by 400 BMP could be larger than 10MB. I havn't done extensive testing with this script either. I have only been using 24-bit BMP files created by windows paint.

UPDATED: Added some info on what BMP files I had been using.

Replies are listed 'Best First'.
Re: Table Pictures
by John M. Dlugosz (Monsignor) on Jun 11, 2001 at 22:18 UTC
    You forgot binmode. Perhaps you didn't use a Windows system, even though you're using Windows BMP files?

    Some other ideas: you can essentially use an offset with unpack by using a "skip a byte" code with a multiplier. That would be far easier than using ord arithmetic and substr. How about (untested)

    my ($col, $row)= unpack ("x18 v2", $temp);
    —John
Re: Table Pictures
by mr_mischief (Monsignor) on Jun 12, 2001 at 05:18 UTC
    I'm not sure how much of a cool 'use' for Perl this is, but I find it to be a pretty cool demonstration. A cool 'abuse' for Perl, perhaps. ;-)

    I don't know where or why this would be a good idea, but if you made the TDs, say, 4 by 4 and had only a small, simple graphic to generate on the fly but didn't have any graphics libs available, it might find actual use somewhere.

    I must admit, the thought of using a table full of 16 * 16 PNGs for a tile-based online game has crossed my mind before. One by one is more than just a little scary, though.

    In any case, this is an interesting thing you've put together.

    ++,
    Chris

    Update: corrected a typo. Thanks to John M. Dlugosz for pointing it out.
Re: Table Pictures
by naChoZ (Curate) on Jun 12, 2001 at 05:42 UTC
    Haha, this brings back some memories... Way back in high school I had this computer teacher that was teaching us basic programming. My friend and I already knew more than he did so it was mostly a boring class. We did manage find ways to keep interesting, though.

    He used to give us extra credit for writing basic programs that drew stuff on the screen. So we wrote this thing that looked at each pixel on the screen and generated output which we then saved as a basic program. The program reproduced what had been on the screen with nothing but pset statements. (This was back in the monochrome days, no color to worry about). So for extra credit we submitted our "program" that drew a picture of a house that we snagged. Total time, about 45 minutes. Total pages of printed output, 38 or 39.... He bought it too, surprisingly enough.

Something very similar...
by gaudior (Pilgrim) on Jun 12, 2001 at 20:08 UTC
    ... was done before.

    Edit: chipmunk 2001-06-12

Log In?
Username:
Password:

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

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

    No recent polls found