Mips BMP File Geting the Pixel Value

0

I am working on some image recognition project with MIPS assembly and trying to get the color value in a .bmp file. I have this piece of code that should give me the correct result in $v0 register but it keeps returning me 0x00000000 (which is black). I used Paint to paint source file in different colors but I keep getting 0x00000000. Can someone help me why this keep happening?

#only 24-bits 600x50 pixels BMP files are supported
.eqv BMP_FILE_SIZE 90122
.eqv BYTES_PER_ROW 1800

    .data
#space for the 600x50px 24-bits bmp image
.align 4
res:    .space 2
image:  .space BMP_FILE_SIZE

fname:  .asciiz "source.bmp"
    .text
main:
    jal read_bmp

    #get pixel color - $a0=x, $a1=y, result $v0=0x00RRGGBB
    li  $a0, 0      #x
    li  $a1, 0      #y
    jal     get_pixel

read_bmp:
#description: 
#   reads the contents of a bmp file into memory
#arguments:
#   none
#return value: none
    sub $sp, $sp, 4     #push $ra to the stack
    sw $ra,4($sp)
    sub $sp, $sp, 4     #push $s1
    sw $s1, 4($sp)
#open file
    li $v0, 13
        la $a0, fname       #file name 
        li $a1, 0       #flags: 0-read file
        li $a2, 0       #mode: ignored
        syscall
    move $s1, $v0      # save the file descriptor

#check for errors - if the file was opened
#...

#read file
    li $v0, 14
    move $a0, $s1
    la $a1, image
    li $a2, BMP_FILE_SIZE
    syscall

#close file
    li $v0, 16
    move $a0, $s1
        syscall

    lw $s1, 4($sp)      #restore (pop) $s1
    add $sp, $sp, 4
    lw $ra, 4($sp)      #restore (pop) $ra
    add $sp, $sp, 4
    jr $ra

get_pixel:
#description: 
#   returns color of specified pixel
#arguments:
#   $a0 - x coordinate
#   $a1 - y coordinate - (0,0) - bottom left corner
#return value:
#   $v0 - 0RGB - pixel color

    sub $sp, $sp, 4     #push $ra to the stack
    sw $ra,4($sp)

    la $t1, image + 10  #adress of file offset to pixel array
    lw $t2, ($t1)       #file offset to pixel array in $t2
    la $t1, image       #adress of bitmap
    add $t2, $t1, $t2   #adress of pixel array in $t2

    #pixel address calculation
    mul $t1, $a1, BYTES_PER_ROW #t1= y*BYTES_PER_ROW
    move $t3, $a0       
    sll $a0, $a0, 1
    add $t3, $t3, $a0   #$t3= 3*x
    add $t1, $t1, $t3   #$t1 = 3x + y*BYTES_PER_ROW
    add $t2, $t2, $t1   #pixel address 

    #get color
    lbu $v0,($t2)       #load B
    lbu $t1,1($t2)      #load G
    sll $t1,$t1,8
    or $v0, $v0, $t1
    lbu $t1,2($t2)      #load R
        sll $t1,$t1,16
    or $v0, $v0, $t1

    lw $ra, 4($sp)      #restore (pop) $ra
    add $sp, $sp, 4
    jr $ra


assembly
mips
pixel
bmp
asked on Stack Overflow Jan 7, 2020 by bumbum

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0