-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.cpp
68 lines (62 loc) · 2.58 KB
/
tools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
#include<stdio.h>
#include<graphics.h>
#include"tools.h"
#pragma comment( lib, "MSIMG32.LIB")
///一个函数,为了实现将png透明输出
void putpng(int x, int y, IMAGE* srcimg, IMAGE* dstimg )
{
HDC dstDC = GetImageHDC(dstimg);
HDC srcDC = GetImageHDC(srcimg);
int w = srcimg->getwidth();
int h = srcimg->getheight();
BLENDFUNCTION bf = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
AlphaBlend(dstDC, x, y, w, h, srcDC, 0, 0, w, h, bf);
}
///这个函数跟上一个的不同之处在于,有的素材合并在一张图中,我们需要而外的定位fromx与fromy
void putpng(int x, int y, int xlength, int ylength, IMAGE* srcimg, int fromx, int fromy, IMAGE* dstimg )
{
HDC dstDC = GetImageHDC(dstimg);
HDC srcDC = GetImageHDC(srcimg);
int w = xlength;
int h = ylength;
BLENDFUNCTION bf = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
AlphaBlend(dstDC, x, y, w, h, srcDC, fromx, fromy, w, h, bf);
}
void drawAlpha(int x, int y, int width, int height, IMAGE* image, int pic_x, int pic_y, int WindowWidth, int WindowHeight, double AA)
{
// 变量初始化
DWORD* dst = GetImageBuffer(); // GetImageBuffer() 函数,用于获取绘图设备的显存指针, EasyX 自带
DWORD* draw = GetImageBuffer();
DWORD* src = GetImageBuffer(image); // 获取 picture 的显存指针
int imageWidth = image->getwidth(); // 获取图片宽度
int imageHeight = image->getheight(); // 获取图片宽度
int dstX = 0; // 在 绘图区域 显存里像素的角标
int srcX = 0; // 在 image 显存里像素的角标
// 实现透明贴图 公式: Cp=αp*FP+(1-αp)*BP , 贝叶斯定理来进行点颜色的概率计算
for (int iy = 0; iy < height; iy++)
{
for (int ix = 0; ix < width; ix++)
{
// 防止越界
if (ix + pic_x >= 0 && ix + pic_x < imageWidth && iy + pic_y >= 0 && iy + pic_y < imageHeight &&
ix + x >= 0 && ix + x < WindowWidth && iy + y >= 0 && iy + y < WindowHeight)
{
// 获取像素角标
int srcX = (ix + pic_x) + (iy + pic_y) * imageWidth;
dstX = (ix + x) + (iy + y) * WindowWidth;
int sa = ((src[srcX] & 0xff000000) >> 24) * AA; // 0xAArrggbb; AA 是透明度
int sr = ((src[srcX] & 0xff0000) >> 16); // 获取 RGB 里的 R
int sg = ((src[srcX] & 0xff00) >> 8); // G
int sb = src[srcX] & 0xff; // B
// 设置对应的绘图区域像素信息
int dr = ((dst[dstX] & 0xff0000) >> 16);
int dg = ((dst[dstX] & 0xff00) >> 8);
int db = dst[dstX] & 0xff;
draw[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) << 16) //公式: Cp=αp*FP+(1-αp)*BP ; αp=sa/255 , FP=sr , BP=dr
| ((sg * sa / 255 + dg * (255 - sa) / 255) << 8) //αp=sa/255 , FP=sg , BP=dg
| (sb * sa / 255 + db * (255 - sa) / 255); //αp=sa/255 , FP=sb , BP=db
}
}
}
}