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
39 struct bmp_header {
40 unsigned short magic;
41 unsigned int size;
42 unsigned int unused;
43 unsigned int start;
44 } __attribute__((__packed__));
45
46 struct 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
66 static void
bmp_header_write(int fd,int width,int height,int bgra,int noflip,int alpha)67 bmp_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
103 void
bmp_dump32(char * buffer,unsigned width,unsigned height,bool bgra,const char * filename)104 bmp_dump32(char *buffer, unsigned width, unsigned height, bool bgra, const char *filename)
105 {
106 int fd;
107
108 fd = open(filename, O_WRONLY| O_TRUNC | O_CREAT, 0666);
109 if (fd == -1) {
110 printf("Failed to open %s: %s\n", filename, strerror(errno));
111 return;
112 }
113
114 bmp_header_write(fd, width, height, bgra, false, true);
115
116 write(fd, buffer, width * height * 4);
117 }
118
119 void
bmp_dump32_noflip(char * buffer,unsigned width,unsigned height,bool bgra,const char * filename)120 bmp_dump32_noflip(char *buffer, unsigned width, unsigned height, bool bgra, const char *filename)
121 {
122 int fd;
123
124 fd = open(filename, O_WRONLY| O_TRUNC | O_CREAT, 0666);
125 if (fd == -1) {
126 printf("Failed to open %s: %s\n", filename, strerror(errno));
127 return;
128 }
129
130 bmp_header_write(fd, width, height, bgra, true, true);
131
132 write(fd, buffer, width * height * 4);
133 }
134
135 void
bmp_dump32_ex(char * buffer,unsigned width,unsigned height,bool flip,bool bgra,bool alpha,const char * filename)136 bmp_dump32_ex(char *buffer, unsigned width, unsigned height, bool flip, bool bgra, bool alpha, const char *filename)
137 {
138 int fd;
139
140 fd = open(filename, O_WRONLY| O_TRUNC | O_CREAT, 0666);
141 if (fd == -1) {
142 printf("Failed to open %s: %s\n", filename, strerror(errno));
143 return;
144 }
145
146 bmp_header_write(fd, width, height, bgra, flip, alpha);
147
148 write(fd, buffer, width * height * 4);
149 }
150