convert BMP 256col to raw image data ?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Is there a perl-ish way to strip the header off a BMP image and get only
the raw image data ?

My BMP input is 256 color, and I need to have only the image data, like
explained in

The forum speaks of BMP2RAW which I can call for the file from Perl, but I
would like to do it natively.

Is pack() / unpack () the solution ?

Re: convert BMP 256col to raw image data ?

Quoted text here. Click to load it

   Unfortunately, there a several variables for BMP files (such as
compressed/non-compressed, different number of bits per pixel), so to
do this correctly you'd have to handle them all.

   Are you familiar with *.ppm files (specifically, of type "P6")?
This looks to be just about exactly what you want, where each pixel is
encoded as 24 bits (that is, one byte for RED, one byte for GREEN, and
one byte for BLUE).  The only difference is that *.ppm files include a
short header to specify the width, height, and range of intensity

   You can read more about this file type here:

The nice thing about this file format is that several free image
manipulator programs & libraries (such as "ImageMagick", "xv", and
"The Gimp") support this type, so converting an image of a well-known
type to a *.ppm file is very simple.

   And writing to this type of file is very simple, as well.  Since
there is no compression and just a very short header, all you really
need to know (aside from the width & height of the image) is the RGB
values of each pixel.

   As for converting a *.bmp file to a *.ppm file, you'll have to
learn how to read a *.bmp file into an list of pixels, as well as
extract out its width & height.  Since there are several variable
things you have to watch out for, it will probably just be better for
you to use a free third-party software tool to convert it to a *.ppm
file for you.

   And once you have a *.ppm file, you can just read it in as a binary
file (using Perl if you want), strip off the short header, and you
have exactly what you want!

   I hope this helps, Torben.

   -- Jean-Luc

Re: convert BMP 256col to raw image data ?

Quoted text here. Click to load it

Yes, but I need only the 8-bit, as described in the page I linked to.

I have the palette, and just need the "raw" image data in 8 bit.

In this way, the pixel color is directly coded in the file, one byte per
pixel. The total file size in byte is exactly equal to height x width,
example would 320x200 pixel be 64,000 bytes.

Maybe it's just a matter of stripping n bytes from the file, but not sure.

The application btw is an embedded controller run by a Xilinx FPGA. So no
operating system, images are directly output by memory copy. That's why I
need the raw format.

Hope this helps.


Re: convert BMP 256col to raw image data ?

Quoted text here. Click to load it

Yes (specifically unpack). You will also need, and
some understanding of C structures.

An alternative would be to use one of the many image-handling libraries
on CPAN, for example Imager, which will let you get the raw bits out in
any format you like.


Re: convert BMP 256col to raw image data ?

Quoted text here. Click to load it

Thank you for the links.

Looked at Imager, but can't really an example of how to do.

Can you show an example ?

Site Timeline