从像素值获得RGB通道没有任何库
Get RGB Channels From Pixel Value Without Any Library
无需任何库即可从像素值获取RGB通道
我试图获得我从图像中读取的每个像素的RGB通道。我使用getchar从图像中读取每个字节。所以在网上搜索了一下之后,我发现了BMP的颜色数据从36字节开始,我知道每个通道是8位,整个RGB是8位的红色,8位的绿色和8位的蓝色。我的问题是如何从像素值中提取它们?
例如:
pixel = getchar(image);
我能做些什么来提取这些通道?另外,我在JAVA上看到了这个例子,但不知道如何在c++上实现它:
int rgb[] = new int[] {
(argb >> 16) & 0xff, //red
(argb >> 8) & 0xff, //green
(argb ) & 0xff //blue
};
我猜argb是我之前提到的"像素"变量。
谢谢。
假设它被编码为ABGR,并且每个像素有一个整数值,这应该可以做到:
int r = color & 0xff;
int g = (color >> 8) & 0xff;
int b = (color >> 16) & 0xff;
int a = (color >> 24) & 0xff;
当读取单个字节时,它取决于格式的端序。由于有两种可能的方式,这当然总是不一致的,所以我将两种方式都写下来,读取作为伪函数完成:
RGBA:
int r = readByte();
int g = readByte();
int b = readByte();
int a = readByte();
ABGR:
int a = readByte();
int b = readByte();
int g = readByte();
int r = readByte();
如何编码取决于你的文件格式是如何布局的。我也见过BGRA和ARGB命令和平面RGB(每个通道是一个宽度x高度字节的单独缓冲区)。
看起来维基百科对BMP文件的样子有一个很好的概述:http://en.wikipedia.org/wiki/BMP_file_format
因为它看起来有点复杂,我强烈建议使用一个库而不是自己编写。
相关文章:
- C++用于从流中读取二进制值的任何标准库
- 任何用于 boost::p roperty 树的标准库实现
- 在RAW图像中提取RGB通道值的库或方法
- 标准库中的任何正则表达式语法是否支持 (?(定义)用于子模式参考?
- 没有外部库的任何日期的C++Int
- 除了 rand() 之外,是否有任何随机生成器库,开发人员可以手动设置种子?
- 适用于 gcc 4.8 的任何正则表达式库
- 在 Cmake 中链接 libavcodec,find_library不适用于任何库
- C 中客户端代理服务器系统的任何库
- 测试一个数字是否具有小数值,而没有任何库
- 如何在没有任何库或框架工作的情况下编写驱动程序
- 如何在不导入任何库的情况下删除矢量
- 无需任何库函数即可将浮点数转换为字符串的有效方法
- 没有任何库的正弦函数
- 我们可以在不使用 c++ 中的任何库的情况下加载、显示和操作图像的矩阵吗?
- 从像素值获得RGB通道没有任何库
- 是否有任何库为非默认可构造的不可复制类型提供容器
- 是否有任何库可以确定数值是否可以转换为特定字符集中的有效、可打印且有意义的字符
- 是否有任何库支持具有EMSA-PSS编码技术和证书验证的RSASSA-PSS.
- 是否有任何库将CAD转换为SVG