Risc-V: How to correctly read a bitmap image and display it in the rars bitmap displayer?

1.2k Views Asked by At

I'm using Rars Risc-V simulator and I have to read a 24 bit bitmap image from my computer and display it in rars. What I did was opening the image with the risc-v system call for opening and then I tried to read it using the call for reading.

la a0, file_name
li a7, 1024     #system call for open
li a1, 0        #open for reading
ecall
mv s6, a0       #save the file descriptor to s6
    
#read file
li t0, -1
beq a0, t0, error

mv  a0, s6
la  a1, buffer
li  a2, 400000
li  a7, 63
ecall

But doing that I obtain this result:

this result.

Does anyone know how to do this correctly?

1

There are 1 best solutions below

1
On

That file format has headers that will direct you to the image content.  You won't be able to simply dump the whole file to video memory.

In particular, note that (from Wikipedia):

The 24-bit pixel (24bpp) format supports 16,777,216 distinct colors and stores 1 pixel value per 3 bytes.

This is a moderate form of compression; whereas the MARS/RARS bitmap tool uses 4 bytes (a whole word) per pixel.

Relatively speaking, the hardware simulator is using an uncompressed form where a byte per word is wasted (the high order byte does not affect the color of the pixel) and this (waste) makes addressing much simpler for the program as well as the hardware display — one pixel per word makes it easier to draw lines and other graphic shapes using math.  (But 1 extra/unused byte per pixel would be a burden on the disc space occupied by a very large image collection or library, so that extra space is forgone on disc at the expense of unpacking code required to view images, and also packing code needed by file creation programs.)

Once you have locate the pixel content within the file, you'll have to unpack groups of 3 file bytes (representing one pixel) into a word (4 bytes), and write that word to a video memory pixel.

So, you'll want to read the whole file into some memory buffer (not the video map), then decode the header to find the first pixel, then assemble 3 color bytes of the pixel into a word and write that word to video memory.  The header will also tell you how many pixels there are in the image in total, as well as how many pixels per row.

Each group of 3 byte (colors for one pixel) goes to another word of video pixel, until you get to a row boundary and then need to skip ahead in video memory to the next video row, as the number of pixels per row in the file will usually be less than the number of pixels per row for the display hardware.