flat assembler
Message board for the users of flat assembler.

Index > Examples and Tutorials > Gradient color computation

Author
Thread Post new topic Reply to topic
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
                                add eax,[.red_step]
                                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
                                add eax,[.green_step]
                                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
                                add eax,[.blue_step]
                                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
    
Post 03 Oct 2013, 18:34
View user's profile Send private message Reply with quote
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)
Post 03 Oct 2013, 18:35
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4225
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.
Post 28 Oct 2013, 08:51
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1398
Location: Toronto, Canada
AsmGuru62
Is it possible for 'DrawLine' to take, say, 95% of the time in that function?
If yes, then why optimize the rest?
Post 28 Oct 2013, 17:03
View user's profile Send private message Send e-mail Reply with quote
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
Post 28 Oct 2013, 17:57
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1398
Location: Toronto, Canada
AsmGuru62
I see... this is why some products never get done (3DRealms fiasco).
Smile
Post 28 Oct 2013, 18:28
View user's profile Send private message Send e-mail Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2914
Location: 0x77760000
typedef
Look up tables and or const values
Post 29 Oct 2013, 18:30
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.