Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- welcome: .asciiz "Welcome! Please enter the name of the file you would like to edit? \n"
- filterType: .asciiz " \n"
- brightnessPrompt: .asciiz "Enter desired brightness percentage (0 to 200)\n"
- shadowfillPrompt: .asciiz "Enter desired shadow/fill light value (-255 to 255)\n"
- header: .space 54 # bitmap header data stored here
- inputFileName: .space 128 # name of the input file, specified by user
- outputNameMessage: .asciiz "Please enter the name of the output file. Use the extension '.bmp'\n"
- impFiltSelMessage: .asciiz "\n***Invalid filter choice***\n\n"
- inputErrorMessage: .asciiz "\nThe input image couldn't be read. Please confirm that you entered the proper file name. Program restarting.\n\n"
- 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"
- nonBitmapEnteredMessage: .asciiz "\nThe file entered is not a bitmap. Please enter a 24-bit bitmap as input. Program restarting.\n\n"
- 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"
- 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"
- outName: .space 128 #name of the outputted file
- xOffset: .byte -5,0,5,-5,0,5,-5,0,5
- yOffset: .byte -5,-5,-5,0,0,0,5,5,5
- vEdgeDetect: .byte 10,20,10,0,0,0,-10,-20,-10 #mask for vertical edge detection
- hEdgeDetect: .byte 10,0,-10,20,0,-20,10,0,-10 #mask for horizontal edge detection
- edgeDetect: .byte 0,10,0,10,-40,10,0,10,0 #mask to detect all edges
- sharpen: .byte 0,-10,0,-10,50,-10,0,-10,0 #mask to sharpen the image
- 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
- gaussianBlur: .byte 10,20,10,20,40,20,10,20,10 #mask to gaussian blur the image
- buffer: .space 1 # just here so that there are no compile time errors
- ######################################################################################################
- # A program to process an image with multiple different filters.
- # The input image needs to be a .bmp image and the output will be the same type.
- # The filters include saturation, grayscale, edge-detection, etc.
- # Authors: Shaun Howard, Emilio Colindres, Bennett Sherman, Josh Tang, Kevin Perera
- #
- # $s0 - the file descriptor
- # $s1 - the size of the data section of the image (after 54 byte offset)
- # $s2 - the pixel array of the bmp image
- #######################################################################################################
- .text
- main:
- #print welcome string
- li $v0, 4 # syscall 4, print string
- la $a0, welcome # load welcome string
- syscall
- #read filename
- li $v0, 8 # syscall 8, read string
- la $a0, inputFileName # store string in inputFileName
- li $a1, 128 # read at most 256 characters
- syscall
- #print 'enter the output filename' message
- li $v0, 4 # syscall 4, print string
- la $a0, outputNameMessage # load welcome string
- syscall
- #read output name
- li $v0, 8 # syscall 8, read string
- la $a0, outName # store string in outName
- li $a1, 128 # read at most 256 characters
- syscall
- # remove trailing newline
- li $t0, '\n' # we are looking for this character
- li $t1, 128 # length of the outName
- li $t2, 0 # clear the current character
- outputRemoveNewLine:
- beqz $t1, newLineLoopInit # if end of string, jump to remove newline from input string
- subu $t1, $t1, 1 # decrement the index
- lb $t2, outName($t1) # load the character at current index position
- bne $t2, $t0, outputRemoveNewLine # if current character != '\n', jump to loop beginning
- li $t0, 0 # else store null character
- sb $t0, outName($t1) # and overwrite newline character with null
- newLineLoopInit:
- # remove trailing newline
- li $t0, '\n' # we are looking for this character
- li $t1, 128 # length of the inputFileName
- li $t2, 0 # clear the current character
- newLineLoop:
- beqz $t1, newLineLoopEnd # if end of string, jump to loop end
- subu $t1, $t1, 1 # decrement the index
- lb $t2, inputFileName($t1) # load the character at current index position
- bne $t2, $t0, newLineLoop # if current character != '\n', jump to loop beginning
- li $t0, 0 # else store null character
- sb $t0, inputFileName($t1) # and overwrite newline character with null
- newLineLoopEnd:
- #open input file
- li $v0, 13 # syscall 13, open file
- la $a0, inputFileName # load filename address
- li $a1, 0 # read flag
- li $a2, 0 # mode 0
- syscall
- bltz $v0, inputFileErrorHandler #if $v0=-1, there was a descriptor error; go to handler.
- move $s0, $v0 # save file descriptor
- #read header data
- li $v0, 14 # syscall 14, read from file
- move $a0, $s0 # load file descriptor
- la $a1, header # load address to store data
- li $a2, 54 # read 54 bytes
- syscall
- #confirm the bitmap is well...a bitmap
- li $t0, 0x4D42 #0X4D42 is the signature for a bitmap
- lhu $t1, header #the signature is stored in the first two bytes
- bne $t0, $t1, inputNotBMPHandler #if these two aren't equal, then the input is not a bitmap
- #save the width
- lw $s7, header+18
- #save height
- lw $s4, header+22
- #confirm that the bitmap is actually 24 bits
- li $t0, 0x18 #store 0x18 into $t0. 0x18 is 24 in decimal, i.e. a 24-bit bitmap
- 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)
- 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
- #we're good! File is the right format. Now we can proceed
- lw $s1, header+34 # store the size of the data section of the image
- #read image data into array
- li $v0, 9 # syscall 9, allocate heap memory
- move $a0, $s1 # load size of data section
- syscall
- move $s2, $v0 # store the base address of the array in $s2
- li $v0, 14 # syscall 14, read from file
- move $a0, $s0 # load file descriptor
- move $a1, $s2 # load base address of array
- move $a2, $s1 # load size of data section
- syscall
- #close file
- move $a0, $s0 # move the file descriptor into argument register
- li $v0, 16 # syscall 16, close file
- syscall
- #determine the filter the user wants to run by input
- la $s3, buffer #load the address of the buffer into $s3
- box_blur:
- move $t6, $s2 #load image
- move $t4, $zero
- #get the box mask
- lb $t7,boxBlur
- j convolution
- #performs matrix convolution on the image
- convolution:
- #set accumulator to 0
- move $t9, $zero
- #find the indexes of the target pixel
- div $t0,$t4,3
- div $t5,$t0,$s7
- mul $t1,$t5,$s7 # S7 szerokosc
- sub $t1,$t0,$t1
- #initializes the kernel counter
- subi $t8, $zero, 1
- #for each pixel loop apply the kernel to the neighbooring pixels
- j kernel_loop
- #stores the pixel values
- accumulate:
- #get blue value of the pixel
- lb $t0,($t6)
- sll $t0, $t0, 24
- srl $t0, $t0, 24
- mul $t0,$t0,$t3
- add $t9,$t0,$t9
- sb $t0, 0($s3)
- #get green value of the pixel
- lb $t0,1($t6)
- sll $t0, $t0, 24
- srl $t0, $t0, 24
- mul $t0,$t0,$t3
- add $t9,$t0,$t9
- sb $t0, 1($s3)
- #get red value of the pixel
- lb $t0,2($t6)
- sll $t0, $t0, 24
- srl $t0, $t0, 24
- mul $t0,$t0,$t3
- add $t9,$t0,$t9
- sb $t0, 2($s3)
- addi $t4, $t4, 3
- #increment counters to use next pixel
- #if we reach the end of the array, exit
- bge $t4, $s1, write_file
- add $t6, $t6, 3
- add $s3, $s3, 3
- #else jump to start of the loop
- j convolution
- #multiplies the value of each surrounding pixel by the corresponding kernel value and sums them
- kernel_loop:
- #increment loop index
- addi $t8, $t8, 1
- bge $t8,9,accumulate
- #calculate x
- lb $t0, xOffset($t8)
- add $t2, $t1, $t0
- #calculate y
- lb $t0, yOffset($t8)
- add $t3, $t5, $t0
- #check x
- bge $t2, $s7, kernel_loop
- ble $t2, $zero, kernel_loop
- #check y
- bge $t3, $s4, kernel_loop
- ble $t3, $zero, kernel_loop
- mul $t0,$s7,$t3
- add $t0,$t0,$t2
- mul $t0, $t0, 3
- #add $t2, $t6, $t0
- add $t3, $t7, $t8
- #lb $t3, 0($t3)
- bge $t8,8,accumulate
- j kernel_loop
- write_file:
- #open output file
- li $v0, 13
- la $a0, outName
- li $a1, 1 #1 to write, 0 to read
- li $a2, 0
- syscall
- move $t1, $v0 #output file descriptor in $t1
- #confirm that file exists (if $v0/$t1 contains -1, it means there was an error opening the output descriptor)
- #If we're here, the file can be written to, so write header to output
- li $v0, 15 #prep $v0 for write syscall
- move $a0, $t1
- la $a1, header
- addi $a2, $zero,54
- syscall
- #write to output file
- li $v0, 15 #prep $v0 for write syscall
- move $a0, $t1
- la $a1, buffer
- move $a2, $s1
- syscall
- #close file
- move $a0, $t1
- li $v0, 16
- syscall
- leave:
- #nicely terminate the program
- li $v0, 10
- syscall
- #SECTION FOR LATE CREATED HANDLERS
- inputFileErrorHandler:
- #print file input error message
- li $v0, 4 # syscall 4, print string
- la $a0, inputErrorMessage # print the message
- syscall
- j main
- improperFormatHandler:
- #print file input error message
- li $v0, 4 # syscall 4, print string
- la $a0, improperFormatMessage # print the message
- syscall
- j main
- inputNotBMPHandler:
- #print file input error message
- li $v0, 4 # syscall 4, print string
- la $a0, nonBitmapEnteredMessage # print the message
- syscall
- j main
- outputFileErrorHandler:
- #print file output error message
- li $v0, 4 # syscall 4, print string
- la $a0, outputErrorMessage # print the message
- syscall
- j main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement