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