gpt4 book ai didi

c - Linux framebuffer像素位域通用实现

转载 作者:IT王子 更新时间:2023-10-29 00:34:15 25 4
gpt4 key购买 nike

我正在编写一个小型库来与 Linux 的帧缓冲区抽象接口(interface)。我所有的图形卡都使用相同的像素格式(每个 channel 一个八位字节,四个 channel ,BGRA 排序),到目前为止,库只采用这种格式。但是,如果我想让库在任何 Linux 帧缓冲区上工作,我必须使用帧缓冲区 API 提供的像素格式数据。您不需要知道帧缓冲区如何工作来回答这个问题(我希望),只是一些我不精通的小摆弄。这是我的标题中提供的像素格式信息:

/* Interpretation of offset for color fields: All offsets are from the right,
* inside a "pixel" value, which is exactly 'bits_per_pixel' wide (means: you
* can use the offset as right argument to <<). A pixel afterwards is a bit
* stream and is written to video memory as that unmodified.
*
* For pseudocolor: offset and length should be the same for all color
* components. Offset specifies the position of the least significant bit
* of the pallette index in a pixel value. Length indicates the number
* of available palette entries (i.e. # of entries = 1 << length).
*/
struct fb_bitfield {
__u32 offset; /* beginning of bitfield */
__u32 length; /* length of bitfield */
__u32 msb_right; /* != 0 : Most significant bit is */
/* right */
};

/* snip */

struct fb_var_screeninfo {
/* snip */

__u32 bits_per_pixel; /* guess what */
__u32 grayscale; /* 0 = color, 1 = grayscale, */
/* >1 = FOURCC */
struct fb_bitfield red; /* bitfield in fb mem if true color, */
struct fb_bitfield green; /* else only length is significant */
struct fb_bitfield blue;
struct fb_bitfield transp; /* transparency */

__u32 nonstd; /* != 0 Non standard pixel format */

/* snip */
};

我需要将使用上述信息格式化的像素从四个字符(R、G、B 和 A)写入一个字符数组。我为此所做的错误尝试看起来像这样:

long pxl = 0;
/* downsample each channel to the length (assuming its less than 8 bits) */
/* with a right-shift, then left shift it over into place */
/* haven't done anything with fb_bitfield.msb_right */
pxl |= (r >> (8 - vinfo.red.length)) << vinfo.red.offset;
pxl |= (g >> (8 - vinfo.green.length)) << vinfo.green.offset;
pxl |= (b >> (8 - vinfo.blue.length)) << vinfo.blue.offset;
pxl |= (a >> (8 - vinfo.transp.length)) << vinfo.transp.offset;
fb[xy2off(x, y)] = /* umm... */;
/* little endian, big endian? Can I just assign here? */

xy2off 将坐标转换为索引。 fb 是指向帧缓冲区(内存映射)的指针。

任何人都可以指出转换和分配这些像素的正确方向吗?

最佳答案

亲爱的可能不存在的人正在寻找这个问题的答案:这是我得到的 hackish 代码,现在似乎可以工作了:

void pixel(int x, int y, uint8_t r, uint8_t g, uint8_t b, uint8_t a){    int i;    long pxl = 0;    pxl |= (r >> (8 - vinfo.red.length)) << vinfo.red.offset;    pxl |= (g >> (8 - vinfo.green.length)) << vinfo.green.offset;    pxl |= (b >> (8 - vinfo.blue.length)) << vinfo.blue.offset;    pxl |= (a >> (8 - vinfo.transp.length)) << vinfo.transp.offset;    for (i = 0; i < sizeof(long); i++) {        fb[xy2off(x, y) + i] = ((char *)(&pxl))[i];    }}

缺少字节序处理、处理 msb_right 或任何类型的优化。

关于c - Linux framebuffer像素位域通用实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32169339/

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