Openstreetmap support (#2765)

This commit is contained in:
Totoo
2025-08-27 08:37:57 +02:00
committed by GitHub
parent e6d4081c06
commit 4dda7cfaa5
20 changed files with 447 additions and 162 deletions
+47 -36
View File
@@ -52,10 +52,10 @@ void BMPFile::close() {
bmpimage.close();
}
// creates a new bmp file. for now, hardcoded to 3 byte colour depth
// creates a new bmp file. hardcoded to 3 byte (24-bit) colour depth
bool BMPFile::create(const std::filesystem::path& file, uint32_t x, uint32_t y) {
is_opened = false;
is_read_ony = true;
is_read_only = true;
bmpimage.close(); // if already open, close before open a new
if (file_exists(file)) {
delete_file(file); // overwrite
@@ -85,7 +85,7 @@ bool BMPFile::create(const std::filesystem::path& file, uint32_t x, uint32_t y)
bmpimage.write(&bmp_header, sizeof(bmp_header_t));
file_pos = bmp_header.image_data;
is_opened = true;
is_read_ony = false;
is_read_only = false;
if (!expand_y(y)) return false; // will fill with 0, and update header data
seek(0, 0);
return true;
@@ -94,30 +94,36 @@ bool BMPFile::create(const std::filesystem::path& file, uint32_t x, uint32_t y)
// opens the file and parses header data. return true on success
bool BMPFile::open(const std::filesystem::path& file, bool readonly) {
is_opened = false;
is_read_ony = true;
bmpimage.close(); // if already open, close before open a new
is_read_only = true;
bmpimage.close();
auto result = bmpimage.open(file, readonly, false);
if (!result.value().ok()) return false;
file_pos = 0;
bmpimage.seek(file_pos);
auto read_size = bmpimage.read(&bmp_header, sizeof(bmp_header_t));
if (!((bmp_header.signature == 0x4D42) && // "BM" Signature
(bmp_header.planes == 1) && // Seems always to be 1
(bmp_header.compression == 0 || bmp_header.compression == 3))) { // No compression
bmpimage.read(&bmp_header, sizeof(bmp_header_t));
if (!((bmp_header.signature == 0x4D42) &&
(bmp_header.planes == 1) &&
(bmp_header.compression == 0 || bmp_header.compression == 3))) {
return false;
}
char buffer[257];
switch (bmp_header.bpp) {
case 8:
type = 4;
byte_per_px = 1;
// bmpimage.seek(sizeof(bmp_header_t));
// bmpimage.read(color_palette, 1024);
return false; // niy
break;
case 16:
file_pos = 0x36;
memset(buffer, 0, 16);
bmpimage.read(buffer, 16);
byte_per_px = 2;
if (buffer[1] == 0x7C)
type = 3; // A1R5G5B5
else
type = 0; // R5G6B5
type = 5;
if (bmp_header.compression == 3) {
return false;
} // niy
break;
case 24:
type = 1;
@@ -130,12 +136,11 @@ bool BMPFile::open(const std::filesystem::path& file, bool readonly) {
default:
// not supported
return false;
break;
}
byte_per_row = (bmp_header.width * byte_per_px % 4 == 0) ? bmp_header.width * byte_per_px : (bmp_header.width * byte_per_px + (4 - ((bmp_header.width * byte_per_px) % 4)));
byte_per_row = (bmp_header.width * byte_per_px + 3) & ~3;
file_pos = bmp_header.image_data;
is_opened = true;
is_read_ony = readonly;
is_read_only = readonly;
currx = 0;
curry = 0;
return true;
@@ -161,20 +166,24 @@ bool BMPFile::read_next_px(ui::Color& px, bool seek = true) {
auto res = bmpimage.read(buffer, byte_per_px);
if (res.is_error()) return false;
switch (type) {
case 0: // R5G6B5
case 3: // A1R5G5B5
if (!type)
px = ui::Color((uint16_t)buffer[0] | ((uint16_t)buffer[1] << 8));
else
px = ui::Color(((uint16_t)buffer[0] & 0x1F) | ((uint16_t)buffer[0] & 0xE0) << 1 | ((uint16_t)buffer[1] & 0x7F) << 9);
case 5: {
uint16_t color16 = (uint16_t)buffer[0] | ((uint16_t)buffer[1] << 8);
px = ui::Color(color16); // has glitches!
break;
}
case 2: // 32
px = ui::Color(buffer[2], buffer[1], buffer[0]);
break;
case 4: { // 8-bit
// uint8_t index = buffer[0];
// px = ui::Color(color_palette[index][2], color_palette[index][1], color_palette[index][0]); // Palette is BGR
// px = ui::Color(buffer[0]); // niy, since needs a lot of ram for the palette
break;
}
case 1: // 24
default:
px = ui::Color(buffer[2], buffer[1], buffer[0]);
break;
case 2: // 32
px = ui::Color(buffer[2], buffer[1], buffer[0]);
break;
}
if (seek) advance_curr_px();
@@ -188,23 +197,23 @@ void BMPFile::set_bg_color(ui::Color background) {
}
// delete bg color. default. creation or expansion will be fast, but the file will contain random garbage. no problem if you write all pixels later.
void BMPFile::delete_db_color() {
void BMPFile::delete_bg_color() {
use_bg = false;
}
// writes a color data to the current position, and advances 1 px. true on success, false on error
bool BMPFile::write_next_px(ui::Color& px) {
if (!is_opened) return false;
if (is_read_ony) return false;
if (!is_opened || is_read_only) return false;
uint8_t buffer[4];
switch (type) {
case 5:
case 0: // R5G6B5
case 3: // A1R5G5B5
if (!type) {
buffer[0] = (px.r() << 3) | (px.g() >> 3); // todo test in future
buffer[0] = (px.r() << 3) | (px.g() >> 3);
buffer[1] = (px.g() << 5) | px.b();
} else {
buffer[0] = (1 << 7) | (px.r() << 2) | (px.g() >> 3); // todo test in future
buffer[0] = (1 << 7) | (px.r() << 2) | (px.g() >> 3);
buffer[1] = (px.g() << 5) | px.b();
}
break;
@@ -220,6 +229,8 @@ bool BMPFile::write_next_px(ui::Color& px) {
buffer[0] = px.b();
buffer[3] = 255;
break;
case 4: // 8-bit
return false;
}
auto res = bmpimage.write(buffer, byte_per_px);
if (res.is_error()) return false;
@@ -260,7 +271,7 @@ bool BMPFile::expand_y(uint32_t new_y) {
if (!is_opened) return false; // not yet opened
uint32_t old_height = get_real_height();
if (new_y < old_height) return true; // already bigger
if (is_read_ony) return false; // can't expand
if (is_read_only) return false; // can't expand
uint32_t delta = (new_y - old_height) * byte_per_row;
bmp_header.size += delta;
bmp_header.data_size += delta;