Unit: BMP
Loads/saves Windows BMP files (256-color indexed, uncompressed).
Types
type
TBMPFileHeader = record
bfType: Word; { 'BM' = $4D42 }
bfSize: LongInt; { File size }
bfReserved1: Word;
bfReserved2: Word;
bfOffBits: LongInt; { Offset to pixel data }
end;
TBMPInfoHeader = record
biSize: LongInt; { Header size (40) }
biWidth: LongInt;
biHeight: LongInt;
biPlanes: Word; { 1 }
biBitCount: Word; { 8 (256 colors) }
biCompression: LongInt; { 0 = BI_RGB (uncompressed) }
biSizeImage: LongInt;
biXPelsPerMeter: LongInt;
biYPelsPerMeter: LongInt;
biClrUsed: LongInt; { 0 or 256 }
biClrImportant: LongInt;
end;
TBMPRGBQuad = record
rgbBlue: Byte;
rgbGreen: Byte;
rgbRed: Byte;
rgbReserved: Byte; { 0 }
end;
Functions
function LoadBMP(const FileName: string; var Img: TImage): Boolean; function LoadBMPWithPalette(const FileName: string; var Img: TImage; var Pal: TPalette): Boolean; function GetLoadBMPError: string; function SaveBMP(const FileName: string; const Img: TImage; const Pal: TPalette): Boolean; function GetSaveBMPError: string;
Example
uses BMP, VGA;
var
Img: TImage;
Pal: TPalette;
begin
InitVGA;
if not LoadBMPWithPalette('PLAYER.BMP', Img, Pal) then
begin
WriteLn('Error: ', GetLoadBMPError);
DoneVGA;
Exit;
end;
SetPalette(Pal);
PutImage(0, 0, @Img, GetScreenBuffer);
ReadKey;
SaveBMP('OUTPUT.BMP', Img, Pal);
FreeImage(@Img);
DoneVGA;
end;
Format Details
- 8-bit indexed color (256 colors)
- BI_RGB (uncompressed)
- Bottom-up scanlines (first scanline = bottom row)
- 4-byte alignment per scanline (padded)
- BGRA palette (Blue, Green, Red, Reserved)
Palette Conversion
BMP uses BGRA format (0-255), VGA uses RGB (0-63). Automatic conversion:
{ BMP → VGA }
Pal[i].R := BMPPalette[i].rgbRed shr 2;
Pal[i].G := BMPPalette[i].rgbGreen shr 2;
Pal[i].B := BMPPalette[i].rgbBlue shr 2;
{ VGA → BMP }
BMPPalette[i].rgbRed := Pal[i].R shl 2;
BMPPalette[i].rgbGreen := Pal[i].G shl 2;
BMPPalette[i].rgbBlue := Pal[i].B shl 2;
Creating BMP Files
Pix8 (Recommended)
Pix8 is our own 256-color indexed pixel art editor (browser + Windows desktop app). Images are 8-bit indexed by default, so no mode conversion is needed.
- File → New...
- Draw your pixel art (up to 256 colors)
- File → Export as... (Ctrl+Shift+E) → BMP
Windows Paint
- Image → Resize → 256 colors
- Save As → 24-bit BMP (auto-converts to 8-bit)
GIMP
- Image → Mode → Indexed (256 colors)
- File → Export As → .bmp
- Options: 8 bits, no color space info
Photoshop
- Image → Mode → Indexed Color
- File → Save As → BMP
- Format: Windows, 8 bits/pixel
BMP vs PCX
| Feature | BMP | PCX |
|---|---|---|
| Compression | None | RLE |
| Scanlines | Bottom-up | Top-down |
| Palette | BGRA (0-255) | RGB (0-255) |
| Tools | Pix8, Paint, Photoshop | Pix8, GrafX2, Aseprite |
| File Size | Larger | Smaller |
Notes
- Max image size: 320×204 (65520 bytes) due to TImage limits
- Handles scanline padding (4-byte alignment) automatically
- Compatible with Pix8, Windows Paint, Photoshop, GIMP
- For RLE compression, use PCX format instead