tags:

views:

139

answers:

3

What is the equavalent of the code below in c/C++?

ref BITMAPFILEHEADER bmfh = ref buffer[offset]; //Where buffer is a byte[] array
A: 

If you #include <windows.h> there should be a BITMAPFILEHEADER struct already.

PBITMAPFILEHEADER bmfh_ptr = (PBITMAPFILEHEADER)(buffer + offset);
// where buffer is a char[] array.
KennyTM
That is true but how about if your not building in or for windows environment?
Fredrick
@Fredrick: Then you don't use the Windows API, which that type is part of.
GMan
@Fredrick: Then copy the declaration of that struct in the link I've included.
KennyTM
+4  A: 

An OS agnostic C++ implementation of a 24-bit BMP reader/writer can be found: http://code.google.com/p/bitmap/source/browse/trunk/bitmap_image.hpp

The following is the struct definitions of the header and information blocks:

struct bitmap_file_header
{
   unsigned short type;
   unsigned int   size;
   unsigned short reserved1;
   unsigned short reserved2;
   unsigned int   off_bits;

   unsigned int struct_size()
   {
      return sizeof(type)     +
            sizeof(size)      +
            sizeof(reserved1) +
            sizeof(reserved2) +
            sizeof(off_bits);
   }
};

struct bitmap_information_header
{
   unsigned int   size;
   unsigned int   width;
   unsigned int   height;
   unsigned short planes;
   unsigned short bit_count;
   unsigned int   compression;
   unsigned int   size_image;
   unsigned int   x_pels_per_meter;
   unsigned int   y_pels_per_meter;
   unsigned int   clr_used;
   unsigned int   clr_important;

   unsigned int struct_size()
   {
      return sizeof(size)             +
             sizeof(width)            +
             sizeof(height)           +
             sizeof(planes)           +
             sizeof(bit_count)        +
             sizeof(compression)      +
             sizeof(size_image)       +
             sizeof(x_pels_per_meter) +
             sizeof(y_pels_per_meter) +
             sizeof(clr_used)         +
             sizeof(clr_important);
   }
};
Beh Tou Cheh
A: 

A literal translation:

BITMAPFILEHEADER *bmfh = &(buffer[offset]);

probably more natural in c++:

PBITMAPFILEHEADER bmfh = (PBITMAPFILEHEADER)(buffer+offset);

but beware that for both of these to work correctly buffer had best be defined as a character type.

Note that PBITMAPFILEHEADER is defined as BITMAPFILEHEADER * as is usual with windows naming.

Elemental