in reply to jpg to bmp in Windows

If you have GD this will work for 24-bit color jpgs:

#! perl -slw use strict; use GD; my $img = GD::Image->newFromJpeg( $ARGV[ 0 ], 1 ) or die "$ARGV[ 0 ] : $!"; my( $width, $height ) = $img->getBounds(); ( my $bmpFile = $ARGV[ 0 ] ) =~ s[.jpg$][.bmp]i; open BMP, '>:raw', $bmpFile or die "$bmpFile : $!"; binmode BMP; printf BMP '%s', pack 'A2ISSI', 'BM', $width * $height +54, 0, 0, 54; printf BMP '%s', pack 'IiiSSIIiiII', 40, $width, $height, 1, 24, 0, $width * $height, 11811, 11811, 0, +0; for my $y ( reverse 0 .. $height-1 ) { for my $x ( 0 .. $width-1 ) { printf BMP '%s', pack 'CCC', reverse $img->rgb( $img->getPixel +( $x, $y ) ); } } close BMP;

Other color depths take a little more effort and it could be sped up quite easily.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Replies are listed 'Best First'.
Re^2: jpg to bmp in Windows
by jettero (Monsignor) on Jun 05, 2007 at 13:33 UTC
    Why isn't this a member function of GD::Image?

    -Paul

      Probably because the underlying GD Library doesn't have support for Windows .BMPs.

      And that's probably because there is no existing open C library available for load/saving .BMPs as there are for .jpgs, .gifs etc.

      And that maybe, though I haven't checked, because MS have never freely published the spcs for .BMPs?

      Although my snippet works, for simple, uncompressed BMPs, the references I've seen suggest that 'the spec' has a multitude of extensions.


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: jpg to bmp in Windows
by Anonymous Monk on Sep 02, 2008 at 14:28 UTC
    Why does this only work with 400x300pix images?
    I have a 600x240 image. The bmp will be created, but i can't look at it nor use it.
    Do i have to change some of these lines to work correctly?

      There are no size limitations with the code. I just ran it on the biggest .jpg I had kicking around and it ran fine. Slow, but fine:

      c:\test\img\tmp>imageinfo --verbose worldx2-2.jpg worldx2-2.jpg File format: JPEG MIME type: image/jpeg Width (pixels): 5400 Height (pixels): 2700 Bits per pixel: 24 Number of images: 1 Physical width (dpi): 72 Physical height (dpi): 72 Physical width (inches): 75 Physical height (inches): 37.500000 Number of textual comments: 0 c:\test\img\tmp>..\..\Jpg2bmp.pl worldx2-2.jpg c:\test\img\tmp>imageinfo --verbose worldx2-2.bmp worldx2-2.bmp File format: BMP MIME type: image/bmp Width (pixels): 5400 Height (pixels): 2700 Bits per pixel: 24 Number of images: 1 Physical width (dpi): 300 Physical height (dpi): 300 Physical width (inches): 18 Physical height (inches): 9 Number of textual comments: 0

      However, as noted above, it does only work for 24-bit images! I've updated the node with a slightly modified version that will abort if the image is not 24-bit.

      It could be modified to handle palette images but it is a fair amount of work and there are far better (faster & more comprehensive) tools out there for performing image conversions.


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.