手动将RGBA像素与RGB像素进行Alpha混合

时间:2012-01-26 06:43:17

标签: algorithm image-processing image-manipulation alphablending alpha-transparency

我正在尝试将RGBA图像(前景图像)的alpha混合操作转换为RGB图像(背景图像)。但是,在这样做的时候,我想我可能正在做错误的alpha混合操作或做错了。例如,我的RGB图像的像素是(127,127,127)的浅灰色。像素的RGBA图像的像素将是(0,0,255)。在进行混合操作后,最终颜色将为(127,0,255)。但是,我认为这更像是一种添加剂混合物,与我正在进行的操作不同。

关于如何设置我的值,请看一下

incPixelColor[0] = 0; (red)
incPixelColor[1] = 0; (green)
incPixelColor[2] = 255; (blue)
incPixelColor[3] = 255; (alpha)

currentPixelColor[0] = 127; (red)
currentPixelColor[1] = 127; (green)
currentPixelColor[2] = 127; (blue)

关于如何设置我的计算,请看一下

float incAlpha = (currentPixelColor[3]/255.0f);

float red = ((float)incPixelColor[0]/255.0f * incAlpha) + ((float)currentPixelColor[0]/255.0f);
float green = ((float)incPixelColor[1]/255.0f * incAlpha) + ((float)currentPixelColor[1]/255.0f);
float blue = ((float)incPixelColor[2]/255.0f * incAlpha) + ((float)currentPixelColor[2]/255.0f);


currentPixelColor[0] = min(red * 255, 255.0f);
currentPixelColor[1] = min(green * 255, 255.0f);
currentPixelColor[2] = min(blue * 255, 255.0f);

对于没有alpha的像素,我希望该值为(0,0,255),然后对于具有alpha的图像,我希望它能够融入。在上面的操作结束时,它将是( 127,127,255)。我应该检查每个像素是否有alpha,如果是,那么进行混合还是有其他方法可以做到这一点?

3 个答案:

答案 0 :(得分:24)

典型的“Over”混合以下列方式完成:

outputRed = (foregroundRed * foregroundAlpha) + (backgroundRed * (1.0 - foregroundAlpha));

然后重复蓝色和绿色通道。为每个像素执行此操作。

答案 1 :(得分:2)

您似乎错过了背景(currentPixelColor)的(1-Alpha)乘数

答案 2 :(得分:1)

alpha_blend_sse :)

;example usage
;mov eax, 0xabcdef
;mov edx, 0xAAAAAADD
;call alph_blend_see


; In\   EAX = background color (ZRBG) 32bit (Z mean zero, always is zero)
; In\   EDX = foreground color (RBGA) 32bit
; Out\  EAX = new color
alph_blend_sse:
    xor r14, r14
    xor r12, r12
    xor r13, r13

    movzx r15, dl               ; av: alpha number (0x00--->0xFF)
    movzx ecx, dl
    not ecx                     ; faster than 255 - dl
    mov r14b, cl                ; rem

    shr edx, 8
    and edx, 0x00FFFFFF
    mov r12d, edx
    mov r13d, eax               ; RBGA ---> ZRGB

    mov rax, 0x0000FF
    movq xmm3, rax

    ; s: eax
    ; d: edx

    ;=============================red = ((s >> 16) * rem + (d >> 16) * av) >> 8;
    movq xmm0, r12
    psrld xmm0, 0x10
    movq xmm1, r14
    pmuludq xmm1, xmm0
    movq xmm0, r13
    psrld xmm0, 0x10
    movq xmm2, r15
    pmuludq xmm2, xmm0
    addps xmm2, xmm1
    psrld xmm2, 0x8
    movq rax, xmm2
    mov r9b, al
    shl r9d, 8

    ;=============================green = (((s >> 8) & 0x0000ff) * rem + ((d >> 8) & 0x0000ff) * av) >> 8;
    movq xmm0, r12
    psrld xmm0, 0x8
    andps xmm0, xmm3
    movq xmm1, r14
    pmuludq xmm1, xmm0
    movq xmm0, r13
    psrld xmm0, 0x8
    andps xmm0, xmm3
    movq xmm2, r15
    pmuludq xmm2, xmm0
    addps xmm2, xmm1
    psrld xmm2, 0x8
    movq rax, xmm2
    mov r9b, al
    shl r9d, 8

    ;=============================blue = ((s & 0x0000ff) * rem + (d & 0x0000ff) * av) >> 8;
    movq xmm0, r12
    andps xmm0, xmm3
    movq xmm1, r14
    andps xmm1, xmm3
    pmuludq xmm1, xmm0
    movq xmm0, r13
    andps xmm0, xmm3
    movq xmm2, r15
    andps xmm2, xmm3
    pmuludq xmm2, xmm0
    addps xmm2, xmm1
    psrld xmm2, 0x8
    movq rax, xmm2
    mov r9b, al

    mov eax, r9d
    ret