-
Notifications
You must be signed in to change notification settings - Fork 1
/
edgeExtractor.asm
241 lines (225 loc) · 5.41 KB
/
edgeExtractor.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
.macro gaussianBlur(%image_pointer, %image_size)
add $t5, %image_pointer, %image_size
li $a1, 3
li $t0, 513
sll $t0, $t0, 2
add %image_pointer, %image_pointer, $t0 # starts at (1,1)
add $t5, $t5, $t0 # end at (510, 510)
createKernel($a1)
move $t0, %image_pointer # %image_pointer
li $t1, -1 # i counter
move $t6, $zero # reset rgb regs
move $t7, $zero
move $t8, $zero
j row
convolute:
srl $t8, $t8, 4
srl $t7, $t7, 4
srl $t6, $t6, 4
sll $t7, $t7, 8
sll $t8, $t8, 16
or $t6, $t6, $t7
or $t6, $t6, $t8
sw $t6, 0($t0)
bge $t0, $t5, return # if at last pixel goto return
move $t1, $zero # i resets to 0
add $t0, $t0, 4 # next image pixel
move $t6, $zero # reset rgb regs
move $t7, $zero
move $t8, $zero
bne $t0, 510, row
add $t0, $t0, 12 # if last pixel jump two pixels ahead
row:
li $t2, -1 # j = -1
addi $t1, $t1, 1 # i++
bgt $t1, 2, convolute
col:
beq $t2, 2, row
addi $t3, $t1, -1 # i - 1
mul $t3, $t3, 512 # 512 * (i - 1)
add $t3, $t3, $t2 # $t3 += j
sll $t3, $t3, 2 # $t3 *= 4
add $t3, $t0, $t3 # pxAddr (or $t3) = %image_pointer +/- 512*(i-1) + j
getKernelValue($t1, $t2)
# loads bytes to $t6, $t7, $t8
lbu $t4, ($t3)
mtc1 $t4, $f3
cvt.s.w $f3, $f3
mul.s $f3, $f3, $f5
cvt.w.s $f3, $f3
mfc1 $t4, $f3
addu $t6, $t6, $t4
addi $t3, $t3, 1
lbu $t4, ($t3)
mtc1 $t4, $f3
cvt.s.w $f3, $f3
mul.s $f3, $f3, $f5
cvt.w.s $f3, $f3
mfc1 $t4, $f3
addu $t7, $t7, $t4
addi $t3, $t3, 1
lbu $t4, ($t3)
mtc1 $t4, $f3
cvt.s.w $f3, $f3
mul.s $f3, $f3, $f5
cvt.w.s $f3, $f3
mfc1 $t4, $f3
addu $t8, $t8, $t4
addi $t3, $t3, 2
addi $t2, $t2, 1
ble $t2, 2, col
j row
return:
move $t5, $a0
.end_macro
.macro createKernel(%kernel_size)
# store kernel into float process
li $t0, 0x3f800000 # 0.
mtc1 $t0, $f0
li $t0, 0x40000000 # 0.44198
mtc1 $t0, $f1
li $t0, 0x3f800000 # 0.27901
mtc1 $t0, $f2
li $t0, 0x40800000
mtc1 $t0, $f4
.end_macro
.macro extractBorders(%image_pointer, %size)
convertGray(%image_pointer, %size)
add $t5, %image_pointer, %size
li $a1, 3
li $t0, 513
sll $t0, $t0, 2
add %image_pointer, %image_pointer, $t0 # starts at (1,1)
sub $t5, $t5, $t0 # end at (510, 510)
move $t0, %image_pointer # %image_pointer
li $t1, -1 # i counter
li $t9, 1 # pixel line counter
move $t6, $zero # reset rgb regs
li $s6, 1023 # min value
li $s7, -1023 # max value
j row
convolute:
sw $t6, 0($t0)
bge $t0, $t5, return # if at last pixel goto return
move $t1, $zero # i resets to 0
add $t0, $t0, 4 # next image pixel
addi $t9, $t9, 1
move $t6, $zero # reset rgb regs
bne $t9, 510, row
add $t0, $t0, 12 # if last pixel jump two pixels ahead
li $t9, 1
row:
li $t2, -1 # j = -1
addi $t1, $t1, 1 # i++
bgt $t1, 2, convolute
col:
beq $t2, 2, row
addi $t3, $t1, -1 # i - 1
mul $t3, $t3, 512 # 512 * (i - 1)
add $t3, $t3, $t2 # $t3 += j
sll $t3, $t3, 2 # $t3 *= 4
add $t3, $t0, $t3 # pxAddr (or $t3) = %image_pointer +/- 512*(i-1) + j
getLaplacianValue($t1, $t2)
# loads bytes to $t6, $t7, $t8
lbu $t4, ($t3)
mul $t4, $t4, $v0
addu $t6, $t6, $t4
addi $t3, $t3, 4
addi $t2, $t2, 1
ble $t2, 2, col
j row
return:
scaleImage(%image_pointer, %size)
.end_macro
.macro scaleImage(%image_pointer, %size)
move $t0, %image_pointer
add $t3, %image_pointer, %size
loop:
lbu $t2, 0($t0) #
convertScaleAbs($t2)
sb $t2, 0($t0) #
sb $t2, 1($t0) #
sb $t2, 2($t0) #
sb $zero, 3($t0)
addi $t0, $t0, 4
bne $t0, $t3, loop
.end_macro
.macro convertGray(%image_pointer, %size)
move $t0, %image_pointer
add $t1, %image_pointer, %size
li $t2, 10
#weights
li $t5, 3
loop:
lbu $t2, 0($t0) #
lbu $t3, 1($t0) #
lbu $t4, 2($t0) #
add $t2, $t2, $t3 # GRAYSCALE
add $t2, $t2, $t4 #
divu $t2, $t2, $t5 #
sb $t2, 0($t0) #
sb $t2, 1($t0) #
sb $t2, 2($t0) #
addi $t0, $t0, 4
bne $t0, $t1, loop
.end_macro
.macro convertScaleAbs(%component)
bgtz %component, checknearz
li %component, 0x00
j return
checknearz:
bge %component, 56, blackPixel
li %component 0x80 # near zero = 128
j return
blackPixel:
li %component, 0xFF
j return
return:
.end_macro
.macro getKernelValue(%x, %y)
addi $t9, %y, 1 #due to logic at above function we need to sum j up to 1
bnez %x, xNotZero
beqz $t9, returnF0
beq $t9, 1, returnF1
beq $t9, 2, returnF2
xNotZero:
bne %x, 1, xNotOne
beqz $t9, returnF1
beq $t9, 1, returnF4
beq $t9, 2, returnF1
xNotOne:
beqz $t9, returnF0
beq $t9, 1, returnF1
beq $t9, 2, returnF2
returnF0: mov.s $f5, $f0
j return
returnF1: mov.s $f5, $f1
j return
returnF2: mov.s $f5, $f2
j return
returnF4: mov.s $f5, $f4
return:
.end_macro
.macro getLaplacianValue(%x, %y)
addi $t9, %y, 1 #due to logic at above function we need to sum j up to 1
bnez %x, xNotZero
beqz $t9, return0
beq $t9, 1, return1
beq $t9, 2, return0
xNotZero:
bne %x, 1, xNotOne
beqz $t9, return1
beq $t9, 1, return4
beq $t9, 2, return1
xNotOne:
beqz $t9, return0
beq $t9, 1, return1
beq $t9, 2, return0
return0: move $v0, $zero
j return
return1: li $v0, -1
j return
return4: li $v0, 4
j return
return:
.end_macro