Skip to content

Commit

Permalink
Merge pull request #33 from chirsz-ever/putpixel-withalpha-2007
Browse files Browse the repository at this point in the history
增加 `ege::putpixel_withalpha` 与 `ege::putpixel_withalpha_f`
  • Loading branch information
wysaid authored Aug 11, 2020
2 parents 0763042 + c92cd0c commit 3db42ec
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,13 @@ hsv2rgb(float H, float S, float V) {
return EGERGB(crgb.r, crgb.g, crgb.b);
}

color_t alphablend(color_t dst, color_t src) {
return alphablend_inline(dst, src, EGEGET_A(src));
}

color_t alphablend(color_t dst, color_t src, unsigned char alpha) {
return alphablend_inline(dst, src, alpha);
}


} // namespace ege
11 changes: 9 additions & 2 deletions src/ege.h
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ typedef enum pattern_type_e {
pattern_texture = 3,
}pattern_type_e;

typedef unsigned int color_t;
typedef DWORD color_t;

struct viewporttype {
int left;
Expand Down Expand Up @@ -705,16 +705,23 @@ void EGEAPI rgb2hsv(color_t rgb, float *H, float *S, float *V);
color_t EGEAPI hsl2rgb(float H, float S, float L);
color_t EGEAPI hsv2rgb(float H, float S, float V);

// 按 Alpha 通道混合颜色,将 src 作为背景色,dst 作为前景色进行混合
color_t EGEAPI alphablend(color_t dst, color_t src); // 使用 EGEGET_A(src) 作为 Alpha 值
color_t EGEAPI alphablend(color_t dst, color_t src, unsigned char alpha);


// 基本绘图函数

color_t EGEAPI getpixel (int x, int y, PCIMAGE pimg = NULL); // 获取点的颜色
void EGEAPI putpixel (int x, int y, color_t color, PIMAGE pimg = NULL); // 画点
color_t EGEAPI getpixel_f(int x, int y, PCIMAGE pimg = NULL); // 获取点的颜色
void EGEAPI putpixel_f(int x, int y, color_t color, PIMAGE pimg = NULL); // 画点
void EGEAPI putpixel_f(int x, int y, color_t color, PIMAGE pimg = NULL); // 绝对坐标画点
void EGEAPI putpixels (int nPoint, int* pPoints, PIMAGE pimg = NULL); // 批量画点
void EGEAPI putpixels_f(int nPoint, int* pPoints, PIMAGE pimg = NULL); // 批量画点

void EGEAPI putpixel_withalpha (int x, int y, color_t color, PIMAGE pimg = NULL); // 带透明度画点
void EGEAPI putpixel_withalpha_f(int x, int y, color_t color, PIMAGE pimg = NULL); // 带透明度绝对坐标画点

void EGEAPI moveto(int x, int y, PIMAGE pimg = NULL); // 移动当前点(绝对坐标)
void EGEAPI moverel(int dx, int dy, PIMAGE pimg = NULL); // 移动当前点(相对坐标)

Expand Down
17 changes: 17 additions & 0 deletions src/ege_head.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
#endif
#endif

#ifdef _MSC_VER
#define EGE_FORCEINLINE __forceinline
#else
#define EGE_FORCEINLINE __attribute__((always_inline)) inline
#endif

#define EGE_GDIPLUS // 使用gdi+函数扩展

#ifdef EGE_GDIPLUS
Expand Down Expand Up @@ -566,6 +572,17 @@ void internal_panic(LPCWSTR errmsg);

HBITMAP newbitmap(int width, int height, PDWORD* p_bmp_buf);

// 以 bkg 为背景色,src 为前景色,alpha 为 0~255 的整数进行混合,
// 混合结果保留 bkg 的 Alpha 通道
EGE_FORCEINLINE color_t alphablend_inline(color_t bkg, color_t src, unsigned char alpha) {
DWORD rb = bkg & 0x00FF00FF;
DWORD g = bkg & 0x0000FF00;

rb += ((src & 0x00FF00FF) - rb) * alpha >> 8;
g += ((src & 0x0000FF00) - g) * alpha >> 8;
return (rb & 0x00FF00FF) | (g & 0x0000FF00) | (bkg & 0xFF000000);
}

} // namespace ege

