19PACK(
struct BitmapFileHeader {
25static_assert(
sizeof(BitmapFileHeader) == 14);
30 int32_t width, height;
31 uint16_t planes, bitcount;
32 uint32_t compression, sizeimage, xpels, ypels, clrused, clrimp;
38 uint8_t blue, green, red, reserved;
40static_assert(
sizeof(
RgbQuad) == 4);
42class ScreenshotProvider_Bmp :
public ScreenshotProvider {
44 ScreenshotProvider_Bmp() : ScreenshotProvider(
"bmp",
"BMP", 10) {}
49 switch (pixelformat) {
50 case 8: bpp = 1;
break;
52 case 32: bpp = 3;
break;
54 default:
return false;
58 if (!of.has_value())
return false;
62 uint bytewidth =
Align(w * bpp, 4);
65 uint pal_size = pixelformat == 8 ?
sizeof(
RgbQuad) * 256 : 0;
69 bfh.type = TO_LE16(
'MB');
70 bfh.size = TO_LE32(
sizeof(BitmapFileHeader) +
sizeof(
BitmapInfoHeader) + pal_size +
static_cast<size_t>(bytewidth) * h);
72 bfh.off_bits = TO_LE32(
sizeof(BitmapFileHeader) +
sizeof(
BitmapInfoHeader) + pal_size);
77 bih.width = TO_LE32(w);
78 bih.height = TO_LE32(h);
79 bih.planes = TO_LE16(1);
80 bih.bitcount = TO_LE16(bpp * 8);
89 if (fwrite(&bfh,
sizeof(bfh), 1, f) != 1 || fwrite(&bih,
sizeof(bih), 1, f) != 1) {
93 if (pixelformat == 8) {
96 for (uint i = 0; i < 256; i++) {
97 rq[i].red = palette[i].r;
98 rq[i].green = palette[i].g;
99 rq[i].blue = palette[i].b;
103 if (fwrite(rq,
sizeof(rq), 1, f) != 1) {
109 uint maxlines =
Clamp(65536 / (w * pixelformat / 8), 16, 128);
111 std::vector<uint8_t> buff(maxlines * w * pixelformat / 8);
112 std::vector<uint8_t> line(bytewidth);
116 uint n = std::min(h, maxlines);
120 callb(buff.data(), h, w, n);
124 if (pixelformat == 8) {
126 std::copy_n(buff.data() + n * w, w, line.data());
130 Colour *src = ((Colour *)buff.data()) + n * w;
131 uint8_t *dst = line.data();
132 for (uint i = 0; i < w; i++) {
133 dst[i * 3 ] = src[i].b;
134 dst[i * 3 + 1] = src[i].g;
135 dst[i * 3 + 2] = src[i].r;
139 if (fwrite(line.data(), bytewidth, 1, f) != 1) {
const std::string_view name
static std::optional< FileHandle > Open(const std::string &filename, std::string_view mode)
Open an RAII file handle if possible.
bool MakeImage(std::string_view name, const ScreenshotCallback &callb, uint w, uint h, int pixelformat, const Colour *palette) const override
Create and write an image to a file.
Function to handling different endian machines.
Functions for standard in/out file operations.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
A number of safeguards to prevent using unsafe methods.
PACK(struct BitmapFileHeader { uint16_t type;uint32_t size;uint32_t reserved;uint32_t off_bits;})
BMP File Header (stored in little endian).
Types related to screenshot providers.
std::function< void(void *buf, uint y, uint pitch, uint n)> ScreenshotCallback
Callback function signature for generating lines of pixel data to be written to the screenshot file.
Definition of base types and functions in a cross-platform compatible way.
Format of palette data in BMP header.