Reputation: 17008
Suppose, I have a 200 x 200 pix 24-bit BMP image.
I want to load the red component into an array named list
.
image: .space 120054
list: .space 8160
# ... ... ...
LoadGreenComponentToList:
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
li $t5, BMP_FILE_SIZE
sub $t5, $t5, $t2 # ?
div $t5, $t5, 3 # ?
la $t1, image # adress of bitmap
add $t2, $t1, $t2 # adress of pixel array in $t2
#fill the array with red values
add $t2, $t2, 1 # ?
li $t3, 0 # $t3 is the counter of loop
li $t4, 0
loop_through_pixels:
beq $t3, $t5, get_pixel_end
la $t6, list # $t6 = array address
mul $t4, $t3, 3 # $t4 = 3* $t3
add $t4, $t2, $t4 # $t4 = $t4 + $t2
lb $t1,($t4) # ?
#save to array
#sb $t1, ($t6)
#add $t6, $t6, 4
#inc array
mul $t1, $t1, 4 #or 4
add $t6, $t6, $t1
lw $t7, ($t6)
addi $t7, $t7, 1
sw $t7, ($t6)
add $t3, $t3, 1
j loop_through_pixels
get_pixel_end:
lw $ra, 4($sp) #restore (pop) $ra
add $sp, $sp, 4
jr $ra
This code loads the green component.
How can I convert it to operate on Red?
Also, I think, the size of list
is not correct.
Upvotes: 0
Views: 190
Reputation: 65077
Assuming they're stored <red_byte><green_byte><blue_byte>
or <blue_byte><green_byte><red_byte>
then I imagine this is your issue:
la $t1, image # adress of bitmap
add $t2, $t1, $t2 # adress of pixel array in $t2
#fill the array with red values
add $t2, $t2, 1 # ?
Essentially that is the equivalent of this pseudo-code:
PixelBuffer * buf = GetBitmap();
buf++;
So all of a sudden you're starting off the pixel iteration by not being at the base of the pixel buffer (you're pointing at the second byte).
[r][g][b][r][g][b][r][g][b][r][g][b][r][g][b][r][g][b][r][g][b]
^ -- pointing here at the start of the loop
This line further down:
mul $t4, $t3, 3 # $t4 = 3* $t3
add $t4, $t2, $t4 # $t4 = $t4 + $t2
This is jumping by 3's - to skip into each next full pixel component:
[r][g][b][r][g][b][r][g][b][r][g][b][r][g][b][r][g][b][r][g][b]
^ ------ ^ ------ ^ ------ ^ ------ ^ ------ ^ ------ ^
So, in either of the two cases (I can't remember what order the pixel data is stored in ..), either remove that first block of code:
########### REMOVED: add $t2, $t2, 1 # ?
...if its RGB. Or, change it to this:
add $t2, $t2, 2 # ?
...if its BGR.
I am almost certain BMPs are stored BGR, so option 2 is probably the correct call.
Upvotes: 2