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);
145 uint8_t pad =
GB(4 - info.
width, 0, 2);
146 for (uint y = info.
height; y > 0; y--) {
148 uint8_t *pixel = &data.bitmap[(y - 1) *
static_cast<size_t>(info.
width)];
149 for (uint i = 0; i < info.
width; i++) *pixel++ = file.
ReadByte();
161 uint8_t *pixel = &data.bitmap[y *
static_cast<size_t>(info.
width)];
162 while (y != 0 || x < info.
width) {
171 if (y == 0)
return false;
172 pixel = &data.bitmap[--y *
static_cast<size_t>(info.
width)];
184 if (x + dx >= info.
width || x + dx < x || dy > y)
return false;
188 pixel = &data.bitmap[y *
static_cast<size_t>(info.
width) + x];
193 for (uint i = 0; i < c; i++) {
207 for (uint i = 0; x < info.
width && i < n; i++) {
219 uint8_t pad =
GB(4 - info.
width * 3, 0, 2);
220 for (uint y = info.
height; y > 0; --y) {
221 uint8_t *pixel_row = &data.bitmap[(y - 1) *
static_cast<size_t>(info.
width) * 3];
222 for (uint x = 0; x < info.
width; ++x) {
241 if (file.
ReadWord() != 0x4D42)
return false;
247 if (header_size < 12)
return false;
249 info.
os2_bmp = (header_size == 12);
261 if (file.
ReadWord() != 1)
return false;
264 if (info.
bpp != 1 && info.
bpp != 4 && info.
bpp != 8 && info.
bpp != 24) {
270 if ((header_size -= 4) >= 4) {
280 if (header_size >= 16) {
286 uint maximum_palette_size = 1U << info.
bpp;
290 if (info.
palette_size > maximum_palette_size)
return false;
294 for (
auto &colour : data.palette) {
315 data.bitmap.resize(
static_cast<size_t>(info.
width) * info.
height * ((info.
bpp == 24) ? 3 : 1));
322 case 1:
return BmpRead1(file, info, data);
323 case 4:
return BmpRead4(file, info, data);
324 case 8:
return BmpRead8(file, info, data);
325 case 24:
return BmpRead24(file, info, data);
326 default: NOT_REACHED();
332 default: NOT_REACHED();
Functions related to bit mathematics.
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.
bool BmpReadBitmap(RandomAccessFile &file, BmpInfo &info, BmpData &data)
Reads the bitmap 1 bpp and 4 bpp bitmaps are converted to 8 bpp bitmaps.
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.
bool BmpReadHeader(RandomAccessFile &file, BmpInfo &info, BmpData &data)
Reads bitmap headers, and palette (if any).
static bool BmpRead4Rle(RandomAccessFile &file, BmpInfo &info, BmpData &data)
Reads a 4-bit RLE compressed 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