ui::Bitmap¶
Header: winui/render/Bitmap.hpp.
A CPU bitmap, 4 bytes per pixel: RGBA with straight alpha unless made in another PixelFormat.
The picture type everything image-shaped uses: ImageSource and the Image element, Icon, PersonPicture, IconSource::bitmap(), the clipboard, and pixel tests, which capture into one and check it with compare(). PNG encodes and decodes byte for byte the same on every platform. decode() and read() handle photos (JPEG, GIF and the other formats the system's image codecs read) with optional downscaling and EXIF orientation. A value type: copying copies the pixels.
A renderer that writes premultiplied BGRA can draw straight into a bitmap made in that format, through bytes(), and the picture then reaches the screen with no pass over its pixels. get(), set() and the encoders work in either format.
Example:
// Load a photo for display, bounded to 1024px and upright.
ui::Bitmap photo = ui::Bitmap::read("holiday.jpg", {.max_width = 1024, .max_height = 1024});
window.set_content(ui::Image{.source = std::move(photo), .stretch = ui::Stretch::Uniform});
// Build one by hand, e.g. a color swatch for a palette export, and save it as PNG.
ui::Bitmap swatch{16, 16, ui::colors::White};
swatch.set(0, 0, ui::Color::from_rgba(255, 0, 0));
swatch.write_png("swatch.png"); // lossless: read_png() gives back the same pixels
// A page a PDF renderer draws right into the bitmap's bytes, shown as they are. Here the
// page is filled opaque white, the same in premultiplied BGRA.
ui::Bitmap page{612, 792, ui::PixelFormat::Bgra8Premultiplied};
std::ranges::fill(page.bytes(), std::uint8_t{0xFF});
Bitmap¶
An empty (0x0) image.
Bitmap¶
A width x height image, every pixel fill (transparent by default).
Bitmap¶
A width x height image in format, every pixel transparent: zero bytes, ready for a renderer to draw into through bytes().
width¶
Width in pixels; 0 when empty.
height¶
Height in pixels; 0 when empty.
empty¶
True when either dimension is zero (nothing to draw).
format¶
How the bytes are laid out; Rgba8 unless the bitmap was made in another format.
converted¶
The same picture with its bytes in format; a copy when it is already in it.
bytes¶
The pixel bytes in format()'s layout, row-major, 4 per pixel, width * 4 per row.
bytes¶
The pixel bytes, writable; width * height * 4 of them. Asking for them counts as a change to the pixels (content_id()).
content_id¶
A number that stays the same while the pixels do and is never reused for other pixels: a copy shares it until either side is written to (fill(), set(), blend(), the writable bytes()). A renderer keeps the copy it uploaded to the GPU under it.
fill¶
Sets every pixel to c (a replace, not a blend).
set¶
Replaces the pixel, ignoring what was there.
get¶
Reads a pixel back as a Color, with straight alpha in either format.
blend¶
Source-over composite of c at coverage (0..255) onto the existing pixel.
encode_png¶
The image as PNG bytes, identical on every platform.
write_png¶
encode_png() written to path. Throws std::runtime_error if the file can't be written.
encode_jpeg¶
Baseline JPEG at quality (1..100). Alpha is dropped; JPEG has none.
oriented¶
This image turned upright, given the orientation it was stored with.
scaled_to_fit¶
This image shrunk to fit inside max_width × max_height (zero: unconstrained), aspect ratio kept, by area averaging with alpha weighting, so a transparent pixel's color doesn't bleed into its neighbors. Never enlarges: an image that already fits comes back as is. The result keeps the format.
decode_png¶
Decodes PNG bytes, identically on every platform. Throws std::runtime_error if data is not a readable PNG.
read_png¶
decode_png() of the file at path. Throws std::runtime_error if it can't be read.
decode¶
Decodes an encoded image held in memory. Throws std::runtime_error if it can't.
read¶
Decodes the file at path. Any file name opens, whatever characters it holds. Throws std::runtime_error if the file can't be read or decoded. Thread-safe and meant to be called from worker threads, which need no setup for it.
read_thumbnail¶
read() into a max_edge × max_edge box, upright.
read_orientation¶
The EXIF orientation recorded in an encoded JPEG or TIFF, Normal when there is none. Reads only the header; decode()/read() already apply it unless told not to.