0

I'm working on a assignment and I'm stuck on the conversion from BGR (Blue, Green, Red) to RGB (Red, Green, Blue) I have been trying to invert the placement of the code taking note that the first 2 bits of the hex code aren't being used and the other six pairs correspond to the RGB or BGR respectively. Here's the code I have been building:

.data
FILENAME: .asciiz "C:/Users/conta/Desktop/mario.ppm"
ERROROPEN: .asciiz "Erro ao abrir o arquivo."
STR: .space 512

.text
    
    li $t7, 4       #Número de quebras de linhas no arquivo ppm
    li $t8, 0x0a        # \n
    li $t9, 0       #contador

    la $a0, FILENAME    #Nome do arquivo a ser aberto.
    li $a1, 0       #Sempre zero.
    li $a2, 0       #0 para leitura 1 para escrita.
    li $v0, 13      #syscall 13 (open file).
    syscall
    
    bltz $v0, end_error #menor que zero significa que nao encontrou o arquivo.

    move $a0, $v0       #v0 possui o descritor de arquivo.
    la $a1, STR     #endereco para armazernar os caracteres lidos.
    li $a2, 1       #ler apenas 1 byte.
    
readloopcabecalho:

    beq $t9, 4, preparacao 

    li $v0, 14      #syscall 14
    syscall
    
    lb $t4, 0($a1)
    beq $t4, $t8, cont
    addi $a1, $a1, 4        #incrementa para o proximo endereco de $a1.
    
    bnez $v0, readloopcabecalho #enquanto v0 for diferente de 0 continua.
    
preparacao:

    la $a1, 0x10040000  
    j readlooppixel
    
readlooppixel:  
    
    beq $t5, 3, zera_pula
    li $v0, 14          #syscall 14
    syscall
    addi $a1, $a1, 1        #incrementa para o proximo endereco de $a1.
    j cont2
            
zera_pula:

    
    move  $t5, $zero
    addi $a1, $a1, 1
    j readlooppixel 
                
                                    
cont2:
    
    addi $t5, $t5, 1
    bnez $v0, readlooppixel     #enquanto v0 for diferente de 0 continua.
    j preparacao2

cont:
    
    addi $t9, $t9, 1
    j readloopcabecalho
    
preparacao2:

    la $a1, 0x10040000  
    j arrumandopixel
    
arrumandopixel:

    lw  $t3, 0($a1)
    sw $t3, 0($a1)
    addi $a1, $a1, 4            #incrementa para o proximo endereco de $a1.
    bne $a1, 0x10080000, arrumandopixel

    j printcabecalho
    
printcabecalho:

    la $a0, STR     #puxando STR para $a0 para aplicar o syscall
    
    li $v0, 4       #syscall para printar o cabeçalho
    syscall         #porem está printando somente o primeiro caractere que foi gravado no endereço
    
    j end
    
end_error:
 
    la $a0, ERROROPEN
    li $v0, 4
    syscall

end:

    li $v0, 10
    syscall
Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
  • My Portuguese? Spanish is not great, so the comments are difficult. But, may I ask what format the incoming data is in? 24bit BGR? 16-bit, 5/6/5/ BGR, maybe 32-bit BGRA? Something else? This will help is to understand the algorithm needed to swap bits/bytes. If the color channels are are bytes, you should be able to swap quite easily by reading in the 3 channels into registers and write them in a different order. – Michael Dorgan Apr 05 '21 at 17:32
  • It's portuguese, and 24 bit BGR. I cant wrap my head around to invert it i've tried using ````srl ```` and ````sll```` but no success. – Thomas Topfstedt Apr 05 '21 at 17:38
  • https://www.cs.swarthmore.edu/~soni/cs35/f13/Labs/extras/01/ppm_info.html - ok, to convert from BGC to RGB, you should read in the 3 8-bit intensity values into 3 registers, then write them back in the opposite order. You could do it with 1 register, but it would be slightly harder to get the write order correct. Could be done though. – Michael Dorgan Apr 05 '21 at 17:40
  • you dont need to revert G its already at the right place, just swap B and R – Antonin GAVREL Apr 05 '21 at 17:40
  • Sure - but reading in 3 pixels at a time would make the code a bit cleaner to read and understand I think. It also fixes the issue of overwriting the data before you have read it. – Michael Dorgan Apr 05 '21 at 17:42
  • Can you think of how to do it in C? Writing some C/pseudo code might help you figure out the algorithm, which you can translate to MIPS assembly. In the worst case, you can extract each 8-bit color value independently, then recreate the color using the ordering you want. If you can figure out how to extract just 1 of the colors, that should get you started. You're showing a lot of code here, but I find no attempt to swap the colors; just ancillary stuff around reading files. – Erik Eidt Apr 05 '21 at 18:09
  • @AntoninGAVREL the problem is how I do that in this stage where I have most of the code alredy working, it already displays an image, but like u said, with B and R inverted – Thomas Topfstedt Apr 06 '21 at 01:09

0 Answers0