QT 给Qimage数据赋值,显示异常,像素对齐的坑

如果使用QImage(width, height, format)的形式创建qimage

有时候QT底层为了效率会使用像素对齐

一行的数据不一定为width

例如:

cpp 复制代码
QImage image = QImage(502, 410, QImage::Format_RGB888);
qDebug() << image.bytesPerLine();
// 按照设想应该输出 502 * 3 = 1506
// 实际输出 1508

// 假设你有一个 rgb888 数据需要赋值给 QImage
const quint8 *src_rgb888_data;

// 这种情况显示QImage的图像就会异常
memcpy(image.bits(), src_rgb888_data, image.width() * image.height() * 3);

// 正确的赋值方法
const quint8 *src = src_rgb888_data;
int srcStride = image.width() * 3;

quint8 *dst = image.bits();
int stride = image.bytesPerLine();
for(int i = 0; i < image.height(); i ++) {
	const quint8 *src_row = src + (i * srcStride);
    quint8 *row = dst + (i * stride);
    
    memcpy(row, src_row, srcStride);
}
相关推荐
小白学大数据1 天前
爬虫性能天花板:asyncio赋能 Aiohttp,并发提速 10 倍
开发语言·爬虫·数据分析
凡人叶枫1 天前
Effective C++ 条款07:为多态基类声明 virtual 析构函数
linux·c语言·开发语言·c++
卷帘依旧1 天前
JavaScript 判断页面加载完成的多种场景
前端
凡人叶枫1 天前
Effective C++ 条款10:令 operator= 返回一个 reference to *this
java·linux·服务器·开发语言·c++·effective c++
光影少年1 天前
React 项目常见优化方案
前端·react.js·前端框架
满天星83035771 天前
【Qt】信号和槽(三) (断开连接和lambda函数)
qt
lichenyang4531 天前
把 demo 里的 console.log 全换成 HiLog:从 %{private} 没脱敏的困惑说起
前端
leo__5201 天前
MATLAB实现牧羊人算法
开发语言·算法·matlab
光影少年1 天前
组件复用:HOC、Render Props、自定义Hook 对比
前端·react.js·掘金·金石计划
Gauss松鼠会1 天前
【GaussDB】GaussDB SMP特性调优详解
java·服务器·前端·数据库·sql·算法·gaussdb