write_bmp.c revision 037b3c26
1/*
2 * Copyright 2011      Luc Verhaegen <libv@codethink.co.uk>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 */
24/*
25 * Quick 'n Dirty bitmap dumper.
26 */
27#include <stdio.h>
28#include <unistd.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <string.h>
33#include <errno.h>
34
35#include "write_bmp.h"
36
37#define FILENAME_SIZE 1024
38
39struct bmp_header {
40	unsigned short magic;
41	unsigned int size;
42	unsigned int unused;
43	unsigned int start;
44} __attribute__((__packed__));
45
46struct dib_header {
47	unsigned int size;
48	unsigned int width;
49	unsigned int height;
50	unsigned short planes;
51	unsigned short bpp;
52	unsigned int compression;
53	unsigned int data_size;
54	unsigned int h_res;
55	unsigned int v_res;
56	unsigned int colours;
57	unsigned int important_colours;
58	unsigned int red_mask;
59	unsigned int green_mask;
60	unsigned int blue_mask;
61	unsigned int alpha_mask;
62	unsigned int colour_space;
63	unsigned int unused[12];
64} __attribute__((__packed__));
65
66static int
67bmp_header_write(int fd, int width, int height, int bgra, int noflip, int alpha)
68{
69	struct bmp_header bmp_header = {
70		.magic = 0x4d42,
71		.size = (width * height * 4) +
72		sizeof(struct bmp_header) + sizeof(struct dib_header),
73		.start = sizeof(struct bmp_header) + sizeof(struct dib_header),
74	};
75	struct dib_header dib_header = {
76		.size = sizeof(struct dib_header),
77		.width = width,
78		.height = noflip ? -height : height,
79		.planes = 1,
80		.bpp = 32,
81		.compression = 3,
82		.data_size = 4 * width * height,
83		.h_res = 0xB13,
84		.v_res = 0xB13,
85		.colours = 0,
86		.important_colours = 0,
87		.red_mask = 0x000000FF,
88		.green_mask = 0x0000FF00,
89		.blue_mask = 0x00FF0000,
90		.alpha_mask = alpha ? 0xFF000000 : 0x00000000,
91		.colour_space = 0x57696E20,
92	};
93
94	if (bgra) {
95		dib_header.red_mask = 0x00FF0000;
96		dib_header.blue_mask = 0x000000FF;
97	}
98
99	write(fd, &bmp_header, sizeof(struct bmp_header));
100	write(fd, &dib_header, sizeof(struct dib_header));
101
102	return 0;
103}
104
105void
106bmp_dump32(char *buffer, unsigned width, unsigned height, bool bgra, const char *filename)
107{
108	int fd;
109
110	fd = open(filename, O_WRONLY| O_TRUNC | O_CREAT, 0666);
111	if (fd == -1) {
112		printf("Failed to open %s: %s\n", filename, strerror(errno));
113		return;
114	}
115
116	bmp_header_write(fd, width, height, bgra, false, true);
117
118	write(fd, buffer, width * height * 4);
119}
120
121void
122bmp_dump32_noflip(char *buffer, unsigned width, unsigned height, bool bgra, const char *filename)
123{
124	int fd;
125
126	fd = open(filename, O_WRONLY| O_TRUNC | O_CREAT, 0666);
127	if (fd == -1) {
128		printf("Failed to open %s: %s\n", filename, strerror(errno));
129		return;
130	}
131
132	bmp_header_write(fd, width, height, bgra, true, true);
133
134	write(fd, buffer, width * height * 4);
135}
136
137void
138bmp_dump32_ex(char *buffer, unsigned width, unsigned height, bool flip, bool bgra, bool alpha, const char *filename)
139{
140	int fd;
141
142	fd = open(filename, O_WRONLY| O_TRUNC | O_CREAT, 0666);
143	if (fd == -1) {
144		printf("Failed to open %s: %s\n", filename, strerror(errno));
145		return;
146	}
147
148	bmp_header_write(fd, width, height, bgra, flip, alpha);
149
150	write(fd, buffer, width * height * 4);
151}
152