函数编写】 棋子位图的原始像素放在全局数组unsigned char *g_pRawChessBmpBits中, 棋盘位图的原始像素放在全局数组unsigned char *g_pRawBoardBmpBits中, 函数的功能是,利用这两个原始像素数组,根据参数传下来的尺寸、坐标和透明度,计算新的半透明效果的玻璃棋子位图的像素值,并放在第一个参数指定的数组中。(不得修改两个全局数组) */ //--------------------------------------------------------------------------------------------------------------------------------------------------------- void ModifyChessBmp( unsigned char *pPixel, //将修改后的像素RGB值放在这个数组中 int iChessBmpWidth, //棋子位图的宽度(以像素为单位) int iChessBmpHeight, //棋子位图的高度(以像素为单位) int iBoardBmpWidth, //棋盘位图的宽度(以像素为单位) int iDrawingX, //棋子位图被贴到棋盘位图的X坐标(水平向右为正向)(以像素为单位) int iDrawingY, //棋子位图被贴到棋盘位图的Y坐标(垂直向下为正向)(以像素为单位) float fTransparency //透明度,值为0.0-1.0 ) { }
时间: 2023-08-15 22:36:52 浏览: 37
这段代码的功能是根据给定的参数,将棋子位图和棋盘位图合成为一个半透明的图像,并将像素值存储在指定的数组中。具体操作如下:
1. 首先需要根据传入的参数计算出棋子位图在棋盘位图上的实际位置。这可以通过将棋子位图的左上角坐标加上棋子在棋盘上的位置坐标得到。这个位置坐标是以像素为单位的,可以通过乘以每个格子的宽度和高度得到实际的坐标值。
2. 然后需要遍历棋子位图的每个像素,根据透明度和棋盘位图的像素值,计算出新的半透明像素值。具体计算方法是将棋子位图的像素值乘以透明度,将棋盘位图的像素值乘以(1-透明度),然后将两者加起来得到新的像素值。
3. 最后需要将新的像素值存储在指定的数组中,这可以通过指针运算来实现。每个像素由三个字节表示,分别是红、绿、蓝三个分量,因此需要将数组指针向后移动三个字节,才能存储下一个像素的值。
相关问题
函数编写】 棋子位图的原始像素放在全局数组unsigned char *g_pRawChessBmpBits中, 棋盘位图的原始像素放在全局数组unsigned char *g_pRawBoardBmpBits中, 函数的功能是,利用这两个原始像素数组,根据参数传下来的尺寸、坐标和透明度,计算新的半透明效果的玻璃棋子位图的像素值,并放在第一个参数指定的数组中。(不得修改两个全局数组) */ void ModifyChessBmp( unsigned char *pPixel, //将修改后的像素RGB值放在这个数组中 int iChessBmpWidth, //棋子位图的宽度(以像素为单位) int iChessBmpHeight, //棋子位图的高度(以像素为单位) int iBoardBmpWidth, //棋盘位图的宽度(以像素为单位) int iDrawingX, //棋子位图被贴到棋盘位图的X坐标(水平向右为正向)(以像素为单位) int iDrawingY, //棋子位图被贴到棋盘位图的Y坐标(垂直向下为正向)(以像素为单位) float fTransparency //透明度,值为0.0-1.0 ) { }
首先,我们需要计算出玻璃棋子位图在棋盘位图中的位置。因为玻璃棋子是半透明的,所以我们需要对每个像素进行计算,将其与棋子位图和棋盘位图对应位置的像素进行混合,得到新的半透明效果的像素值。
具体的实现步骤如下:
1. 计算出玻璃棋子位图在棋盘位图中的左上角像素的坐标。
int iGlassX = iDrawingX;
int iGlassY = iDrawingY;
2. 遍历玻璃棋子位图中的每个像素,计算其在棋盘位图中的坐标,并将其与棋子位图和棋盘位图对应位置的像素进行混合。
for (int i = 0; i < iChessBmpHeight; i++) {
for (int j = 0; j < iChessBmpWidth; j++) {
// 计算出玻璃棋子位图中当前像素的坐标
int x = j;
int y = i;
// 计算出棋盘位图中当前像素的坐标
int boardX = iGlassX + j;
int boardY = iGlassY + i;
// 如果当前像素越界,则跳过
if (boardX < 0 || boardX >= iBoardBmpWidth || boardY < 0 || boardY >= iBoardBmpHeight) {
continue;
}
// 计算出玻璃棋子位图和棋盘位图对应位置的像素的索引
int glassIndex = (y * iChessBmpWidth + x) * 3; // 一个像素占3个字节(RGB)
int boardIndex = (boardY * iBoardBmpWidth + boardX) * 3;
// 计算出混合后的像素值
float alpha = fTransparency * g_pRawChessBmpBits[glassIndex + 3] / 255.0; // 玻璃棋子的透明度
pPixel[boardIndex + 0] = alpha * g_pRawChessBmpBits[glassIndex + 0] + (1 - alpha) * g_pRawBoardBmpBits[boardIndex + 0];
pPixel[boardIndex + 1] = alpha * g_pRawChessBmpBits[glassIndex + 1] + (1 - alpha) * g_pRawBoardBmpBits[boardIndex + 1];
pPixel[boardIndex + 2] = alpha * g_pRawChessBmpBits[glassIndex + 2] + (1 - alpha) * g_pRawBoardBmpBits[boardIndex + 2];
}
}
最终,我们得到的新的半透明效果的玻璃棋子位图的像素值存储在参数pPixel指定的数组中。
函数编写】 棋子位图的原始像素放在全局数组unsigned char *g_pRawChessBmpBits中, 棋盘位图的原始像素放在全局数组unsigned char *g_pRawBoardBmpBits中, 函数的功能是,利用这两个原始像素数组,根据参数传下来的尺寸、坐标和透明度,计算新的半透明效果的玻璃棋子位图的像素值,并放在第一个参数指定的数组中。(不得修改两个全局数组) */ //--------------------------------------------------------------------------------------------------------------------------------------------------------- void ModifyChessBmp( unsigned char *pPixel, //将修改后的像素RGB值放在这个数组中 int iChessBmpWidth, //棋子位图的宽度(以像素为单位) int iChessBmpHeight, //棋子位图的高度(以像素为单位) int iBoardBmpWidth, //棋盘位图的宽度(以像素为单位) int iDrawingX, //棋子位图被贴到棋盘位图的X坐标(水平向右为正向)(以像素为单位) int iDrawingY, //棋子位图被贴到棋盘位图的Y坐标(垂直向下为正向)(以像素为单位) float fTransparency //透明度,值为0.0-1.0 ) { }
下面是一个实现上述功能的函数代码:
```c
void ModifyChessBmp(unsigned char *pPixel, int iChessBmpWidth, int iChessBmpHeight, int iBoardBmpWidth, int iDrawingX, int iDrawingY, float fTransparency)
{
unsigned char *pChessPixel = g_pRawChessBmpBits;
unsigned char *pBoardPixel = g_pRawBoardBmpBits;
int i, j, k, l;
float fAlpha = 1.0f - fTransparency;
for (i = 0; i < iChessBmpHeight; i++) {
for (j = 0; j < iChessBmpWidth; j++) {
int iPixelIndex = 4 * (i * iChessBmpWidth + j);
int iChessX = iDrawingX + j;
int iChessY = iDrawingY + i;
int iBoardX = iChessX - iDrawingX;
int iBoardY = iChessY - iDrawingY;
int iBoardIndex = 4 * (iBoardY * iBoardBmpWidth + iBoardX);
if (iBoardX >= 0 && iBoardX < iBoardBmpWidth && iBoardY >= 0 && iBoardY < iBoardBmpWidth) {
float fRed = fAlpha * pBoardPixel[iBoardIndex] + fTransparency * pChessPixel[iPixelIndex];
float fGreen = fAlpha * pBoardPixel[iBoardIndex + 1] + fTransparency * pChessPixel[iPixelIndex + 1];
float fBlue = fAlpha * pBoardPixel[iBoardIndex + 2] + fTransparency * pChessPixel[iPixelIndex + 2];
float fAlphaChannel = fAlpha * pBoardPixel[iBoardIndex + 3] + fTransparency * pChessPixel[iPixelIndex + 3];
pPixel[iPixelIndex] = (unsigned char)fRed;
pPixel[iPixelIndex + 1] = (unsigned char)fGreen;
pPixel[iPixelIndex + 2] = (unsigned char)fBlue;
pPixel[iPixelIndex + 3] = (unsigned char)fAlphaChannel;
}
else {
pPixel[iPixelIndex] = pChessPixel[iPixelIndex];
pPixel[iPixelIndex + 1] = pChessPixel[iPixelIndex + 1];
pPixel[iPixelIndex + 2] = pChessPixel[iPixelIndex + 2];
pPixel[iPixelIndex + 3] = pChessPixel[iPixelIndex + 3];
}
}
}
}
```
该函数首先将棋子位图和棋盘位图的原始像素分别保存在全局数组 `g_pRawChessBmpBits` 和 `g_pRawBoardBmpBits` 中。然后使用两个嵌套的循环遍历棋子位图的每个像素,并根据棋子在棋盘上的位置和指定的透明度计算新的半透明像素值。最后将计算得到的新像素值保存到指定的数组中。注意,这里使用了浮点数计算透明度和颜色值,因为在计算过程中可能会出现小数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)