23 uint8_t pad =
GB(4 - info.
width / 8, 0, 2);
24 for (uint y = info.
height; y > 0; y--) {
26 uint8_t *pixel_row = &data.bitmap[(y - 1) *
static_cast<size_t>(info.
width)];
27 while (x < info.
width) {
30 for (uint i = 8; i > 0; i--) {
31 if (x < info.
width) *pixel_row++ =
GB(b, i - 1, 1);
47 uint8_t pad =
GB(4 - info.
width / 2, 0, 2);
48 for (uint y = info.
height; y > 0; y--) {
50 uint8_t *pixel_row = &data.bitmap[(y - 1) *
static_cast<size_t>(info.
width)];
51 while (x < info.
width) {
54 *pixel_row++ =
GB(b, 4, 4);
57 *pixel_row++ =
GB(b, 0, 4);
75 uint8_t *pixel = &data.bitmap[y *
static_cast<size_t>(info.
width)];
76 while (y != 0 || x < info.
width) {
85 if (y == 0)
return false;
86 pixel = &data.bitmap[--y *
static_cast<size_t>(info.
width)];
98 if (x + dx >= info.
width || x + dx < x || dy > y)
return false;
102 pixel = &data.bitmap[y * info.
width + x];
111 *pixel++ =
GB(b, 4, 4);
114 if (x >= info.
width)
return false;
115 *pixel++ =
GB(b, 0, 4);
129 while (x < info.
width && i++ < n) {
130 *pixel++ =
GB(c, 4, 4);
132 if (x < info.
width && i++ < n) {
133 *pixel++ =
GB(c, 0, 4);
147 uint8_t pad =
GB(4 - info.
width, 0, 2);
148 for (uint y = info.
height; y > 0; y--) {
150 uint8_t *pixel = &data.bitmap[(y - 1) *
static_cast<size_t>(info.
width)];
151 for (uint i = 0; i < info.
width; i++) *pixel++ = file.
ReadByte();
165 uint8_t *pixel = &data.bitmap[y *
static_cast<size_t>(info.
width)];
166 while (y != 0 || x < info.
width) {
175 if (y == 0)
return false;
176 pixel = &data.bitmap[--y *
static_cast<size_t>(info.
width)];
188 if (x + dx >= info.
width || x + dx < x || dy > y)
return false;
192 pixel = &data.bitmap[y *
static_cast<size_t>(info.
width) + x];
197 for (uint i = 0; i < c; i++) {
211 for (uint i = 0; x < info.
width && i < n; i++) {
225 uint8_t pad =
GB(4 - info.
width * 3, 0, 2);
226 for (uint y = info.
height; y > 0; --y) {
227 uint8_t *pixel_row = &data.bitmap[(y - 1) *
static_cast<size_t>(info.
width) * 3];
228 for (uint x = 0; x < info.
width; ++x) {
249 if (file.
ReadWord() != 0x4D42)
return false;
255 if (header_size < 12)
return false;
257 info.
os2_bmp = (header_size == 12);
269 if (file.
ReadWord() != 1)
return false;
272 if (info.
bpp != 1 && info.
bpp != 4 && info.
bpp != 8 && info.
bpp != 24) {
278 if ((header_size -= 4) >= 4) {
288 if (header_size >= 16) {
294 uint maximum_palette_size = 1U << info.
bpp;
298 if (info.
palette_size > maximum_palette_size)
return false;
302 for (
auto &colour : data.palette) {
319 data.bitmap.resize(
static_cast<size_t>(info.
width) * info.
height * ((info.
bpp == 24) ? 3 : 1));
326 case 1:
return BmpRead1(file, info, data);
327 case 4:
return BmpRead4(file, info, data);
328 case 8:
return BmpRead8(file, info, data);
329 case 24:
return BmpRead24(file, info, data);
330 default: NOT_REACHED();
336 default: NOT_REACHED();
Functions related to bit mathematics.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
static bool BmpRead1(RandomAccessFile &file, BmpInfo &info, BmpData &data)
Reads a 1 bpp uncompressed bitmap The bitmap is converted to a 8 bpp bitmap.
static bool BmpRead8Rle(RandomAccessFile &file, BmpInfo &info, BmpData &data)
Reads a 8-bit RLE compressed bpp bitmap.
static bool BmpRead24(RandomAccessFile &file, BmpInfo &info, BmpData &data)
Reads a 24 bpp uncompressed bitmap.
static bool BmpRead8(RandomAccessFile &file, BmpInfo &info, BmpData &data)
Reads a 8 bpp bitmap.
static bool BmpRead4(RandomAccessFile &file, BmpInfo &info, BmpData &data)
Reads a 4 bpp uncompressed bitmap The bitmap is converted to a 8 bpp bitmap.
static bool BmpRead4Rle(RandomAccessFile &file, BmpInfo &info, BmpData &data)
Reads a 4-bit RLE compressed bitmap The bitmap is converted to a 8 bpp bitmap.
Read and write support for bmps.
A file from which bytes, words and double words are read in (potentially) a random order.
size_t GetPos() const
Get position in the file.
bool AtEndOfFile() const
Test if we have reached the end of the file.
void SeekTo(size_t pos, int mode)
Seek in the current file.
uint8_t ReadByte()
Read a byte from the file.
uint32_t ReadDword()
Read a double word (32 bits) from the file (in low endian format).
void SkipBytes(size_t n)
Skip n bytes ahead in the file.
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
Class related to random access to files.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
uint32_t height
bitmap height
uint32_t width
bitmap width
uint32_t compression
compression method (0 = none, 1 = 8-bit RLE, 2 = 4-bit RLE)
uint32_t palette_size
number of colours in palette
uint16_t bpp
bits per pixel
size_t offset
offset of bitmap data from .bmp file beginning
bool os2_bmp
true if OS/2 1.x or windows 2.x bitmap