When Parsing Bitmap files, how do I replace the WORD and DWORD data types?

In windows you can include the windows.h header file when trying to parse bmp files and the dataTypes WORD and DWORD are predefined for you (from what I have read). In linux, I need to use these dataTypes, but I do not know how to define them and cannot include the windows.h header. How do I go about doing this in C++?

Answers


Include <stdint.h>, and then you can portably replace WORD with uint16_t and DWORD with uint32_t. And BYTE with uint8_t, of course:

If you want you can add the following code:

#include <stdint.h>
typedef uint32_t DWORD;
typedef uint16_t WORD;
typedef uint8_t BYTE;

But beware! If you want to be portable, you must have in mind the endianness of the multi-byte values. Microsoft code almost always assume little-endian, but Linux can be run in both little and big-endian machines.

UPDATE:

Your new problem you talk about in the comments about the BITMAPFILEHEADER is not related to the type of the fields but to the packing of the struct: the compiler may add padding bytes between fields in the struct in order to satisfy alignment (or other requirements).

Microsoft compilers align integer type to an address multiple of its size. That is, WORD and SHORT values are aligned to an address multiple of 2, and DWORD and LONG values to multiple of 4.

If you see the definition of your struct:

typedef struct {
    WORD    bfType;      //offset 0
    DWORD   bfSize;      //offset 4
    WORD    bfReserved1; //offset 8
    WORD    bfReserved2; //offset 10
    DWORD   bfOffBits;   //offset 12
} BITMAPFILEHEADER;

Note that the bfSize field is sized 4 bytes, so it will be aligned to a multiple of 4. Instead of being in offset 2 of the struct, it will be in offset 4, adding 2 padding bytes.

Now, all Windows headers all compiled with the option #pragma pack that tells the compiler to ignore the alignment restrictions in struct fields. So the former struct is actually:

#pragma pack(push)
typedef struct {
    WORD    bfType;      //offset 0
    DWORD   bfSize;      //offset 2
    WORD    bfReserved1; //offset 6
    WORD    bfReserved2; //offset 8
    DWORD   bfOffBits;   //offset 10
} BITMAPFILEHEADER;
#pragma pack(pop)

The GCC compiler supports the #pragma pack only as a compatibility with MS compilers, and only with x86 compilers.

If you want to go fully portable you should read the struct directly, but read the bytes as a stream and build the values one by one (read 2 bytes into bfType, read 4 bytes into bfSize, etc.)

If you want to go portable to other Linux you can use the GCC pragma (but beware of the endiannes):

typedef struct __attribute__((packed)) {
    WORD    bfType;      //offset 0
    DWORD   bfSize;      //offset 2
    WORD    bfReserved1; //offset 6
    WORD    bfReserved2; //offset 8
    DWORD   bfOffBits;   //offset 10
} BITMAPFILEHEADER;

Need Your Help

MvcMailer unit tests: System.ArgumentNullException httpContext cannot be null

unit-testing asp.net-mvc-3 moq mvcmailer

I cannot successfully run unit tests for MvcMailer using the visual studio test suite and Moq.

WinBUGS Weibull Network Meta-Analysis

bayesian survival-analysis winbugs r2winbugs winbugs14

I am currently working on a meta-analysis of survival data across several clinical trials.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.