微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 mips 中初始化 bmp 文件

如何解决在 mips 中初始化 bmp 文件

代码创建一个包含所有白色像素的 bmp 文件

initialize_bmp:
#description: 
#   fill the header of the bmp file an initialize all pixels with white
#arguments:
#   none
#return value: none
    la $t0,image  #image is a buffer or we can say array that stores all the 90122 bytes (equivalent to the image size) in register t0.
    li $t1,'B'    #Loading the first letter 'B' in the register t1
    sb $t1,($t0)  #Storing the letter 'B' from t1 to the 0th byte of t0
    li $t1,'M'    #Storing the letter 'M' in t1
    sb $t1,1($t0) #Loading the letter 'M' in the 1st byte of register t0.
    li $t1,BMP_FILE_SIZE 
    sw $t1,2($t0)
    sw $0,6($t0)
    li $t1,0x7a
    sw $t1,10($t0)
    addi $t0,$t0,14
    li $t1,27          # loop counter  
    la $t2,header
    copy_loop:          # loop to copy header in the image
        lw $t3,($t2)
        sw $t3,($t0)
        addi $t0,4
        addi $t2,$t2,4
        addi $t1,$t1,-1
        bnez $t1,copy_loop
    li $t1,5625            # loop counter      
    li $t3,0xFFFFFFFF      # white color
    initialize_loop:        # loop to initialize the image with all white pixels
        sw $t3,($t0)
        sw $t3,4($t0)
        sw $t3,8($t0)
        sw $t3,12($t0)
        addi $t0,16
        addi $t1,initialize_loop
    jr $ra

我有这段代码,可以用 mips 中的所有白色像素创建一个 bmp 文件。我不明白这部分代码发生了什么:

li $t1,BMP_FILE_SIZE 
sw $t1,2($t0)
sw $0,6($t0)
li $t1,0x7a
sw $t1,10($t0)
addi $t0,14

谁能解释一下代码

这是bmp文件格式,所以有人可以根据这张图片向我解释我们如何用白色像素实际初始化bmp文件代码

annotated hexdump of BMP header

解决方法

  1. li $t1,BMP_FILE_SIZE 文件的总大小。在这种情况下,位图文件头为 14,位图头为 108 (4 * 27) 加上像素数据的 22500 (4 * 5625) = 22,622。
  2. sw $t1,2($t0) 将总文件大小存储到文件头中。
  3. sw $0,6($t0) 将接下来的四个字节设置为零。
  4. li $t1,0x7a 文件中像素数据起始位置的偏移量。
  5. sw $t1,10($t0) 将像素数据偏移量存储到文件头中。
  6. addi $t0,$t0,14 将指针指向位图标头的开头。

下一段代码将 108 字节的位图头复制到文件头之后的缓冲区中:

li $t1,27          # loop counter  
la $t2,header
copy_loop:          # loop to copy header in the image
    lw $t3,($t2)
    sw $t3,($t0)
    addi $t0,4
    addi $t2,$t2,4
    addi $t1,$t1,-1
    bnez $t1,copy_loop

剩下的代码用白色像素填充像素数据。由于循环存储了 4 个字节 5625 次,总共 22500 个字节。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。