core/vil/file_formats/vil_bmp_file_header.cxx
Go to the documentation of this file.
00001 // This is core/vil/file_formats/vil_bmp_file_header.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "vil_bmp_file_header.h"
00010 #include <vcl_iomanip.h> // for vcl_hex, vcl_dec
00011 #include <vcl_iostream.h>
00012 #include <vil/vil_stream.h>
00013 #include <vil/vil_stream_write.h>
00014 #include <vil/vil_stream_read.h>
00015 
00016 // The signature consists of the two bytes 42, 4D in that order.
00017 // It is not supposed to be read as a 16-bit integer.
00018 #define BMP_SIGNATURE_BYTE_0 0x42
00019 #define BMP_SIGNATURE_BYTE_1 0x4D
00020 
00021 vil_bmp_file_header::vil_bmp_file_header()
00022 {
00023   magic[0] = BMP_SIGNATURE_BYTE_0;
00024   magic[1] = BMP_SIGNATURE_BYTE_1;
00025   file_size = 0;
00026   reserved1 = 0;
00027   reserved2 = 0;
00028   bitmap_offset = 0;
00029 }
00030 
00031 void vil_bmp_file_header::print(vcl_ostream &s) const
00032 {
00033   s << "vil_bmp_file_header:\n"
00034     << "  magic   : " << vcl_hex
00035     << "0x" << unsigned(magic[0]) << ' '
00036     << "0x" << unsigned(magic[1]) << vcl_endl
00037     << "  filesize: 0x" << file_size << vcl_endl
00038     << "  reserved: 0x" << reserved1 << vcl_endl
00039     << "  reserved: 0x" << reserved2 << vcl_endl
00040     << "  offset  : 0x" << bitmap_offset << vcl_endl
00041     << vcl_dec << vcl_endl;
00042 }
00043 
00044 void vil_bmp_file_header::read(vil_stream *s)
00045 {
00046   if (s->read(&magic, sizeof(magic)) == 0) {magic[0] = magic[1] = 0;}
00047   file_size = vil_stream_read_little_endian_uint_32(s);
00048   reserved1 = vil_stream_read_little_endian_uint_16(s);
00049   reserved2 = vil_stream_read_little_endian_uint_16(s);
00050   bitmap_offset = vil_stream_read_little_endian_uint_32(s);
00051 }
00052 
00053 void vil_bmp_file_header::write(vil_stream *s) const
00054 {
00055   s->write(&magic, sizeof(magic));
00056   vil_stream_write_little_endian_uint_32(s, file_size);
00057   vil_stream_write_little_endian_uint_16(s, reserved1);
00058   vil_stream_write_little_endian_uint_16(s, reserved2);
00059   vil_stream_write_little_endian_uint_32(s, bitmap_offset);
00060 }
00061 
00062 bool vil_bmp_file_header::signature_valid() const
00063 {
00064   return
00065     magic[0] == BMP_SIGNATURE_BYTE_0 &&
00066     magic[1] == BMP_SIGNATURE_BYTE_1;
00067 }