Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
some explanations about how your code works.

Both formats are very simple, uncompressed files, with almost the same data format; which makes conversion simpler.

I detailed the gd format header in Re: About GD Image Data Output Methods.

I used this for the bmp format details.

Hopefully these comments will explain the rest.

## Get the gd format data as a string my $gd = $image->gd; ## Unpack the header (we're only interested in the width & height) and + strip that header from the image data my( undef, $width, $height, undef, undef ) = unpack 'nnnCV', substr( $ +gd, 0, 11, '' ); ## GD image data is 4 bytes (rgba); BMP image data is 3 bytes; so calu +late the length the bmp image data with occupy my $len = length( $gd ) / 4 * 3; ## Pack the required information to create a .bmp header (in two parts +). (see link above) my $bmp = pack 'a2 V V V l< l< l< v v V V l< l< V V', ## The .bmp file header has 5 fields (but 2 U16s do nothing so pack 1x +U32 value 0 for them) ## sig file-length (data+header length), 2 unused shorts as U32, an +d the offset to the rgb data. 'BM', $len + 54, 0, 54, ## BITMAPINFOHEADER 40 bytes, image width & height (negative to turn i +t the right way up!), ## color planes (1), bits/pel(24), compression type(none:0), length of + rgb data, 3 unused fields. 40, $width, -$height, 1, 24, 0, $len, 0, 0, 0; ## Convert the rgba gd data to rgb bmp data (x skips the alpha bytes) ## And tack it onto the end of the header. $bmp .= join'', unpack '(xaaa)*', $gd;

Of course, the code is crude as .bmps have a multitude of other possibilities, and (as you've seen) I don't even check for truecolor .v. palette in the gd format; but the intention was to give you something that did the job, not duplicate the functionality of image conversion software.

It wouldn't be too hard to handle palette images; but I don't think it is necessary for your purpose.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

In reply to Re^7: About GD Image Data Output Methods by BrowserUk
in thread About GD Image Data Output Methods [SOLVED] by karlgoethebier

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (5)
    As of 2018-01-24 11:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How did you see in the new year?










      Results (258 votes). Check out past polls.

      Notices?