2014-11-16 2 views
0

완전히 불투명 한 이미지 위에 투명도가있는 이미지를 오버레이하는 알고리즘을 만들려고합니다. 다음 샘플에서 나는 완전히 완전히 불투명 한 이미지가 있고 앞면 이미지는 확산 된 가장자리가있는 파란색 프레임입니다. 문제는 반투명 영역이 어두운 픽셀을 생성하는 잘못 구현 된 것입니다.C/C++에서 알파 값이있는 픽셀을 오버레이

#define OPAQUE 0xFF 
#define TRANSPARENT 0 
#define ALPHA(argb) (uint8_t)(argb >> 24) 
#define RED(argb) (uint8_t)(argb >> 16) 
#define GREEN(argb) (uint8_t)(argb >> 8) 
#define BLUE(argb) (uint8_t)(argb) 
#define ARGB(a, r, g, b) (a << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff) 
#define BLEND(a, b, alpha) ((a * alpha) + (b * (255 - alpha)))/255 

void ImageUtil::overlay(const uint32_t* front, uint32_t* back, const unsigned int width, const unsigned int height) 
{ 
    const size_t totalPixels = width * height; 

    for (unsigned long index = 0; index < totalPixels; index++) 
    { 
     const uint32_t alpha = ALPHA(*front); 

     const uint32_t R = BLEND(RED(*front), RED(*back), alpha); 
     const uint32_t G = BLEND(GREEN(*front), GREEN(*back), alpha); 
     const uint32_t B = BLEND(BLUE(*front), BLUE(*back), alpha); 

     *backPixels++ = ARGB(OPAQUE, R , G, B); 
     *frontPixels++; 
    } 
} 

UPDATE :

테스트 이미지 파일

DOWNLOAD

+0

이 디버깅 할 때 당신은 무엇을 발견 했는가? –

+2

이미지를 어떻게로드 했습니까? 이미지 로더가 그 이미지를 미리 곱 해줄 수 있습니까? – gman

+0

이미지가 올바르게로드되었습니다. 병합하지 않고 별도로 다시로드하고 저장하여 동일한 원본 이미지를 만드는 방법을 시도했습니다. 문제는 픽셀을 혼합하는 수식에 있다고 생각하지만 문제를 발견 할 수는 없습니다. – PerracoLabs

답변

0

팔로우 여기

enter image description here

내 구현 의견의 팁을 gmaninterjay으로 보내 주시면 더 자세히 조사한 결과 데이터가 미리 곱해진 알파로로드됩니다. 블렌딩 할 때 어두워집니다. 해결책은 프론트 픽셀을 다중화하지 않고 마지막으로 예상 된 결과를 얻는 것입니다.

Unmultiply 공식 :

((0xFF * color)/alpha) 

최종 코드 :

#define OPAQUE 0xFF; 
#define TRANSPARENT 0; 

#define ALPHA(rgb) (uint8_t)(rgb >> 24) 
#define RED(rgb) (uint8_t)(rgb >> 16) 
#define GREEN(rgb) (uint8_t)(rgb >> 8) 
#define BLUE(rgb) (uint8_t)(rgb) 

#define UNMULTIPLY(color, alpha) ((0xFF * color)/alpha) 
#define BLEND(back, front, alpha) ((front * alpha) + (back * (255 - alpha)))/255 
#define ARGB(a, r, g, b) (a << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF) 

void ImageUtil::overlay(const uint32_t* front, uint32_t* back, const unsigned int width, const unsigned int height) 
{ 
    const size_t totalPixels = width * height; 

    for (unsigned long index = 0; index < totalPixels; index++) 
    { 
     const uint32_t frontAlpha = ALPHA(*front); 

     if (frontAlpha == TRANSPARENT) 
     { 
      *back++; 
      *front++; 
      continue; 
     } 

     if (frontAlpha == OPAQUE) 
     { 
      *back++ = *front++; 
      continue; 
     } 

     const uint8_t backR = RED(*back); 
     const uint8_t backG = GREEN(*back); 
     const uint8_t backB = BLUE(*back); 

     const uint8_t frontR = UNMULTIPLY(RED(*front), frontAlpha); 
     const uint8_t frontG = UNMULTIPLY(GREEN(*front), frontAlpha); 
     const uint8_t frontB = UNMULTIPLY(BLUE(*front), frontAlpha); 

     const uint32_t R = BLEND(backR, frontR, frontAlpha); 
     const uint32_t G = BLEND(backG, frontG, frontAlpha); 
     const uint32_t B = BLEND(backB, frontB, frontAlpha); 

     *back++ = ARGB(OPAQUE, R , G, B); 
     *front++; 
    } 
}