1- // BMP-related data types based on Microsoft's own
1+ // bmp.h
2+ // Cross-platform BMP header definitions (CS50-style)
23
3- #include <stdint.h>
4-
5- // These data types are essentially aliases for C/C++ primitive data types.
6- // Adapted from http://msdn.microsoft.com/en-us/library/cc230309.aspx.
7- // See https://en.wikipedia.org/wiki/C_data_types#stdint.h for more on stdint.h.
4+ #ifndef BMP_H
5+ #define BMP_H
86
9- typedef uint8_t BYTE ;
10- typedef uint32_t DWORD ;
11- typedef int32_t LONG ;
12- typedef uint16_t WORD ;
7+ #include <stdint.h>
138
14- // The BITMAPFILEHEADER structure contains information about the type, size,
15- // and layout of a file that contains a DIB [device-independent bitmap].
16- // Adapted from http://msdn.microsoft.com/en-us/library/dd183374(VS.85).aspx.
9+ // Use 1-byte alignment across all compilers
10+ #ifdef _MSC_VER
11+ #pragma pack(push, 1)
12+ #else
13+ #pragma pack(push, 1)
14+ #endif
1715
16+ // --- Bitmap File Header (14 bytes) ---
1817typedef struct
1918{
20- WORD bfType ;
21- DWORD bfSize ;
22- WORD bfReserved1 ;
23- WORD bfReserved2 ;
24- DWORD bfOffBits ;
25- } __attribute__((__packed__ ))
26- BITMAPFILEHEADER ;
27-
28- // The BITMAPINFOHEADER structure contains information about the
29- // dimensions and color format of a DIB [device-independent bitmap].
30- // Adapted from http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx.
31-
19+ uint16_t bfType ; // File type ("BM")
20+ uint32_t bfSize ; // Size of the file in bytes
21+ uint16_t bfReserved1 ; // Reserved; must be 0
22+ uint16_t bfReserved2 ; // Reserved; must be 0
23+ uint32_t bfOffBits ; // Offset to start of pixel data
24+ } BITMAPFILEHEADER ;
25+
26+ // --- Bitmap Info Header (40 bytes for BITMAPINFOHEADER) ---
3227typedef struct
3328{
34- DWORD biSize ;
35- LONG biWidth ;
36- LONG biHeight ;
37- WORD biPlanes ;
38- WORD biBitCount ;
39- DWORD biCompression ;
40- DWORD biSizeImage ;
41- LONG biXPelsPerMeter ;
42- LONG biYPelsPerMeter ;
43- DWORD biClrUsed ;
44- DWORD biClrImportant ;
45- } __attribute__((__packed__ ))
46- BITMAPINFOHEADER ;
47-
48- // The RGBTRIPLE structure describes a color consisting of relative intensities of
49- // red, green, and blue. Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
50-
29+ uint32_t biSize ; // Header size (40 bytes)
30+ int32_t biWidth ; // Image width in pixels
31+ int32_t biHeight ; // Image height in pixels
32+ uint16_t biPlanes ; // Number of color planes (1)
33+ uint16_t biBitCount ; // Bits per pixel (24 for RGB)
34+ uint32_t biCompression ; // Compression type (0 = none)
35+ uint32_t biSizeImage ; // Image size (may be 0 if uncompressed)
36+ int32_t biXPelsPerMeter ; // Horizontal resolution (pixels/meter)
37+ int32_t biYPelsPerMeter ; // Vertical resolution (pixels/meter)
38+ uint32_t biClrUsed ; // Number of colors used
39+ uint32_t biClrImportant ; // Number of important colors
40+ } BITMAPINFOHEADER ;
41+
42+ // --- RGB Triple (3 bytes per pixel) ---
5143typedef struct
5244{
53- BYTE rgbtBlue ;
54- BYTE rgbtGreen ;
55- BYTE rgbtRed ;
56- } __attribute__((__packed__ ))
57- RGBTRIPLE ;
45+ uint8_t rgbtBlue ;
46+ uint8_t rgbtGreen ;
47+ uint8_t rgbtRed ;
48+ } RGBTRIPLE ;
49+
50+ #pragma pack(pop)
51+
52+ #endif // BMP_H
0 commit comments