flat assembler
Message board for the users of flat assembler.

Index > Windows > per pixel alpha blending 2 images artifact problem

Author
Thread Post new topic Reply to topic
dev_89



Joined: 24 Feb 2013
Posts: 8
dev_89 02 May 2013, 12:23
i made a template using dib sections and made some simple animations.
i used the following formula to calculate each pixel by alpha value:
dest = alpha * (source - dest) / 255 + dest
and it works fine except when i alpha is below 100 or lower at some positions black artifacts appears specially when alpha is nearby zero.
here is my code:(i know it looks like crap and i can optimize it but that's not what matters now)
Code:
movzx eax,byte [Color];this is the new pixel 
movzx ebx,byte [esi];current pixel(red)
sub eax,ebx
imul eax,[Alpha]
shr eax,8;since divide by 255 requires div instruction and it's slow and 256 ;is nearby 255 so i sacrificed 1 and used shr instruction to speed up the ;calculation
add eax,ebx
mov byte [Color],al

movzx eax,byte [Color+1]
movzx ebx,byte [esi+1];current pixel(green)
sub eax,ebx
imul eax,[Alpha]
shr eax,8
add eax,ebx
mov byte [Color+1],al

movzx eax,byte [Color+2]
movzx ebx,byte [esi+2];current pixel(blue)
sub eax,ebx
imul eax,[Alpha]
shr eax,8
add eax,ebx
mov byte [Color+2],al

mov eax,[Color]
    

any ideas what causes the black artifact on lower alpha value?
i know the there's something wrong with the calculation but dunno what is it.
btw,here is the compiled template.just look at the blood drops when they slowly disappears and you'll notice the black artifacts on some positions which completely destroys the back image and turns it to black.
edit:btw,the blood drop is 24 bit bitmap and fire is 32 bit bitmap.


Description:
Download
Filename: g.zip
Filesize: 340.9 KB
Downloaded: 238 Time(s)

Post 02 May 2013, 12:23
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 02 May 2013, 13:14
dev_89,

Artifacts appear in overlapping regions. Probably not only blending involved.
Post 02 May 2013, 13:14
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4338
Location: Now
edfed 02 May 2013, 13:30
you maybe should compute the mean of pixels you add at the line add eax,ebx.

after this line, you maybe need a shr eax,1 to recover the hypothetical 9th bit eventually set by the addition.

means, the application of your formulae is certainly wrong.
Post 02 May 2013, 13:30
View user's profile Send private message Visit poster's website Reply with quote
dev_89



Joined: 24 Feb 2013
Posts: 8
dev_89 02 May 2013, 14:37
@edfed:
i know that i'm doing the calculation wrong and i tried what you mentioned but more artifacts appeared.
could it be that 1 which got sacrificed causing this?maybe i should use div instruction to get the exact results?damn i'm confused!
Post 02 May 2013, 14:37
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 04 May 2013, 11:42
I haven't tried your code but you will end up with a skew towards zero unless you correct the alpha value for the shr 8 optimisation:
Code:
        mov ecx,dword [alpha]  ;   alpha : [0,255]
        inc ecx                ; 1+alpha : [1,256]

        movzx eax,byte [Color] ;       S : [0,255]
        movzx ebx,byte [esi]   ;       D : [0,255]

        sub eax,ebx            ;  S - D                             : [-255,255]
        mul eax,ecx            ; (S - D) * (1 + alpha)              : [-255 shl 8,255 shl 8]
        add ah,bl              ;((S - D) * (1 + alpha)) + (256 * D) : [0 shl 8,255 shl 8]
        mov byte [Color],ah    ;    
Post 04 May 2013, 11:42
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20343
Location: In your JS exploiting you and your system
revolution 04 May 2013, 17:43
Perhaps you could keep the two source bitmaps intact and render on demand to the screen buffer:
Code:
movzx eax,byte [Source1];from source1
movzx ebx,byte [Source2];from source2
;... mixing function here
mov byte [ScreenBuffer],al    
This way you can alter Alpha arbitrarily up or down and always have the output be regenerated on demand.

Plus this has the advantage of not needing to read the screen buffer directly (you only write to the screen), so this might be a consideration if you need to improve rendering speed.
Post 04 May 2013, 17:43
View user's profile Send private message Visit poster's website Reply with quote
dev_89



Joined: 24 Feb 2013
Posts: 8
dev_89 08 May 2013, 08:04
@revolution:thanks for the tip.
@cod3b453:thanks for the help but still the artifacts appears.
Post 08 May 2013, 08:04
View user's profile Send private message Reply with quote
asmdev



Joined: 21 Dec 2006
Posts: 18
asmdev 13 May 2013, 10:23
perfectly fine working alpha blending: http://www.masmforum.com/board/index.php?topic=8783.0
but it will not produce either 100% solid or transparent - don't remember
Post 13 May 2013, 10:23
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.