gpt4 book ai didi

c++ - 如何在 32-bpp 显示器上高效渲染 24-bpp 图像?

转载 作者:行者123 更新时间:2023-11-28 07:12:23 24 4
gpt4 key购买 nike

首先,我在内核上下文中编程,因此不存在现有库。事实上,这段代码将进入我自己的库。

两个问题,一个比另一个重要:

  1. 如标题所示,假设我有帧缓冲区的地址,我如何才能有效地将 24-bpp 图像渲染到 32-bpp 设备上?

目前我有这段代码:

void BitmapImage::Render24(uint16_t x, uint16_t y, void (*r)(uint16_t, uint16_t, uint32_t))
{
uint32_t imght = Math::AbsoluteValue(this->DIB->GetBitmapHeight());
uint64_t ptr = (uint64_t)this->ActualBMP + this->Header->BitmapArrayOffset;

uint64_t rowsize = ((this->DIB->GetBitsPerPixel() * this->DIB->GetBitmapWidth() + 31) / 32) * 4;

uint64_t oposx = x;
uint64_t posx = oposx;
uint64_t posy = y + (this->DIB->Type == InfoHeaderV1 && this->DIB->GetBitmapHeight() < 0 ? 0 : this->DIB->GetBitmapHeight());


for(uint32_t d = 0; d < imght; d++)
{
for(uint32_t w = 0; w < rowsize / (this->DIB->GetBitsPerPixel() / 8); w++)
{
r(posx, posy, (*((uint32_t*)ptr) & 0xFFFFFF));
ptr += this->DIB->GetBitsPerPixel() / 8;
posx++;
}
posx = oposx;
posy--;
}
}

r 是一个函数指针,指向接受 x、y 和颜色参数的 PutPixel 类事物。显然这段代码非常慢,因为一次绘制一个像素从来都不是一个好主意。

对于我的 32-bpp 渲染代码(我也有一个问题,稍后会详细介绍)我可以轻松地将 Memory::Copy() 位图数组(我在这里加载 bmp 文件)到帧缓冲区。但是,如何使用 24bpp 图像执行此操作?在 24bpp 的显示器上这会很好,但我正在使用 32bpp 的显示器。

我现在能想到的一个解决方案是创建另一个位图数组,它基本上包含 0x00(colour) 的值并使用它来绘制到屏幕上——不过我认为这不是很好,所以我'正在寻找更好的选择。

下一个问题:2. 鉴于显而易见的原因,不能简单地将整个数组立即复制到帧缓冲区中,因此下一个最好的办法是逐行复制它们。

有没有更好的办法?

最佳答案

基本上是这样的:

for (uint32_t l = 0; l < h; ++l) // l line index in pixels
{
// srcPitch is distance between lines in bytes
char* srcLine = (char*)srcBuffer + l * srcPitch;
unsigned* trgLine = ((unsigned*)trgBuffer) + l * trgPitch;

for (uint32_t c = 0; c < w; ++c) // c is column index in pixels
{
// build target pixel. arrange indexes to fit your render target (0, 1, 2)
++(*trgLine) = (srcLine[0] << 16) | (srcLine[1] << 8)
| srcLine[2] | (0xff << 24);
srcLine += 3;
}
}

一些注意事项: - 最好写入与渲染缓冲区不同的缓冲区,以便立即显示图像。 - 像您一样使用函数进行像素放置非常(非常非常)慢。

关于c++ - 如何在 32-bpp 显示器上高效渲染 24-bpp 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20765680/

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