6 #include "endianness.h"
10 static int pad_bmp(ImageData
*d
) {
11 unsigned stride
= ((((d
->width
* d
->bytesperpixel
*8) + 31) & ~31) >> 3);
12 if(stride
== d
->width
* d
->bytesperpixel
) return -1;
14 unsigned outsz
= d
->height
* stride
;
15 unsigned char *out
= malloc(outsz
), *p
= d
->data
, *q
= out
;
17 for(y
= 0; y
< d
->height
; ++y
) {
18 for(x
= 0; x
< d
->width
*d
->bytesperpixel
; ++x
)
20 for(; x
< stride
; ++x
)
28 static void write_bmp(char *name
, ImageData
*d
) {
29 FILE *f
= fopen(name
, "w");
31 struct BITMAPINFOHEADER_X hdr
= {
32 .bfType
= le16(0x4D42),
33 .bfSize
= le32(sizeof(hdr
) + d
->data_size
),
34 .bfOffsetBits
= le32(sizeof(hdr
)),
35 .biSize
= le32(sizeof(hdr
)-14),
36 .biWidth
= le32(d
->width
),
37 /* negative height means bmp is stored from top to bottom */
38 .biHeight
= le32( - d
->height
),
40 .biBitCount
= le32(d
->bytesperpixel
* 8),
43 .biXPelsPerMeter
= le32(0xb11),
44 .biYPelsPerMeter
= le32(0xb11),
46 fwrite(&hdr
, 1, sizeof hdr
, f
);
48 fwrite(d
->data
, 1, d
->data_size
, f
);
51 fprintf(stderr
, "error opening %s\n", name
);