23 uint8_t blue, green, red, reserved;
25static_assert(
sizeof(
RgbQuad) == 4);
27class ScreenshotProvider_Bmp :
public ScreenshotProvider {
29 ScreenshotProvider_Bmp() : ScreenshotProvider(
"bmp",
"BMP", 10) {}
34 switch (pixelformat) {
35 case 8: bpp = 1;
break;
37 case 32: bpp = 3;
break;
39 default:
return false;
43 if (!of.has_value())
return false;
47 uint bytewidth =
Align(w * bpp, 4);
50 uint pal_size = pixelformat == 8 ?
sizeof(
RgbQuad) * 256 : 0;
74 if (fwrite(header.data(), header.size(), 1, f) != 1) {
78 if (pixelformat == 8) {
81 for (uint i = 0; i < 256; i++) {
82 rq[i].red = palette[i].r;
83 rq[i].green = palette[i].g;
84 rq[i].blue = palette[i].b;
88 if (fwrite(rq,
sizeof(rq), 1, f) != 1) {
94 uint maxlines =
Clamp(65536 / (w * pixelformat / 8), 16, 128);
96 std::vector<uint8_t> buff(maxlines * w * pixelformat / 8);
97 std::vector<uint8_t> line(bytewidth);
101 uint n = std::min(h, maxlines);
105 callb(buff.data(), h, w, n);
109 if (pixelformat == 8) {
111 std::copy_n(buff.data() + n * w, w, line.data());
115 Colour *src = ((Colour *)buff.data()) + n * w;
116 uint8_t *dst = line.data();
117 for (uint i = 0; i < w; i++) {
118 dst[i * 3 ] = src[i].b;
119 dst[i * 3 + 1] = src[i].g;
120 dst[i * 3 + 2] = src[i].r;
124 if (fwrite(line.data(), bytewidth, 1, f) != 1) {
const std::string_view name
void PutUint32LE(uint32_t value)
Append binary uint32 using little endian.
void PutUint16LE(uint16_t value)
Append binary uint16 using little endian.
void Put(std::string_view str)
Append string.
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.
Compose data into a growing std::string.
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.
constexpr size_t BITMAP_FILE_HEADER_SIZE
The size of a bitmap file header.
constexpr size_t BITMAP_INFO_HEADER_SIZE
The size of a bitmap info header.
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.
Compose strings from textual and binary data.
Format of palette data in BMP header.