Search code examples
bitmaprasterfile-format

Bitmap 1bpp image from raster video RAM - Windows can't open file


I'm working on an Intel 8080 CPU emulator and I need to check if the video RAM is ok on Space Invaders game.

I got the raster data from RAM and I created the bitmap 1bpp header in order to check how it is the video output.

Space Invaders resolution is 256x224 pixels so the video RAM is 7168 bytes. This is my header:

FILEHEADER (14 bytes)
42 4D
36 1C 00 00
00 00 00 00
36 00 00 00

INFOHEADER (40 bytes)
28 00 00 00
00 01 00 00
E0 00 00 00
01 00
01 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00

RASTER DATA (7168 bytes)
1st ...  31th (bytes for line 1)
32th ... 63th (bytes for line 2)
...           (bytes for line n)
...           (bytes for line 224)

Bitmap's header is in little endian order.

When I try to open the file on Windows 10 it gives me an error about unsupported file.

Is Windows 10 capable of open a Bitmap v3 1bpp image?


Solution

  • 1bpp is regarded as an indexed color format (same as for 4bpp and 8bpp). So the color table is mandatory.

    So your image has too look something like this:

    FILEHEADER (14 bytes)
    42 4D
    3E 1C 00 00  <-- updated
    00 00 00 00
    3E 00 00 00  <-- updated
    
    INFOHEADER (40 bytes)
    28 00 00 00
    00 01 00 00
    E0 00 00 00
    01 00
    01 00
    00 00 00 00
    00 00 00 00 <-- is this correct?
    00 00 00 00
    00 00 00 00
    00 00 00 00
    00 00 00 00
    
    COLOR TABLE (8 bytes)  <-- added
    FF FF FF 00
    00 00 00 00
    
    RASTER DATA (7168 bytes)
    1st ...  31th (bytes for line 1)
    32th ... 63th (bytes for line 2)
    ...           (bytes for line n)
    ...           (bytes for line 224)
    

    Update

    The pixel values in the raster data section are interpreted as indexes into the color table. And the color table is an array of RGBA values. You can either specify the number of entries in the header or - if it's 0 - it's assumed to be 2**n where n i the number of bits per pixel.

    In your case, a 0 bit will be index 0 and the color will be 0xffffff00, i.e. white. A 1 bit will be index 1 and the color will be 0x00000000, i.e. white. If a 0 bit is black, swap the color entries.