October 21, 2024
Chicago 12, Melborne City, USA
C++

Bitmapped file data displays incorrectly


I have a BMP image file of the moon graphic from the original Space Invaders arcade game. I’m displaying this on a 240×320 TFT display using a RPi Pico. I can’t get it to display correctly, so I’m wondering if I pulled the data from the file incorrectly, or if it’s something else.

This is my first time doing this. I was excited to see results, but confused by it being only somewhat correct.

I pulled the pixel data from the BMP file, assuming it was stored as Red byte, Green byte, Blue byte, Red…etc. This data starts at the offset given early in the file’s info. I should add that I was surprised to see data after the pixel data and it made me thing I had something wrong. I had previously looked at smaller bitmaps and the last pixel data were the last bytes of the files with nothing after.

Note that since my display is only 240 pixels wide I’m therefore only displaying the first 240 pixels of the image data. With the image being 327 pixels wide I’m expecting to see at about 3/4ths of the image, so the moon should already be curving down at the right side. And I have no idea about the potato-like thing on the left. The little artifact that looks like the ISS is actually on the right side of the moon in the original image.

This is not a issue with the display. I have displayed other hardcoded images just fine. This is the first time I’ve pulled data from a larger bitmap.

Python script to extract the data:

#!/bin/python3

# 327 W x 150 H pixels
# data starts at 138 (0x8a)

with open("moon24.bmp", "rb") as inp, open("moon24_data.txt", "w") as out:
    data = inp.read()
    out.write("const char moon[150][981] = {\n")
    x = 0
    for i in range(138, 138+147150-1):
        if x==0:
            out.write("    {")
        out.write('0x%02X' % data[i])
        x = x + 1
        if (x==327*3):
            out.write("},\n")
            x = 0
        else:
            out.write(",")
    out.write('0x%02X' % data[138+147150-1])
    out.write("}")
    out.write("\n};")

RPi Pico code to display the data:

void draw_moon(PIO pio, uint sm) {
    uint16_t r, c;
    uint8_t red, green, blue;
    uint16_t R, G, B, val;
    
    set_window(pio, sm, 0, 165, SCREEN_WIDTH-1, 165+150-1);
    st7789_lcd_wait_idle(pio, sm);
    lcd_set_dc_cs(1, 0);

    for (r = 0; r < 150; r++) {
        for (c = 0; c < 240*3; c+=3) {
            blue = moon[149-r][c];
            green = moon[149-r][c+1];
            red = moon[149-r][c+2];

            B =  (blue  >> 3)        & 0x001F;
            G = ((green >> 2) <<  5) & 0x07E0;
            R = ((red   >> 3) << 11) & 0xF800;

            val = (R | G | B);
            
            st7789_lcd_put(pio, sm, val >> 8);
            st7789_lcd_put(pio, sm, val & 0xff);
        }
    }
}

Bitmap:
moon24.bmp

What I get:
enter image description here



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video