flat assembler
Message board for the users of flat assembler.

 Index > Tutorials and Examples > Gradient color computation
Author
trolly

Joined: 26 Sep 2013
Posts: 2
trolly
Hi,

i have a code to make a gradient color filled rectangle.

to achieve this, it do an iteration for each line of the rectanglen
inside the loop, he compute each canal of the color, like this:

((EndRed-StartRed)/[endY-starty]) * yindex

then he combine the 3 canals (red<<16|green <<8 |blue) and draw the line in the computed color.

the code below works, but i think it could be optimized.
Can somebody help me with this?

Code:
```.VerticalGradient:
push ebp
mov ebp,esp
pusha

mov eax,[ebp+8]
mov [.tmp_bcolor],eax
mov eax,[ebp+12]
mov [.tmp_color],eax
mov eax,[ebp+16]
mov [.tmp_y2],eax
mov eax,[ebp+20]
mov [.tmp_x2],eax
mov eax,[ebp+24]
mov [.tmp_y1],eax
mov eax,[ebp+28]
mov [.tmp_x1],eax

mov eax,[.tmp_y2]
cmp eax,[.tmp_y1]
ja @f
mov ebx,[.tmp_y1]
mov [.tmp_y1],eax
mov [.tmp_y2],ebx
@@:
mov eax,[.tmp_y2]
sub eax,[.tmp_y1]
mov [.diff_y],eax

mov edx,[.tmp_y1]
.forY1:
cmp edx,[.tmp_y2]
jnb .nextY1
mov [.tmp_y],edx
mov ebx,[.tmp_color]
shr ebx,16
mov ecx,[.tmp_bcolor]
shr ecx,16
cmp ebx,ecx
jnb @f
sub ecx,ebx   ;ecx contains red
mov [.red_inc],0x1
jmp .endred1
@@:
sub ebx,ecx
mov [.red_inc],0x0
mov ecx,ebx
.endred1:

mov eax,ecx
cmp [.diff_y],0x0
jna @f
push edx
xor edx,edx
mov ebx,[.diff_y]
div ebx
pop edx
push edx
sub edx,[.tmp_y1]
imul eax,edx
pop edx
@@:

shl eax,16
and eax,0xff0000
mov [.red_step],eax

mov ebx,[.tmp_color]
shr ebx,8
and ebx,0xff
mov ecx,[.tmp_bcolor]
shr ecx,8
and ecx,0xff
cmp ebx,ecx
jnb @f
sub ecx,ebx   ;ecx contains red
mov [.green_inc],0x1
jmp .endgreen1
@@:
sub ebx,ecx
mov [.green_inc],0x0
mov ecx,ebx
.endgreen1:
mov eax,ecx
cmp [.diff_y],0x0
jna @f
push edx
xor edx,edx
mov ebx,[.diff_y]
div ebx
pop edx
push edx
sub edx,[.tmp_y1]
imul eax,edx
pop edx
@@:

shl eax,8
and eax,0x00ff00
mov [.green_step],eax

mov ebx,[.tmp_color]
and ebx,0xff
mov ecx,[.tmp_bcolor]
and ecx,0xff
cmp ebx,ecx
jnb @f
sub ecx,ebx   ;ecx contains red
mov [.blue_inc],0x1
jmp .endblue1
@@:
sub ebx,ecx
mov [.blue_inc],0x0
mov ecx,ebx
.endblue1:
mov eax,ecx
cmp [.diff_y],0x0
jna @f
push edx
xor edx,edx
mov ebx,[.diff_y]
div ebx
pop edx
push edx
sub edx,[.tmp_y1]
imul eax,edx
pop edx
@@:

and eax,0x0000ff
mov [.blue_step],eax

mov eax,[.tmp_color]
and eax,0xff0000
cmp [.red_inc],0x1
jne @f
jmp .suite_red1
@@:
sub eax,[.red_step]
.suite_red1:
and eax,0xff0000
mov [.red_step],eax

mov eax,[.tmp_color]
and eax,0x00ff00
cmp [.green_inc],0x1
jne @f
jmp .suite_green1
@@:
sub eax,[.green_step]
.suite_green1:
and eax,0x00ff00
mov [.green_step],eax

mov eax,[.tmp_color]
and eax,0x0000ff
cmp [.blue_inc],0x1
jne @f
jmp .suite_blue1
@@:
sub eax,[.blue_step]
.suite_blue1:
and eax,0x0000ff
mov [.blue_step],eax

mov eax,[.red_step]
mov ebx,[.green_step]
mov ecx,[.blue_step]
or eax,ebx
or eax,ecx
DrawLine [.tmp_x1],[.tmp_y],[.tmp_x2],[.tmp_y],eax
mov edx,[.tmp_y]
inc edx
jmp .forY1
.nextY1:

popa
mov esp,ebp
pop ebp
ret 24
```
03 Oct 2013, 18:34
trolly

Joined: 26 Sep 2013
Posts: 2
trolly
note: this code can do a gradient from 00 to FF, and from FF to 00 (for each canal independently)
03 Oct 2013, 18:35
edfed

Joined: 20 Feb 2006
Posts: 4240
Location: 2018
edfed
it can be optimised by computing the gradient with besenham algorithm, and putting this algo in a single function called for each color component.
28 Oct 2013, 08:51
AsmGuru62

Joined: 28 Jan 2004
Posts: 1418
AsmGuru62
Is it possible for 'DrawLine' to take, say, 95% of the time in that function?
If yes, then why optimize the rest?
28 Oct 2013, 17:03
HaHaAnonymous

Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
[ Post removed by author. ]

Last edited by HaHaAnonymous on 28 Feb 2015, 19:15; edited 1 time in total
28 Oct 2013, 17:57
AsmGuru62

Joined: 28 Jan 2004
Posts: 1418
AsmGuru62
I see... this is why some products never get done (3DRealms fiasco).
28 Oct 2013, 18:28
typedef

Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
Look up tables and or const values
29 Oct 2013, 18:30
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum