1 // This is core/vil1/file_formats/vil1_bmp_file_header.cxx
2 //:
3 // \file
4 // \author fsm
5 
6 #include <iomanip>
7 #include <iostream>
8 #include "vil1_bmp_file_header.h"
9 #ifdef _MSC_VER
10 #  include "vcl_msvc_warnings.h"
11 #endif
12 #include "vil1/vil1_stream.h"
13 #include "vil1/vil1_16bit.h"
14 #include "vil1/vil1_32bit.h"
15 
16 // The signature consists of the two bytes 42, 4D in that order.
17 // It is not supposed to be read as a 16-bit integer.
18 #define BMP_SIGNATURE_BYTE_0 0x42
19 #define BMP_SIGNATURE_BYTE_1 0x4D
20 
vil1_bmp_file_header()21 vil1_bmp_file_header::vil1_bmp_file_header()
22 {
23   magic[0] = BMP_SIGNATURE_BYTE_0;
24   magic[1] = BMP_SIGNATURE_BYTE_1;
25   file_size = 0;
26   reserved1 = 0;
27   reserved2 = 0;
28   bitmap_offset = 0;
29 }
30 
31 void
print(std::ostream & s) const32 vil1_bmp_file_header::print(std::ostream & s) const
33 {
34   s << "vil1_bmp_file_header:\n"
35     << "  magic   : " << std::hex << "0x" << unsigned(magic[0]) << ' ' << "0x" << unsigned(magic[1]) << std::endl
36     << "  filesize: 0x" << file_size << std::endl
37     << "  reserved: 0x" << reserved1 << std::endl
38     << "  reserved: 0x" << reserved2 << std::endl
39     << "  offset  : 0x" << bitmap_offset << std::endl
40     << std::dec << std::endl;
41 }
42 
43 void
read(vil1_stream * s)44 vil1_bmp_file_header::read(vil1_stream * s)
45 {
46   if (s->read(&magic, sizeof(magic)) == 0)
47   {
48     magic[0] = magic[1] = 0;
49   }
50   file_size = vil1_32bit_read_little_endian(s);
51   reserved1 = vil1_16bit_read_little_endian(s);
52   reserved2 = vil1_16bit_read_little_endian(s);
53   bitmap_offset = vil1_32bit_read_little_endian(s);
54 }
55 
56 void
write(vil1_stream * s) const57 vil1_bmp_file_header::write(vil1_stream * s) const
58 {
59   s->write(&magic, sizeof(magic));
60   vil1_32bit_write_little_endian(s, file_size);
61   vil1_16bit_write_little_endian(s, reserved1);
62   vil1_16bit_write_little_endian(s, reserved2);
63   vil1_32bit_write_little_endian(s, bitmap_offset);
64 }
65 
66 bool
signature_valid() const67 vil1_bmp_file_header::signature_valid() const
68 {
69   return magic[0] == BMP_SIGNATURE_BYTE_0 && magic[1] == BMP_SIGNATURE_BYTE_1;
70 }
71