能帮到你的话,就给个赞吧 😘
#include <graphics.h>
#include <iostream>
#include <vector>
#pragma comment(lib, "MSIMG32.LIB")using namespace std;
/*
图片就是由像素组成的生成像素就是生成图片像素透明度rgb
*/
//以下函数是一些原理示例
void extractPixel() {DWORD pixel = 0x12345678;//提取透明度//提取pixel高8位,再将值右移24位BYTE a = (pixel & 0xFF000000) >> 24;//easyX中的顺序位bgr,所以BYTE r = GetBValue(pixel);BYTE g = GetGValue(pixel);BYTE b = GetRValue(pixel);
}
void generatePixel() {//不透明白色像素//RGB本身也是4字节DWORD whitePixel = DWORD(255) << 24 | BGR(RGB(255, 255, 255));}
void colorBlend() {auto rgb1 = RGB(255, 255, 255);auto rgb2 = RGB(255, 255, 255);int a1 = 03;auto rgb3 = rgb1 * a1 + rgb2 * (1 - a1);
}
void interceptTraversal() {vector<int> pixels{ 1,2,3,4,5,6,7,8,9,10 };int start = 0;const int intercept = 4;int end = start + intercept;while (1) {for (int i = start; i < end; i++)cout << pixels[i] << ' ';cout << endl;Sleep(500);start = start + intercept >= pixels.size() ? 0 : start += intercept;end = start + intercept > pixels.size() ? pixels.size() : start + intercept;}
}
void test() {DWORD pixel = 0x12345678;auto r = GetBValue(pixel);BYTE r1 = GetBValue(pixel);
}//图片翻转
void horizontalFlip() {IMAGE playerLeft;loadimage(&playerLeft, _T("resources/img1/paimon_left_0.png"));const int width = playerLeft.getwidth();const int height = playerLeft.getheight();IMAGE playerRight;Resize(&playerRight, width, height);//像素翻转auto pixelRight = GetImageBuffer(&playerRight);auto pixelLeft = GetImageBuffer(&playerLeft);for (int h = 0; h < height; h++)for (int w = 0; w < width; w++)pixelRight[h * width + w] = pixelLeft[h * width + width - 1 - w];initgraph(600, 600);putimage(200, 300, &playerLeft);putimage(300, 300, &playerRight);while (1)Sleep(100);
}
//白色剪影
void whiteSilhouette() {IMAGE playerRaw;loadimage(&playerRaw, _T("resources/img1/paimon_left_0.png"));const int width = playerRaw.getwidth();const int height = playerRaw.getheight();IMAGE silhouette;Resize(&silhouette, width, height);//生成白色剪影//将透明度不为0的像素变为不透明纯白auto pixelRaw = GetImageBuffer(&playerRaw);auto pixelSilhouette = GetImageBuffer(&silhouette);for (int h = 0; h < height; h++)for (int w = 0; w < width; w++)if (pixelRaw[h * width + w] & 0xFF000000)pixelSilhouette[h * width + w] = (DWORD(255) << 24) | BGR(RGB(255, 255, 255));initgraph(600, 600);BeginBatchDraw();bool isSwitch = true;while (1) {cleardevice();if (isSwitch) {putimage(200, 300, &playerRaw);isSwitch = false;}else {putimage(200, 300, &silhouette);isSwitch = true;}FlushBatchDraw();Sleep(1000 / 30);}EndBatchDraw();
}
//冰冻特效
void iceEffect() {//加载原始图片IMAGE player;loadimage(&player, _T("resources/img1/paimon_left_0.png"));const int width = player.getwidth();const int height = player.getheight();//加载冰冻图片IMAGE ice;loadimage(&ice, _T("resources/img1/ice.png"));auto playerPixel = GetImageBuffer(&player);auto icePixel = GetImageBuffer(&ice);initgraph(600, 600);BeginBatchDraw();//玩家冰冻效果——将冰冻像素混合到玩家像素 //设置混合系数static const double ratio = 0.5;for (int h = 0; h < height; h++)for (int w = 0; w < width; w++) {int i = h * width + w;if (playerPixel[i] & 0xFF000000) {//混合颜色//冰冻图与玩家图大小相同BYTE r = GetBValue(playerPixel[i]) * ratio + GetBValue(icePixel[i]) * (1 - ratio);BYTE g = GetGValue(playerPixel[i]) * ratio + GetGValue(icePixel[i]) * (1 - ratio);BYTE b = GetRValue(playerPixel[i]) * ratio + GetRValue(icePixel[i]) * (1 - ratio);playerPixel[i] = DWORD(255) << 24 | BGR(RGB(r, g, b)); //不透明}}IMAGE playerIce = player;//截断遍历int start = 0;const int intercept = 5;int end = start + intercept;while (1) {//白条高亮效果//设置亮度阈值static const double threshold = 0.84;for (int h = start; h < end; h++)for (int w = 0; w < width; w++) {int i = h * width + w;if (playerPixel[i] & 0xFF000000) {BYTE r = GetBValue(playerPixel[i]) * ratio + GetBValue(icePixel[i]) * (1 - ratio);BYTE g = GetGValue(playerPixel[i]) * ratio + GetGValue(icePixel[i]) * (1 - ratio);BYTE b = GetRValue(playerPixel[i]) * ratio + GetRValue(icePixel[i]) * (1 - ratio);//根据亮度判断高亮 亮度计算——经验公式if ((r * 0.2126 + g * 0.7152 + b * 0.0722) / 255 > threshold)playerPixel[i] = DWORD(255) << 24 | BGR(RGB(255, 255, 255)); //白色高亮 }}start = start + intercept >= height ? 0 : start += intercept;end = start + intercept > height ? height : start + intercept;if (!start) {player = playerIce;playerPixel = GetImageBuffer(&player);}//渲染cleardevice();putimage(200, 300, &player);FlushBatchDraw();Sleep(1000 / 30);}EndBatchDraw();
}int main(){iceEffect();
}