这里如何获取和修改一个像素值呢?
Apple 问答列表 2 A 显示了如何修改 CGImageRef 中的像素的示例。问题是:他们没有展示如何获取像素并修改它的 RGB 和 A 值。
有趣的部分在这里:
void *data = CGBitmapContextGetData (cgctx);
if (data != NULL)
{
// **** You have a pointer to the image data ****
// **** Do stuff with the data here ****
}
现在,假设我想从 x = 100、y = 50 处的像素读取红色、绿色、蓝色和 Alpha。我如何访问该像素及其 R、G、B 和 A 分量?
Listing 2 of Apple's Q & A shows an example of how to modify pixels in a CGImageRef. The problem is: They're not showing how to obtain a pixel and modify it's R G B and A values.
The interesting part is here:
void *data = CGBitmapContextGetData (cgctx);
if (data != NULL)
{
// **** You have a pointer to the image data ****
// **** Do stuff with the data here ****
}
Now, lets say I want to read Red, Green, Blue and Alpha from pixel at x = 100, y = 50. How do I get access to that pixel and it's R, G, B and A components?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要了解位图的 bytesPerRow,以及位图中像素的数据类型和颜色格式。 bytesPerRow 可能与 width_in_pixels*bytesPerPixel 不同,因为每行末尾可能有填充。像素可以是 16 位或 32 位,也可能是其他大小。像素的格式可以是 ARGB 或 BRGA,或某种其他格式。
对于 32 位 ARGB 数据:
请注意,根据您的视图变换,Y 轴也可能看起来颠倒,具体取决于您的期望。
First, you need to know the bytesPerRow of your bitmap, as well as the data type and color format of the pixels in your bitmap. bytesPerRow can be different from the width_in_pixels*bytesPerPixel, as there might be padding at the end of each line. The pixels can be 16-bits or 32-bits, or possibly some other size. The format of the pixels can be ARGB or BRGA, or some other format.
For 32-bit ARGB data:
Note that depending on your view transform, the Y axis might also appear to look upside down, depending on what you expect.