@@ -420,15 +420,20 @@ bool OpenStreetMap::downloadAndDecodeTile(CachedTile &tile, uint32_t x, uint32_t
420
420
421
421
bool OpenStreetMap::writeHeader (const LGFX_Sprite &map, File &file)
422
422
{
423
- // BMP Header (54 bytes)
424
- uint16_t bfType = 0x4D42 ; // "BM"
423
+ uint8_t header[54 ] = {0 }; // BMP header (54 bytes)
424
+
425
+ // BMP File Header (14 bytes)
426
+ header[0 ] = ' B' ;
427
+ header[1 ] = ' M' ;
428
+
425
429
uint32_t biSizeImage = map.width () * map.height () * 3 ; // 3 bytes per pixel (RGB888)
426
430
uint32_t bfSize = 54 + biSizeImage; // Total file size
427
431
uint32_t bfOffBits = 54 ; // Offset to pixel data
428
432
433
+ // BMP Info Header (40 bytes)
429
434
uint32_t biSize = 40 ; // Info header size
430
435
int32_t biWidth = map.width ();
431
- int32_t biHeight = -map.height (); // Negative to store in top-down order
436
+ int32_t biHeight = -map.height (); // Negative for top-down order
432
437
uint16_t biPlanes = 1 ;
433
438
uint16_t biBitCount = 24 ; // RGB888 format
434
439
uint32_t biCompression = 0 ;
@@ -437,51 +442,28 @@ bool OpenStreetMap::writeHeader(const LGFX_Sprite &map, File &file)
437
442
uint32_t biClrUsed = 0 ;
438
443
uint32_t biClrImportant = 0 ;
439
444
440
- auto writeLE = [&]( uint32_t value, uint8_t size) -> bool
445
+ auto writeLE = []( uint8_t *buffer, size_t offset, uint32_t value, uint8_t size)
441
446
{
442
447
for (uint8_t i = 0 ; i < size; i++)
443
- if (file.write (static_cast <uint8_t >(value >> (8 * i))) != 1 )
444
- return false ;
445
-
446
- return true ;
448
+ buffer[offset + i] = static_cast <uint8_t >((value >> (8 * i)) & 0xFF );
447
449
};
448
450
449
- bool success = true ;
450
-
451
- if (!(success &= writeLE (bfType, 2 )))
452
- return false ;
453
- if (!(success &= writeLE (bfSize, 4 )))
454
- return false ;
455
- if (!(success &= writeLE (0 , 2 )))
456
- return false ;
457
- if (!(success &= writeLE (0 , 2 )))
458
- return false ;
459
- if (!(success &= writeLE (bfOffBits, 4 )))
460
- return false ;
461
- if (!(success &= writeLE (biSize, 4 )))
462
- return false ;
463
- if (!(success &= writeLE (biWidth, 4 )))
464
- return false ;
465
- if (!(success &= writeLE (biHeight, 4 )))
466
- return false ;
467
- if (!(success &= writeLE (biPlanes, 2 )))
468
- return false ;
469
- if (!(success &= writeLE (biBitCount, 2 )))
470
- return false ;
471
- if (!(success &= writeLE (biCompression, 4 )))
472
- return false ;
473
- if (!(success &= writeLE (biSizeImage, 4 )))
474
- return false ;
475
- if (!(success &= writeLE (biXPelsPerMeter, 4 )))
476
- return false ;
477
- if (!(success &= writeLE (biYPelsPerMeter, 4 )))
478
- return false ;
479
- if (!(success &= writeLE (biClrUsed, 4 )))
480
- return false ;
481
- if (!(success &= writeLE (biClrImportant, 4 )))
482
- return false ;
483
-
484
- return success;
451
+ // Populate the header array with the correct offsets
452
+ writeLE (header, 2 , bfSize, 4 ); // File size
453
+ writeLE (header, 10 , bfOffBits, 4 ); // Pixel data offset
454
+ writeLE (header, 14 , biSize, 4 ); // Info header size
455
+ writeLE (header, 18 , biWidth, 4 ); // Image width
456
+ writeLE (header, 22 , biHeight, 4 ); // Image height (negative for top-down)
457
+ writeLE (header, 26 , biPlanes, 2 ); // Number of planes (always 1)
458
+ writeLE (header, 28 , biBitCount, 2 ); // Bits per pixel (24-bit RGB)
459
+ writeLE (header, 30 , biCompression, 4 ); // Compression (0 = none)
460
+ writeLE (header, 34 , biSizeImage, 4 ); // Image size in bytes
461
+ writeLE (header, 38 , biXPelsPerMeter, 4 ); // Horizontal resolution (not used)
462
+ writeLE (header, 42 , biYPelsPerMeter, 4 ); // Vertical resolution (not used)
463
+ writeLE (header, 46 , biClrUsed, 4 ); // Colors in palette (not used)
464
+ writeLE (header, 50 , biClrImportant, 4 ); // Important colors (not used)
465
+
466
+ return file.write (header, sizeof (header)) == sizeof (header);
485
467
}
486
468
487
469
bool OpenStreetMap::writeMap (LGFX_Sprite &map, File &file, MemoryBuffer &buffer)
0 commit comments