#endif /*_EGE_HEAD_H_*/
20 changes: 20 additions & 0 deletions src/egegapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,26 @@ putpixel_f(int x, int y, color_t color, PIMAGE pimg) {
}
}

void putpixel_withalpha(int x, int y, color_t color, PIMAGE pimg) {
PIMAGE img = CONVERT_IMAGE(pimg);
x += img->m_vpt.left;
y += img->m_vpt.top;
if (in_rect(x, y, img->m_vpt.right, img->m_vpt.bottom)) {
color_t& dst_color = img->m_pBuffer[y * img->m_width + x];
dst_color = alphablend_inline(dst_color, color, EGEGET_A(color));
}
CONVERT_IMAGE_END;
}

void putpixel_withalpha_f(int x, int y, color_t color, PIMAGE pimg) {
PIMAGE img = CONVERT_IMAGE_F(pimg);
if (in_rect(x, y, img->m_width, img->m_height)) {
color_t& dst_color = img->m_pBuffer[y * img->m_width + x];
dst_color = alphablend_inline(dst_color, color, EGEGET_A(color));
}
CONVERT_IMAGE_END;
}

void
moveto(int x, int y, PIMAGE pimg) {
PIMAGE img = CONVERT_IMAGE(pimg);
Expand Down
17 changes: 4 additions & 13 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,15 +859,6 @@ IMAGE::putimage_transparent(
return grOk;
}

#define EGEALPHABLEND(d, s, pd, alpha) do { \
DWORD rb = d & 0x00FF00FF; \
DWORD g = d & 0x0000FF00; \
\
rb += ((s & 0x00FF00FF) - rb) * alpha >> 8; \
g += ((s & 0x0000FF00) - g) * alpha >> 8; \
*pd = (rb & 0x00FF00FF) | (g & 0x0000FF00) | (d & 0xFF000000); \
} while(0)

int
IMAGE::putimage_alphablend(
PIMAGE imgdest, // handle to dest
Expand Down Expand Up @@ -905,7 +896,7 @@ IMAGE::putimage_alphablend(
for (y=0; y<nHeightSrc; ++y) {
for (x=0; x<nWidthSrc; ++x, ++psp, ++pdp) {
DWORD d=*pdp, s=*psp;
EGEALPHABLEND(d, s, pdp, alpha);
*pdp = alphablend_inline(d, s, alpha);
}
pdp += ddx;
psp += dsx;
Expand Down Expand Up @@ -955,7 +946,7 @@ IMAGE::putimage_alphatransparent(
for (x=0; x<nWidthSrc; ++x, ++psp, ++pdp) {
if ((*psp & 0x00FFFFFF) != cr) {
DWORD d=*pdp, s=*psp;
EGEALPHABLEND(d, s, pdp, alpha);
*pdp = alphablend_inline(d, s, alpha);
}
}
pdp += ddx;
Expand Down Expand Up @@ -1003,7 +994,7 @@ IMAGE::putimage_withalpha(
for (x=0; x<nWidthSrc; ++x, ++psp, ++pdp) {
DWORD d=*pdp, s=*psp;
DWORD alpha = EGEGET_A(s);
EGEALPHABLEND(d, s, pdp, alpha);
*pdp = alphablend_inline(d, s, alpha);
}
pdp += ddx;
psp += dsx;
Expand Down Expand Up @@ -1054,7 +1045,7 @@ IMAGE::putimage_alphafilter(
DWORD d=*pdp, s=*psp;
DWORD alpha = *pap & 0xFF;
if (*pap) {
EGEALPHABLEND(d, s, pdp, alpha);
*pdp = alphablend_inline(d, s, alpha);
}
}
pdp += ddx;
Expand Down

0 comments on commit 3db42ec

Please sign in to comment.