gpt4 book ai didi

ios - 替换 CGBitmapContext 中的特定颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:14:04 25 4
gpt4 key购买 nike

如何替换 CGBitmapContext 中已绘制的特定颜色(RGB 值)?

有什么简单的方法吗?

提前致谢。

最佳答案

您需要通过执行以下操作来获得指向像素和有关其格式的信息的指针:

// This assumes the data is RGBA format, 8-bits per channel. 
// You'll need to verify that by calling CGBitmapContextGetBitsPerPixel (), etc.
typedef struct RGBA8 {
UInt8 red;
UInt8 green;
UInt8 blue;
UInt8 alpha;
} RGBA8;

RGBA8* pixels = CGBitmapContextGetData (context);
UInt32 height = CGBitmapContextGetHeight (context);
UInt32 width = CGBitmapContextGetWidth (context);
UInt32 rowBytes = CGBitmapContextGetBytesPerRow (context);
UInt32 x, y;
for (y = 0; y < height; y++)
{
RGBA8* currentRow = (RGBA8*)((UInt8*)pixels + y * rowBytes);
for (x = 0; x < width; x++)
{
if ((currentRow->red == replaceRed) && (currentRow->green == replaceGreen) &&
(currentRow->blue == replaceBlue) && (currentRow->alpha == replaceAlpha))
{
currentRow->red = newRed;
currentRow->green = newGreen;
currentRow->blue = newBlue;
currentRow->alpha = newAlpha;
}
currentRow++;
}
}

关于ios - 替换 CGBitmapContext 中的特定颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12103083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com