http://qs321.pair.com?node_id=11147860


in reply to Re^2: Convert BMP to HTML
in thread Convert BMP to HTML

Have you compared yours to what cavac published as Re^3: Shameless plug and QR japh in April, in the second code block? Since GD::Image can handle multiple image types, cavac's has more input options. But looking at yours, I see that it might have color reduction/compression (like changing the default color on the table to the most prevalent color in the image). I also see "canvas" mentioned in your code, but I'm not sure if that's just there because it isn't using GD::Image, or whether it was doing something unique with a real HTML5 <canvas> , or whether you just borrowed that term from HTML5 (since Perl Monks Approved HTML tags doesn't list <canvas> among our assets).

Replies are listed 'Best First'.
Re^4: Convert BMP to HTML
by harangzsolt33 (Chaplain) on Oct 31, 2022 at 14:38 UTC
    Yes! It seems that does exactly the same things. Except I noticed that he uses "</TD>" and "</TR>" closing elements also. Mine doesn't do that, because it's unnecessary. All web browsers know that a "</TD>" block ends when another one begins or when TABLE object ends. Every byte counts. In my code, I also compress colors. For example, FC0102 becomes "RED" because it looks like red. Then black becomes a single zero, because web browsers accept it. Of course, it might not be standard or official, but if I can get away with it, then that's fine. So instead of saying BGCOLOR=BLACK, I just do BGCOLOR=0. Green would be BGCOLOR=00FF00 but I just write it as BGCOLOR=00F# because web browsers correctly interpret it as green.

    I have thought about using ROWSPAN as well, but I would have to significantly rewrite the code to detect not just long repeating lines of same color but box shaped areas which are all made up of the same color.

    I use the word "CANVAS" in my code simply because ReadBMP converts the BMP file to an intermediary format that my script can work with. This image format is a single string that always starts with the word "CANVAS." That's how the program recognizes that the string is an image. The next 2 bytes hold the image format, which is then followed by 4-byte width and 4-byte height of the image. Then 3 bytes for the red, green, blue values for the first pixel starting in the upper left hand corner. This format is pretty straightforward (to me), because I designed it. And I also wrote ReadRAS() ReadRGB() and ReadPXR() subs, which are obsolete formats that are not supported by Imager. I don't understand why, because, for example, SUN Raster image format is a very clean and easy to understand format, and many times RAS files are smaller than BMP files. Writing a script to read and write RAS images is a whole lot simpler than writing a script that reads and writes BMP files. BMP files contain so much unnecessary complexity! Anyway, once the image is read and decoded, it is stored as a string whose first few bytes start with the word "CANVAS" and then the functions that I write simply act on that string. I can read a pixel using either substr() and vec() or unpack(). Those are my tools. I found that calling a SetPixel() or GetPixel() function slows down the code, because if you have to call a function millions of times, it adds significant delay. And when you're writing it in pure perl, it's better not to put that into separate subs. So, my getpixel and putpixel methods look like single substr() calls.

    my ($R, $G, $B) = unpack('CCC', substr($$CANVAS, $PTR, 3)); # Reads a single pixel.

    substr($$CANVAS, $PTR, 3) = "\0\0\xFF"; # Writes a single blue pixel somewhere.

      Making assumptions about error correcting heuristics of some current browsers just to save some bytes is not a good strategy.

      For instance: The markup of the monastery is basic enough to be interpreted by a new reader, like a Tk or mobile app. Those will not show the same intelligence.

      Furthermore, we do parse and filter the markup here. Unbalanced tags might lead to unwanted effects.

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

      Mine doesn't do that, because it's unnecessary. All web browsers know that a "</TD>" block ends when another one begins or when TABLE object ends

      Just because all most browsers will deal with erroneous HTML code is hardly an excuse to deliberately produce it.

        The closing tags for TD and TR are optional by the spec, it's not "erroneous HTML".

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        please put your "</TD>" into code tags.