c++ - 将 R、G、B 数组转换为 RGB 数组
问题描述
我有我的 R、G 和 B 数组,它们是 uint8_t
uint8_t * R, * G, * B, * A;
int stride_r, stride_g, stride_b, stride_a;
getRGB(img, &R, &G, &B, &A, &stride_r, &stride_g, &stride_b, &stride_a);
// All the strides are equal and the img is img_width and img_height dimension
int RBG[stride_r * img_height];
所以我的问题是以下工作:
for(int i=0; i<stride_r * img_height; ++i)
{
RGB[i] = R[i] << 24 + G[i] << 16 + B[i] << 8 + A[i];
}
解决方案
你在正确的轨道上。但是,您需要使用按位或操作将它们组合起来,而不是将四个组成字节添加到四字节整数中:
for(int i = 0; i < stride_r * img_height; ++i)
{
RGB[i] = (R[i] << 24) | (G[i] << 16) | (B[i] << 8) | A[i];
}
另外,请记住,在许多/大多数平台上,组件实际上以“反向”顺序存储(以十六进制格式,0xaaBBGGRR);因此,如果您的系统是这种情况,那么您将需要:
for(int i = 0; i < stride_r * img_height; ++i)
{
RGB[i] = (A[i] << 24) | (B[i] << 16) | (G[i] << 8) | R[i];
}
此外,正如评论中提到的,您应该使用类型创建RGB
一个无符号整数数组,最好是固定的32 位大小uint32_t
。
推荐阅读
- python - Python:从 A 类中的 B 类调用函数返回 AttributeError
- apache - 如何在不创建新子目录的情况下获取要在父目录中处理的 URL 的最后部分
- mysql - 如何在 R 中转义撇号,以便将字符串插入 MySQL 表
- javascript - 试图将标签添加到 d3,无法找出文本标签
- python - 由于 window-build-tools 从命令行运行脚本的 Python 错误
- google-apps-script - Google Apps 脚本 - 格式化特定工作表中的列
- amazon-web-services - 有什么方法可以通过 AWS 上的配置来限制 s3 存储桶的大小
- javascript - Firebase Chrome 扩展安全通过 SSR
- c - C语言中“%c”和“%s”的区别
- node.js - 每次变量更改 DISOCRD.JS 时,Nodejs 都会更改机器人的昵称