c++ - Linux 绘制像素缓冲区
问题描述
问题很简单,我有一个生成像素缓冲区的代码。现在我需要呈现这个像素缓冲区而不是保存图像然后分析它。
解决方案是什么:
- 打开窗户
- 用我的像素 RGB888 替换此窗口中的所有像素
到目前为止的建议是:要使用opengl,为覆盖窗口的矩形创建一个顶点缓冲区,并使用像素着色器来绘制像素。这显然不是在窗口中交换像素缓冲区的最佳方式。
平台:Ubuntu 18
解决方案
您还可以使用SFML在窗口中轻松显示位图图像。实际上,在我的其他答案中,它似乎比CImg快得多。我不是这方面的专家,但以下代码可以满足您的需求:
// g++ -std=c++11 main.cpp $(pkg-config --libs --cflags sfml-graphics sfml-window)
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdint>
int main()
{
const unsigned width = 1024;
const unsigned height= 768;
// create the window
sf::RenderWindow window(sf::VideoMode(width, height), "Some Funky Title");
// create a texture
sf::Texture texture;
texture.create(width, height);
// Create a pixel buffer to fill with RGBA data
unsigned char *pixbuff = new unsigned char[width * height * 4];
// Create uint32_t pointer to above for easy access as RGBA
uint32_t * intptr = (uint32_t *)pixbuff;
// The colour we will fill the window with
unsigned char red = 0;
unsigned char blue = 255;
// run the program as long as the window is open
int frame = 0;
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// clear the window with black color
window.clear(sf::Color::Black);
// Create RGBA value to fill screen with.
// Increment red and decrement blue on each cycle. Leave green=0, and make opaque
uint32_t RGBA;
RGBA = (red++ << 24) | (blue-- << 16) | 255;
// Stuff data into buffer
for(int i=0;i<width*height;i++){
intptr[i] = RGBA;
}
// Update screen
texture.update(pixbuff);
sf::Sprite sprite(texture);
window.draw(sprite);
// end the current frame
window.display();
std::cout << "Frame: " << frame << std::endl;
frame++;
if(frame==1000)break;
}
return 0;
}
在我的 Mac 上,我实现了以下帧速率:
- 700 fps @ 640x480 分辨率
- 384 fps @ 1024x768 分辨率
如果您想提高性能,您可以/可以在第二个线程中创建和填充屏幕外的纹理,但这已经非常快了。
关键词:C++、图像处理、显示、位图图形、像素缓冲区、SFML、imshow、素数。
推荐阅读
- python - re.sub 改变被替换的波斯语/阿拉伯语内容的方向
- c++ - 如何在小于 O(n) 的时间复杂度内检查两个 std::vectors 是否相等
- python - 推文中图片的 URL
- c - 我应该如何修复错误“比数据参数更多的 '%' 转换”?
- tensorflow - 为什么 Keras 模型仅使用 imagenet 权重实例化?
- html - 带有 Bootstrap 和 Django 的轮播
- ios - SwiftUI:API URL 获取失败
- android - 如何让 Webview 超链接在 webview 应用程序而不是浏览器中打开
- javascript - DOM 元素转换为整数并将它们转换为总时间量 - 小时/分钟 parseFloat
- c# - 如何使用 foreach 循环生成文本框以在 C# Windows 窗体中显示问题列表