-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.asm
277 lines (223 loc) · 8.13 KB
/
main.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
.686
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
;Libraries created
include clamp.inc
include console-messages.inc
include myIO.inc
.data
;Read file variables
inputImage_fileHandle dd 0H
inputImage_readCount dd 0H
;Write file variables
outputImage_fileHandle dd 0H
outputImage_writeCount dd 0H
;File Buffers
read_buffer_size dd 1H
inputImage_fileBuffer db 54 dup(0)
bgr_color_buffer byte 3 DUP(0)
;Core variables
color_index dd 1H ; index to operate
value_to_add dd 0H ; value to add
outputImage_name db 50 dup(0) ; name of output image file
inputImage_name db 50 dup(0) ; name of input image file
.code
;Copy the first 54 bytes of the input image to the output image file
_CopyFirst54Bytes:
;Prologue of the subroutine --------
push ebp
mov ebp, esp
;Read input image
invoke ReadFile, inputImage_fileHandle, addr inputImage_fileBuffer, 54, addr inputImage_readCount, NULL
;Write readed buffer to output image
invoke WriteFile, outputImage_fileHandle, addr inputImage_fileBuffer, 54, addr outputImage_writeCount, NULL ; Escreve os 54 bytes do arquivo
;Epilogue of the subroutine --------
mov esp, ebp
pop ebp
ret
;Filter pixel by adding value to one of the color bands
;params: BGR array address, band index to operate, value to add
;Note: modifies the EAX, ECX and EDX registers
_FilterPixel:
;Prologue of the subroutine --------
push ebp
mov ebp, esp
sub esp, 12
;value to add
mov eax, DWORD PTR[ebp+8]
mov DWORD PTR[ebp-4], eax
;index to operate
mov eax, DWORD PTR[ebp+12]
mov DWORD PTR[ebp-8], eax
;BGR array address
mov eax, DWORD PTR[ebp+16]
mov DWORD PTR[ebp-12], eax
mov ecx, DWORD PTR[ebp-12]
;R-Band
mov eax, [ecx][2]
push eax
;G-Band
mov eax, [ecx][1]
push eax
;B-Band
mov eax, [ecx][0]
push eax
;----- Getting value in the desired band (using index) -------
cmp DWORD PTR[ebp-8], 0
je _FilterPixel_AddValue ; when index == 0, you do not need to configure anything, because the zero index value is already in eax
cmp DWORD PTR[ebp-8], 1
je _FilterPixel_OnIndexEquals1 ; when index == 1
cmp DWORD PTR[ebp-8], 2
je _FilterPixel_OnIndexEquals2 ; when index == 2
_FilterPixel_OnIndexEquals1:
mov eax, [ecx][1]
jmp _FilterPixel_AddValue
_FilterPixel_OnIndexEquals2:
mov eax, [ecx][2]
jmp _FilterPixel_AddValue
;Add value in the chosen band
_FilterPixel_AddValue:
xor ah, ah ; ah = 0
add eax, DWORD PTR[ebp-4]
;Clamp new value in range [0, 255] -----------
_FilterPixel_Clamp:
push eax
call _Clamp
mov edx, eax
;----------- Filtering image adding value in chosen color band --------------
cmp DWORD PTR[ebp-8], 0
je Filter_Blue
cmp DWORD PTR[ebp-8], 1 ;
je Filter_Green
cmp DWORD PTR[ebp-8], 2 ;
jmp Filter_Red
Filter_Blue:
pop eax
mov DWORD PTR[ecx], edx ; B
pop eax
mov DWORD PTR[ecx+1], eax ; G
pop eax
mov DWORD PTR[ecx+2], eax ; R
jmp _FilterPixel_Return
Filter_Green:
pop eax
mov DWORD PTR[ecx], eax ; B
pop eax
mov DWORD PTR[ecx+1], edx ; G
pop eax
mov DWORD PTR[ecx+2], eax ; R
jmp _FilterPixel_Return
Filter_Red:
pop eax
mov DWORD PTR[ecx], eax ; B
pop eax
mov DWORD PTR[ecx+1], eax ; G
pop eax
mov DWORD PTR[ecx+2], edx ; R
jmp _FilterPixel_Return
;-------------------------------------------------------------------
_FilterPixel_Return:
;Epilogue of the subroutine --------
mov esp, ebp
pop ebp
ret 12 ;deallocate function parameters
;Filters the image, applying the filter logic to each pixel
_FilterImage:
;Prologue of the subroutine --------
push ebp
mov ebp, esp
sub esp, 12
;value to add
mov eax, DWORD PTR[ebp+8]
mov DWORD PTR[ebp-4], eax
;index to operate
mov eax, DWORD PTR[ebp+12]
mov DWORD PTR[ebp-8], eax
;brg offset
mov eax, DWORD PTR[ebp+16]
mov DWORD PTR[ebp-12], eax
_FilterImage_Loop:
;Read BGR pixel
invoke ReadFile, inputImage_fileHandle, addr bgr_color_buffer, 3, addr inputImage_readCount, NULL
;Filter readed pixel
push DWORD PTR[ebp-12] ; BGR array address
push DWORD PTR[ebp-8] ; index
push DWORD PTR[ebp-4] ; value to add
call _FilterPixel
;Write BGR pixel to file
invoke WriteFile, outputImage_fileHandle, addr bgr_color_buffer, 3, addr outputImage_writeCount, NULL
;Verifies EOF ---------
cmp inputImage_readCount, 0
je _FilterImage_Return
jne _FilterImage_Loop
_FilterImage_Return:
pop ebx
;Epilogue of the subroutine --------
mov esp, ebp
pop ebp
ret 12 ;deallocate parameters
;Read the input data to apply in the program
_ReadInputs:
;Prologue of the subroutine --------
push ebp
mov ebp, esp
;Request input file name -----------------
;---- Print message ----
push offset CONSOLE_MSG_INPUT_IMG_FILENAME
call _MyIO_LogMessage
;---- Wait and handle input ----
push sizeof inputImage_name
push offset inputImage_name
call _MyIO_ReadString
;Request output file name -----------------
;---- Print message ----
push offset CONSOLE_MSG_OUTPUT_IMG_FILENAME
call _MyIO_LogMessage
;---- Wait and handle input ----
push sizeof outputImage_name
push offset outputImage_name
call _MyIO_ReadString
;Request color band index ------------------
;---- Print message ----
push offset CONSOLE_MSG_INDEX_COLORBAND
call _MyIO_LogMessage
;---- Wait and handle input ----
call _MyIO_ReadInteger
mov color_index, eax
;Request value to add in the band ------------
;---- Print message ----
push offset CONSOLE_MSG_VALUE_TO_ADD
call _MyIO_LogMessage
;---- Wait and handle input ----
call _MyIO_ReadInteger
mov value_to_add, eax
;Epilogue of the subroutine --------
mov esp, ebp
pop ebp
ret ;deallocate parameters
start:
call _MyIO_Setup ; Console input and output handler setup
call _ReadInputs ; Receives and processes entries, saving them in memory
; Open input image and then request reading mode ----------
invoke CreateFile, addr inputImage_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
mov inputImage_fileHandle, eax
; Create output image and request writing mode ----------
invoke CreateFile, addr outputImage_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
mov outputImage_fileHandle, eax
;Copies first 54 bytes of the image
call _CopyFirst54Bytes
;Filters BGR image pixels
push offset bgr_color_buffer
push color_index
push value_to_add
call _FilterImage
;Close the files and exit the program
invoke CloseHandle, inputImage_fileHandle
invoke CloseHandle, outputImage_fileHandle
invoke ExitProcess, 0
end start