Image::BMP - Bitmap parser/viewer
use Image::BMP; # Example one: my $img = Image::BMP->new( file => 'some.bmp', debug => 1, ); $img->view_ascii; # Example two: my $img2 = Image::BMP->new(); $img2->open_file('another.bmp'); my $color = $img2->xy(100,100); # Get pixel at 100,100 my ($r,$g,$b) = $img2->xy_rgb(100,200);
Image::BMP objects can parse and even ascii view bitmaps of the .BMP format. It can read most of the common forms of this format.
Image::BMP
It can be used to:
my $img = Image::BMP->new(file => 'some.bmp'); print "Resolution: $img->{Width} x $img->{Height}\n";
(See C<SYNOPSIS> example one)
(See C<SYNOPSIS> example two)
(See C<ADD_PIXEL> below)
It does not currently write bmap data, simply because I didn't have a use for that yet. Convince me and I'll add it.
The following data/keys are read when opening an image:
FileSize, DataOffset, HeaderSize, Width, Height, Planes, BitCount, ColorBytes, Compression, (compression enum: RGB, RLE8, RLE4, BITFIELDS) ImageSize, XpixelsPerM, YpixelsPerM, ColorsUsed, ColorsImportant
Constructs a new Image::BMP object:
Opens a file and reads the initial image information and colormap.
Opens a pipe to a command that outputs a bitmap (and reads image info/colormap). Example:
$img->open_pipe("convert some.jpg bmp:-");
Close a file.
Read the image in. Uses the file in %options if not specified.
Lookup an index in the colormap;
Lookup or set a pixel in the image by color. (Calls load if necessary)
load
Lookup or set a pixel in the image by index. (Calls load if necessary)
Lookup or set a pixel in the image by rgb values. (Calls load if necessary)
Do a print of the image in crude ASCII fashion. Useful for debugging of small images. For kicks, open an xterm, set the font to "unreadable" and view the output. (Calls load if necessary) Optional filename as a parameter to save output to a file
Get/set the debug setting. Values are:
debug
Generally only debug=0 or =1 are useful.
Get/set the remember_image setting. See ADD_PIXEL below.
remember_image
ADD_PIXEL
Get/set the add_pixel subroutine pointer.
Instead of having the object read the image into memory (or in addition to), you can process all the image data yourself by supplying a callback function:
sub my_add { my ($img,$x,$y,$r,$g,$b) = @_; print "add pixel $x,$y = $r,$g,$b\n"; } my $img = Image::BMP->new(file => 'some.bmp', add_pixel = \&my_add); $img->load;
It may be useful to note that most bitmaps are read from left to right and bottom to top (x from 0 to width, y from height to 0), though the compression can skip values.
If you supply an add_pixel callback then load will not store the image data for efficiency. This means, however, that view_ascii, xy and xy_rgb will not work. You can use add_pixel and still save the image in memory by setting remember_image.
add_pixel
view_ascii
xy
xy_rgb
I haven't seen an image like this yet, it wouldn't be hard to add.
I don't even know what that is..
This isn't tested yet - I haven't seen an image that uses this portion of RLE compression, so it currently does what I think is right and then prints a message asking you to send me the image/results.
Copyright 2004 <a href="http://GetDave.com/">David Ljung Madison</a>. All rights reserved. See: <a href="http://MarginalHacks.com/">MarginalHacks.com</a>
To install Image::BMP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Image::BMP
CPAN shell
perl -MCPAN -e shell install Image::BMP
For more information on module installation, please visit the detailed CPAN module installation guide.