Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

游戏图标支持彩色掩码图标 #956

Merged
merged 2 commits into from
Jun 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/Magpie.App/IconHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ static bool CopyPixelsOfHBmp(HBITMAP hBmp, LONG width, LONG height, void* data)
}

static SoftwareBitmap HIcon2SoftwareBitmap(HICON hIcon) {
// 单色图标: 不处理
// 彩色掩码图标: 忽略掩码
// 支持彩色光标和彩色掩码图标,不支持单色图标

ICONINFO iconInfo{};
if (!GetIconInfo(hIcon, &iconInfo)) {
Expand All @@ -58,7 +57,7 @@ static SoftwareBitmap HIcon2SoftwareBitmap(HICON hIcon) {
{
BitmapBuffer buffer = bitmap.LockBuffer(BitmapBufferAccessMode::Write);
uint8_t* pixels = buffer.CreateReference().data();

if (!CopyPixelsOfHBmp(iconInfo.hbmColor, bmp.bmWidth, bmp.bmHeight, pixels)) {
return nullptr;
}
Expand All @@ -84,8 +83,24 @@ static SoftwareBitmap HIcon2SoftwareBitmap(HICON hIcon) {
pixels[i + 1] = (uint8_t)std::lroundf(pixels[i + 1] * alpha);
pixels[i + 2] = (uint8_t)std::lroundf(pixels[i + 2] * alpha);
}
} else {
} else if (iconInfo.hbmMask) {
// 彩色掩码图标
std::unique_ptr<uint8_t[]> maskData = std::make_unique<uint8_t[]>(pixelsSize);
if (!CopyPixelsOfHBmp(iconInfo.hbmMask, bmp.bmWidth, bmp.bmHeight, maskData.get())) {
return nullptr;
}

for (uint32_t i = 0; i < pixelsSize; i += 4) {
// hbmMask 表示是否应用掩码
// 如果需要应用掩码而掩码不为零,那么这个图标无法转换为彩色图标,这种情况下直接忽略掩码
if (maskData[i] != 0 && pixels[i] == 0 && pixels[i + 1] == 0 && pixels[i + 2] == 0) {
// 掩码全为 0 表示透明像素
std::memset(pixels + i, 0, 4);
} else {
pixels[i + 3] = 255;
}
}
} else {
for (uint32_t i = 3; i < pixelsSize; i += 4) {
pixels[i] = 255;
}
Expand Down