Advertisement
Guest User

wyciete

a guest
Apr 25th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .data
  2. welcome:        .asciiz "Welcome! Please enter the name of the file you would like to edit?  \n"
  3. filterType:     .asciiz " \n"
  4.  
  5. brightnessPrompt:    .asciiz "Enter desired brightness percentage (0 to 200)\n"
  6. shadowfillPrompt:    .asciiz "Enter desired shadow/fill light value (-255 to 255)\n"
  7.  
  8. header:         .space   54     # bitmap header data stored here
  9. inputFileName:  .space  128     # name of the input file, specified by user
  10. outputNameMessage:  .asciiz "Please enter the name of the output file. Use the extension '.bmp'\n"
  11. impFiltSelMessage:  .asciiz "\n***Invalid filter choice***\n\n"
  12. inputErrorMessage:  .asciiz "\nThe input image couldn't be read. Please confirm that you entered the proper file name. Program restarting.\n\n"
  13. improperFormatMessage:  .asciiz "\nThe file entered is a bitmap, but is not 24-bit, and thus is an invalid input for this program. Program restarting.\n\n"
  14. nonBitmapEnteredMessage: .asciiz "\nThe file entered is not a bitmap. Please enter a 24-bit bitmap as input. Program restarting.\n\n"
  15. outputErrorMessage: .asciiz "\nThe selected output file cannot be created. This likely is a result of having entered a directory that doesn't exist. The program will now restart.\n"
  16. filterTimeMessage:  .asciiz "\nFiltering initiated. Although the program appears to be unresponsive, it is working. Filtering can take anywhere from a few seconds to a minute, so please be patient.\n"
  17. outName:        .space  128 #name of the outputted file
  18. xOffset:            .byte   -5,0,5,-5,0,5,-5,0,5
  19. yOffset:            .byte   -5,-5,-5,0,0,0,5,5,5
  20. vEdgeDetect:            .byte   10,20,10,0,0,0,-10,-20,-10 #mask for vertical edge detection
  21. hEdgeDetect:            .byte   10,0,-10,20,0,-20,10,0,-10 #mask for horizontal edge detection
  22. edgeDetect:                     .byte   0,10,0,10,-40,10,0,10,0    #mask to detect all edges
  23. sharpen:                        .byte   0,-10,0,-10,50,-10,0,-10,0 #mask to sharpen the image
  24. boxBlur:            .byte   10,20,10,20,40,20,10,20,10 #.byte   256,256,256,256,256,256,256,256,256 #mask to box blur the image
  25. gaussianBlur:           .byte   10,20,10,20,40,20,10,20,10 #mask to gaussian blur the image
  26.  
  27. buffer:         .space  1   # just here so that there are no compile time errors
  28.  
  29. ######################################################################################################
  30. # A program to process an image with multiple different filters.
  31. # The input image needs to be a .bmp image and the output will be the same type.
  32. # The filters include saturation, grayscale, edge-detection, etc.
  33. # Authors: Shaun Howard, Emilio Colindres, Bennett Sherman, Josh Tang, Kevin Perera
  34. #
  35. #   $s0 - the file descriptor
  36. #   $s1 - the size of the data section of the image (after 54 byte offset)
  37. #   $s2 - the pixel array of the bmp image
  38. #######################################################################################################
  39. .text
  40. main:
  41.    
  42.     #print welcome string
  43.     li      $v0, 4          # syscall 4, print string
  44.     la      $a0, welcome        # load welcome string
  45.     syscall
  46.    
  47.     #read filename
  48.     li      $v0, 8          # syscall 8, read string
  49.     la      $a0, inputFileName  # store string in inputFileName
  50.     li      $a1, 128        # read at most 256 characters
  51.     syscall
  52.    
  53.     #print 'enter the output filename' message
  54.     li      $v0, 4          # syscall 4, print string
  55.     la      $a0, outputNameMessage  # load welcome string
  56.     syscall
  57.    
  58.     #read output name
  59.     li      $v0, 8          # syscall 8, read string
  60.     la      $a0, outName    # store string in outName
  61.     li      $a1, 128        # read at most 256 characters
  62.     syscall
  63.    
  64.     # remove trailing newline
  65.     li      $t0, '\n'       # we are looking for this character
  66.     li      $t1, 128        # length of the outName
  67.     li      $t2, 0          # clear the current character
  68.    
  69. outputRemoveNewLine:
  70.     beqz    $t1, newLineLoopInit    # if end of string, jump to remove newline from input string
  71.     subu    $t1, $t1, 1     # decrement the index
  72.     lb      $t2, outName($t1)   # load the character at current index position
  73.     bne     $t2, $t0, outputRemoveNewLine   # if current character != '\n', jump to loop beginning
  74.     li      $t0, 0          # else store null character
  75.     sb      $t0, outName($t1) # and overwrite newline character with null
  76.    
  77. newLineLoopInit:
  78.     # remove trailing newline
  79.     li      $t0, '\n'   # we are looking for this character
  80.     li      $t1, 128    # length of the inputFileName
  81.     li      $t2, 0      # clear the current character
  82.    
  83. newLineLoop:
  84.     beqz    $t1, newLineLoopEnd # if end of string, jump to loop end
  85.     subu    $t1, $t1, 1         # decrement the index
  86.     lb      $t2, inputFileName($t1) # load the character at current index position
  87.     bne     $t2, $t0, newLineLoop   # if current character != '\n', jump to loop beginning
  88.     li      $t0, 0          # else store null character
  89.     sb      $t0, inputFileName($t1) # and overwrite newline character with null
  90.    
  91. newLineLoopEnd:
  92.    
  93.     #open input file
  94.     li      $v0, 13     # syscall 13, open file
  95.     la      $a0, inputFileName  # load filename address
  96.     li      $a1, 0      # read flag
  97.     li      $a2, 0      # mode 0
  98.     syscall
  99.     bltz    $v0, inputFileErrorHandler  #if $v0=-1, there was a descriptor error; go to handler.
  100.     move    $s0, $v0    # save file descriptor
  101.    
  102.     #read header data
  103.     li      $v0, 14     # syscall 14, read from file
  104.     move    $a0, $s0    # load file descriptor
  105.     la      $a1, header # load address to store data
  106.     li      $a2, 54     # read 54 bytes
  107.     syscall
  108.    
  109.     #confirm the bitmap is well...a bitmap
  110.     li      $t0, 0x4D42 #0X4D42 is the signature for a bitmap
  111.     lhu     $t1, header #the signature is stored in the first two bytes
  112.     bne     $t0, $t1, inputNotBMPHandler #if these two aren't equal, then the input is not a bitmap
  113.    
  114.     #save the width
  115.     lw  $s7, header+18
  116.     #save height
  117.     lw  $s4, header+22
  118.    
  119.     #confirm that the bitmap is actually 24 bits
  120.     li      $t0, 0x18   #store 0x18 into $t0. 0x18 is 24 in decimal, i.e. a 24-bit bitmap
  121.     lb      $t1, header+28  #offset of 28 points at the header's indication of how many bits the bmp is (2 bytes, we only need the first one)
  122.     bne     $t0, $t1, improperFormatHandler #if the two aren't equal, it means the entered file is not a 24 bit bmp, so go to the handler to start again
  123.    
  124.     #we're good! File is the right format. Now we can proceed
  125.     lw      $s1, header+34  # store the size of the data section of the image
  126.    
  127.     #read image data into array
  128.     li      $v0, 9      # syscall 9, allocate heap memory
  129.     move    $a0, $s1    # load size of data section
  130.     syscall
  131.     move    $s2, $v0    # store the base address of the array in $s2
  132.    
  133.     li      $v0, 14     # syscall 14, read from file
  134.     move    $a0, $s0    # load file descriptor
  135.     move    $a1, $s2    # load base address of array
  136.     move    $a2, $s1    # load size of data section
  137.     syscall
  138.    
  139.     #close file
  140.     move    $a0, $s0        # move the file descriptor into argument register
  141.     li      $v0, 16         # syscall 16, close file
  142.     syscall
  143.    
  144. #determine the filter the user wants to run by input
  145.  
  146.     la $s3, buffer  #load the address of the buffer into $s3   
  147.  
  148. box_blur:
  149.     move $t6, $s2   #load image
  150.     move $t4, $zero
  151.     #get the box mask
  152.     lb $t7,boxBlur
  153.     j convolution
  154.                    
  155. #performs matrix convolution on the image                  
  156. convolution:
  157.     #set accumulator to 0
  158.     move $t9, $zero
  159.    
  160.     #find the indexes of the target pixel
  161.     div $t0,$t4,3
  162.     div $t5,$t0,$s7
  163.     mul $t1,$t5,$s7 # S7 szerokosc
  164.     sub $t1,$t0,$t1
  165.    
  166.     #initializes the kernel counter
  167.     subi $t8, $zero, 1
  168.     #for each pixel loop apply the kernel to the neighbooring pixels
  169.     j kernel_loop
  170.  
  171. #stores the pixel values
  172. accumulate:
  173.     #get blue value of the pixel
  174.     lb $t0,($t6)
  175.     sll $t0, $t0, 24
  176.     srl $t0, $t0, 24
  177.     mul $t0,$t0,$t3
  178.     add $t9,$t0,$t9
  179.     sb $t0, 0($s3)
  180.    
  181.     #get green value of the pixel
  182.     lb $t0,1($t6)
  183.     sll $t0, $t0, 24
  184.     srl $t0, $t0, 24
  185.     mul $t0,$t0,$t3
  186.     add $t9,$t0,$t9
  187.     sb $t0, 1($s3)
  188.    
  189.     #get red value of the pixel
  190.     lb $t0,2($t6)
  191.     sll $t0, $t0, 24
  192.     srl $t0, $t0, 24
  193.     mul $t0,$t0,$t3
  194.     add $t9,$t0,$t9
  195.     sb $t0, 2($s3)
  196.    
  197.     addi $t4, $t4, 3
  198.     #increment counters to use next pixel
  199.     #if we reach the end of the array, exit
  200.     bge $t4, $s1, write_file
  201.     add $t6, $t6, 3
  202.     add $s3, $s3, 3
  203.     #else jump to start of the loop
  204.     j convolution
  205.  
  206. #multiplies the value of each surrounding pixel by the corresponding kernel value and sums them    
  207. kernel_loop:
  208.     #increment loop index
  209.     addi $t8, $t8, 1
  210.    
  211.     bge $t8,9,accumulate
  212.     #calculate x
  213.     lb $t0, xOffset($t8)
  214.     add $t2, $t1, $t0
  215.    
  216.     #calculate y
  217.     lb $t0, yOffset($t8)
  218.     add $t3, $t5, $t0
  219.    
  220.     #check x
  221.     bge $t2, $s7, kernel_loop
  222.     ble $t2, $zero, kernel_loop
  223.     #check y
  224.     bge $t3, $s4, kernel_loop
  225.     ble $t3, $zero, kernel_loop
  226.    
  227.     mul $t0,$s7,$t3
  228.     add $t0,$t0,$t2
  229.     mul $t0, $t0, 3
  230.     #add $t2, $t6, $t0
  231.    
  232.     add $t3, $t7, $t8
  233.     #lb $t3, 0($t3)
  234.            
  235.     bge $t8,8,accumulate
  236.     j kernel_loop
  237.            
  238.    
  239.     write_file:
  240.    
  241.     #open output file
  242.     li      $v0, 13
  243.     la      $a0, outName
  244.     li      $a1, 1      #1 to write, 0 to read
  245.     li      $a2, 0
  246.     syscall
  247.     move    $t1, $v0    #output file descriptor in $t1
  248.    
  249.     #confirm that file exists (if $v0/$t1 contains -1, it means there was an error opening the output descriptor)
  250.  
  251.  
  252.     #If we're here, the file can be written to, so write header to output
  253.     li      $v0, 15     #prep $v0 for write syscall
  254.     move    $a0, $t1
  255.     la      $a1, header
  256.     addi    $a2, $zero,54
  257.     syscall
  258.    
  259.     #write to output file
  260.     li      $v0, 15     #prep $v0 for write syscall
  261.     move    $a0, $t1
  262.     la      $a1, buffer
  263.     move    $a2, $s1
  264.     syscall
  265.    
  266.     #close file
  267.     move    $a0, $t1
  268.     li      $v0, 16
  269.     syscall
  270.  
  271. leave:
  272.     #nicely terminate the program
  273.     li      $v0, 10
  274.     syscall
  275.    
  276.     #SECTION FOR LATE CREATED HANDLERS
  277.  
  278.  
  279.    
  280. inputFileErrorHandler:
  281.     #print file input error message
  282.     li      $v0, 4          # syscall 4, print string
  283.     la      $a0, inputErrorMessage  # print the message
  284.     syscall
  285.     j       main
  286.    
  287. improperFormatHandler:
  288.     #print file input error message
  289.     li      $v0, 4          # syscall 4, print string
  290.     la      $a0, improperFormatMessage  # print the message
  291.     syscall
  292.     j       main
  293.    
  294. inputNotBMPHandler:
  295.     #print file input error message
  296.     li      $v0, 4          # syscall 4, print string
  297.     la      $a0, nonBitmapEnteredMessage    # print the message
  298.     syscall
  299.     j       main
  300.    
  301. outputFileErrorHandler:
  302.     #print file output error message
  303.     li      $v0, 4          # syscall 4, print string
  304.     la      $a0, outputErrorMessage # print the message
  305.     syscall
  306.     j       main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement