2 * Copyright 2011 Luc Verhaegen <libv@codethink.co.uk>
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:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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.
25 * Quick 'n Dirty bitmap dumper.
29 #include <sys/types.h>
35 #include "write_bmp.h"
37 #define FILENAME_SIZE 1024
44 } __attribute__((__packed__
));
50 unsigned short planes
;
52 unsigned int compression
;
53 unsigned int data_size
;
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__
));
67 bmp_header_write(int fd
, int width
, int height
, int bgra
, int noflip
, int alpha
)
69 struct bmp_header bmp_header
= {
71 .size
= (width
* height
* 4) +
72 sizeof(struct bmp_header
) + sizeof(struct dib_header
),
73 .start
= sizeof(struct bmp_header
) + sizeof(struct dib_header
),
75 struct dib_header dib_header
= {
76 .size
= sizeof(struct dib_header
),
78 .height
= noflip
? -height
: height
,
82 .data_size
= 4 * width
* height
,
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,
95 dib_header
.red_mask
= 0x00FF0000;
96 dib_header
.blue_mask
= 0x000000FF;
99 write(fd
, &bmp_header
, sizeof(struct bmp_header
));
100 write(fd
, &dib_header
, sizeof(struct dib_header
));
104 bmp_dump32(char *buffer
, unsigned width
, unsigned height
, bool bgra
, const char *filename
)
108 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0666);
110 printf("Failed to open %s: %s\n", filename
, strerror(errno
));
114 bmp_header_write(fd
, width
, height
, bgra
, false, true);
116 write(fd
, buffer
, width
* height
* 4);
120 bmp_dump32_noflip(char *buffer
, unsigned width
, unsigned height
, bool bgra
, const char *filename
)
124 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0666);
126 printf("Failed to open %s: %s\n", filename
, strerror(errno
));
130 bmp_header_write(fd
, width
, height
, bgra
, true, true);
132 write(fd
, buffer
, width
* height
* 4);
136 bmp_dump32_ex(char *buffer
, unsigned width
, unsigned height
, bool flip
, bool bgra
, bool alpha
, const char *filename
)
140 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0666);
142 printf("Failed to open %s: %s\n", filename
, strerror(errno
));
146 bmp_header_write(fd
, width
, height
, bgra
, flip
, alpha
);
148 write(fd
, buffer
, width
* height
* 4);