-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp_output.cpp
56 lines (47 loc) · 2.14 KB
/
bmp_output.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdint.h>
#include "bmp_output.h"
#define BMP_FILE_HEADER_SIZE (14) // size of bitmap file header
#pragma pack(push,1) /* no padding between struct members */
struct BitmapHeader {
// bitmap file header
char fileType[2]; /* 'B', 'M' for bitmap*/
uint32_t fileSize; /* size of the whole file in bytes */
uint32_t reserved; /* 0 */
uint32_t byteOffset; /* byte offset from file start to image data */
// DIB header
uint32_t infoHeaderSize; /* size of the DIB header */
int32_t imageWidth;
int32_t imageHeight; /* image is top-down if this is negative */
uint16_t imagePlanes; /* 1 on non-alien devices */
uint16_t bitsPerPixel; /* 24 for RGB, 32 for XRGB */
uint32_t compressionType; /* 0 for no compression */
uint32_t imageSize; /* size of compressed image (0 for uncompressed)*/
int32_t pixelsPerMeterX;
int32_t pixelsPerMeterY;
uint32_t colorTableSize; /* number of colors in colortable 0 means no table*/
uint32_t importantColorSize; /* if 0, then all colors are 'important' */
};
#pragma pack(pop)
bool write_bmp(const char* filename, uint32_t width, uint32_t height, uint8_t* imageData) {
uint32_t imageSize = width * height * BYTESPERPIXEL;
struct BitmapHeader bmpHeader = {0};
bmpHeader.fileType[0] = 'B';
bmpHeader.fileType[1] = 'M';
bmpHeader.byteOffset = sizeof(BitmapHeader);
bmpHeader.fileSize = bmpHeader.byteOffset + imageSize;
bmpHeader.infoHeaderSize = sizeof(BitmapHeader) - BMP_FILE_HEADER_SIZE;
bmpHeader.imageWidth = width;
bmpHeader.imageHeight = height;
bmpHeader.imagePlanes = 1;
bmpHeader.bitsPerPixel = 8 * BYTESPERPIXEL;
FILE* outputFile = fopen(filename, "wb");
if (!outputFile) return false;
/* write the header */
fwrite(&bmpHeader, 1, sizeof(bmpHeader), outputFile);
/* write the image (no padding per row, assumes BYTESPERPIXEL == 4) */
fwrite(imageData, 1, imageSize, outputFile);
fclose(outputFile);
return true;
}