Disassembled the title. On this plate all painted.

http://c-site.h1.ru/infa/bmp_struct.htm 

But it is not clear that the size of the BITMAP header is supposed to be 40, and I have 108. Does this mean that in this gap is BITMAPV5HEADER? I decided not to slow down the headline for a long time, but in passing to begin to disassemble the image. The @header array contains all the headers in order (as in the first link) of the file header and the BITMAP header. $ header [1] is the file size in bytes, $ header [4] is the offset from which the image begins. My image is 32-bit, which means it has no palette and 4 bytes define 3 components BGR and Alpha. Specifically, my image has a size of 48x48, so I decided to disassemble 1 line (Perl code):

 read (BMP, $data, $header[1]-$header[4], $header[4]); @image = unpack("C192", $data); print "@image"; 

On the console that's what brought.

http://saveimg.ru/show-image.php?id=6126fbac69c18fb308cd8d7b5d109595

Although I do an analysis of such a picture:

 http://saveimg.ru/show-image.php?id=26c569e841b17e84d624b377ac9aab82 <br> 

And here's the question: is my mistake in misinterpreting, misinterpreting the format or not knowing the language?

    1 answer 1

    Is it possible to just see how the BMP files are processed in the Image :: BMP package?

    UPDATE : 2011-10-14 18:26

    If you do everything with your hands, you get something like this code:

     #!/usr/bin/perl -w -- use strict; use warnings; use IO::File; use Data::Dumper; my $fh = IO::File->new( $ARGV[0] ); $fh->binmode(":bytes"); my %header; unless ($fh->sysread($_, 2)) { die "Error read first 2 bytes of file"; } ( $header{bfType} ) = unpack("A2"); if ($header{bfType} ne 'BM') { die "Invalid file header"; } unless ($fh->sysread($_, 12)) { die "Error read BMP header"; } ( $header{bfSize}, $header{bfOffset} ) = unpack("Lx2x2L"); if ($header{bfSize} != ($fh->stat)[7]) { die "Invalid BMP file size"; } print Dumper \%header; my %bitmap; unless ($fh->sysread($_, 4)) { die "Error read BITMAP length of BMP file"; } ( $bitmap{biSize} ) = unpack("l"); unless ($fh->sysread($_, $bitmap{biSize} - 4)) { die "Error read BITMAP of BMP file"; } ( $bitmap{biWidth}, $bitmap{biHeight}, $bitmap{biPlanes}, $bitmap{biBitCount}, $bitmap{biCompression}, $bitmap{biSizeImage}, $bitmap{biXPelsPerMeter}, $bitmap{biYPelsPerMeter}, $bitmap{biClrUsed}, $bitmap{biClrImportant} ) = unpack("llSSLLllLL"); print Dumper \%bitmap; my @palette; if ($bitmap{biClrUsed} > 0) { unless ($fh->sysread($_, 4 * $bitmap{biClrUsed})) { die "Error read palette of BMP file"; } @palette = map { sprintf("%08X", $_) } unpack("L" x $bitmap{biClrUsed}); print Dumper \@palette; } # Read file here... unless ($fh->seek( $header{bfOffset}, 0 )) { die "Invalid bfOffset in header found"; } my @data; my $row_bytes_length = (4 * int(($bitmap{biBitCount} * $bitmap{biWidth} + 4) / 4)) / 8; for (my $row = $bitmap{biHeight} - 1; $row >= 0; $row--) { if ($fh->sysread($_, $row_bytes_length)) { $data[ $row ] = []; for (my $col = 0; $col < $bitmap{biWidth}; $col++ ) { my $pixel = vec ($_, $col, $bitmap{biBitCount}); push (@{ $data[ $row ] }, scalar(@palette) ? ($palette[$pixel] || "{xUNDEF}") : sprintf("%08X", $pixel) ); } } else { die "Error reading image data"; } } print Dumper \@data; 
    • In this package, only the header analysis, but I need a pixel-by-pixel analysis of the raster, the image itself, in order to display it later - Dmitry Fedotkin
    • Why, are there xy methods that allow you to figure out the color of a particular pixel. Then there is add_pixel: sub my_add {my ($ img, $ x, $ y, $ r, $ g, $ b) = @_; print "add pixel $ x, $ y = $ r, $ g, $ b \ n"; } my $ img = new Image :: BMP (file => 'some.bmp', add_pixel = \ & my_add); $ img-> load; With which you can simply process the entire file. - chernomyrdin
    • And if you want to write your handler, the beginning may look something like this: gist.github.com/1286725 - chernomyrdin