-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBitmapFuncs.h
More file actions
56 lines (52 loc) · 1.46 KB
/
BitmapFuncs.h
File metadata and controls
56 lines (52 loc) · 1.46 KB
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
#ifndef BITMAPFUNCS_H
#define BITMAPFUNCS_H
#include "Bitmap.h"
#include "ImageData.h"
#include "endianness.h"
#include <stdlib.h>
#include <stdio.h>
static int pad_bmp(ImageData *d) {
unsigned stride = ((((d->width * d->bytesperpixel*8) + 31) & ~31) >> 3);
if(stride == d->width * d->bytesperpixel) return -1;
unsigned x,y;
unsigned outsz = d->height * stride;
unsigned char *out = malloc(outsz), *p = d->data, *q = out;
if(!out) return 0;
for(y = 0; y < d->height; ++y) {
for(x = 0; x < d->width*d->bytesperpixel; ++x)
*(q++) = *(p++);
for(; x < stride; ++x)
*(q++) = 0;
}
free(d->data);
d->data = out;
d->data_size = outsz;
return 1;
}
static void write_bmp(char *name, ImageData *d) {
FILE *f = fopen(name, "wb");
if(f) {
struct BITMAPINFOHEADER_X hdr = {
.bfType = end_htole16(0x4D42),
.bfSize = end_htole32(sizeof(hdr) + d->data_size),
.bfOffsetBits = end_htole32(sizeof(hdr)),
.biSize = end_htole32(sizeof(hdr)-14),
.biWidth = end_htole32(d->width),
/* negative height means bmp is stored from top to bottom */
.biHeight = end_htole32( - d->height),
.biPlanes = end_htole32(1),
.biBitCount = end_htole32(d->bytesperpixel * 8),
.biCompression = 0,
.biSizeImage = 0,
.biXPelsPerMeter = end_htole32(0xb11),
.biYPelsPerMeter = end_htole32(0xb11),
};
fwrite(&hdr, 1, sizeof hdr, f);
pad_bmp(d);
fwrite(d->data, 1, d->data_size, f);
fclose(f);
} else {
fprintf(stderr, "error opening %s\n", name);
}
}
#endif